]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 2000-2018 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 | */ | |
37 | int | |
38 | main (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 | { | |
47a03c51 UD |
152 | if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG) |
153 | continue; | |
47a03c51 UD |
154 | |
155 | if (d->d_ino == st2.st_ino) | |
156 | { | |
157 | /* Might be it. Test the device. We could use the st_dev | |
158 | element from st1 but what the heck, do more testing. */ | |
159 | if (stat64 (d->d_name, &st3) < 0) | |
160 | { | |
161 | printf ("cannot stat entry from readdir: %m\n"); | |
162 | result = 1; | |
163 | d = NULL; | |
164 | break; | |
165 | } | |
166 | ||
167 | if (st3.st_dev == st2.st_dev) | |
168 | break; | |
169 | } | |
170 | } | |
171 | ||
172 | if (d == NULL) | |
173 | { | |
174 | puts ("haven't found program in object directory"); | |
175 | result = 1; | |
176 | } | |
177 | ||
178 | /* We leave dir1 open. */ | |
179 | ||
180 | /* Stat using file descriptor. */ | |
181 | if (fstat64 (dirfd (dir1), &st2) < 0) | |
182 | { | |
183 | printf ("cannot fstat object directory: %m\n"); | |
184 | result = 1; | |
185 | } | |
186 | if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) | |
187 | { | |
188 | printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n", | |
189 | (long long int) st1.st_dev, (long long int) st1.st_ino, | |
190 | (long long int) st2.st_dev, (long long int) st2.st_ino); | |
191 | exit (1); | |
192 | } | |
193 | ||
194 | if (chdir ("..") < 0) | |
195 | { | |
196 | printf ("cannot go to common object directory with \"..\": %m\n"); | |
197 | exit (1); | |
198 | } | |
199 | ||
200 | if (stat64 (".", &st1) < 0) | |
201 | { | |
202 | printf ("cannot stat common object directory: %m\n"); | |
203 | exit (1); | |
204 | } | |
205 | /* Is this the same we get as with the full path? */ | |
206 | if (stat64 (common_objdir, &st2) < 0) | |
207 | { | |
208 | printf ("cannot stat common object directory with full path: %m\n"); | |
209 | exit (1); | |
210 | } | |
211 | if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) | |
212 | { | |
213 | printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n", | |
214 | (long long int) st1.st_dev, (long long int) st1.st_ino, | |
215 | (long long int) st2.st_dev, (long long int) st2.st_ino); | |
216 | exit (1); | |
217 | } | |
218 | ||
219 | /* Stat using file descriptor. */ | |
220 | if (fstat64 (dirfd (dir1), &st2) < 0) | |
221 | { | |
222 | printf ("cannot fstat object directory: %m\n"); | |
223 | result = 1; | |
224 | } | |
225 | ||
226 | dir2 = opendir (common_objdir); | |
227 | if (dir2 == NULL) | |
228 | { | |
229 | printf ("cannot open common object directory: %m\n"); | |
230 | exit (1); | |
231 | } | |
232 | ||
233 | while ((d = readdir64 (dir2)) != NULL) | |
234 | { | |
47a03c51 UD |
235 | if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR) |
236 | continue; | |
47a03c51 UD |
237 | |
238 | if (d->d_ino == st2.st_ino) | |
239 | { | |
240 | /* Might be it. Test the device. We could use the st_dev | |
241 | element from st1 but what the heck, do more testing. */ | |
242 | if (stat64 (d->d_name, &st3) < 0) | |
243 | { | |
244 | printf ("cannot stat entry from readdir: %m\n"); | |
245 | result = 1; | |
246 | d = NULL; | |
247 | break; | |
248 | } | |
249 | ||
250 | if (st3.st_dev == st2.st_dev) | |
251 | break; | |
252 | } | |
253 | } | |
254 | ||
255 | /* This better should be the object directory again. */ | |
256 | if (fchdir (dirfd (dir1)) < 0) | |
257 | { | |
258 | printf ("cannot fchdir to object directory: %m\n"); | |
259 | exit (1); | |
260 | } | |
261 | ||
262 | objdir_copy2 = getcwd (NULL, 0); | |
263 | if (objdir_copy2 == NULL) | |
264 | { | |
265 | printf ("cannot get current directory name for object directory: %m\n"); | |
266 | result = 1; | |
267 | } | |
268 | if (strcmp (objdir_copy1, objdir_copy2) != 0) | |
269 | { | |
270 | puts ("getcwd returned a different string the second time"); | |
271 | result = 1; | |
272 | } | |
273 | ||
274 | /* This better should be the common object directory again. */ | |
275 | if (fchdir (dirfd (dir2)) < 0) | |
276 | { | |
277 | printf ("cannot fchdir to common object directory: %m\n"); | |
278 | exit (1); | |
279 | } | |
280 | ||
281 | if (stat64 (".", &st2) < 0) | |
282 | { | |
283 | printf ("cannot stat common object directory: %m\n"); | |
284 | exit (1); | |
285 | } | |
286 | if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) | |
287 | { | |
288 | printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n", | |
289 | (long long int) st1.st_dev, (long long int) st1.st_ino, | |
290 | (long long int) st2.st_dev, (long long int) st2.st_ino); | |
291 | exit (1); | |
292 | } | |
293 | ||
294 | buf = (char *) malloc (strlen (objdir_copy1) + 1 + sizeof "tst-dir.XXXXXX"); | |
295 | if (buf == NULL) | |
296 | { | |
297 | printf ("cannot allocate buffer: %m"); | |
298 | exit (1); | |
299 | } | |
300 | ||
301 | stpcpy (stpcpy (stpcpy (buf, objdir_copy1), "/"), "tst-dir.XXXXXX"); | |
302 | if (mkdtemp (buf) == NULL) | |
303 | { | |
304 | printf ("cannot create test directory in object directory: %m\n"); | |
305 | exit (1); | |
306 | } | |
307 | if (stat64 (buf, &st1) < 0) | |
308 | { | |
309 | printf ("cannot stat new directory \"%s\": %m\n", buf); | |
310 | exit (1); | |
311 | } | |
312 | if (chmod (buf, 0700) < 0) | |
313 | { | |
314 | printf ("cannot change mode of new directory: %m\n"); | |
315 | exit (1); | |
316 | } | |
317 | ||
7584a3f9 FW |
318 | /* The test below covers the deprecated readdir64_r function. */ |
319 | DIAG_PUSH_NEEDS_COMMENT; | |
320 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); | |
321 | ||
47a03c51 UD |
322 | /* Try to find the new directory. */ |
323 | rewinddir (dir1); | |
ffa8d2a0 | 324 | while (readdir64_r (dir1, &direntbuf.d, &d) == 0 && d != NULL) |
47a03c51 | 325 | { |
47a03c51 UD |
326 | if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR) |
327 | continue; | |
47a03c51 UD |
328 | |
329 | if (d->d_ino == st1.st_ino) | |
330 | { | |
331 | /* Might be it. Test the device. We could use the st_dev | |
332 | element from st1 but what the heck, do more testing. */ | |
333 | size_t len = strlen (objdir) + 1 + _D_EXACT_NAMLEN (d) + 1; | |
334 | char tmpbuf[len]; | |
335 | ||
336 | stpcpy (stpcpy (stpcpy (tmpbuf, objdir), "/"), d->d_name); | |
337 | ||
338 | if (stat64 (tmpbuf, &st3) < 0) | |
339 | { | |
340 | printf ("cannot stat entry from readdir: %m\n"); | |
341 | result = 1; | |
342 | d = NULL; | |
343 | break; | |
344 | } | |
345 | ||
346 | if (st3.st_dev == st2.st_dev | |
347 | && strcmp (d->d_name, buf + strlen (buf) - 14) == 0) | |
348 | break; | |
349 | } | |
350 | } | |
351 | ||
7584a3f9 FW |
352 | DIAG_POP_NEEDS_COMMENT; |
353 | ||
47a03c51 UD |
354 | if (d == NULL) |
355 | { | |
356 | printf ("haven't found new directory \"%s\"\n", buf); | |
357 | exit (1); | |
358 | } | |
359 | ||
360 | if (closedir (dir2) < 0) | |
361 | { | |
362 | printf ("closing dir2 failed: %m\n"); | |
363 | result = 1; | |
364 | } | |
365 | ||
8c02ee02 | 366 | if (chdir (buf) < 0) |
47a03c51 UD |
367 | { |
368 | printf ("cannot change to new directory: %m\n"); | |
369 | exit (1); | |
370 | } | |
371 | ||
372 | dir2 = opendir (buf); | |
373 | if (dir2 == NULL) | |
374 | { | |
375 | printf ("cannot open new directory: %m\n"); | |
376 | exit (1); | |
377 | } | |
378 | ||
379 | if (fstat64 (dirfd (dir2), &st2) < 0) | |
380 | { | |
381 | printf ("cannot fstat new directory \"%s\": %m\n", buf); | |
382 | exit (1); | |
383 | } | |
384 | if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) | |
385 | { | |
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); | |
390 | } | |
391 | ||
392 | if (mkdir ("another-dir", 0777) < 0) | |
393 | { | |
394 | printf ("cannot create \"another-dir\": %m\n"); | |
395 | exit (1); | |
396 | } | |
397 | fd = open ("and-a-file", O_RDWR | O_CREAT | O_EXCL, 0666); | |
398 | if (fd == -1) | |
399 | { | |
400 | printf ("cannot create \"and-a-file\": %m\n"); | |
401 | exit (1); | |
402 | } | |
403 | close (fd); | |
404 | ||
8c02ee02 UD |
405 | /* Some tests about error reporting. */ |
406 | errno = 0; | |
407 | if (chdir ("and-a-file") >= 0) | |
408 | { | |
409 | printf ("chdir to \"and-a-file\" succeeded\n"); | |
410 | exit (1); | |
411 | } | |
412 | if (errno != ENOTDIR) | |
413 | { | |
414 | printf ("chdir to \"and-a-file\" didn't set correct error\n"); | |
415 | result = 1; | |
416 | } | |
417 | ||
418 | errno = 0; | |
419 | if (chdir ("and-a-file/..") >= 0) | |
420 | { | |
421 | printf ("chdir to \"and-a-file/..\" succeeded\n"); | |
422 | exit (1); | |
423 | } | |
424 | if (errno != ENOTDIR) | |
425 | { | |
426 | printf ("chdir to \"and-a-file/..\" didn't set correct error\n"); | |
427 | result = 1; | |
428 | } | |
429 | ||
430 | errno = 0; | |
431 | if (chdir ("another-dir/../and-a-file") >= 0) | |
432 | { | |
433 | printf ("chdir to \"another-dir/../and-a-file\" succeeded\n"); | |
434 | exit (1); | |
435 | } | |
436 | if (errno != ENOTDIR) | |
437 | { | |
438 | printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n"); | |
439 | result = 1; | |
440 | } | |
441 | ||
7584a3f9 FW |
442 | /* The test below covers the deprecated readdir64_r function. */ |
443 | DIAG_PUSH_NEEDS_COMMENT; | |
444 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); | |
445 | ||
47a03c51 UD |
446 | /* We now should have a directory and a file in the new directory. */ |
447 | rewinddir (dir2); | |
ffa8d2a0 | 448 | while (readdir64_r (dir2, &direntbuf.d, &d) == 0 && d != NULL) |
47a03c51 UD |
449 | { |
450 | if (strcmp (d->d_name, ".") == 0 | |
451 | || strcmp (d->d_name, "..") == 0 | |
452 | || strcmp (d->d_name, "another-dir") == 0) | |
453 | { | |
47a03c51 UD |
454 | if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR) |
455 | { | |
456 | printf ("d_type for \"%s\" is wrong\n", d->d_name); | |
457 | result = 1; | |
458 | } | |
47a03c51 UD |
459 | if (stat64 (d->d_name, &st3) < 0) |
460 | { | |
461 | printf ("cannot stat \"%s\" is wrong\n", d->d_name); | |
462 | result = 1; | |
463 | } | |
464 | else if (! S_ISDIR (st3.st_mode)) | |
465 | { | |
466 | printf ("\"%s\" is no directory\n", d->d_name); | |
467 | result = 1; | |
468 | } | |
469 | } | |
470 | else if (strcmp (d->d_name, "and-a-file") == 0) | |
471 | { | |
47a03c51 UD |
472 | if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG) |
473 | { | |
474 | printf ("d_type for \"%s\" is wrong\n", d->d_name); | |
475 | result = 1; | |
476 | } | |
47a03c51 UD |
477 | if (stat64 (d->d_name, &st3) < 0) |
478 | { | |
479 | printf ("cannot stat \"%s\" is wrong\n", d->d_name); | |
480 | result = 1; | |
481 | } | |
482 | else if (! S_ISREG (st3.st_mode)) | |
483 | { | |
484 | printf ("\"%s\" is no regular file\n", d->d_name); | |
485 | result = 1; | |
486 | } | |
487 | } | |
488 | else | |
489 | { | |
490 | printf ("unexpected directory entry \"%s\"\n", d->d_name); | |
491 | result = 1; | |
492 | } | |
493 | } | |
494 | ||
7584a3f9 FW |
495 | DIAG_POP_NEEDS_COMMENT; |
496 | ||
ea639044 UD |
497 | if (stat64 ("does-not-exist", &st1) >= 0) |
498 | { | |
499 | puts ("stat for unexisting file did not fail"); | |
500 | result = 1; | |
501 | } | |
502 | ||
47a03c51 UD |
503 | /* Free all resources. */ |
504 | ||
505 | if (closedir (dir1) < 0) | |
506 | { | |
507 | printf ("closing dir1 failed: %m\n"); | |
508 | result = 1; | |
509 | } | |
510 | if (closedir (dir2) < 0) | |
511 | { | |
512 | printf ("second closing dir2 failed: %m\n"); | |
513 | result = 1; | |
514 | } | |
515 | ||
516 | if (rmdir ("another-dir") < 0) | |
517 | { | |
518 | printf ("cannot remove \"another-dir\": %m\n"); | |
519 | result = 1; | |
520 | } | |
521 | ||
522 | if (unlink ("and-a-file") < 0) | |
523 | { | |
524 | printf ("cannot remove \"and-a-file\": %m\n"); | |
525 | result = 1; | |
526 | } | |
527 | ||
354e6102 UD |
528 | /* One more test before we leave: mkdir() is supposed to fail with |
529 | EEXIST if the named file is a symlink. */ | |
530 | if (symlink ("a-symlink", "a-symlink") != 0) | |
531 | { | |
532 | printf ("cannot create symlink \"a-symlink\": %m\n"); | |
533 | result = 1; | |
534 | } | |
535 | else | |
536 | { | |
537 | if (mkdir ("a-symlink", 0666) == 0) | |
538 | { | |
539 | puts ("can make directory \"a-symlink\""); | |
540 | result = 1; | |
541 | } | |
542 | else if (errno != EEXIST) | |
543 | { | |
544 | puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n"); | |
545 | result = 1; | |
546 | } | |
547 | if (unlink ("a-symlink") < 0) | |
548 | { | |
549 | printf ("cannot unlink \"a-symlink\": %m\n"); | |
550 | result = 1; | |
551 | } | |
552 | } | |
553 | ||
47a03c51 UD |
554 | if (chdir (srcdir) < 0) |
555 | { | |
556 | printf ("cannot change back to source directory: %m\n"); | |
557 | exit (1); | |
558 | } | |
559 | ||
560 | if (rmdir (buf) < 0) | |
561 | { | |
562 | printf ("cannot remove \"%s\": %m\n", buf); | |
563 | result = 1; | |
564 | } | |
565 | free (objdir_copy1); | |
566 | free (objdir_copy2); | |
567 | ||
568 | if (result == 0) | |
569 | puts ("all OK"); | |
570 | ||
571 | return result; | |
572 | } |