1 /* test32: rename() Author: Jan-Mark Wams (jms@cs.vu.nl) */
18 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
19 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
20 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
21 #define Creat(f) if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
26 char *MaxName
; /* Name of maximum length */
27 char MaxPath
[PATH_MAX
]; /* Same for path */
28 char *ToLongName
; /* Name of maximum +1 length */
29 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
34 void makelongnames(void);
36 int main(int argc
, char *argv
[])
42 if (argc
== 2) m
= atoi(argv
[1]);
44 superuser
= (geteuid() == 0);
46 for (i
= 0; i
< ITERATIONS
; i
++) {
47 if (m
& 0001) test32a();
48 if (m
& 0002) test32b();
49 if (m
& 0004) test32c();
53 return(-1); /* Unreachable */
59 { /* Test normal operation. */
62 time_t time1
, time2
, time3
;
66 System("rm -rf ../DIR_32/*");
68 /* Test normal file renamal. */
69 System("echo haha > old");
71 if (rename("old", "new") != 0) e(1);
74 /* The status of new should be the same as old. */
75 if (st1
.st_dev
!= st2
.st_dev
) e(2);
76 if (st1
.st_ino
!= st2
.st_ino
) e(3);
77 if (st1
.st_mode
!= st2
.st_mode
) e(4);
78 if (st1
.st_nlink
!= st2
.st_nlink
) e(5);
79 if (st1
.st_uid
!= st2
.st_uid
) e(6);
80 if (st1
.st_gid
!= st2
.st_gid
) e(7);
81 if (st1
.st_rdev
!= st2
.st_rdev
) e(8);
82 if (st1
.st_size
!= st2
.st_size
) e(9);
83 if (st1
.st_atime
!= st2
.st_atime
) e(10);
84 if (st1
.st_mtime
!= st2
.st_mtime
) e(11);
85 if (st1
.st_ctime
!= st2
.st_ctime
) e(12);
87 /* If new exists, it should be removed. */
88 System("ln new new2");
89 System("echo foobar > old");
91 if (rename("old", "new") != 0) e(13);
94 /* The status of new should be the same as old. */
95 if (st1
.st_dev
!= st2
.st_dev
) e(14);
96 if (st1
.st_ino
!= st2
.st_ino
) e(15);
97 if (st1
.st_mode
!= st2
.st_mode
) e(16);
98 if (st1
.st_nlink
!= st2
.st_nlink
) e(17);
99 if (st1
.st_uid
!= st2
.st_uid
) e(18);
100 if (st1
.st_gid
!= st2
.st_gid
) e(19);
101 if (st1
.st_rdev
!= st2
.st_rdev
) e(20);
102 if (st1
.st_size
!= st2
.st_size
) e(21);
103 if (st1
.st_atime
!= st2
.st_atime
) e(22);
104 if (st1
.st_mtime
!= st2
.st_mtime
) e(23);
105 if (st1
.st_ctime
!= st2
.st_ctime
) e(24);
107 /* The link count on new2 should be one since the old new is removed. */
109 if (st1
.st_nlink
!= 1) e(25);
111 /* Check if status for "." is updated. */
115 while (time1
== time((time_t *)0))
118 rename("OLD", "NEW");
121 while (time3
== time((time_t *)0))
124 if (st1
.st_ctime
>= st2
.st_ctime
) e(26);
125 if (st1
.st_mtime
>= st2
.st_mtime
) e(27);
126 if (st1
.st_ctime
> time1
) e(28);
127 if (st1
.st_mtime
> time1
) e(29);
128 if (st1
.st_ctime
>= time2
) e(30);
129 if (st1
.st_mtime
>= time2
) e(31);
130 if (st2
.st_ctime
< time2
) e(32);
131 if (st2
.st_mtime
< time2
) e(33);
132 if (st2
.st_ctime
>= time3
) e(34);
133 if (st2
.st_mtime
>= time3
) e(35);
135 /* If the new file is removed while it's open it should still be
137 System("rm -rf new NEW old OLD");
138 if ((fd1
= creat("new", 0644)) != 3) e(36);
139 if (write(fd1
, "Hi there! I am Sammy the string", 33) != 33) e(37);
140 if (close(fd1
) != 0) e(38);
141 if ((fd1
= creat("old", 0644)) != 3) e(39);
142 if (write(fd1
, "I need a new name", 18) != 18) e(40);
143 if (close(fd1
) != 0) e(41);
144 if ((fd1
= open("new", O_RDONLY
)) != 3) e(42);
145 if ((fd2
= open("new", O_RDONLY
)) != 4) e(43);
146 if (rename("old", "new") != 0) e(44);
147 if (stat("old", &st1
) == 0) e(45);
148 if (close(fd1
) != 0) e(46);
149 if ((fd1
= open("new", O_RDONLY
)) != 3) e(47);
150 if (read(fd2
, buf
, BUF_SIZE
) != 33) e(48);
151 if (strcmp(buf
, "Hi there! I am Sammy the string") != 0) e(49);
152 if (read(fd1
, buf
, BUF_SIZE
) != 18) e(50);
153 if (strcmp(buf
, "I need a new name") != 0) e(51);
154 if (close(fd1
) != 0) e(52);
155 if (close(fd2
) != 0) e(53);
160 char MaxPath2
[PATH_MAX
]; /* Same for path */
161 char MaxName2
[NAME_MAX
+ 1]; /* Name of maximum length */
167 System("rm -rf ../DIR_32/*");
169 /* Test maximal file name length. */
170 if ((fd
= creat(MaxName
, 0777)) != 3) e(1);
171 if (close(fd
) != 0) e(2);
172 strcpy(MaxName2
, MaxName
);
173 MaxName2
[strlen(MaxName2
) - 1] ^= 1;
174 if (rename(MaxName
, MaxName2
) != 0) e(3);
175 if (rename(MaxName2
, MaxName
) != 0) e(4);
176 MaxName2
[strlen(MaxName2
) - 1] ^= 2;
177 if (rename(MaxName
, MaxName2
) != 0) e(5);
178 MaxPath
[strlen(MaxPath
) - 2] = '/';
179 MaxPath
[strlen(MaxPath
) - 1] = 'a'; /* make ././.../a */
180 if ((fd
= creat(MaxPath
, 0777)) != 3) e(6);
181 if (close(fd
) != 0) e(7);
182 strcpy(MaxPath2
, MaxPath
);
183 MaxPath2
[strlen(MaxPath2
) - 1] ^= 1;
184 if (rename(MaxPath
, MaxPath2
) != 0) e(8);
185 if (rename(MaxPath2
, MaxPath
) != 0) e(9);
186 MaxPath2
[strlen(MaxPath2
) - 1] ^= 2;
187 if (rename(MaxPath
, MaxPath2
) != 0) e(10);
188 MaxPath
[strlen(MaxPath
) - 1] = '/'; /* make ././.../a */
190 /* Test if linked files are renamable. */
191 System("> foo; ln foo bar");
192 if (rename("foo", "bar") != 0) e(11);
193 if (rename("bar", "foo") != 0) e(12);
194 System("ln foo foobar");
195 if (rename("foo", "foobar") != 0) e(13);
196 if (rename("bar", "foobar") != 0) e(14);
198 /* Since the same files have the same links.... */
199 if (rename("bar", "bar") != 0) e(15);
200 if (rename("foo", "foo") != 0) e(16);
201 if (rename("foobar", "foobar") != 0) e(17);
203 /* In ``rename(old, new)'' with new existing, there is always an new
205 for (i
= 0; i
< 5; i
++) {
206 System("echo old > old");
207 System("echo news > new");
209 case -1: printf("Can't fork\n"); break;
213 rename("old", "new");
216 while (stat("old", &st
) == 0)
217 if (stat("new", &st
) != 0) e(18);
219 if (stat_loc
!= 0) e(19); /* Alarm? */
226 { /* Test behavior under error contitions. */
231 System("rm -rf ../DIR_32/*");
233 /* Test if we have access. */
234 system("chmod 777 noacc nowrite > /dev/null 2>/dev/null");
235 system("rm -rf noacc nowrite");
237 System("mkdir noacc nowrite");
238 System("> noacc/file");
239 System("> nowrite/file");
241 System("chmod 677 noacc");
242 System("chmod 577 nowrite");
244 if (rename("noacc/file", "nono") != -1) e(1);
245 if (errno
!= EACCES
) e(2);
246 if (rename("nowrite/file", "nono") != -1) e(3);
247 if (errno
!= EACCES
) e(4);
248 if (rename("file", "noacc/file") != -1) e(5);
249 if (errno
!= EACCES
) e(6);
250 if (rename("file", "nowrite/file") != -1) e(7);
251 if (errno
!= EACCES
) e(8);
254 /* Super user heeft access. */
255 if (rename("noacc/file", "noacc/yes") != 0) e(9);
256 if (rename("nowrite/file", "nowrite/yes") != 0) e(10);
257 if (rename("file", "yes") != 0) e(11);
258 if (rename("noacc/yes", "noacc/file") != 0) e(12);
259 if (rename("nowrite/yes", "nowrite/file") != 0) e(13);
260 if (rename("yes", "file") != 0) e(14);
262 System("chmod 777 noacc nowrite");
264 /* If rmdir() doesn't remove a directory, rename() shouldn't eighter. */
265 System("mkdir newdir olddir");
266 System("rm -rf /tmp/sema.11[ab]");
268 case -1: printf("Can't fork\n"); break;
272 case -1: printf("Can't fork\n"); break;
276 if (chdir("newdir") != 0) e(15);
277 Creat("/tmp/sema.11a");
278 while (stat("/tmp/sema.11a", &st1
) == 0) sleep(1);
282 if (stat_loc
!= 0) e(16); /* Alarm? */
286 if (chdir("olddir") != 0) e(17);
287 Creat("/tmp/sema.11b");
288 while (stat("/tmp/sema.11b", &st1
) == 0) sleep(1);
291 /* Wait for child A. It will keep ``newdir'' bussy. */
292 while (stat("/tmp/sema.11a", &st1
) == -1) sleep(1);
293 if (rmdir("newdir") == -1) {
294 if (rename("olddir", "newdir") != -1) e(18);
295 if (errno
!= EBUSY
) e(19);
297 (void) unlink("/tmp/sema.11a");
299 /* Wait for child B. It will keep ``olddir'' bussy. */
300 while (stat("/tmp/sema.11b", &st1
) == -1) sleep(1);
301 if (rmdir("olddir") == -1) {
302 if (rename("olddir", "newdir") != -1) e(20);
303 if (errno
!= EBUSY
) e(21);
305 (void) unlink("/tmp/sema.11b");
307 if (stat_loc
!= 0) e(22); /* Alarm? */
316 max_name_length
= name_max("."); /* Aka NAME_MAX, but not every FS supports
317 * the same length, hence runtime check */
318 MaxName
= malloc(max_name_length
+ 1);
319 ToLongName
= malloc(max_name_length
+ 1 + 1); /* Name of maximum +1 length */
320 memset(MaxName
, 'a', max_name_length
);
321 MaxName
[max_name_length
] = '\0';
323 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
327 MaxPath
[PATH_MAX
- 1] = '\0';
329 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
330 strcpy(ToLongPath
, MaxPath
);
332 ToLongName
[max_name_length
] = 'a';
333 ToLongName
[max_name_length
+1] = '\0';/* extend ToLongName by one too many */
334 ToLongPath
[PATH_MAX
- 1] = '/';
335 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */