]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/tst-dir.c
posix: Use enum for __glob_pattern_type result
[thirdparty/glibc.git] / posix / tst-dir.c
CommitLineData
bfff8b1b 1/* Copyright (C) 2000-2017 Free Software Foundation, Inc.
8c02ee02
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
8c02ee02
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
8c02ee02 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
8c02ee02 18
47a03c51 19#include <dirent.h>
8c02ee02 20#include <errno.h>
47a03c51
UD
21#include <fcntl.h>
22#include <mcheck.h>
ffa8d2a0 23#include <stddef.h>
47a03c51
UD
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <sys/stat.h>
e15f7de6 29#include <libc-diag.h>
47a03c51
UD
30
31/* We expect four arguments:
32 - source directory name
33 - object directory
34 - common object directory
35 - the program name with path
36*/
37int
38main (int argc, char *argv[])
39{
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;
ffa8d2a0
RM
51 union
52 {
53 struct dirent64 d;
54 char room [offsetof (struct dirent64, d_name[0]) + NAME_MAX + 1];
55 }
56 direntbuf;
47a03c51
UD
57 char *objdir_copy1;
58 char *objdir_copy2;
59 char *buf;
60 int fd;
61
62 mtrace ();
63
64 if (argc < 5)
65 {
66 puts ("not enough parameters");
67 exit (1);
68 }
69
70 /* Make parameters available with nicer names. */
71 srcdir = argv[1];
72 objdir = argv[2];
73 common_objdir = argv[3];
74 progpath = argv[4];
75
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)
79 {
80 printf ("cannot stat starting directory: %m\n");
81 exit (1);
82 }
83
84 if (chdir (srcdir) < 0)
85 {
86 printf ("cannot change to source directory: %m\n");
87 exit (1);
88 }
89 if (stat64 (".", &st2) < 0)
90 {
91 printf ("cannot stat source directory: %m\n");
92 exit (1);
93 }
94
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)
97 {
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);
102 }
103
104 /* Change to the object directory. */
105 if (chdir (objdir) < 0)
106 {
107 printf ("cannot change to object directory: %m\n");
108 exit (1);
109 }
110 if (stat64 (".", &st1) < 0)
111 {
112 printf ("cannot stat object directory: %m\n");
113 exit (1);
114 }
115 /* Is this the same we get as with the full path? */
116 if (stat64 (objdir, &st2) < 0)
117 {
118 printf ("cannot stat object directory with full path: %m\n");
119 exit (1);
120 }
121 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
122 {
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);
127 }
128
129 objdir_copy1 = getcwd (NULL, 0);
130 if (objdir_copy1 == NULL)
131 {
132 printf ("cannot get current directory name for object directory: %m\n");
133 result = 1;
134 }
135
136 /* First test: this directory must include our program. */
137 if (stat64 (progpath, &st2) < 0)
138 {
139 printf ("cannot stat program: %m\n");
140 exit (1);
141 }
142
143 dir1 = opendir (".");
144 if (dir1 == NULL)
145 {
146 printf ("cannot open object directory: %m\n");
147 exit (1);
148 }
149
150 while ((d = readdir64 (dir1)) != NULL)
151 {
152#ifdef _DIRENT_HAVE_D_TYPE
153 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
154 continue;
155#endif
156
157 if (d->d_ino == st2.st_ino)
158 {
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)
162 {
163 printf ("cannot stat entry from readdir: %m\n");
164 result = 1;
165 d = NULL;
166 break;
167 }
168
169 if (st3.st_dev == st2.st_dev)
170 break;
171 }
172 }
173
174 if (d == NULL)
175 {
176 puts ("haven't found program in object directory");
177 result = 1;
178 }
179
180 /* We leave dir1 open. */
181
182 /* Stat using file descriptor. */
183 if (fstat64 (dirfd (dir1), &st2) < 0)
184 {
185 printf ("cannot fstat object directory: %m\n");
186 result = 1;
187 }
188 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
189 {
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);
194 }
195
196 if (chdir ("..") < 0)
197 {
198 printf ("cannot go to common object directory with \"..\": %m\n");
199 exit (1);
200 }
201
202 if (stat64 (".", &st1) < 0)
203 {
204 printf ("cannot stat common object directory: %m\n");
205 exit (1);
206 }
207 /* Is this the same we get as with the full path? */
208 if (stat64 (common_objdir, &st2) < 0)
209 {
210 printf ("cannot stat common object directory with full path: %m\n");
211 exit (1);
212 }
213 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
214 {
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);
219 }
220
221 /* Stat using file descriptor. */
222 if (fstat64 (dirfd (dir1), &st2) < 0)
223 {
224 printf ("cannot fstat object directory: %m\n");
225 result = 1;
226 }
227
228 dir2 = opendir (common_objdir);
229 if (dir2 == NULL)
230 {
231 printf ("cannot open common object directory: %m\n");
232 exit (1);
233 }
234
235 while ((d = readdir64 (dir2)) != NULL)
236 {
237#ifdef _DIRENT_HAVE_D_TYPE
238 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
239 continue;
240#endif
241
242 if (d->d_ino == st2.st_ino)
243 {
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)
247 {
248 printf ("cannot stat entry from readdir: %m\n");
249 result = 1;
250 d = NULL;
251 break;
252 }
253
254 if (st3.st_dev == st2.st_dev)
255 break;
256 }
257 }
258
259 /* This better should be the object directory again. */
260 if (fchdir (dirfd (dir1)) < 0)
261 {
262 printf ("cannot fchdir to object directory: %m\n");
263 exit (1);
264 }
265
266 objdir_copy2 = getcwd (NULL, 0);
267 if (objdir_copy2 == NULL)
268 {
269 printf ("cannot get current directory name for object directory: %m\n");
270 result = 1;
271 }
272 if (strcmp (objdir_copy1, objdir_copy2) != 0)
273 {
274 puts ("getcwd returned a different string the second time");
275 result = 1;
276 }
277
278 /* This better should be the common object directory again. */
279 if (fchdir (dirfd (dir2)) < 0)
280 {
281 printf ("cannot fchdir to common object directory: %m\n");
282 exit (1);
283 }
284
285 if (stat64 (".", &st2) < 0)
286 {
287 printf ("cannot stat common object directory: %m\n");
288 exit (1);
289 }
290 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
291 {
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);
296 }
297
298 buf = (char *) malloc (strlen (objdir_copy1) + 1 + sizeof "tst-dir.XXXXXX");
299 if (buf == NULL)
300 {
301 printf ("cannot allocate buffer: %m");
302 exit (1);
303 }
304
305 stpcpy (stpcpy (stpcpy (buf, objdir_copy1), "/"), "tst-dir.XXXXXX");
306 if (mkdtemp (buf) == NULL)
307 {
308 printf ("cannot create test directory in object directory: %m\n");
309 exit (1);
310 }
311 if (stat64 (buf, &st1) < 0)
312 {
313 printf ("cannot stat new directory \"%s\": %m\n", buf);
314 exit (1);
315 }
316 if (chmod (buf, 0700) < 0)
317 {
318 printf ("cannot change mode of new directory: %m\n");
319 exit (1);
320 }
321
7584a3f9
FW
322 /* The test below covers the deprecated readdir64_r function. */
323 DIAG_PUSH_NEEDS_COMMENT;
324 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
325
47a03c51
UD
326 /* Try to find the new directory. */
327 rewinddir (dir1);
ffa8d2a0 328 while (readdir64_r (dir1, &direntbuf.d, &d) == 0 && d != NULL)
47a03c51
UD
329 {
330#ifdef _DIRENT_HAVE_D_TYPE
331 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
332 continue;
333#endif
334
335 if (d->d_ino == st1.st_ino)
336 {
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;
340 char tmpbuf[len];
341
342 stpcpy (stpcpy (stpcpy (tmpbuf, objdir), "/"), d->d_name);
343
344 if (stat64 (tmpbuf, &st3) < 0)
345 {
346 printf ("cannot stat entry from readdir: %m\n");
347 result = 1;
348 d = NULL;
349 break;
350 }
351
352 if (st3.st_dev == st2.st_dev
353 && strcmp (d->d_name, buf + strlen (buf) - 14) == 0)
354 break;
355 }
356 }
357
7584a3f9
FW
358 DIAG_POP_NEEDS_COMMENT;
359
47a03c51
UD
360 if (d == NULL)
361 {
362 printf ("haven't found new directory \"%s\"\n", buf);
363 exit (1);
364 }
365
366 if (closedir (dir2) < 0)
367 {
368 printf ("closing dir2 failed: %m\n");
369 result = 1;
370 }
371
8c02ee02 372 if (chdir (buf) < 0)
47a03c51
UD
373 {
374 printf ("cannot change to new directory: %m\n");
375 exit (1);
376 }
377
378 dir2 = opendir (buf);
379 if (dir2 == NULL)
380 {
381 printf ("cannot open new directory: %m\n");
382 exit (1);
383 }
384
385 if (fstat64 (dirfd (dir2), &st2) < 0)
386 {
387 printf ("cannot fstat new directory \"%s\": %m\n", buf);
388 exit (1);
389 }
390 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
391 {
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);
395 exit (1);
396 }
397
398 if (mkdir ("another-dir", 0777) < 0)
399 {
400 printf ("cannot create \"another-dir\": %m\n");
401 exit (1);
402 }
403 fd = open ("and-a-file", O_RDWR | O_CREAT | O_EXCL, 0666);
404 if (fd == -1)
405 {
406 printf ("cannot create \"and-a-file\": %m\n");
407 exit (1);
408 }
409 close (fd);
410
8c02ee02
UD
411 /* Some tests about error reporting. */
412 errno = 0;
413 if (chdir ("and-a-file") >= 0)
414 {
415 printf ("chdir to \"and-a-file\" succeeded\n");
416 exit (1);
417 }
418 if (errno != ENOTDIR)
419 {
420 printf ("chdir to \"and-a-file\" didn't set correct error\n");
421 result = 1;
422 }
423
424 errno = 0;
425 if (chdir ("and-a-file/..") >= 0)
426 {
427 printf ("chdir to \"and-a-file/..\" succeeded\n");
428 exit (1);
429 }
430 if (errno != ENOTDIR)
431 {
432 printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
433 result = 1;
434 }
435
436 errno = 0;
437 if (chdir ("another-dir/../and-a-file") >= 0)
438 {
439 printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
440 exit (1);
441 }
442 if (errno != ENOTDIR)
443 {
444 printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
445 result = 1;
446 }
447
7584a3f9
FW
448 /* The test below covers the deprecated readdir64_r function. */
449 DIAG_PUSH_NEEDS_COMMENT;
450 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
451
47a03c51
UD
452 /* We now should have a directory and a file in the new directory. */
453 rewinddir (dir2);
ffa8d2a0 454 while (readdir64_r (dir2, &direntbuf.d, &d) == 0 && d != NULL)
47a03c51
UD
455 {
456 if (strcmp (d->d_name, ".") == 0
457 || strcmp (d->d_name, "..") == 0
458 || strcmp (d->d_name, "another-dir") == 0)
459 {
460#ifdef _DIRENT_HAVE_D_TYPE
461 if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
462 {
463 printf ("d_type for \"%s\" is wrong\n", d->d_name);
464 result = 1;
465 }
466#endif
467 if (stat64 (d->d_name, &st3) < 0)
468 {
469 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
470 result = 1;
471 }
472 else if (! S_ISDIR (st3.st_mode))
473 {
474 printf ("\"%s\" is no directory\n", d->d_name);
475 result = 1;
476 }
477 }
478 else if (strcmp (d->d_name, "and-a-file") == 0)
479 {
480#ifdef _DIRENT_HAVE_D_TYPE
481 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
482 {
483 printf ("d_type for \"%s\" is wrong\n", d->d_name);
484 result = 1;
485 }
486#endif
487 if (stat64 (d->d_name, &st3) < 0)
488 {
489 printf ("cannot stat \"%s\" is wrong\n", d->d_name);
490 result = 1;
491 }
492 else if (! S_ISREG (st3.st_mode))
493 {
494 printf ("\"%s\" is no regular file\n", d->d_name);
495 result = 1;
496 }
497 }
498 else
499 {
500 printf ("unexpected directory entry \"%s\"\n", d->d_name);
501 result = 1;
502 }
503 }
504
7584a3f9
FW
505 DIAG_POP_NEEDS_COMMENT;
506
ea639044
UD
507 if (stat64 ("does-not-exist", &st1) >= 0)
508 {
509 puts ("stat for unexisting file did not fail");
510 result = 1;
511 }
512
47a03c51
UD
513 /* Free all resources. */
514
515 if (closedir (dir1) < 0)
516 {
517 printf ("closing dir1 failed: %m\n");
518 result = 1;
519 }
520 if (closedir (dir2) < 0)
521 {
522 printf ("second closing dir2 failed: %m\n");
523 result = 1;
524 }
525
526 if (rmdir ("another-dir") < 0)
527 {
528 printf ("cannot remove \"another-dir\": %m\n");
529 result = 1;
530 }
531
532 if (unlink ("and-a-file") < 0)
533 {
534 printf ("cannot remove \"and-a-file\": %m\n");
535 result = 1;
536 }
537
354e6102
UD
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)
541 {
542 printf ("cannot create symlink \"a-symlink\": %m\n");
543 result = 1;
544 }
545 else
546 {
547 if (mkdir ("a-symlink", 0666) == 0)
548 {
549 puts ("can make directory \"a-symlink\"");
550 result = 1;
551 }
552 else if (errno != EEXIST)
553 {
554 puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
555 result = 1;
556 }
557 if (unlink ("a-symlink") < 0)
558 {
559 printf ("cannot unlink \"a-symlink\": %m\n");
560 result = 1;
561 }
562 }
563
47a03c51
UD
564 if (chdir (srcdir) < 0)
565 {
566 printf ("cannot change back to source directory: %m\n");
567 exit (1);
568 }
569
570 if (rmdir (buf) < 0)
571 {
572 printf ("cannot remove \"%s\": %m\n", buf);
573 result = 1;
574 }
575 free (objdir_copy1);
576 free (objdir_copy2);
577
578 if (result == 0)
579 puts ("all OK");
580
581 return result;
582}