djdelorie / rpms / glibc

Forked from rpms/glibc 3 years ago
Clone
6ca2fbe
commit 137fe72eca6923a00381a3ca9f0e7672c1f85e3f
6ca2fbe
Author: Florian Weimer <fweimer@redhat.com>
6ca2fbe
Date:   Fri Apr 29 09:33:07 2016 +0200
6ca2fbe
6ca2fbe
    glob: Simplify the interface for the GLOB_ALTDIRFUNC callback gl_readdir
6ca2fbe
    
6ca2fbe
    Previously, application code had to set up the d_namlen member if
6ca2fbe
    the target supported it, involving conditional compilation.  After
6ca2fbe
    this change, glob will use the length of the string in d_name instead
6ca2fbe
    of d_namlen to determine the file name length.  All glibc targets
6ca2fbe
    provide the d_type and d_ino members, and setting them as needed for
6ca2fbe
    gl_readdir is straightforward.
6ca2fbe
    
6ca2fbe
    Changing the behavior with regards to d_ino is left to a future
6ca2fbe
    cleanup.
6ca2fbe
6ca2fbe
Index: b/manual/examples/mkdirent.c
6ca2fbe
===================================================================
6ca2fbe
--- /dev/null
6ca2fbe
+++ b/manual/examples/mkdirent.c
6ca2fbe
@@ -0,0 +1,42 @@
6ca2fbe
+/* Example for creating a struct dirent object for use with glob.
6ca2fbe
+   Copyright (C) 2016 Free Software Foundation, Inc.
6ca2fbe
+
6ca2fbe
+   This program is free software; you can redistribute it and/or
6ca2fbe
+   modify it under the terms of the GNU General Public License
6ca2fbe
+   as published by the Free Software Foundation; either version 2
6ca2fbe
+   of the License, or (at your option) any later version.
6ca2fbe
+
6ca2fbe
+   This program is distributed in the hope that it will be useful,
6ca2fbe
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6ca2fbe
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6ca2fbe
+   GNU General Public License for more details.
6ca2fbe
+
6ca2fbe
+   You should have received a copy of the GNU General Public License
6ca2fbe
+   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
6ca2fbe
+*/
6ca2fbe
+
6ca2fbe
+#include <dirent.h>
6ca2fbe
+#include <errno.h>
6ca2fbe
+#include <stddef.h>
6ca2fbe
+#include <stdlib.h>
6ca2fbe
+#include <string.h>
6ca2fbe
+
6ca2fbe
+struct dirent *
6ca2fbe
+mkdirent (const char *name)
6ca2fbe
+{
6ca2fbe
+  size_t dirent_size = offsetof (struct dirent, d_name) + 1;
6ca2fbe
+  size_t name_length = strlen (name);
6ca2fbe
+  size_t total_size = dirent_size + name_length;
6ca2fbe
+  if (total_size < dirent_size)
6ca2fbe
+    {
6ca2fbe
+      errno = ENOMEM;
6ca2fbe
+      return NULL;
6ca2fbe
+    }
6ca2fbe
+  struct dirent *result = malloc (total_size);
6ca2fbe
+  if (result == NULL)
6ca2fbe
+    return NULL;
6ca2fbe
+  result->d_type = DT_UNKNOWN;
6ca2fbe
+  result->d_ino = 1;            /* Do not skip this entry.  */
6ca2fbe
+  memcpy (result->d_name, name, name_length + 1);
6ca2fbe
+  return result;
6ca2fbe
+}
6ca2fbe
Index: b/manual/pattern.texi
6ca2fbe
===================================================================
6ca2fbe
--- a/manual/pattern.texi
6ca2fbe
+++ b/manual/pattern.texi
6ca2fbe
@@ -237,7 +237,44 @@ function used to read the contents of a
6ca2fbe
 @code{GLOB_ALTDIRFUNC} bit is set in the flag parameter.  The type of
6ca2fbe
 this field is @w{@code{struct dirent *(*) (void *)}}.
6ca2fbe
 
6ca2fbe
-This is a GNU extension.
6ca2fbe
+An implementation of @code{gl_readdir} needs to initialize the following
6ca2fbe
+members of the @code{struct dirent} object:
6ca2fbe
+
6ca2fbe
+@table @code
6ca2fbe
+@item d_type
6ca2fbe
+This member should be set to the file type of the entry if it is known.
6ca2fbe
+Otherwise, the value @code{DT_UNKNOWN} can be used.  The @code{glob}
6ca2fbe
+function may use the specified file type to avoid callbacks in cases
6ca2fbe
+where the file type indicates that the data is not required.
6ca2fbe
+
6ca2fbe
+@item d_ino
6ca2fbe
+This member needs to be non-zero, otherwise @code{glob} may skip the
6ca2fbe
+current entry and call the @code{gl_readdir} callback function again to
6ca2fbe
+retrieve another entry.
6ca2fbe
+
6ca2fbe
+@item d_name
6ca2fbe
+This member must be set to the name of the entry.  It must be
6ca2fbe
+null-terminated.
6ca2fbe
+@end table
6ca2fbe
+
6ca2fbe
+The example below shows how to allocate a @code{struct dirent} object
6ca2fbe
+containing a given name.
6ca2fbe
+
6ca2fbe
+@smallexample
6ca2fbe
+@include mkdirent.c.texi
6ca2fbe
+@end smallexample
6ca2fbe
+
6ca2fbe
+The @code{glob} function reads the @code{struct dirent} members listed
6ca2fbe
+above and makes a copy of the file name in the @code{d_name} member
6ca2fbe
+immediately after the @code{gl_readdir} callback function returns.
6ca2fbe
+Future invocations of any of the callback functions may dealloacte or
6ca2fbe
+reuse the buffer.  It is the responsibility of the caller of the
6ca2fbe
+@code{glob} function to allocate and deallocate the buffer, around the
6ca2fbe
+call to @code{glob} or using the callback functions.  For example, an
6ca2fbe
+application could allocate the buffer in the @code{gl_readdir} callback
6ca2fbe
+function, and deallocate it in the @code{gl_closedir} callback function.
6ca2fbe
+
6ca2fbe
+The @code{gl_readdir} member is a GNU extension.
6ca2fbe
 
6ca2fbe
 @item gl_opendir
6ca2fbe
 The address of an alternative implementation of the @code{opendir}
6ca2fbe
Index: b/posix/bug-glob2.c
6ca2fbe
===================================================================
6ca2fbe
--- a/posix/bug-glob2.c
6ca2fbe
+++ b/posix/bug-glob2.c
6ca2fbe
@@ -193,7 +193,7 @@ my_readdir (void *gdir)
6ca2fbe
       return NULL;
6ca2fbe
     }
6ca2fbe
 
6ca2fbe
-  dir->d.d_ino = dir->idx;
6ca2fbe
+  dir->d.d_ino = 1;		/* glob should not skip this entry.  */
6ca2fbe
 
6ca2fbe
 #ifdef _DIRENT_HAVE_D_TYPE
6ca2fbe
   dir->d.d_type = filesystem[dir->idx].type;
6ca2fbe
Index: b/posix/glob.c
6ca2fbe
===================================================================
6ca2fbe
--- a/posix/glob.c
6ca2fbe
+++ b/posix/glob.c
6ca2fbe
@@ -57,10 +57,8 @@
6ca2fbe
 
6ca2fbe
 #if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
6ca2fbe
 # include <dirent.h>
6ca2fbe
-# define NAMLEN(dirent) strlen((dirent)->d_name)
6ca2fbe
 #else
6ca2fbe
 # define dirent direct
6ca2fbe
-# define NAMLEN(dirent) (dirent)->d_namlen
6ca2fbe
 # ifdef HAVE_SYS_NDIR_H
6ca2fbe
 #  include <sys/ndir.h>
6ca2fbe
 # endif
6ca2fbe
@@ -76,12 +74,6 @@
6ca2fbe
 #endif
6ca2fbe
 
6ca2fbe
 
6ca2fbe
-/* In GNU systems, <dirent.h> defines this macro for us.  */
6ca2fbe
-#ifdef _D_NAMLEN
6ca2fbe
-# undef NAMLEN
6ca2fbe
-# define NAMLEN(d) _D_NAMLEN(d)
6ca2fbe
-#endif
6ca2fbe
-
6ca2fbe
 /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
6ca2fbe
    if the `d_type' member for `struct dirent' is available.
6ca2fbe
    HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB.  */
6ca2fbe
@@ -105,12 +97,6 @@
6ca2fbe
 
6ca2fbe
 /* If the system has the `struct dirent64' type we use it internally.  */
6ca2fbe
 #if defined _LIBC && !defined COMPILE_GLOB64
6ca2fbe
-# if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__
6ca2fbe
-#  define CONVERT_D_NAMLEN(d64, d32)
6ca2fbe
-# else
6ca2fbe
-#  define CONVERT_D_NAMLEN(d64, d32) \
6ca2fbe
-  (d64)->d_namlen = (d32)->d_namlen;
6ca2fbe
-# endif
6ca2fbe
 
6ca2fbe
 # if (defined POSIX || defined WINDOWS32) && !defined __GNU_LIBRARY__
6ca2fbe
 #  define CONVERT_D_INO(d64, d32)
6ca2fbe
@@ -127,8 +113,7 @@
6ca2fbe
 # endif
6ca2fbe
 
6ca2fbe
 # define CONVERT_DIRENT_DIRENT64(d64, d32) \
6ca2fbe
-  memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1);		      \
6ca2fbe
-  CONVERT_D_NAMLEN (d64, d32)						      \
6ca2fbe
+  strcpy ((d64)->d_name, (d32)->d_name);				      \
6ca2fbe
   CONVERT_D_INO (d64, d32)						      \
6ca2fbe
   CONVERT_D_TYPE (d64, d32)
6ca2fbe
 #endif
6ca2fbe
@@ -1562,7 +1547,6 @@ glob_in_dir (const char *pattern, const
6ca2fbe
 	  while (1)
6ca2fbe
 	    {
6ca2fbe
 	      const char *name;
6ca2fbe
-	      size_t len;
6ca2fbe
 #if defined _LIBC && !defined COMPILE_GLOB64
6ca2fbe
 	      struct dirent64 *d;
6ca2fbe
 	      union
6ca2fbe
@@ -1630,12 +1614,10 @@ glob_in_dir (const char *pattern, const
6ca2fbe
 			  names = newnames;
6ca2fbe
 			  cur = 0;
6ca2fbe
 			}
6ca2fbe
-		      len = NAMLEN (d);
6ca2fbe
-		      names->name[cur] = (char *) malloc (len + 1);
6ca2fbe
+		      names->name[cur] = strdup (d->d_name);
6ca2fbe
 		      if (names->name[cur] == NULL)
6ca2fbe
 			goto memory_error;
6ca2fbe
-		      *((char *) mempcpy (names->name[cur++], name, len))
6ca2fbe
-			= '\0';
6ca2fbe
+		      ++cur;
6ca2fbe
 		      ++nfound;
6ca2fbe
 		    }
6ca2fbe
 		}
6ca2fbe
Index: b/posix/tst-gnuglob.c
6ca2fbe
===================================================================
6ca2fbe
--- a/posix/tst-gnuglob.c
6ca2fbe
+++ b/posix/tst-gnuglob.c
6ca2fbe
@@ -211,7 +211,7 @@ my_readdir (void *gdir)
6ca2fbe
       return NULL;
6ca2fbe
     }
6ca2fbe
 
6ca2fbe
-  dir->d.d_ino = dir->idx;
6ca2fbe
+  dir->d.d_ino = 1;		/* glob should not skip this entry.  */
6ca2fbe
 
6ca2fbe
 #ifdef _DIRENT_HAVE_D_TYPE
6ca2fbe
   dir->d.d_type = filesystem[dir->idx].type;