Update.
[glibc.git] / posix / tst-dir.c
blob5946ea9d8c8c14bdc24f638e2e97bf69e87fa2cb
1 /* Copyright (C) 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <mcheck.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
31 /* We expect four arguments:
32 - source directory name
33 - object directory
34 - common object directory
35 - the program name with path
37 int
38 main (int argc, char *argv[])
40 const char *srcdir;
41 const char *objdir;
42 const char *common_objdir;
43 const char *progpath;
44 struct stat64 st1;
45 struct stat64 st2;
46 struct stat64 st3;
47 DIR *dir1;
48 DIR *dir2;
49 int result = 0;
50 struct dirent64 *d;
51 struct dirent64 direntbuf;
52 char *objdir_copy1;
53 char *objdir_copy2;
54 char *buf;
55 int fd;
57 mtrace ();
59 if (argc < 5)
61 puts ("not enough parameters");
62 exit (1);
65 /* Make parameters available with nicer names. */
66 srcdir = argv[1];
67 objdir = argv[2];
68 common_objdir = argv[3];
69 progpath = argv[4];
71 /* First test the current source dir. We cannot really compare the
72 result of `getpwd' with the srcdir string but we have other means. */
73 if (stat64 (".", &st1) < 0)
75 printf ("cannot stat starting directory: %m\n");
76 exit (1);
79 if (chdir (srcdir) < 0)
81 printf ("cannot change to source directory: %m\n");
82 exit (1);
84 if (stat64 (".", &st2) < 0)
86 printf ("cannot stat source directory: %m\n");
87 exit (1);
90 /* The two last stat64 calls better were for the same directory. */
91 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
93 printf ("stat of source directory failed: (%lld,%lld) vs (%lld,%lld)\n",
94 (long long int) st1.st_dev, (long long int) st1.st_ino,
95 (long long int) st2.st_dev, (long long int) st2.st_ino);
96 exit (1);
99 /* Change to the object directory. */
100 if (chdir (objdir) < 0)
102 printf ("cannot change to object directory: %m\n");
103 exit (1);
105 if (stat64 (".", &st1) < 0)
107 printf ("cannot stat object directory: %m\n");
108 exit (1);
110 /* Is this the same we get as with the full path? */
111 if (stat64 (objdir, &st2) < 0)
113 printf ("cannot stat object directory with full path: %m\n");
114 exit (1);
116 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
118 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
119 (long long int) st1.st_dev, (long long int) st1.st_ino,
120 (long long int) st2.st_dev, (long long int) st2.st_ino);
121 exit (1);
124 objdir_copy1 = getcwd (NULL, 0);
125 if (objdir_copy1 == NULL)
127 printf ("cannot get current directory name for object directory: %m\n");
128 result = 1;
131 /* First test: this directory must include our program. */
132 if (stat64 (progpath, &st2) < 0)
134 printf ("cannot stat program: %m\n");
135 exit (1);
138 dir1 = opendir (".");
139 if (dir1 == NULL)
141 printf ("cannot open object directory: %m\n");
142 exit (1);
145 while ((d = readdir64 (dir1)) != NULL)
147 #ifdef _DIRENT_HAVE_D_TYPE
148 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
149 continue;
150 #endif
152 if (d->d_ino == st2.st_ino)
154 /* Might be it. Test the device. We could use the st_dev
155 element from st1 but what the heck, do more testing. */
156 if (stat64 (d->d_name, &st3) < 0)
158 printf ("cannot stat entry from readdir: %m\n");
159 result = 1;
160 d = NULL;
161 break;
164 if (st3.st_dev == st2.st_dev)
165 break;
169 if (d == NULL)
171 puts ("haven't found program in object directory");
172 result = 1;
175 /* We leave dir1 open. */
177 /* Stat using file descriptor. */
178 if (fstat64 (dirfd (dir1), &st2) < 0)
180 printf ("cannot fstat object directory: %m\n");
181 result = 1;
183 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
185 printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
186 (long long int) st1.st_dev, (long long int) st1.st_ino,
187 (long long int) st2.st_dev, (long long int) st2.st_ino);
188 exit (1);
191 if (chdir ("..") < 0)
193 printf ("cannot go to common object directory with \"..\": %m\n");
194 exit (1);
197 if (stat64 (".", &st1) < 0)
199 printf ("cannot stat common object directory: %m\n");
200 exit (1);
202 /* Is this the same we get as with the full path? */
203 if (stat64 (common_objdir, &st2) < 0)
205 printf ("cannot stat common object directory with full path: %m\n");
206 exit (1);
208 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
210 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
211 (long long int) st1.st_dev, (long long int) st1.st_ino,
212 (long long int) st2.st_dev, (long long int) st2.st_ino);
213 exit (1);
216 /* Stat using file descriptor. */
217 if (fstat64 (dirfd (dir1), &st2) < 0)
219 printf ("cannot fstat object directory: %m\n");
220 result = 1;
223 dir2 = opendir (common_objdir);
224 if (dir2 == NULL)
226 printf ("cannot open common object directory: %m\n");
227 exit (1);
230 while ((d = readdir64 (dir2)) != NULL)
232 #ifdef _DIRENT_HAVE_D_TYPE
233 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
234 continue;
235 #endif
237 if (d->d_ino == st2.st_ino)
239 /* Might be it. Test the device. We could use the st_dev
240 element from st1 but what the heck, do more testing. */
241 if (stat64 (d->d_name, &st3) < 0)
243 printf ("cannot stat entry from readdir: %m\n");
244 result = 1;
245 d = NULL;
246 break;
249 if (st3.st_dev == st2.st_dev)
250 break;
254 /* This better should be the object directory again. */
255 if (fchdir (dirfd (dir1)) < 0)
257 printf ("cannot fchdir to object directory: %m\n");
258 exit (1);
261 objdir_copy2 = getcwd (NULL, 0);
262 if (objdir_copy2 == NULL)
264 printf ("cannot get current directory name for object directory: %m\n");
265 result = 1;
267 if (strcmp (objdir_copy1, objdir_copy2) != 0)
269 puts ("getcwd returned a different string the second time");
270 result = 1;
273 /* This better should be the common object directory again. */
274 if (fchdir (dirfd (dir2)) < 0)
276 printf ("cannot fchdir to common object directory: %m\n");
277 exit (1);
280 if (stat64 (".", &st2) < 0)
282 printf ("cannot stat common object directory: %m\n");
283 exit (1);
285 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
287 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
288 (long long int) st1.st_dev, (long long int) st1.st_ino,
289 (long long int) st2.st_dev, (long long int) st2.st_ino);
290 exit (1);
293 buf = (char *) malloc (strlen (objdir_copy1) + 1 + sizeof "tst-dir.XXXXXX");
294 if (buf == NULL)
296 printf ("cannot allocate buffer: %m");
297 exit (1);
300 stpcpy (stpcpy (stpcpy (buf, objdir_copy1), "/"), "tst-dir.XXXXXX");
301 if (mkdtemp (buf) == NULL)
303 printf ("cannot create test directory in object directory: %m\n");
304 exit (1);
306 if (stat64 (buf, &st1) < 0)
308 printf ("cannot stat new directory \"%s\": %m\n", buf);
309 exit (1);
311 if (chmod (buf, 0700) < 0)
313 printf ("cannot change mode of new directory: %m\n");
314 exit (1);
317 /* Try to find the new directory. */
318 rewinddir (dir1);
319 while (readdir64_r (dir1, &direntbuf, &d) == 0 && d != NULL)
321 #ifdef _DIRENT_HAVE_D_TYPE
322 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
323 continue;
324 #endif
326 if (d->d_ino == st1.st_ino)
328 /* Might be it. Test the device. We could use the st_dev
329 element from st1 but what the heck, do more testing. */
330 size_t len = strlen (objdir) + 1 + _D_EXACT_NAMLEN (d) + 1;
331 char tmpbuf[len];
333 stpcpy (stpcpy (stpcpy (tmpbuf, objdir), "/"), d->d_name);
335 if (stat64 (tmpbuf, &st3) < 0)
337 printf ("cannot stat entry from readdir: %m\n");
338 result = 1;
339 d = NULL;
340 break;
343 if (st3.st_dev == st2.st_dev
344 && strcmp (d->d_name, buf + strlen (buf) - 14) == 0)
345 break;
349 if (d == NULL)
351 printf ("haven't found new directory \"%s\"\n", buf);
352 exit (1);
355 if (closedir (dir2) < 0)
357 printf ("closing dir2 failed: %m\n");
358 result = 1;
361 if (chdir (buf) < 0)
363 printf ("cannot change to new directory: %m\n");
364 exit (1);
367 dir2 = opendir (buf);
368 if (dir2 == NULL)
370 printf ("cannot open new directory: %m\n");
371 exit (1);
374 if (fstat64 (dirfd (dir2), &st2) < 0)
376 printf ("cannot fstat new directory \"%s\": %m\n", buf);
377 exit (1);
379 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
381 printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
382 (long long int) st1.st_dev, (long long int) st1.st_ino,
383 (long long int) st2.st_dev, (long long int) st2.st_ino);
384 exit (1);
387 if (mkdir ("another-dir", 0777) < 0)
389 printf ("cannot create \"another-dir\": %m\n");
390 exit (1);
392 fd = open ("and-a-file", O_RDWR | O_CREAT | O_EXCL, 0666);
393 if (fd == -1)
395 printf ("cannot create \"and-a-file\": %m\n");
396 exit (1);
398 close (fd);
400 /* Some tests about error reporting. */
401 errno = 0;
402 if (chdir ("and-a-file") >= 0)
404 printf ("chdir to \"and-a-file\" succeeded\n");
405 exit (1);
407 if (errno != ENOTDIR)
409 printf ("chdir to \"and-a-file\" didn't set correct error\n");
410 result = 1;
413 errno = 0;
414 if (chdir ("and-a-file/..") >= 0)
416 printf ("chdir to \"and-a-file/..\" succeeded\n");
417 exit (1);
419 if (errno != ENOTDIR)
421 printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
422 result = 1;
425 errno = 0;
426 if (chdir ("another-dir/../and-a-file") >= 0)
428 printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
429 exit (1);
431 if (errno != ENOTDIR)
433 printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
434 result = 1;
437 /* We now should have a directory and a file in the new directory. */
438 rewinddir (dir2);
439 while (readdir64_r (dir2, &direntbuf, &d) == 0 && d != NULL)
441 if (strcmp (d->d_name, ".") == 0
442 || strcmp (d->d_name, "..") == 0
443 || strcmp (d->d_name, "another-dir") == 0)
445 #ifdef _DIRENT_HAVE_D_TYPE
446 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
448 printf ("d_type for \"%s\" is wrong\n", d->d_name);
449 result = 1;
451 #endif
452 if (stat64 (d->d_name, &st3) < 0)
454 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
455 result = 1;
457 else if (! S_ISDIR (st3.st_mode))
459 printf ("\"%s\" is no directory\n", d->d_name);
460 result = 1;
463 else if (strcmp (d->d_name, "and-a-file") == 0)
465 #ifdef _DIRENT_HAVE_D_TYPE
466 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
468 printf ("d_type for \"%s\" is wrong\n", d->d_name);
469 result = 1;
471 #endif
472 if (stat64 (d->d_name, &st3) < 0)
474 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
475 result = 1;
477 else if (! S_ISREG (st3.st_mode))
479 printf ("\"%s\" is no regular file\n", d->d_name);
480 result = 1;
483 else
485 printf ("unexpected directory entry \"%s\"\n", d->d_name);
486 result = 1;
490 if (stat64 ("does-not-exist", &st1) >= 0)
492 puts ("stat for unexisting file did not fail");
493 result = 1;
496 /* Free all resources. */
498 if (closedir (dir1) < 0)
500 printf ("closing dir1 failed: %m\n");
501 result = 1;
503 if (closedir (dir2) < 0)
505 printf ("second closing dir2 failed: %m\n");
506 result = 1;
509 if (rmdir ("another-dir") < 0)
511 printf ("cannot remove \"another-dir\": %m\n");
512 result = 1;
515 if (unlink ("and-a-file") < 0)
517 printf ("cannot remove \"and-a-file\": %m\n");
518 result = 1;
521 /* One more test before we leave: mkdir() is supposed to fail with
522 EEXIST if the named file is a symlink. */
523 if (symlink ("a-symlink", "a-symlink") != 0)
525 printf ("cannot create symlink \"a-symlink\": %m\n");
526 result = 1;
528 else
530 if (mkdir ("a-symlink", 0666) == 0)
532 puts ("can make directory \"a-symlink\"");
533 result = 1;
535 else if (errno != EEXIST)
537 puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
538 result = 1;
540 if (unlink ("a-symlink") < 0)
542 printf ("cannot unlink \"a-symlink\": %m\n");
543 result = 1;
547 if (chdir (srcdir) < 0)
549 printf ("cannot change back to source directory: %m\n");
550 exit (1);
553 if (rmdir (buf) < 0)
555 printf ("cannot remove \"%s\": %m\n", buf);
556 result = 1;
558 free (objdir_copy1);
559 free (objdir_copy2);
561 if (result == 0)
562 puts ("all OK");
564 return result;