xref: /minix3/minix/tests/test28.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc  /* test28: mkdir() rmdir()	Author: Jan-Mark Wams (jms@cs.vu.nl) */
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc /*
4*433d6423SLionel Sambuc ** Not tested readonly file systems (EROFS.)
5*433d6423SLionel Sambuc ** Not tested fs full (ENOSPC.)
6*433d6423SLionel Sambuc ** Not really tested EBUSY.
7*433d6423SLionel Sambuc ** Not tested unlinking busy directories.
8*433d6423SLionel Sambuc */
9*433d6423SLionel Sambuc 
10*433d6423SLionel Sambuc #include <sys/types.h>
11*433d6423SLionel Sambuc #include <sys/stat.h>
12*433d6423SLionel Sambuc #include <sys/wait.h>
13*433d6423SLionel Sambuc #include <string.h>
14*433d6423SLionel Sambuc #include <stdlib.h>
15*433d6423SLionel Sambuc #include <unistd.h>
16*433d6423SLionel Sambuc #include <fcntl.h>
17*433d6423SLionel Sambuc #include <errno.h>
18*433d6423SLionel Sambuc #include <dirent.h>
19*433d6423SLionel Sambuc #include <limits.h>
20*433d6423SLionel Sambuc #include <time.h>
21*433d6423SLionel Sambuc #include <stdio.h>
22*433d6423SLionel Sambuc 
23*433d6423SLionel Sambuc int max_error = 	4;
24*433d6423SLionel Sambuc #include "common.h"
25*433d6423SLionel Sambuc 
26*433d6423SLionel Sambuc #define ITERATIONS      2
27*433d6423SLionel Sambuc 
28*433d6423SLionel Sambuc 
29*433d6423SLionel Sambuc #define DIRENT0		((struct dirent *) NULL)
30*433d6423SLionel Sambuc 
31*433d6423SLionel Sambuc #define System(cmd)	if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
32*433d6423SLionel Sambuc #define Chdir(dir)	if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
33*433d6423SLionel Sambuc 
34*433d6423SLionel Sambuc int subtest = 1;
35*433d6423SLionel Sambuc int superuser;
36*433d6423SLionel Sambuc char *MaxName;			/* Name of maximum length */
37*433d6423SLionel Sambuc char MaxPath[PATH_MAX];
38*433d6423SLionel Sambuc char *ToLongName;		/* Name of maximum +1 length */
39*433d6423SLionel Sambuc char ToLongPath[PATH_MAX + 1];
40*433d6423SLionel Sambuc 
41*433d6423SLionel Sambuc void test28a(void);
42*433d6423SLionel Sambuc void test28c(void);
43*433d6423SLionel Sambuc void test28b(void);
44*433d6423SLionel Sambuc void makelongnames(void);
45*433d6423SLionel Sambuc 
main(int argc,char * argv[])46*433d6423SLionel Sambuc int main(int argc, char *argv[])
47*433d6423SLionel Sambuc {
48*433d6423SLionel Sambuc   int i, m = 0xFFFF;
49*433d6423SLionel Sambuc 
50*433d6423SLionel Sambuc   sync();
51*433d6423SLionel Sambuc   if (argc == 2) m = atoi(argv[1]);
52*433d6423SLionel Sambuc   start(28);
53*433d6423SLionel Sambuc   superuser = (getuid() == 0);
54*433d6423SLionel Sambuc   makelongnames();
55*433d6423SLionel Sambuc   umask(0000);			/* no umask */
56*433d6423SLionel Sambuc 
57*433d6423SLionel Sambuc   for (i = 0; i < ITERATIONS; i++) {
58*433d6423SLionel Sambuc 	if (m & 0001) test28a();
59*433d6423SLionel Sambuc 	if (m & 0002) test28b();
60*433d6423SLionel Sambuc 	if (m & 0004) test28c();
61*433d6423SLionel Sambuc   }
62*433d6423SLionel Sambuc   quit();
63*433d6423SLionel Sambuc 
64*433d6423SLionel Sambuc   return(-1);	/* Unreachable */
65*433d6423SLionel Sambuc }
66*433d6423SLionel Sambuc 
test28a()67*433d6423SLionel Sambuc void test28a()
68*433d6423SLionel Sambuc {
69*433d6423SLionel Sambuc   int mode;			/* used in for loop */
70*433d6423SLionel Sambuc   struct stat st;
71*433d6423SLionel Sambuc   time_t time1, time2;
72*433d6423SLionel Sambuc   DIR *dirp;
73*433d6423SLionel Sambuc   struct dirent *dep;
74*433d6423SLionel Sambuc   int dot = 0, dotdot = 0;
75*433d6423SLionel Sambuc 
76*433d6423SLionel Sambuc   subtest = 1;
77*433d6423SLionel Sambuc 
78*433d6423SLionel Sambuc   System("rm -rf foo /tmp/foo");/* clean up junk */
79*433d6423SLionel Sambuc 
80*433d6423SLionel Sambuc   /* Check relative path names */
81*433d6423SLionel Sambuc   if (mkdir("./foo", 0777) != 0) e(1);	/* make a dir foo */
82*433d6423SLionel Sambuc   if (mkdir("./foo/bar", 0777) != 0) e(2);	/* make foo/bar */
83*433d6423SLionel Sambuc   if (rmdir("foo/bar") != 0) e(3);	/* delete bar */
84*433d6423SLionel Sambuc   if (mkdir("foo/../foo/bar", 0777) != 0) e(4);	/* make bar again */
85*433d6423SLionel Sambuc   if (rmdir("./foo/bar") != 0) e(5);	/* and remove again */
86*433d6423SLionel Sambuc 
87*433d6423SLionel Sambuc   /* Foo should be empty (ie. contain only "." and ".." */
88*433d6423SLionel Sambuc   if ((dirp = opendir("foo")) == (DIR *) NULL) e(6);	/* open foo */
89*433d6423SLionel Sambuc   if ((dep = readdir(dirp)) == DIRENT0) e(7);	/* get first entry */
90*433d6423SLionel Sambuc   if (strcmp(dep->d_name, ".") == 0) dot += 1;	/* record what it is */
91*433d6423SLionel Sambuc   if (strcmp(dep->d_name, "..") == 0) dotdot += 1;
92*433d6423SLionel Sambuc   if ((dep = readdir(dirp)) == DIRENT0) e(8);	/* get second entry */
93*433d6423SLionel Sambuc   if (strcmp(dep->d_name, ".") == 0) dot += 1;	/* record again */
94*433d6423SLionel Sambuc   if (strcmp(dep->d_name, "..") == 0) dotdot += 1;
95*433d6423SLionel Sambuc   if ((dep = readdir(dirp)) != DIRENT0) e(9);	/* no 3d entry */
96*433d6423SLionel Sambuc   if (dot == 1 && dotdot != 1) e(10);	/* only . and .. */
97*433d6423SLionel Sambuc   if (closedir(dirp) != 0) e(11);	/* close foo */
98*433d6423SLionel Sambuc   if (rmdir("./foo") != 0) e(12);	/* remove dir foo */
99*433d6423SLionel Sambuc 
100*433d6423SLionel Sambuc   /* Check absolute path names */
101*433d6423SLionel Sambuc   if (mkdir("/tmp/foo", 0777) != 0) e(13);
102*433d6423SLionel Sambuc   if (mkdir("/tmp/foo/bar", 0777) != 0) e(14);
103*433d6423SLionel Sambuc   if (rmdir("/tmp/foo/bar") != 0) e(15);	/* make some dirs */
104*433d6423SLionel Sambuc   if (rmdir("/tmp/foo") != 0) e(16);
105*433d6423SLionel Sambuc 
106*433d6423SLionel Sambuc   /* Check the mode arument for mkdir() */
107*433d6423SLionel Sambuc   for (mode = 0; mode <= 0777; mode++) {
108*433d6423SLionel Sambuc 	if (mkdir("foo", mode) != 0) e(17);	/* make foo */
109*433d6423SLionel Sambuc 	if (stat("foo", &st) != 0) e(18);
110*433d6423SLionel Sambuc 	if ((st.st_mode & 0777) != mode) e(19);	/* check it's mode */
111*433d6423SLionel Sambuc 	if (rmdir("foo") != 0) e(20);	/* and remove it */
112*433d6423SLionel Sambuc   }
113*433d6423SLionel Sambuc 
114*433d6423SLionel Sambuc   /* Check the stat */
115*433d6423SLionel Sambuc   time(&time1);
116*433d6423SLionel Sambuc   while (time1 >= time((time_t *)0))
117*433d6423SLionel Sambuc 	;
118*433d6423SLionel Sambuc   if (mkdir("foo", 0765) != 0) e(21);	/* make foo */
119*433d6423SLionel Sambuc   if (stat("foo", &st) != 0) e(22);
120*433d6423SLionel Sambuc   time(&time2);
121*433d6423SLionel Sambuc   while (time2 >= time((time_t *)0))
122*433d6423SLionel Sambuc 	;
123*433d6423SLionel Sambuc   time(&time2);
124*433d6423SLionel Sambuc   if (st.st_nlink != 2) e(23);
125*433d6423SLionel Sambuc   if (st.st_uid != geteuid()) e(24);
126*433d6423SLionel Sambuc   if (st.st_gid != getegid()) e(25);
127*433d6423SLionel Sambuc   if (st.st_size < 0) e(26);
128*433d6423SLionel Sambuc   if ((st.st_mode & 0777) != 0765) e(27);
129*433d6423SLionel Sambuc   if (st.st_atime <= time1) e(28);
130*433d6423SLionel Sambuc   if (st.st_atime >= time2) e(29);
131*433d6423SLionel Sambuc   if (st.st_ctime <= time1) e(30);
132*433d6423SLionel Sambuc   if (st.st_ctime >= time2) e(31);
133*433d6423SLionel Sambuc   if (st.st_mtime <= time1) e(32);
134*433d6423SLionel Sambuc   if (st.st_mtime >= time2) e(33);
135*433d6423SLionel Sambuc 
136*433d6423SLionel Sambuc   /* Check if parent is updated */
137*433d6423SLionel Sambuc   if (stat(".", &st) != 0) e(34);
138*433d6423SLionel Sambuc   time(&time2);
139*433d6423SLionel Sambuc   while (time2 >= time((time_t *)0))
140*433d6423SLionel Sambuc 	;
141*433d6423SLionel Sambuc   time(&time2);
142*433d6423SLionel Sambuc   if (st.st_ctime <= time1) e(35);
143*433d6423SLionel Sambuc   if (st.st_ctime >= time2) e(36);
144*433d6423SLionel Sambuc   if (st.st_mtime <= time1) e(37);
145*433d6423SLionel Sambuc   if (st.st_mtime >= time2) e(38);
146*433d6423SLionel Sambuc   time(&time1);
147*433d6423SLionel Sambuc   while (time1 >= time((time_t *)0))
148*433d6423SLionel Sambuc 	;
149*433d6423SLionel Sambuc   if (rmdir("foo") != 0) e(39);
150*433d6423SLionel Sambuc   if (stat(".", &st) != 0) e(40);
151*433d6423SLionel Sambuc   time(&time2);
152*433d6423SLionel Sambuc   while (time2 >= time((time_t *)0))
153*433d6423SLionel Sambuc 	;
154*433d6423SLionel Sambuc   time(&time2);
155*433d6423SLionel Sambuc   if (st.st_ctime <= time1) e(41);
156*433d6423SLionel Sambuc   if (st.st_ctime >= time2) e(42);
157*433d6423SLionel Sambuc   if (st.st_mtime <= time1) e(43);
158*433d6423SLionel Sambuc   if (st.st_mtime >= time2) e(44);
159*433d6423SLionel Sambuc }
160*433d6423SLionel Sambuc 
test28b()161*433d6423SLionel Sambuc void test28b()
162*433d6423SLionel Sambuc {				/* Test critical values. */
163*433d6423SLionel Sambuc   struct stat st;
164*433d6423SLionel Sambuc   DIR *dirp;
165*433d6423SLionel Sambuc   struct dirent *dep;
166*433d6423SLionel Sambuc   int fd;			/* file descriptor */
167*433d6423SLionel Sambuc   int other = 0, dot = 0, dotdot = 0;	/* dirent counters */
168*433d6423SLionel Sambuc   int r;			/* Intermediate result */
169*433d6423SLionel Sambuc   int rmdir_result;		/* tmp var */
170*433d6423SLionel Sambuc   int stat_loc, does_truncate;
171*433d6423SLionel Sambuc 
172*433d6423SLionel Sambuc   subtest = 2;
173*433d6423SLionel Sambuc 
174*433d6423SLionel Sambuc   System("rm -rf ../DIR_28/*");
175*433d6423SLionel Sambuc 
176*433d6423SLionel Sambuc   /* Check funny but valid path names */
177*433d6423SLionel Sambuc   if (mkdir("/../../..////.//../tmp/foo/", 0777) != 0) e(1);
178*433d6423SLionel Sambuc   if (mkdir("/tmp/foo//////..//foo//../foo/bar/", 0777) != 0) e(2);
179*433d6423SLionel Sambuc   if (rmdir("///tmp/..//tmp/foo/bar//../..//foo/bar") != 0) e(3);
180*433d6423SLionel Sambuc   if (mkdir("///tmp/foo/foobar//", 0777) != 0) e(4);
181*433d6423SLionel Sambuc   if (rmdir("/tmp/foo/foobar//") != 0) e(5);
182*433d6423SLionel Sambuc   if (rmdir("/.././/././/tmp/foo///////////////") != 0) e(6);
183*433d6423SLionel Sambuc   if (rmdir("/tmp/foo") != -1) e(7);	/* try again */
184*433d6423SLionel Sambuc 
185*433d6423SLionel Sambuc   /* Test max path ed. */
186*433d6423SLionel Sambuc   if (mkdir(MaxName, 0777) != 0) e(9);	/* make dir MaxName */
187*433d6423SLionel Sambuc   if (rmdir(MaxName) != 0) e(10);	/* and remove it */
188*433d6423SLionel Sambuc   MaxPath[strlen(MaxPath) - 2] = '/';	/* convert MaxPath */
189*433d6423SLionel Sambuc   MaxPath[strlen(MaxPath) - 1] = 'a';	/* to ././.../a */
190*433d6423SLionel Sambuc   if (mkdir(MaxPath, 0777) != 0) e(11);	/* it should be */
191*433d6423SLionel Sambuc   if (rmdir(MaxPath) != 0) e(12);	/* ok */
192*433d6423SLionel Sambuc 
193*433d6423SLionel Sambuc   /* Test too long path ed. */
194*433d6423SLionel Sambuc   does_truncate = does_fs_truncate();
195*433d6423SLionel Sambuc   r =  mkdir(ToLongName, 0777);
196*433d6423SLionel Sambuc   if (does_truncate ) {
197*433d6423SLionel Sambuc   	/* FS truncates names, mkdir should've worked */
198*433d6423SLionel Sambuc   	if (r != 0) e(13);	/* Try ToLongName */
199*433d6423SLionel Sambuc 	if (rmdir(ToLongName) != 0) e(14);	/* and remove it */
200*433d6423SLionel Sambuc   } else {
201*433d6423SLionel Sambuc   	/* Too long, should've failed with ENAMETOOLONG */
202*433d6423SLionel Sambuc   	if (r == 0) e(15);
203*433d6423SLionel Sambuc   	if (errno != ENAMETOOLONG) e(16);
204*433d6423SLionel Sambuc   }
205*433d6423SLionel Sambuc   ToLongPath[strlen(ToLongPath) - 2] = '/';	/* make ToLongPath */
206*433d6423SLionel Sambuc   ToLongPath[strlen(ToLongPath) - 1] = 'a';	/* contain ././.../a */
207*433d6423SLionel Sambuc   if (mkdir(ToLongPath, 0777) != -1) e(17);	/* it should */
208*433d6423SLionel Sambuc   if (errno != ENAMETOOLONG) e(18);	/* not be ok */
209*433d6423SLionel Sambuc   if (rmdir(ToLongPath) != -1) e(19);
210*433d6423SLionel Sambuc   if (errno != ENAMETOOLONG) e(20);
211*433d6423SLionel Sambuc 
212*433d6423SLionel Sambuc   if (mkdir("foo", 0777) != 0) e(21);
213*433d6423SLionel Sambuc   System("touch foo/xyzzy");
214*433d6423SLionel Sambuc 
215*433d6423SLionel Sambuc   /* Test if rmdir removes only empty dirs */
216*433d6423SLionel Sambuc   if (rmdir("foo") != -1) e(29);/* not empty */
217*433d6423SLionel Sambuc   if (errno != EEXIST && errno != ENOTEMPTY) e(30);
218*433d6423SLionel Sambuc   /* Test if rmdir removes a dir with an empty file (it shouldn't.) */
219*433d6423SLionel Sambuc   System("rm -rf foo");		/* cleanup */
220*433d6423SLionel Sambuc   if (mkdir("foo", 0777) != 0) e(31);
221*433d6423SLionel Sambuc   System("> foo/empty");	/* > empty */
222*433d6423SLionel Sambuc   if (rmdir("foo") != -1) e(32);/* not empty */
223*433d6423SLionel Sambuc   if (errno != EEXIST && errno != ENOTEMPTY) e(33);
224*433d6423SLionel Sambuc   if (unlink("foo/empty") != 0) e(34);	/* rm empty */
225*433d6423SLionel Sambuc 
226*433d6423SLionel Sambuc   /* See what happens if foo is linked. */
227*433d6423SLionel Sambuc #if 0
228*433d6423SLionel Sambuc   if (superuser) {
229*433d6423SLionel Sambuc 	if (link("foo", "footoo") != 0) e(35);	/* foo still */
230*433d6423SLionel Sambuc 	if (rmdir("footoo") != 0) e(36);	/* exist */
231*433d6423SLionel Sambuc 	if (chdir("footoo") != -1) e(37);	/* footoo */
232*433d6423SLionel Sambuc 	if (errno != ENOENT) e(38);	/* is gone */
233*433d6423SLionel Sambuc   }
234*433d6423SLionel Sambuc #endif
235*433d6423SLionel Sambuc #if defined(__minix) && defined(_NETBSD_SOURCE)
236*433d6423SLionel Sambuc   /* Some implementations might allow users to link directories. */
237*433d6423SLionel Sambuc   if (!superuser) {
238*433d6423SLionel Sambuc 	if (link("foo", "footoo") != -1) e(39);
239*433d6423SLionel Sambuc 	if (errno != EPERM) e(40);
240*433d6423SLionel Sambuc 	if (unlink("foo") != -1) e(41);
241*433d6423SLionel Sambuc 	if (errno != EPERM) e(42);
242*433d6423SLionel Sambuc   }
243*433d6423SLionel Sambuc #endif
244*433d6423SLionel Sambuc 
245*433d6423SLionel Sambuc   /* See if ".." and "." are removed from the dir, and if it is
246*433d6423SLionel Sambuc    * unwriteable
247*433d6423SLionel Sambuc    * Note, we can not remove any files in the PARENT
248*433d6423SLionel Sambuc    * process, because this
249*433d6423SLionel Sambuc    * will make readdir unpredicatble. (see
250*433d6423SLionel Sambuc    * 1003.1 page 84 line 30.) However
251*433d6423SLionel Sambuc    * removal of the directory is
252*433d6423SLionel Sambuc    * not specified in the standard.
253*433d6423SLionel Sambuc    */
254*433d6423SLionel Sambuc   System("rm -rf /tmp/sema[12].07");
255*433d6423SLionel Sambuc   switch (fork()) {
256*433d6423SLionel Sambuc       case -1:	printf("Can't fork\n");	break;
257*433d6423SLionel Sambuc 
258*433d6423SLionel Sambuc       case 0:
259*433d6423SLionel Sambuc 	alarm(20);
260*433d6423SLionel Sambuc 	if ((fd = open("foo", O_RDONLY)) <= 2) e(43);	/* open */
261*433d6423SLionel Sambuc 	if ((dirp = opendir("foo")) == (DIR *) NULL) e(44);	/* opendir */
262*433d6423SLionel Sambuc 	/* UpA downB */
263*433d6423SLionel Sambuc 	system(">/tmp/sema1.07; while test -f /tmp/sema1.07; do sleep 1;done");
264*433d6423SLionel Sambuc 	while ((dep = readdir(dirp)) != DIRENT0) {
265*433d6423SLionel Sambuc 		if (strcmp(dep->d_name, "..") == 0)
266*433d6423SLionel Sambuc 			dotdot += 1;
267*433d6423SLionel Sambuc 		else if (strcmp(dep->d_name, ".") == 0)
268*433d6423SLionel Sambuc 			dot += 1;
269*433d6423SLionel Sambuc 		else
270*433d6423SLionel Sambuc 			other += 1;
271*433d6423SLionel Sambuc 	}
272*433d6423SLionel Sambuc 	if (dotdot != 0) e(45);	/* no entrys */
273*433d6423SLionel Sambuc 	if (dot != 0) e(46);	/* shoul be */
274*433d6423SLionel Sambuc 	if (other != 0) e(47);	/* left or */
275*433d6423SLionel Sambuc 
276*433d6423SLionel Sambuc 	/* No new files (entrys) are allowed on foo */
277*433d6423SLionel Sambuc 	if (creat("foo/nono", 0777) != -1) e(48);	/* makeable */
278*433d6423SLionel Sambuc 	if (closedir(dirp) != 0) e(49);	/* close foo */
279*433d6423SLionel Sambuc 	system("while test ! -f /tmp/sema2.07; do sleep 1; done");  /* downA */
280*433d6423SLionel Sambuc 	System("rm -f /tmp/sema2.07");	/* clean up */
281*433d6423SLionel Sambuc 
282*433d6423SLionel Sambuc 	/* Foo still exist, so we should be able to get a fstat */
283*433d6423SLionel Sambuc 	if (fstat(fd, &st) != 0) e(50);
284*433d6423SLionel Sambuc 	if (st.st_nlink != (nlink_t) 0) e(51);	/* 0 left */
285*433d6423SLionel Sambuc 	if (close(fd) != 0) e(52);	/* last one */
286*433d6423SLionel Sambuc 	exit(0);
287*433d6423SLionel Sambuc 
288*433d6423SLionel Sambuc       default:
289*433d6423SLionel Sambuc 	system("while test ! -f /tmp/sema1.07; do sleep 1; done");  /* downA */
290*433d6423SLionel Sambuc 	if (rmdir("foo") != 0) e(53);	/* cleanerup */
291*433d6423SLionel Sambuc 	System("rm -f /tmp/sema1.07");	/* upB */
292*433d6423SLionel Sambuc 	if (chdir("foo") != -1) e(54);	/* it should */
293*433d6423SLionel Sambuc 	if (errno != ENOENT) e(55);	/* be gone */
294*433d6423SLionel Sambuc 	System("> /tmp/sema2.07");	/* upA */
295*433d6423SLionel Sambuc 	if (wait(&stat_loc) == -1) e(56);
296*433d6423SLionel Sambuc 	if (stat_loc != 0) e(57);
297*433d6423SLionel Sambuc   }
298*433d6423SLionel Sambuc 
299*433d6423SLionel Sambuc   /* See if foo isn't accessible any more */
300*433d6423SLionel Sambuc   if (chdir("foo") != -1) e(58);
301*433d6423SLionel Sambuc   if (errno != ENOENT) e(59);
302*433d6423SLionel Sambuc 
303*433d6423SLionel Sambuc   /* Let's see if we can get a EBUSSY..... */
304*433d6423SLionel Sambuc   if (mkdir("foo", 0777) != 0) e(60);	/* mkdir foo */
305*433d6423SLionel Sambuc   System("rm -f /tmp/sema.07");	/* unness */
306*433d6423SLionel Sambuc   switch (fork()) {
307*433d6423SLionel Sambuc       case -1:	printf("Can't fork\n");	break;
308*433d6423SLionel Sambuc       case 0:
309*433d6423SLionel Sambuc 	alarm(20);
310*433d6423SLionel Sambuc 	if (chdir("foo") != 0) e(61);	/* child goes */
311*433d6423SLionel Sambuc 	System("> /tmp/sema.07");	/* upA */
312*433d6423SLionel Sambuc 	system("while test -f /tmp/sema.07; do sleep 1; done");	/* downB */
313*433d6423SLionel Sambuc 	sleep(1);
314*433d6423SLionel Sambuc 	exit(0);
315*433d6423SLionel Sambuc       default:
316*433d6423SLionel Sambuc 	system("while test ! -f /tmp/sema.07; do sleep 1; done");   /* downA */
317*433d6423SLionel Sambuc 	rmdir_result = rmdir("foo");	/* try remove */
318*433d6423SLionel Sambuc 	if (rmdir_result == -1) {	/* if it failed */
319*433d6423SLionel Sambuc 		if (errno != EBUSY) e(62);	/* foo is busy */
320*433d6423SLionel Sambuc 	} else {
321*433d6423SLionel Sambuc 		if (rmdir_result != 0) e(63);
322*433d6423SLionel Sambuc 		if (rmdir("foo") != -1) e(64);	/* not removable */
323*433d6423SLionel Sambuc 		if (errno != ENOENT) e(65);	/* again. */
324*433d6423SLionel Sambuc 		if (chdir("foo") != -1) e(66);	/* we can't go */
325*433d6423SLionel Sambuc 		if (errno != ENOENT) e(67);	/* there any more */
326*433d6423SLionel Sambuc 		if (mkdir("foo", 0777) != 0) e(68);	/* we can remake foo */
327*433d6423SLionel Sambuc 	}
328*433d6423SLionel Sambuc 	System("rm -f /tmp/sema.07");	/* upB */
329*433d6423SLionel Sambuc 	if (wait(&stat_loc) == -1) e(69);
330*433d6423SLionel Sambuc 	if (stat_loc != 0) e(70);
331*433d6423SLionel Sambuc   }
332*433d6423SLionel Sambuc   if (rmdir("foo") != 0) e(71);	/* clean up */
333*433d6423SLionel Sambuc }
334*433d6423SLionel Sambuc 
test28c()335*433d6423SLionel Sambuc void test28c()
336*433d6423SLionel Sambuc {				/* Test error handeling. */
337*433d6423SLionel Sambuc   subtest = 3;
338*433d6423SLionel Sambuc 
339*433d6423SLionel Sambuc   System("rm -rf ../DIR_28/*");
340*433d6423SLionel Sambuc   System("rm -rf foo /tmp/foo");/* clean up junk */
341*433d6423SLionel Sambuc 
342*433d6423SLionel Sambuc   /* Test common errors */
343*433d6423SLionel Sambuc   if (mkdir("foo", 0777) != 0) e(1);	/* mkdir shouldn't fail */
344*433d6423SLionel Sambuc   if (mkdir("foo", 0777) != -1) e(2);	/* should fail the 2d time */
345*433d6423SLionel Sambuc   if (errno != EEXIST) e(3);	/* because it exists already */
346*433d6423SLionel Sambuc   if (rmdir("foo") != 0) e(4);	/* rmdir shouldn't fail */
347*433d6423SLionel Sambuc   if (rmdir("foo") != -1) e(5);	/* but it should now because */
348*433d6423SLionel Sambuc   if (errno != ENOENT) e(6);	/* it's gone the 1st time */
349*433d6423SLionel Sambuc   /* Test on access etc. */
350*433d6423SLionel Sambuc   if (mkdir("foo", 0777) != 0) e(7);
351*433d6423SLionel Sambuc   if (mkdir("foo/bar", 0777) != 0) e(8);
352*433d6423SLionel Sambuc   if (!superuser) {
353*433d6423SLionel Sambuc 	System("chmod 677 foo");/* make foo inaccesable */
354*433d6423SLionel Sambuc 	if (mkdir("foo/foo", 0777) != -1) e(9);
355*433d6423SLionel Sambuc 	if (errno != EACCES) e(10);
356*433d6423SLionel Sambuc 	if (rmdir("foo/bar") != -1) e(11);
357*433d6423SLionel Sambuc 	if (errno != EACCES) e(12);
358*433d6423SLionel Sambuc 	System("chmod 577 foo");/* make foo unwritable */
359*433d6423SLionel Sambuc 	if (mkdir("foo/foo", 0777) != -1) e(13);
360*433d6423SLionel Sambuc 	if (errno != EACCES) e(14);
361*433d6423SLionel Sambuc 	if (rmdir("foo/bar") != -1) e(15);
362*433d6423SLionel Sambuc 	if (errno != EACCES) e(16);
363*433d6423SLionel Sambuc 	System("chmod 777 foo");/* make foo full accessable */
364*433d6423SLionel Sambuc   }
365*433d6423SLionel Sambuc   if (rmdir("foo/bar") != 0) e(17);	/* bar should be removable */
366*433d6423SLionel Sambuc   if (mkdir("foo/no/foo", 0777) != -1) e(18);	/* Note: "no" doesn't exist */
367*433d6423SLionel Sambuc   if (errno != ENOENT) e(19);
368*433d6423SLionel Sambuc   if (mkdir("", 0777) != -1) e(20);	/* empty string isn't ok */
369*433d6423SLionel Sambuc   if (errno != ENOENT) e(21);
370*433d6423SLionel Sambuc   if (rmdir("") != -1) e(22);	/* empty string isn't ok */
371*433d6423SLionel Sambuc   if (errno != ENOENT) e(23);
372*433d6423SLionel Sambuc   System("> foo/no");		/* make a file "no" */
373*433d6423SLionel Sambuc   if (mkdir("foo/no/foo", 0777) != -1) e(24);
374*433d6423SLionel Sambuc   if (errno != ENOTDIR) e(25);	/* note: "no" is not a a dir */
375*433d6423SLionel Sambuc   if (rmdir("foo/no/foo") != -1) e(26);
376*433d6423SLionel Sambuc   if (errno != ENOTDIR) e(27);
377*433d6423SLionel Sambuc   System("rm -rf foo");		/* clean up */
378*433d6423SLionel Sambuc }
379*433d6423SLionel Sambuc 
makelongnames()380*433d6423SLionel Sambuc void makelongnames()
381*433d6423SLionel Sambuc {
382*433d6423SLionel Sambuc   register int i;
383*433d6423SLionel Sambuc   int max_name_length;
384*433d6423SLionel Sambuc 
385*433d6423SLionel Sambuc   max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
386*433d6423SLionel Sambuc 				    * the same length, hence runtime check */
387*433d6423SLionel Sambuc   MaxName = malloc(max_name_length + 1);
388*433d6423SLionel Sambuc   ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
389*433d6423SLionel Sambuc   memset(MaxName, 'a', max_name_length);
390*433d6423SLionel Sambuc   MaxName[max_name_length] = '\0';
391*433d6423SLionel Sambuc 
392*433d6423SLionel Sambuc   for (i = 0; i < PATH_MAX - 1; i++) {	/* idem path */
393*433d6423SLionel Sambuc 	MaxPath[i++] = '.';
394*433d6423SLionel Sambuc 	MaxPath[i] = '/';
395*433d6423SLionel Sambuc   }
396*433d6423SLionel Sambuc   MaxPath[PATH_MAX - 1] = '\0';
397*433d6423SLionel Sambuc 
398*433d6423SLionel Sambuc   strcpy(ToLongName, MaxName);	/* copy them Max to ToLong */
399*433d6423SLionel Sambuc   strcpy(ToLongPath, MaxPath);
400*433d6423SLionel Sambuc 
401*433d6423SLionel Sambuc   ToLongName[max_name_length] = 'a';
402*433d6423SLionel Sambuc   ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
403*433d6423SLionel Sambuc   ToLongPath[PATH_MAX - 1] = '/';
404*433d6423SLionel Sambuc   ToLongPath[PATH_MAX] = '\0';	/* inc ToLongPath by one */
405*433d6423SLionel Sambuc }
406