|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 | + * Authors: David Fenner, Jon Hendrickson |
| 5 | + * Copyright (C) 2024 Andrea Cervesato <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +/*\ |
| 9 | + * [Description] |
| 10 | + * |
| 11 | + * This test verifies that utime() is working correctly on symlink() |
| 12 | + * generated files. |
| 13 | + * |
| 14 | + * Also verify if utime() fails: |
| 15 | + * |
| 16 | + * - ENOENT when symlink points to nowhere |
| 17 | + * - ELOOP when symlink is looping |
| 18 | + */ |
| 19 | + |
| 20 | +#include <utime.h> |
| 21 | +#include "tst_test.h" |
| 22 | + |
| 23 | +#define TIME_DIFF 100 |
| 24 | + |
| 25 | +static void create_symlink(const char *path, const char *symname) |
| 26 | +{ |
| 27 | + struct stat asymlink; |
| 28 | + |
| 29 | + SAFE_SYMLINK(path, symname); |
| 30 | + SAFE_LSTAT(symname, &asymlink); |
| 31 | + |
| 32 | + if ((asymlink.st_mode & S_IFMT) != S_IFLNK) { |
| 33 | + tst_brk(TBROK, "symlink generated a non-symbolic link %s to %s", |
| 34 | + symname, path); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +static void test_utime(void) |
| 39 | +{ |
| 40 | + char *symname = "my_symlink0"; |
| 41 | + struct stat oldsym_stat; |
| 42 | + struct stat newsym_stat; |
| 43 | + |
| 44 | + tst_res(TINFO, "Test if utime() changes access time"); |
| 45 | + |
| 46 | + create_symlink(tst_get_tmpdir(), symname); |
| 47 | + SAFE_STAT(symname, &oldsym_stat); |
| 48 | + |
| 49 | + struct utimbuf utimes = { |
| 50 | + .actime = oldsym_stat.st_atime + TIME_DIFF, |
| 51 | + .modtime = oldsym_stat.st_mtime + TIME_DIFF |
| 52 | + }; |
| 53 | + |
| 54 | + TST_EXP_PASS(utime(symname, &utimes)); |
| 55 | + SAFE_STAT(symname, &newsym_stat); |
| 56 | + |
| 57 | + TST_EXP_EQ_LI(newsym_stat.st_atime - oldsym_stat.st_atime, TIME_DIFF); |
| 58 | + TST_EXP_EQ_LI(newsym_stat.st_mtime - oldsym_stat.st_mtime, TIME_DIFF); |
| 59 | + |
| 60 | + SAFE_UNLINK(symname); |
| 61 | +} |
| 62 | + |
| 63 | +static void test_utime_no_path(void) |
| 64 | +{ |
| 65 | + char *symname = "my_symlink1"; |
| 66 | + struct utimbuf utimes; |
| 67 | + |
| 68 | + tst_res(TINFO, "Test if utime() raises ENOENT when symlink points to nowhere"); |
| 69 | + |
| 70 | + create_symlink("bc+eFhi!k", symname); |
| 71 | + TST_EXP_FAIL(utime(symname, &utimes), ENOENT); |
| 72 | + |
| 73 | + SAFE_UNLINK(symname); |
| 74 | +} |
| 75 | + |
| 76 | +static void test_utime_loop(void) |
| 77 | +{ |
| 78 | + char *symname = "my_symlink2"; |
| 79 | + struct utimbuf utimes; |
| 80 | + |
| 81 | + tst_res(TINFO, "Test if utime() raises ELOOP when symlink is looping"); |
| 82 | + |
| 83 | + create_symlink(symname, symname); |
| 84 | + TST_EXP_FAIL(utime(symname, &utimes), ELOOP); |
| 85 | + |
| 86 | + SAFE_UNLINK(symname); |
| 87 | +} |
| 88 | + |
| 89 | +static void run(void) |
| 90 | +{ |
| 91 | + test_utime(); |
| 92 | + test_utime_no_path(); |
| 93 | + test_utime_loop(); |
| 94 | +} |
| 95 | + |
| 96 | +static struct tst_test test = { |
| 97 | + .test_all = run, |
| 98 | + .needs_tmpdir = 1, |
| 99 | +}; |
0 commit comments