]>
git.ipfire.org Git - thirdparty/glibc.git/blob - posix/tst-dir.c
11 /* We expect four arguments:
12 - source directory name
14 - common object directory
15 - the program name with path
18 main (int argc
, char *argv
[])
22 const char *common_objdir
;
31 struct dirent64 direntbuf
;
41 puts ("not enough parameters");
45 /* Make parameters available with nicer names. */
48 common_objdir
= argv
[3];
51 /* First test the current source dir. We cannot really compare the
52 result of `getpwd' with the srcdir string but we have other means. */
53 if (stat64 (".", &st1
) < 0)
55 printf ("cannot stat starting directory: %m\n");
59 if (chdir (srcdir
) < 0)
61 printf ("cannot change to source directory: %m\n");
64 if (stat64 (".", &st2
) < 0)
66 printf ("cannot stat source directory: %m\n");
70 /* The two last stat64 calls better were for the same directory. */
71 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
73 printf ("stat of source directory failed: (%lld,%lld) vs (%lld,%lld)\n",
74 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
75 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
79 /* Change to the object directory. */
80 if (chdir (objdir
) < 0)
82 printf ("cannot change to object directory: %m\n");
85 if (stat64 (".", &st1
) < 0)
87 printf ("cannot stat object directory: %m\n");
90 /* Is this the same we get as with the full path? */
91 if (stat64 (objdir
, &st2
) < 0)
93 printf ("cannot stat object directory with full path: %m\n");
96 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
98 printf ("stat of object 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 objdir_copy1
= getcwd (NULL
, 0);
105 if (objdir_copy1
== NULL
)
107 printf ("cannot get current directory name for object directory: %m\n");
111 /* First test: this directory must include our program. */
112 if (stat64 (progpath
, &st2
) < 0)
114 printf ("cannot stat program: %m\n");
118 dir1
= opendir (".");
121 printf ("cannot open object directory: %m\n");
125 while ((d
= readdir64 (dir1
)) != NULL
)
127 #ifdef _DIRENT_HAVE_D_TYPE
128 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
132 if (d
->d_ino
== st2
.st_ino
)
134 /* Might be it. Test the device. We could use the st_dev
135 element from st1 but what the heck, do more testing. */
136 if (stat64 (d
->d_name
, &st3
) < 0)
138 printf ("cannot stat entry from readdir: %m\n");
144 if (st3
.st_dev
== st2
.st_dev
)
151 puts ("haven't found program in object directory");
155 /* We leave dir1 open. */
157 /* Stat using file descriptor. */
158 if (fstat64 (dirfd (dir1
), &st2
) < 0)
160 printf ("cannot fstat object directory: %m\n");
163 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
165 printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
166 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
167 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
171 if (chdir ("..") < 0)
173 printf ("cannot go to common object directory with \"..\": %m\n");
177 if (stat64 (".", &st1
) < 0)
179 printf ("cannot stat common object directory: %m\n");
182 /* Is this the same we get as with the full path? */
183 if (stat64 (common_objdir
, &st2
) < 0)
185 printf ("cannot stat common object directory with full path: %m\n");
188 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
190 printf ("stat 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 /* Stat using file descriptor. */
197 if (fstat64 (dirfd (dir1
), &st2
) < 0)
199 printf ("cannot fstat object directory: %m\n");
203 dir2
= opendir (common_objdir
);
206 printf ("cannot open common object directory: %m\n");
210 while ((d
= readdir64 (dir2
)) != NULL
)
212 #ifdef _DIRENT_HAVE_D_TYPE
213 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
217 if (d
->d_ino
== st2
.st_ino
)
219 /* Might be it. Test the device. We could use the st_dev
220 element from st1 but what the heck, do more testing. */
221 if (stat64 (d
->d_name
, &st3
) < 0)
223 printf ("cannot stat entry from readdir: %m\n");
229 if (st3
.st_dev
== st2
.st_dev
)
234 /* This better should be the object directory again. */
235 if (fchdir (dirfd (dir1
)) < 0)
237 printf ("cannot fchdir to object directory: %m\n");
241 objdir_copy2
= getcwd (NULL
, 0);
242 if (objdir_copy2
== NULL
)
244 printf ("cannot get current directory name for object directory: %m\n");
247 if (strcmp (objdir_copy1
, objdir_copy2
) != 0)
249 puts ("getcwd returned a different string the second time");
253 /* This better should be the common object directory again. */
254 if (fchdir (dirfd (dir2
)) < 0)
256 printf ("cannot fchdir to common object directory: %m\n");
260 if (stat64 (".", &st2
) < 0)
262 printf ("cannot stat common object directory: %m\n");
265 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
267 printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
268 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
269 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
273 buf
= (char *) malloc (strlen (objdir_copy1
) + 1 + sizeof "tst-dir.XXXXXX");
276 printf ("cannot allocate buffer: %m");
280 stpcpy (stpcpy (stpcpy (buf
, objdir_copy1
), "/"), "tst-dir.XXXXXX");
281 if (mkdtemp (buf
) == NULL
)
283 printf ("cannot create test directory in object directory: %m\n");
286 if (stat64 (buf
, &st1
) < 0)
288 printf ("cannot stat new directory \"%s\": %m\n", buf
);
291 if (chmod (buf
, 0700) < 0)
293 printf ("cannot change mode of new directory: %m\n");
297 /* Try to find the new directory. */
299 while (readdir64_r (dir1
, &direntbuf
, &d
) == 0)
301 #ifdef _DIRENT_HAVE_D_TYPE
302 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
306 if (d
->d_ino
== st1
.st_ino
)
308 /* Might be it. Test the device. We could use the st_dev
309 element from st1 but what the heck, do more testing. */
310 size_t len
= strlen (objdir
) + 1 + _D_EXACT_NAMLEN (d
) + 1;
313 stpcpy (stpcpy (stpcpy (tmpbuf
, objdir
), "/"), d
->d_name
);
315 if (stat64 (tmpbuf
, &st3
) < 0)
317 printf ("cannot stat entry from readdir: %m\n");
323 if (st3
.st_dev
== st2
.st_dev
324 && strcmp (d
->d_name
, buf
+ strlen (buf
) - 14) == 0)
331 printf ("haven't found new directory \"%s\"\n", buf
);
335 if (closedir (dir2
) < 0)
337 printf ("closing dir2 failed: %m\n");
343 printf ("cannot change to new directory: %m\n");
347 dir2
= opendir (buf
);
350 printf ("cannot open new directory: %m\n");
354 if (fstat64 (dirfd (dir2
), &st2
) < 0)
356 printf ("cannot fstat new directory \"%s\": %m\n", buf
);
359 if (st1
.st_dev
!= st2
.st_dev
|| st1
.st_ino
!= st2
.st_ino
)
361 printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
362 (long long int) st1
.st_dev
, (long long int) st1
.st_ino
,
363 (long long int) st2
.st_dev
, (long long int) st2
.st_ino
);
367 if (mkdir ("another-dir", 0777) < 0)
369 printf ("cannot create \"another-dir\": %m\n");
372 fd
= open ("and-a-file", O_RDWR
| O_CREAT
| O_EXCL
, 0666);
375 printf ("cannot create \"and-a-file\": %m\n");
380 /* We now should have a directory and a file in the new directory. */
382 while (readdir64_r (dir2
, &direntbuf
, &d
) == 0 && d
!= NULL
)
384 if (strcmp (d
->d_name
, ".") == 0
385 || strcmp (d
->d_name
, "..") == 0
386 || strcmp (d
->d_name
, "another-dir") == 0)
388 #ifdef _DIRENT_HAVE_D_TYPE
389 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_DIR
)
391 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
395 if (stat64 (d
->d_name
, &st3
) < 0)
397 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
400 else if (! S_ISDIR (st3
.st_mode
))
402 printf ("\"%s\" is no directory\n", d
->d_name
);
406 else if (strcmp (d
->d_name
, "and-a-file") == 0)
408 #ifdef _DIRENT_HAVE_D_TYPE
409 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
)
411 printf ("d_type for \"%s\" is wrong\n", d
->d_name
);
415 if (stat64 (d
->d_name
, &st3
) < 0)
417 printf ("cannot stat \"%s\" is wrong\n", d
->d_name
);
420 else if (! S_ISREG (st3
.st_mode
))
422 printf ("\"%s\" is no regular file\n", d
->d_name
);
428 printf ("unexpected directory entry \"%s\"\n", d
->d_name
);
433 /* Free all resources. */
435 if (closedir (dir1
) < 0)
437 printf ("closing dir1 failed: %m\n");
440 if (closedir (dir2
) < 0)
442 printf ("second closing dir2 failed: %m\n");
446 if (rmdir ("another-dir") < 0)
448 printf ("cannot remove \"another-dir\": %m\n");
452 if (unlink ("and-a-file") < 0)
454 printf ("cannot remove \"and-a-file\": %m\n");
458 if (chdir (srcdir
) < 0)
460 printf ("cannot change back to source directory: %m\n");
466 printf ("cannot remove \"%s\": %m\n", buf
);