1 /* Copyright (C) 2000-2017 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/>. */
29 #include <libc-diag.h>
31 /* We expect four arguments:
32 - source directory name
34 - common object directory
35 - the program name with path
38 main (int argc
, char *argv
[])
42 const char *common_objdir
;
54 char room
[offsetof (struct dirent64
, d_name
[0]) + NAME_MAX
+ 1];
66 puts ("not enough parameters");
70 /* Make parameters available with nicer names. */
73 common_objdir
= argv
[3];
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");
84 if (chdir (srcdir
) < 0)
86 printf ("cannot change to source directory: %m\n");
89 if (stat64 (".", &st2
) < 0)
91 printf ("cannot stat source directory: %m\n");
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
);
104 /* Change to the object directory. */
105 if (chdir (objdir
) < 0)
107 printf ("cannot change to object directory: %m\n");
110 if (stat64 (".", &st1
) < 0)
112 printf ("cannot stat object directory: %m\n");
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");
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
);
129 objdir_copy1
= getcwd (NULL
, 0);
130 if (objdir_copy1
== NULL
)
132 printf ("cannot get current directory name for object directory: %m\n");
136 /* First test: this directory must include our program. */
137 if (stat64 (progpath
, &st2
) < 0)
139 printf ("cannot stat program: %m\n");
143 dir1
= opendir (".");
146 printf ("cannot open object directory: %m\n");
150 while ((d
= readdir64 (dir1
)) != NULL
)
152 #ifdef _DIRENT_HAVE_D_TYPE
153 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
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");
169 if (st3
.st_dev
== st2
.st_dev
)
176 puts ("haven't found program in object directory");
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");
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
);
196 if (chdir ("..") < 0)
198 printf ("cannot go to common object directory with \"..\": %m\n");
202 if (stat64 (".", &st1
) < 0)
204 printf ("cannot stat common object directory: %m\n");
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");
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
);
221 /* Stat using file descriptor. */
222 if (fstat64 (dirfd (dir1
), &st2
) < 0)
224 printf ("cannot fstat object directory: %m\n");
228 dir2
= opendir (common_objdir
);
231 printf ("cannot open common object directory: %m\n");
235 while ((d
= readdir64 (dir2
)) != NULL
)
237 #ifdef _DIRENT_HAVE_D_TYPE
238 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
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");
254 if (st3
.st_dev
== st2
.st_dev
)
259 /* This better should be the object directory again. */
260 if (fchdir (dirfd (dir1
)) < 0)
262 printf ("cannot fchdir to object directory: %m\n");
266 objdir_copy2
= getcwd (NULL
, 0);
267 if (objdir_copy2
== NULL
)
269 printf ("cannot get current directory name for object directory: %m\n");
272 if (strcmp (objdir_copy1
, objdir_copy2
) != 0)
274 puts ("getcwd returned a different string the second time");
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");
285 if (stat64 (".", &st2
) < 0)
287 printf ("cannot stat common object directory: %m\n");
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
);
298 buf
= (char *) malloc (strlen (objdir_copy1
) + 1 + sizeof "tst-dir.XXXXXX");
301 printf ("cannot allocate buffer: %m");
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");
311 if (stat64 (buf
, &st1
) < 0)
313 printf ("cannot stat new directory \"%s\": %m\n", buf
);
316 if (chmod (buf
, 0700) < 0)
318 printf ("cannot change mode of new directory: %m\n");
322 /* The test below covers the deprecated readdir64_r function. */
323 DIAG_PUSH_NEEDS_COMMENT
;
324 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
326 /* Try to find the new directory. */
328 while (readdir64_r (dir1
, &direntbuf
.d
, &d
) == 0 && d
!= NULL
)
330 #ifdef _DIRENT_HAVE_D_TYPE
331 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
335 if (d
->d_ino
== st1
.st_ino
)
337 /* Might be it. Test the device. We could use the st_dev
338 element from st1 but what the heck, do more testing. */
339 size_t len
= strlen (objdir
) + 1 + _D_EXACT_NAMLEN (d
) + 1;
342 stpcpy (stpcpy (stpcpy (tmpbuf
, objdir
), "/"), d
->d_name
);
344 if (stat64 (tmpbuf
, &st3
) < 0)
346 printf ("cannot stat entry from readdir: %m\n");
352 if (st3
.st_dev
== st2
.st_dev
353 && strcmp (d
->d_name
, buf
+ strlen (buf
) - 14) == 0)
358 DIAG_POP_NEEDS_COMMENT
;
362 printf ("haven't found new directory \"%s\"\n", buf
);
366 if (closedir (dir2
) < 0)
368 printf ("closing dir2 failed: %m\n");
374 printf ("cannot change to new directory: %m\n");
378 dir2
= opendir (buf
);
381 printf ("cannot open new directory: %m\n");
385 if (fstat64 (dirfd (dir2
), &st2
) < 0)
387 printf ("cannot fstat new directory \"%s\": %m\n", buf
);
390 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
392 printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
393 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
394 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
398 if (mkdir ("another-dir", 0777) < 0)
400 printf ("cannot create \"another-dir\": %m\n");
403 fd
= open ("and-a-file", O_RDWR
| O_CREAT
| O_EXCL
, 0666);
406 printf ("cannot create \"and-a-file\": %m\n");
411 /* Some tests about error reporting. */
413 if (chdir ("and-a-file") >= 0)
415 printf ("chdir to \"and-a-file\" succeeded\n");
418 if (errno
!= ENOTDIR
)
420 printf ("chdir to \"and-a-file\" didn't set correct error\n");
425 if (chdir ("and-a-file/..") >= 0)
427 printf ("chdir to \"and-a-file/..\" succeeded\n");
430 if (errno
!= ENOTDIR
)
432 printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
437 if (chdir ("another-dir/../and-a-file") >= 0)
439 printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
442 if (errno
!= ENOTDIR
)
444 printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
448 /* The test below covers the deprecated readdir64_r function. */
449 DIAG_PUSH_NEEDS_COMMENT
;
450 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
452 /* We now should have a directory and a file in the new directory. */
454 while (readdir64_r (dir2
, &direntbuf
.d
, &d
) == 0 && d
!= NULL
)
456 if (strcmp (d
->d_name
, ".") == 0
457 || strcmp (d
->d_name
, "..") == 0
458 || strcmp (d
->d_name
, "another-dir") == 0)
460 #ifdef _DIRENT_HAVE_D_TYPE
461 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
463 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
467 if (stat64 (d
->d_name
, &st3
) < 0)
469 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
472 else if (! S_ISDIR (st3
.st_mode
))
474 printf ("\"%s\" is no directory\n", d
->d_name
);
478 else if (strcmp (d
->d_name
, "and-a-file") == 0)
480 #ifdef _DIRENT_HAVE_D_TYPE
481 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
483 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
487 if (stat64 (d
->d_name
, &st3
) < 0)
489 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
492 else if (! S_ISREG (st3
.st_mode
))
494 printf ("\"%s\" is no regular file\n", d
->d_name
);
500 printf ("unexpected directory entry \"%s\"\n", d
->d_name
);
505 DIAG_POP_NEEDS_COMMENT
;
507 if (stat64 ("does-not-exist", &st1
) >= 0)
509 puts ("stat for unexisting file did not fail");
513 /* Free all resources. */
515 if (closedir (dir1
) < 0)
517 printf ("closing dir1 failed: %m\n");
520 if (closedir (dir2
) < 0)
522 printf ("second closing dir2 failed: %m\n");
526 if (rmdir ("another-dir") < 0)
528 printf ("cannot remove \"another-dir\": %m\n");
532 if (unlink ("and-a-file") < 0)
534 printf ("cannot remove \"and-a-file\": %m\n");
538 /* One more test before we leave: mkdir() is supposed to fail with
539 EEXIST if the named file is a symlink. */
540 if (symlink ("a-symlink", "a-symlink") != 0)
542 printf ("cannot create symlink \"a-symlink\": %m\n");
547 if (mkdir ("a-symlink", 0666) == 0)
549 puts ("can make directory \"a-symlink\"");
552 else if (errno
!= EEXIST
)
554 puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
557 if (unlink ("a-symlink") < 0)
559 printf ("cannot unlink \"a-symlink\": %m\n");
564 if (chdir (srcdir
) < 0)
566 printf ("cannot change back to source directory: %m\n");
572 printf ("cannot remove \"%s\": %m\n", buf
);