hppa: fix __O_SYNC to match the kernel
[glibc.git] / posix / tst-dir.c
blob50f75807b7dab95ef33f5bbc16a5b2830b4b25c1
1 /* Copyright (C) 2000-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <mcheck.h>
23 #include <stddef.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 union
53 struct dirent64 d;
54 char room [offsetof (struct dirent64, d_name[0]) + NAME_MAX + 1];
56 direntbuf;
57 char *objdir_copy1;
58 char *objdir_copy2;
59 char *buf;
60 int fd;
62 mtrace ();
64 if (argc < 5)
66 puts ("not enough parameters");
67 exit (1);
70 /* Make parameters available with nicer names. */
71 srcdir = argv[1];
72 objdir = argv[2];
73 common_objdir = argv[3];
74 progpath = argv[4];
76 /* First test the current source dir. We cannot really compare the
77 result of `getpwd' with the srcdir string but we have other means. */
78 if (stat64 (".", &st1) < 0)
80 printf ("cannot stat starting directory: %m\n");
81 exit (1);
84 if (chdir (srcdir) < 0)
86 printf ("cannot change to source directory: %m\n");
87 exit (1);
89 if (stat64 (".", &st2) < 0)
91 printf ("cannot stat source directory: %m\n");
92 exit (1);
95 /* The two last stat64 calls better were for the same directory. */
96 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
98 printf ("stat of source directory failed: (%lld,%lld) vs (%lld,%lld)\n",
99 (long long int) st1.st_dev, (long long int) st1.st_ino,
100 (long long int) st2.st_dev, (long long int) st2.st_ino);
101 exit (1);
104 /* Change to the object directory. */
105 if (chdir (objdir) < 0)
107 printf ("cannot change to object directory: %m\n");
108 exit (1);
110 if (stat64 (".", &st1) < 0)
112 printf ("cannot stat object directory: %m\n");
113 exit (1);
115 /* Is this the same we get as with the full path? */
116 if (stat64 (objdir, &st2) < 0)
118 printf ("cannot stat object directory with full path: %m\n");
119 exit (1);
121 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
123 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
124 (long long int) st1.st_dev, (long long int) st1.st_ino,
125 (long long int) st2.st_dev, (long long int) st2.st_ino);
126 exit (1);
129 objdir_copy1 = getcwd (NULL, 0);
130 if (objdir_copy1 == NULL)
132 printf ("cannot get current directory name for object directory: %m\n");
133 result = 1;
136 /* First test: this directory must include our program. */
137 if (stat64 (progpath, &st2) < 0)
139 printf ("cannot stat program: %m\n");
140 exit (1);
143 dir1 = opendir (".");
144 if (dir1 == NULL)
146 printf ("cannot open object directory: %m\n");
147 exit (1);
150 while ((d = readdir64 (dir1)) != NULL)
152 #ifdef _DIRENT_HAVE_D_TYPE
153 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
154 continue;
155 #endif
157 if (d->d_ino == st2.st_ino)
159 /* Might be it. Test the device. We could use the st_dev
160 element from st1 but what the heck, do more testing. */
161 if (stat64 (d->d_name, &st3) < 0)
163 printf ("cannot stat entry from readdir: %m\n");
164 result = 1;
165 d = NULL;
166 break;
169 if (st3.st_dev == st2.st_dev)
170 break;
174 if (d == NULL)
176 puts ("haven't found program in object directory");
177 result = 1;
180 /* We leave dir1 open. */
182 /* Stat using file descriptor. */
183 if (fstat64 (dirfd (dir1), &st2) < 0)
185 printf ("cannot fstat object directory: %m\n");
186 result = 1;
188 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
190 printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
191 (long long int) st1.st_dev, (long long int) st1.st_ino,
192 (long long int) st2.st_dev, (long long int) st2.st_ino);
193 exit (1);
196 if (chdir ("..") < 0)
198 printf ("cannot go to common object directory with \"..\": %m\n");
199 exit (1);
202 if (stat64 (".", &st1) < 0)
204 printf ("cannot stat common object directory: %m\n");
205 exit (1);
207 /* Is this the same we get as with the full path? */
208 if (stat64 (common_objdir, &st2) < 0)
210 printf ("cannot stat common object directory with full path: %m\n");
211 exit (1);
213 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
215 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
216 (long long int) st1.st_dev, (long long int) st1.st_ino,
217 (long long int) st2.st_dev, (long long int) st2.st_ino);
218 exit (1);
221 /* Stat using file descriptor. */
222 if (fstat64 (dirfd (dir1), &st2) < 0)
224 printf ("cannot fstat object directory: %m\n");
225 result = 1;
228 dir2 = opendir (common_objdir);
229 if (dir2 == NULL)
231 printf ("cannot open common object directory: %m\n");
232 exit (1);
235 while ((d = readdir64 (dir2)) != NULL)
237 #ifdef _DIRENT_HAVE_D_TYPE
238 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
239 continue;
240 #endif
242 if (d->d_ino == st2.st_ino)
244 /* Might be it. Test the device. We could use the st_dev
245 element from st1 but what the heck, do more testing. */
246 if (stat64 (d->d_name, &st3) < 0)
248 printf ("cannot stat entry from readdir: %m\n");
249 result = 1;
250 d = NULL;
251 break;
254 if (st3.st_dev == st2.st_dev)
255 break;
259 /* This better should be the object directory again. */
260 if (fchdir (dirfd (dir1)) < 0)
262 printf ("cannot fchdir to object directory: %m\n");
263 exit (1);
266 objdir_copy2 = getcwd (NULL, 0);
267 if (objdir_copy2 == NULL)
269 printf ("cannot get current directory name for object directory: %m\n");
270 result = 1;
272 if (strcmp (objdir_copy1, objdir_copy2) != 0)
274 puts ("getcwd returned a different string the second time");
275 result = 1;
278 /* This better should be the common object directory again. */
279 if (fchdir (dirfd (dir2)) < 0)
281 printf ("cannot fchdir to common object directory: %m\n");
282 exit (1);
285 if (stat64 (".", &st2) < 0)
287 printf ("cannot stat common object directory: %m\n");
288 exit (1);
290 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
292 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
293 (long long int) st1.st_dev, (long long int) st1.st_ino,
294 (long long int) st2.st_dev, (long long int) st2.st_ino);
295 exit (1);
298 buf = (char *) malloc (strlen (objdir_copy1) + 1 + sizeof "tst-dir.XXXXXX");
299 if (buf == NULL)
301 printf ("cannot allocate buffer: %m");
302 exit (1);
305 stpcpy (stpcpy (stpcpy (buf, objdir_copy1), "/"), "tst-dir.XXXXXX");
306 if (mkdtemp (buf) == NULL)
308 printf ("cannot create test directory in object directory: %m\n");
309 exit (1);
311 if (stat64 (buf, &st1) < 0)
313 printf ("cannot stat new directory \"%s\": %m\n", buf);
314 exit (1);
316 if (chmod (buf, 0700) < 0)
318 printf ("cannot change mode of new directory: %m\n");
319 exit (1);
322 /* Try to find the new directory. */
323 rewinddir (dir1);
324 while (readdir64_r (dir1, &direntbuf.d, &d) == 0 && d != NULL)
326 #ifdef _DIRENT_HAVE_D_TYPE
327 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
328 continue;
329 #endif
331 if (d->d_ino == st1.st_ino)
333 /* Might be it. Test the device. We could use the st_dev
334 element from st1 but what the heck, do more testing. */
335 size_t len = strlen (objdir) + 1 + _D_EXACT_NAMLEN (d) + 1;
336 char tmpbuf[len];
338 stpcpy (stpcpy (stpcpy (tmpbuf, objdir), "/"), d->d_name);
340 if (stat64 (tmpbuf, &st3) < 0)
342 printf ("cannot stat entry from readdir: %m\n");
343 result = 1;
344 d = NULL;
345 break;
348 if (st3.st_dev == st2.st_dev
349 && strcmp (d->d_name, buf + strlen (buf) - 14) == 0)
350 break;
354 if (d == NULL)
356 printf ("haven't found new directory \"%s\"\n", buf);
357 exit (1);
360 if (closedir (dir2) < 0)
362 printf ("closing dir2 failed: %m\n");
363 result = 1;
366 if (chdir (buf) < 0)
368 printf ("cannot change to new directory: %m\n");
369 exit (1);
372 dir2 = opendir (buf);
373 if (dir2 == NULL)
375 printf ("cannot open new directory: %m\n");
376 exit (1);
379 if (fstat64 (dirfd (dir2), &st2) < 0)
381 printf ("cannot fstat new directory \"%s\": %m\n", buf);
382 exit (1);
384 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
386 printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
387 (long long int) st1.st_dev, (long long int) st1.st_ino,
388 (long long int) st2.st_dev, (long long int) st2.st_ino);
389 exit (1);
392 if (mkdir ("another-dir", 0777) < 0)
394 printf ("cannot create \"another-dir\": %m\n");
395 exit (1);
397 fd = open ("and-a-file", O_RDWR | O_CREAT | O_EXCL, 0666);
398 if (fd == -1)
400 printf ("cannot create \"and-a-file\": %m\n");
401 exit (1);
403 close (fd);
405 /* Some tests about error reporting. */
406 errno = 0;
407 if (chdir ("and-a-file") >= 0)
409 printf ("chdir to \"and-a-file\" succeeded\n");
410 exit (1);
412 if (errno != ENOTDIR)
414 printf ("chdir to \"and-a-file\" didn't set correct error\n");
415 result = 1;
418 errno = 0;
419 if (chdir ("and-a-file/..") >= 0)
421 printf ("chdir to \"and-a-file/..\" succeeded\n");
422 exit (1);
424 if (errno != ENOTDIR)
426 printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
427 result = 1;
430 errno = 0;
431 if (chdir ("another-dir/../and-a-file") >= 0)
433 printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
434 exit (1);
436 if (errno != ENOTDIR)
438 printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
439 result = 1;
442 /* We now should have a directory and a file in the new directory. */
443 rewinddir (dir2);
444 while (readdir64_r (dir2, &direntbuf.d, &d) == 0 && d != NULL)
446 if (strcmp (d->d_name, ".") == 0
447 || strcmp (d->d_name, "..") == 0
448 || strcmp (d->d_name, "another-dir") == 0)
450 #ifdef _DIRENT_HAVE_D_TYPE
451 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
453 printf ("d_type for \"%s\" is wrong\n", d->d_name);
454 result = 1;
456 #endif
457 if (stat64 (d->d_name, &st3) < 0)
459 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
460 result = 1;
462 else if (! S_ISDIR (st3.st_mode))
464 printf ("\"%s\" is no directory\n", d->d_name);
465 result = 1;
468 else if (strcmp (d->d_name, "and-a-file") == 0)
470 #ifdef _DIRENT_HAVE_D_TYPE
471 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
473 printf ("d_type for \"%s\" is wrong\n", d->d_name);
474 result = 1;
476 #endif
477 if (stat64 (d->d_name, &st3) < 0)
479 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
480 result = 1;
482 else if (! S_ISREG (st3.st_mode))
484 printf ("\"%s\" is no regular file\n", d->d_name);
485 result = 1;
488 else
490 printf ("unexpected directory entry \"%s\"\n", d->d_name);
491 result = 1;
495 if (stat64 ("does-not-exist", &st1) >= 0)
497 puts ("stat for unexisting file did not fail");
498 result = 1;
501 /* Free all resources. */
503 if (closedir (dir1) < 0)
505 printf ("closing dir1 failed: %m\n");
506 result = 1;
508 if (closedir (dir2) < 0)
510 printf ("second closing dir2 failed: %m\n");
511 result = 1;
514 if (rmdir ("another-dir") < 0)
516 printf ("cannot remove \"another-dir\": %m\n");
517 result = 1;
520 if (unlink ("and-a-file") < 0)
522 printf ("cannot remove \"and-a-file\": %m\n");
523 result = 1;
526 /* One more test before we leave: mkdir() is supposed to fail with
527 EEXIST if the named file is a symlink. */
528 if (symlink ("a-symlink", "a-symlink") != 0)
530 printf ("cannot create symlink \"a-symlink\": %m\n");
531 result = 1;
533 else
535 if (mkdir ("a-symlink", 0666) == 0)
537 puts ("can make directory \"a-symlink\"");
538 result = 1;
540 else if (errno != EEXIST)
542 puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
543 result = 1;
545 if (unlink ("a-symlink") < 0)
547 printf ("cannot unlink \"a-symlink\": %m\n");
548 result = 1;
552 if (chdir (srcdir) < 0)
554 printf ("cannot change back to source directory: %m\n");
555 exit (1);
558 if (rmdir (buf) < 0)
560 printf ("cannot remove \"%s\": %m\n", buf);
561 result = 1;
563 free (objdir_copy1);
564 free (objdir_copy2);
566 if (result == 0)
567 puts ("all OK");
569 return result;