]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - posix/tst-dir.c
Assume that _DIRENT_HAVE_D_TYPE is always defined.
[thirdparty/glibc.git] / posix / tst-dir.c
index 1eff3b5e06529c54ec3b162053d85531c310c429..52ff36fb6e7097d3bb6126c7ed5e1fcfe6d9e361 100644 (file)
@@ -1,32 +1,32 @@
-/* Copyright (C) 2000 Free Software Foundation, Inc.
+/* Copyright (C) 2000-2017 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <mcheck.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
-
+#include <libc-diag.h>
 
 /* We expect four arguments:
    - source directory name
@@ -48,7 +48,12 @@ main (int argc, char *argv[])
   DIR *dir2;
   int result = 0;
   struct dirent64 *d;
-  struct dirent64 direntbuf;
+  union
+    {
+      struct dirent64 d;
+      char room [offsetof (struct dirent64, d_name[0]) + NAME_MAX + 1];
+    }
+    direntbuf;
   char *objdir_copy1;
   char *objdir_copy2;
   char *buf;
@@ -144,10 +149,8 @@ main (int argc, char *argv[])
 
   while ((d = readdir64 (dir1)) != NULL)
     {
-#ifdef _DIRENT_HAVE_D_TYPE
       if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
        continue;
-#endif
 
       if (d->d_ino == st2.st_ino)
        {
@@ -229,10 +232,8 @@ main (int argc, char *argv[])
 
   while ((d = readdir64 (dir2)) != NULL)
     {
-#ifdef _DIRENT_HAVE_D_TYPE
       if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
        continue;
-#endif
 
       if (d->d_ino == st2.st_ino)
        {
@@ -314,14 +315,16 @@ main (int argc, char *argv[])
       exit (1);
     }
 
+  /* The test below covers the deprecated readdir64_r function.  */
+  DIAG_PUSH_NEEDS_COMMENT;
+  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
+
   /* Try to find the new directory.  */
   rewinddir (dir1);
-  while (readdir64_r (dir1, &direntbuf, &d) == 0 && d != NULL)
+  while (readdir64_r (dir1, &direntbuf.d, &d) == 0 && d != NULL)
     {
-#ifdef _DIRENT_HAVE_D_TYPE
       if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
        continue;
-#endif
 
       if (d->d_ino == st1.st_ino)
        {
@@ -346,6 +349,8 @@ main (int argc, char *argv[])
        }
     }
 
+  DIAG_POP_NEEDS_COMMENT;
+
   if (d == NULL)
     {
       printf ("haven't found new directory \"%s\"\n", buf);
@@ -434,21 +439,23 @@ main (int argc, char *argv[])
       result = 1;
     }
 
+  /* The test below covers the deprecated readdir64_r function.  */
+  DIAG_PUSH_NEEDS_COMMENT;
+  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
+
   /* We now should have a directory and a file in the new directory.  */
   rewinddir (dir2);
-  while (readdir64_r (dir2, &direntbuf, &d) == 0 && d != NULL)
+  while (readdir64_r (dir2, &direntbuf.d, &d) == 0 && d != NULL)
     {
       if (strcmp (d->d_name, ".") == 0
          || strcmp (d->d_name, "..") == 0
          || strcmp (d->d_name, "another-dir") == 0)
        {
-#ifdef _DIRENT_HAVE_D_TYPE
          if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
            {
              printf ("d_type for \"%s\" is wrong\n", d->d_name);
              result = 1;
            }
-#endif
          if (stat64 (d->d_name, &st3) < 0)
            {
              printf ("cannot stat \"%s\" is wrong\n", d->d_name);
@@ -462,13 +469,11 @@ main (int argc, char *argv[])
        }
       else if (strcmp (d->d_name, "and-a-file") == 0)
        {
-#ifdef _DIRENT_HAVE_D_TYPE
          if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
            {
              printf ("d_type for \"%s\" is wrong\n", d->d_name);
              result = 1;
            }
-#endif
          if (stat64 (d->d_name, &st3) < 0)
            {
              printf ("cannot stat \"%s\" is wrong\n", d->d_name);
@@ -487,6 +492,8 @@ main (int argc, char *argv[])
        }
     }
 
+  DIAG_POP_NEEDS_COMMENT;
+
   if (stat64 ("does-not-exist", &st1) >= 0)
     {
       puts ("stat for unexisting file did not fail");
@@ -518,6 +525,32 @@ main (int argc, char *argv[])
       result = 1;
     }
 
+  /* One more test before we leave: mkdir() is supposed to fail with
+     EEXIST if the named file is a symlink.  */
+  if (symlink ("a-symlink", "a-symlink") != 0)
+    {
+      printf ("cannot create symlink \"a-symlink\": %m\n");
+      result = 1;
+    }
+  else
+    {
+      if (mkdir ("a-symlink", 0666) == 0)
+       {
+         puts ("can make directory \"a-symlink\"");
+         result = 1;
+       }
+      else if (errno != EEXIST)
+       {
+         puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
+         result = 1;
+       }
+      if (unlink ("a-symlink") < 0)
+       {
+         printf ("cannot unlink \"a-symlink\": %m\n");
+         result = 1;
+       }
+    }
+
   if (chdir (srcdir) < 0)
     {
       printf ("cannot change back to source directory: %m\n");