1 /* Copyright (C) 2000-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
28 #include <libc-diag.h>
30 /* We expect four arguments:
31 - source directory name
33 - common object directory
34 - the program name with path
37 main (int argc
, char *argv
[])
41 const char *common_objdir
;
53 char room
[offsetof (struct dirent64
, d_name
[0]) + NAME_MAX
+ 1];
65 puts ("not enough parameters");
69 /* Make parameters available with nicer names. */
72 common_objdir
= argv
[3];
75 /* First test the current source dir. We cannot really compare the
76 result of `getpwd' with the srcdir string but we have other means. */
77 if (stat64 (".", &st1
) < 0)
79 printf ("cannot stat starting directory: %m\n");
83 if (chdir (srcdir
) < 0)
85 printf ("cannot change to source directory: %m\n");
88 if (stat64 (".", &st2
) < 0)
90 printf ("cannot stat source directory: %m\n");
94 /* The two last stat64 calls better were for the same directory. */
95 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
97 printf ("stat of source directory failed: (%lld,%lld) vs (%lld,%lld)\n",
98 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
99 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
103 /* Change to the object directory. */
104 if (chdir (objdir
) < 0)
106 printf ("cannot change to object directory: %m\n");
109 if (stat64 (".", &st1
) < 0)
111 printf ("cannot stat object directory: %m\n");
114 /* Is this the same we get as with the full path? */
115 if (stat64 (objdir
, &st2
) < 0)
117 printf ("cannot stat object directory with full path: %m\n");
120 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
122 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
123 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
124 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
128 objdir_copy1
= getcwd (NULL
, 0);
129 if (objdir_copy1
== NULL
)
131 printf ("cannot get current directory name for object directory: %m\n");
135 /* First test: this directory must include our program. */
136 if (stat64 (progpath
, &st2
) < 0)
138 printf ("cannot stat program: %m\n");
142 dir1
= opendir (".");
145 printf ("cannot open object directory: %m\n");
149 while ((d
= readdir64 (dir1
)) != NULL
)
151 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
154 if (d
->d_ino
== st2
.st_ino
)
156 /* Might be it. Test the device. We could use the st_dev
157 element from st1 but what the heck, do more testing. */
158 if (stat64 (d
->d_name
, &st3
) < 0)
160 printf ("cannot stat entry from readdir: %m\n");
166 if (st3
.st_dev
== st2
.st_dev
)
173 puts ("haven't found program in object directory");
177 /* We leave dir1 open. */
179 /* Stat using file descriptor. */
180 if (fstat64 (dirfd (dir1
), &st2
) < 0)
182 printf ("cannot fstat object directory: %m\n");
185 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
187 printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
188 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
189 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
193 if (chdir ("..") < 0)
195 printf ("cannot go to common object directory with \"..\": %m\n");
199 if (stat64 (".", &st1
) < 0)
201 printf ("cannot stat common object directory: %m\n");
204 /* Is this the same we get as with the full path? */
205 if (stat64 (common_objdir
, &st2
) < 0)
207 printf ("cannot stat common object directory with full path: %m\n");
210 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
212 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
213 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
214 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
218 /* Stat using file descriptor. */
219 if (fstat64 (dirfd (dir1
), &st2
) < 0)
221 printf ("cannot fstat object directory: %m\n");
225 dir2
= opendir (common_objdir
);
228 printf ("cannot open common object directory: %m\n");
232 while ((d
= readdir64 (dir2
)) != NULL
)
234 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
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");
249 if (st3
.st_dev
== st2
.st_dev
)
254 /* This better should be the object directory again. */
255 if (fchdir (dirfd (dir1
)) < 0)
257 printf ("cannot fchdir to object directory: %m\n");
261 objdir_copy2
= getcwd (NULL
, 0);
262 if (objdir_copy2
== NULL
)
264 printf ("cannot get current directory name for object directory: %m\n");
267 if (strcmp (objdir_copy1
, objdir_copy2
) != 0)
269 puts ("getcwd returned a different string the second time");
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");
280 if (stat64 (".", &st2
) < 0)
282 printf ("cannot stat common object directory: %m\n");
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
);
293 buf
= (char *) malloc (strlen (objdir_copy1
) + 1 + sizeof "tst-dir.XXXXXX");
296 printf ("cannot allocate buffer: %m");
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");
306 if (stat64 (buf
, &st1
) < 0)
308 printf ("cannot stat new directory \"%s\": %m\n", buf
);
311 if (chmod (buf
, 0700) < 0)
313 printf ("cannot change mode of new directory: %m\n");
317 /* The test below covers the deprecated readdir64_r function. */
318 DIAG_PUSH_NEEDS_COMMENT
;
319 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
321 /* Try to find the new directory. */
323 while (readdir64_r (dir1
, &direntbuf
.d
, &d
) == 0 && d
!= NULL
)
325 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
328 if (d
->d_ino
== st1
.st_ino
)
330 /* Might be it. Test the device. We could use the st_dev
331 element from st1 but what the heck, do more testing. */
332 size_t len
= strlen (objdir
) + 1 + _D_EXACT_NAMLEN (d
) + 1;
335 stpcpy (stpcpy (stpcpy (tmpbuf
, objdir
), "/"), d
->d_name
);
337 if (stat64 (tmpbuf
, &st3
) < 0)
339 printf ("cannot stat entry from readdir: %m\n");
345 if (st3
.st_dev
== st2
.st_dev
346 && strcmp (d
->d_name
, buf
+ strlen (buf
) - 14) == 0)
351 DIAG_POP_NEEDS_COMMENT
;
355 printf ("haven't found new directory \"%s\"\n", buf
);
359 if (closedir (dir2
) < 0)
361 printf ("closing dir2 failed: %m\n");
367 printf ("cannot change to new directory: %m\n");
371 dir2
= opendir (buf
);
374 printf ("cannot open new directory: %m\n");
378 if (fstat64 (dirfd (dir2
), &st2
) < 0)
380 printf ("cannot fstat new directory \"%s\": %m\n", buf
);
383 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
385 printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
386 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
387 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
391 if (mkdir ("another-dir", 0777) < 0)
393 printf ("cannot create \"another-dir\": %m\n");
396 fd
= open ("and-a-file", O_RDWR
| O_CREAT
| O_EXCL
, 0666);
399 printf ("cannot create \"and-a-file\": %m\n");
404 /* Some tests about error reporting. */
406 if (chdir ("and-a-file") >= 0)
408 printf ("chdir to \"and-a-file\" succeeded\n");
411 if (errno
!= ENOTDIR
)
413 printf ("chdir to \"and-a-file\" didn't set correct error\n");
418 if (chdir ("and-a-file/..") >= 0)
420 printf ("chdir to \"and-a-file/..\" succeeded\n");
423 if (errno
!= ENOTDIR
)
425 printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
430 if (chdir ("another-dir/../and-a-file") >= 0)
432 printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
435 if (errno
!= ENOTDIR
)
437 printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
441 /* The test below covers the deprecated readdir64_r function. */
442 DIAG_PUSH_NEEDS_COMMENT
;
443 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
445 /* We now should have a directory and a file in the new directory. */
447 while (readdir64_r (dir2
, &direntbuf
.d
, &d
) == 0 && d
!= NULL
)
449 if (strcmp (d
->d_name
, ".") == 0
450 || strcmp (d
->d_name
, "..") == 0
451 || strcmp (d
->d_name
, "another-dir") == 0)
453 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
455 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
458 if (stat64 (d
->d_name
, &st3
) < 0)
460 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
463 else if (! S_ISDIR (st3
.st_mode
))
465 printf ("\"%s\" is no directory\n", d
->d_name
);
469 else if (strcmp (d
->d_name
, "and-a-file") == 0)
471 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
473 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
476 if (stat64 (d
->d_name
, &st3
) < 0)
478 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
481 else if (! S_ISREG (st3
.st_mode
))
483 printf ("\"%s\" is no regular file\n", d
->d_name
);
489 printf ("unexpected directory entry \"%s\"\n", d
->d_name
);
494 DIAG_POP_NEEDS_COMMENT
;
496 if (stat64 ("does-not-exist", &st1
) >= 0)
498 puts ("stat for unexisting file did not fail");
502 /* Free all resources. */
504 if (closedir (dir1
) < 0)
506 printf ("closing dir1 failed: %m\n");
509 if (closedir (dir2
) < 0)
511 printf ("second closing dir2 failed: %m\n");
515 if (rmdir ("another-dir") < 0)
517 printf ("cannot remove \"another-dir\": %m\n");
521 if (unlink ("and-a-file") < 0)
523 printf ("cannot remove \"and-a-file\": %m\n");
527 /* One more test before we leave: mkdir() is supposed to fail with
528 EEXIST if the named file is a symlink. */
529 if (symlink ("a-symlink", "a-symlink") != 0)
531 printf ("cannot create symlink \"a-symlink\": %m\n");
536 if (mkdir ("a-symlink", 0666) == 0)
538 puts ("can make directory \"a-symlink\"");
541 else if (errno
!= EEXIST
)
543 puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
546 if (unlink ("a-symlink") < 0)
548 printf ("cannot unlink \"a-symlink\": %m\n");
553 if (chdir (srcdir
) < 0)
555 printf ("cannot change back to source directory: %m\n");
561 printf ("cannot remove \"%s\": %m\n", buf
);