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