12
#include <sys/types.h>
14
void create_file(const char *path, const char *buffer, int mode) {
15
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
18
int err = write(fd, buffer, sizeof(char) * strlen(buffer));
19
assert(err == (sizeof(char) * strlen(buffer)));
25
create_file("file", "abcdef", 0777);
26
symlink("file", "file-link");
27
// some platforms use 777, some use 755 by default for symlinks
28
// make sure we're using 777 for the test
29
lchmod("file-link", 0777);
30
mkdir("folder", 0777);
47
// get the current ctime for the file
48
memset(&s, 0, sizeof s);
50
lastctime = s.st_ctime;
53
// do the actual chmod
54
err = chmod("file", 0200);
57
memset(&s, 0, sizeof s);
59
assert(s.st_mode == (0200 | S_IFREG));
60
assert(s.st_ctime != lastctime);
65
lastctime = s.st_ctime;
68
err = fchmod(open("file", O_WRONLY), 0100);
71
memset(&s, 0, sizeof s);
73
assert(s.st_mode == (0100 | S_IFREG));
74
assert(s.st_ctime != lastctime);
79
// get the current ctime for the folder
80
memset(&s, 0, sizeof s);
82
lastctime = s.st_ctime;
85
// do the actual chmod
86
err = chmod("folder", 0300);
88
memset(&s, 0, sizeof s);
90
assert(s.st_mode == (0300 | S_IFDIR));
91
assert(s.st_ctime != lastctime);
94
// chmod a symlink's target
96
err = chmod("file-link", 0400);
99
// make sure the file it references changed
100
stat("file-link", &s);
101
assert(s.st_mode == (0400 | S_IFREG));
103
// but the link didn't
104
lstat("file-link", &s);
105
assert(s.st_mode == (0777 | S_IFLNK));
108
// chmod the actual symlink
110
err = lchmod("file-link", 0500);
113
// make sure the file it references didn't change
114
stat("file-link", &s);
115
assert(s.st_mode == (0400 | S_IFREG));
118
lstat("file-link", &s);
119
assert(s.st_mode == (0500 | S_IFLNK));
126
signal(SIGABRT, cleanup);