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_MAX
+ 1]; /* Name of maximum length */
27 char MaxPath
[PATH_MAX
]; /* Same for path */
28 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
29 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
31 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
32 _PROTOTYPE(void test32a
, (void));
33 _PROTOTYPE(void test32b
, (void));
34 _PROTOTYPE(void test32c
, (void));
35 _PROTOTYPE(void makelongnames
, (void));
36 _PROTOTYPE(void e
, (int number
));
37 _PROTOTYPE(void quit
, (void));
46 if (argc
== 2) m
= atoi(argv
[1]);
49 System("rm -rf DIR_32; mkdir DIR_32");
52 superuser
= (geteuid() == 0);
54 for (i
= 0; i
< ITERATIONS
; i
++) {
55 if (m
& 0001) test32a();
56 if (m
& 0002) test32b();
57 if (m
& 0004) test32c();
65 { /* Test normal operation. */
68 time_t time1
, time2
, time3
;
72 System("rm -rf ../DIR_32/*");
74 /* Test normal file renamal. */
75 System("echo haha > old");
77 if (rename("old", "new") != 0) e(1);
80 /* The status of new should be the same as old. */
81 if (st1
.st_dev
!= st2
.st_dev
) e(2);
82 if (st1
.st_ino
!= st2
.st_ino
) e(3);
83 if (st1
.st_mode
!= st2
.st_mode
) e(4);
84 if (st1
.st_nlink
!= st2
.st_nlink
) e(5);
85 if (st1
.st_uid
!= st2
.st_uid
) e(6);
86 if (st1
.st_gid
!= st2
.st_gid
) e(7);
87 if (st1
.st_rdev
!= st2
.st_rdev
) e(8);
88 if (st1
.st_size
!= st2
.st_size
) e(9);
89 if (st1
.st_atime
!= st2
.st_atime
) e(10);
90 if (st1
.st_mtime
!= st2
.st_mtime
) e(11);
91 if (st1
.st_ctime
!= st2
.st_ctime
) e(12);
93 /* If new exists, it should be removed. */
94 System("ln new new2");
95 System("echo foobar > old");
97 if (rename("old", "new") != 0) e(13);
100 /* The status of new should be the same as old. */
101 if (st1
.st_dev
!= st2
.st_dev
) e(14);
102 if (st1
.st_ino
!= st2
.st_ino
) e(15);
103 if (st1
.st_mode
!= st2
.st_mode
) e(16);
104 if (st1
.st_nlink
!= st2
.st_nlink
) e(17);
105 if (st1
.st_uid
!= st2
.st_uid
) e(18);
106 if (st1
.st_gid
!= st2
.st_gid
) e(19);
107 if (st1
.st_rdev
!= st2
.st_rdev
) e(20);
108 if (st1
.st_size
!= st2
.st_size
) e(21);
109 if (st1
.st_atime
!= st2
.st_atime
) e(22);
110 if (st1
.st_mtime
!= st2
.st_mtime
) e(23);
111 if (st1
.st_ctime
!= st2
.st_ctime
) e(24);
113 /* The link count on new2 should be one since the old new is removed. */
115 if (st1
.st_nlink
!= 1) e(25);
117 /* Check if status for "." is updated. */
121 while (time1
== time((time_t *)0))
124 rename("OLD", "NEW");
127 while (time3
== time((time_t *)0))
130 if (st1
.st_ctime
>= st2
.st_ctime
) e(26);
131 if (st1
.st_mtime
>= st2
.st_mtime
) e(27);
132 if (st1
.st_ctime
> time1
) e(28);
133 if (st1
.st_mtime
> time1
) e(29);
134 if (st1
.st_ctime
>= time2
) e(30);
135 if (st1
.st_mtime
>= time2
) e(31);
136 if (st2
.st_ctime
< time2
) e(32);
137 if (st2
.st_mtime
< time2
) e(33);
138 if (st2
.st_ctime
>= time3
) e(34);
139 if (st2
.st_mtime
>= time3
) e(35);
141 /* If the new file is removed while it's open it should still be
143 System("rm -rf new NEW old OLD");
144 if ((fd1
= creat("new", 0644)) != 3) e(36);
145 if (write(fd1
, "Hi there! I am Sammy the string", 33) != 33) e(37);
146 if (close(fd1
) != 0) e(38);
147 if ((fd1
= creat("old", 0644)) != 3) e(39);
148 if (write(fd1
, "I need a new name", 18) != 18) e(40);
149 if (close(fd1
) != 0) e(41);
150 if ((fd1
= open("new", O_RDONLY
)) != 3) e(42);
151 if ((fd2
= open("new", O_RDONLY
)) != 4) e(43);
152 if (rename("old", "new") != 0) e(44);
153 if (stat("old", &st1
) == 0) e(45);
154 if (close(fd1
) != 0) e(46);
155 if ((fd1
= open("new", O_RDONLY
)) != 3) e(47);
156 if (read(fd2
, buf
, BUF_SIZE
) != 33) e(48);
157 if (strcmp(buf
, "Hi there! I am Sammy the string") != 0) e(49);
158 if (read(fd1
, buf
, BUF_SIZE
) != 18) e(50);
159 if (strcmp(buf
, "I need a new name") != 0) e(51);
160 if (close(fd1
) != 0) e(52);
161 if (close(fd2
) != 0) e(53);
166 char MaxPath2
[PATH_MAX
]; /* Same for path */
167 char MaxName2
[NAME_MAX
+ 1]; /* Name of maximum length */
173 System("rm -rf ../DIR_32/*");
175 /* Test maximal file name length. */
176 if ((fd
= creat(MaxName
, 0777)) != 3) e(1);
177 if (close(fd
) != 0) e(2);
178 strcpy(MaxName2
, MaxName
);
179 MaxName2
[strlen(MaxName2
) - 1] ^= 1;
180 if (rename(MaxName
, MaxName2
) != 0) e(3);
181 if (rename(MaxName2
, MaxName
) != 0) e(4);
182 MaxName2
[strlen(MaxName2
) - 1] ^= 2;
183 if (rename(MaxName
, MaxName2
) != 0) e(5);
184 MaxPath
[strlen(MaxPath
) - 2] = '/';
185 MaxPath
[strlen(MaxPath
) - 1] = 'a'; /* make ././.../a */
186 if ((fd
= creat(MaxPath
, 0777)) != 3) e(6);
187 if (close(fd
) != 0) e(7);
188 strcpy(MaxPath2
, MaxPath
);
189 MaxPath2
[strlen(MaxPath2
) - 1] ^= 1;
190 if (rename(MaxPath
, MaxPath2
) != 0) e(8);
191 if (rename(MaxPath2
, MaxPath
) != 0) e(9);
192 MaxPath2
[strlen(MaxPath2
) - 1] ^= 2;
193 if (rename(MaxPath
, MaxPath2
) != 0) e(10);
194 MaxPath
[strlen(MaxPath
) - 1] = '/'; /* make ././.../a */
196 /* Test if linked files are renamable. */
197 System("> foo; ln foo bar");
198 if (rename("foo", "bar") != 0) e(11);
199 if (rename("bar", "foo") != 0) e(12);
200 System("ln foo foobar");
201 if (rename("foo", "foobar") != 0) e(13);
202 if (rename("bar", "foobar") != 0) e(14);
204 /* Since the same files have the same links.... */
205 if (rename("bar", "bar") != 0) e(15);
206 if (rename("foo", "foo") != 0) e(16);
207 if (rename("foobar", "foobar") != 0) e(17);
209 /* In ``rename(old, new)'' with new existing, there is always an new
211 for (i
= 0; i
< 5; i
++) {
212 System("echo old > old");
213 System("echo news > new");
215 case -1: printf("Can't fork\n"); break;
219 rename("old", "new");
222 while (stat("old", &st
) != 0)
223 if (stat("new", &st
) != 0) e(18);
225 if (stat_loc
!= 0) e(19); /* Alarm? */
232 { /* Test behavior under error contitions. */
237 System("rm -rf ../DIR_32/*");
239 /* Test if we have access. */
240 system("chmod 777 noacc nowrite > /dev/null 2>/dev/null");
241 system("rm -rf noacc nowrite");
243 System("mkdir noacc nowrite");
244 System("> noacc/file");
245 System("> nowrite/file");
247 System("chmod 677 noacc");
248 System("chmod 577 nowrite");
250 if (rename("noacc/file", "nono") != -1) e(1);
251 if (errno
!= EACCES
) e(2);
252 if (rename("nowrite/file", "nono") != -1) e(3);
253 if (errno
!= EACCES
) e(4);
254 if (rename("file", "noacc/file") != -1) e(5);
255 if (errno
!= EACCES
) e(6);
256 if (rename("file", "nowrite/file") != -1) e(7);
257 if (errno
!= EACCES
) e(8);
260 /* Super user heeft access. */
261 if (rename("noacc/file", "noacc/yes") != 0) e(9);
262 if (rename("nowrite/file", "nowrite/yes") != 0) e(10);
263 if (rename("file", "yes") != 0) e(11);
264 if (rename("noacc/yes", "noacc/file") != 0) e(12);
265 if (rename("nowrite/yes", "nowrite/file") != 0) e(13);
266 if (rename("yes", "file") != 0) e(14);
268 System("chmod 777 noacc nowrite");
270 /* If rmdir() doesn't remove a directory, rename() shouldn't eighter. */
271 System("mkdir newdir olddir");
272 System("rm -rf /tmp/sema.11[ab]");
274 case -1: printf("Can't fork\n"); break;
278 case -1: printf("Can't fork\n"); break;
282 if (chdir("newdir") != 0) e(15);
283 Creat("/tmp/sema.11a");
284 while (stat("/tmp/sema.11a", &st1
) == -1) sleep(1);
288 if (stat_loc
!= 0) e(16); /* Alarm? */
292 if (chdir("olddir") != 0) e(17);
293 Creat("/tmp/sema.11b");
294 while (stat("/tmp/sema.11b", &st1
) == -1) sleep(1);
297 /* Wait for child A. It will keep ``newdir'' bussy. */
298 while (stat("/tmp/sema.11a", &st1
) == -1) sleep(1);
299 if (rmdir("newdir") == -1) {
300 if (rename("olddir", "newdir") != -1) e(18);
301 if (errno
!= EBUSY
) e(19);
303 (void) unlink("/tmp/sema.11a");
305 /* Wait for child B. It will keep ``olddir'' bussy. */
306 while (stat("/tmp/sema.11b", &st1
) == -1) sleep(1);
307 if (rmdir("olddir") == -1) {
308 if (rename("olddir", "newdir") != -1) e(20);
309 if (errno
!= EBUSY
) e(21);
311 (void) unlink("/tmp/sema.11b");
313 if (stat_loc
!= 0) e(22); /* Alarm? */
321 memset(MaxName
, 'a', NAME_MAX
);
322 MaxName
[NAME_MAX
] = '\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
[NAME_MAX
] = 'a';
333 ToLongName
[NAME_MAX
+ 1] = '\0'; /* extend ToLongName by one too many */
334 ToLongPath
[PATH_MAX
- 1] = '/';
335 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */
341 int err_num
= errno
; /* Save in case printf clobbers it. */
343 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
346 if (errct
++ > MAX_ERROR
) {
347 printf("Too many errors; test aborted\n");
349 system("rm -rf DIR*");
358 System("rm -rf DIR_32");
364 printf("%d errors\n", errct
);