076f82
commit c66c92181ddbd82306537a608e8c0282587131de
076f82
Author: DJ Delorie <dj@redhat.com>
076f82
Date:   Wed Mar 30 17:44:02 2022 -0400
076f82
076f82
    posix/glob.c: update from gnulib
076f82
    
076f82
    Copied from gnulib/lib/glob.c in order to fix rhbz 1982608
076f82
    Also fixes swbz 25659
076f82
    
076f82
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
076f82
    Tested-by: Carlos O'Donell <carlos@redhat.com>
076f82
    (cherry picked from commit 7c477b57a31487eda516db02b9e04f22d1a6e6af)
076f82
076f82
diff --git a/posix/glob.c b/posix/glob.c
076f82
index 593a4c358f3d42e5..6af310a1aa31401a 100644
076f82
--- a/posix/glob.c
076f82
+++ b/posix/glob.c
076f82
@@ -21,13 +21,14 @@
076f82
    optimizes away the pattern == NULL test below.  */
076f82
 # define _GL_ARG_NONNULL(params)
076f82
 
076f82
-# include <config.h>
076f82
+# include <libc-config.h>
076f82
 
076f82
 #endif
076f82
 
076f82
 #include <glob.h>
076f82
 
076f82
 #include <errno.h>
076f82
+#include <fcntl.h>
076f82
 #include <sys/types.h>
076f82
 #include <sys/stat.h>
076f82
 #include <stdbool.h>
076f82
@@ -56,6 +57,8 @@
076f82
 # define sysconf(id) __sysconf (id)
076f82
 # define closedir(dir) __closedir (dir)
076f82
 # define opendir(name) __opendir (name)
076f82
+# undef dirfd
076f82
+# define dirfd(str) __dirfd (str)
076f82
 # define readdir(str) __readdir64 (str)
076f82
 # define getpwnam_r(name, bufp, buf, len, res) \
076f82
     __getpwnam_r (name, bufp, buf, len, res)
076f82
@@ -69,11 +72,8 @@
076f82
 # ifndef GLOB_LSTAT
076f82
 #  define GLOB_LSTAT            gl_lstat
076f82
 # endif
076f82
-# ifndef GLOB_STAT64
076f82
-#  define GLOB_STAT64           __stat64
076f82
-# endif
076f82
-# ifndef GLOB_LSTAT64
076f82
-#  define GLOB_LSTAT64          __lstat64
076f82
+# ifndef GLOB_FSTATAT64
076f82
+#  define GLOB_FSTATAT64        __fstatat64
076f82
 # endif
076f82
 # include <shlib-compat.h>
076f82
 #else /* !_LIBC */
076f82
@@ -88,8 +88,7 @@
076f82
 # define struct_stat            struct stat
076f82
 # define struct_stat64          struct stat
076f82
 # define GLOB_LSTAT             gl_lstat
076f82
-# define GLOB_STAT64            stat
076f82
-# define GLOB_LSTAT64           lstat
076f82
+# define GLOB_FSTATAT64         fstatat
076f82
 #endif /* _LIBC */
076f82
 
076f82
 #include <fnmatch.h>
076f82
@@ -215,7 +214,8 @@ glob_lstat (glob_t *pglob, int flags, const char *fullname)
076f82
   } ust;
076f82
   return (__glibc_unlikely (flags & GLOB_ALTDIRFUNC)
076f82
           ? pglob->GLOB_LSTAT (fullname, &ust.st)
076f82
-          : GLOB_LSTAT64 (fullname, &ust.st64));
076f82
+          : GLOB_FSTATAT64 (AT_FDCWD, fullname, &ust.st64,
076f82
+                            AT_SYMLINK_NOFOLLOW));
076f82
 }
076f82
 
076f82
 /* Set *R = A + B.  Return true if the answer is mathematically
076f82
@@ -257,7 +257,8 @@ is_dir (char const *filename, int flags, glob_t const *pglob)
076f82
   struct_stat64 st64;
076f82
   return (__glibc_unlikely (flags & GLOB_ALTDIRFUNC)
076f82
           ? pglob->gl_stat (filename, &st) == 0 && S_ISDIR (st.st_mode)
076f82
-          : GLOB_STAT64 (filename, &st64) == 0 && S_ISDIR (st64.st_mode));
076f82
+          : (GLOB_FSTATAT64 (AT_FDCWD, filename, &st64, 0) == 0
076f82
+             && S_ISDIR (st64.st_mode)));
076f82
 }
076f82
 
076f82
 /* Find the end of the sub-pattern in a brace expression.  */
076f82
@@ -747,6 +748,8 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
076f82
       else
076f82
         {
076f82
 #ifndef WINDOWS32
076f82
+          /* Recognize ~user as a shorthand for the specified user's home
076f82
+             directory.  */
076f82
           char *end_name = strchr (dirname, '/');
076f82
           char *user_name;
076f82
           int malloc_user_name = 0;
076f82
@@ -885,7 +888,22 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
076f82
               }
076f82
             scratch_buffer_free (&pwtmpbuf);
076f82
           }
076f82
-#endif /* !WINDOWS32 */
076f82
+#else /* WINDOWS32 */
076f82
+          /* On native Windows, access to a user's home directory
076f82
+             (via GetUserProfileDirectory) or to a user's environment
076f82
+             variables (via ExpandEnvironmentStringsForUser) requires
076f82
+             the credentials of the user.  Therefore we cannot support
076f82
+             the ~user syntax on this platform.
076f82
+             Handling ~user specially (and treat it like plain ~) if
076f82
+             user is getenv ("USERNAME") would not be a good idea,
076f82
+             since it would make people think that ~user is supported
076f82
+             in general.  */
076f82
+          if (flags & GLOB_TILDE_CHECK)
076f82
+            {
076f82
+              retval = GLOB_NOMATCH;
076f82
+              goto out;
076f82
+            }
076f82
+#endif /* WINDOWS32 */
076f82
         }
076f82
     }
076f82
 
076f82
@@ -1266,6 +1284,8 @@ glob_in_dir (const char *pattern, const char *directory, int flags,
076f82
 {
076f82
   size_t dirlen = strlen (directory);
076f82
   void *stream = NULL;
076f82
+  struct scratch_buffer s;
076f82
+  scratch_buffer_init (&s);
076f82
 # define GLOBNAMES_MEMBERS(nnames) \
076f82
     struct globnames *next; size_t count; char *name[nnames];
076f82
   struct globnames { GLOBNAMES_MEMBERS (FLEXIBLE_ARRAY_MEMBER) };
076f82
@@ -1337,6 +1357,7 @@ glob_in_dir (const char *pattern, const char *directory, int flags,
076f82
         }
076f82
       else
076f82
         {
076f82
+          int dfd = dirfd (stream);
076f82
           int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
076f82
                            | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0));
076f82
           flags |= GLOB_MAGCHAR;
076f82
@@ -1364,8 +1385,32 @@ glob_in_dir (const char *pattern, const char *directory, int flags,
076f82
               if (flags & GLOB_ONLYDIR)
076f82
                 switch (readdir_result_type (d))
076f82
                   {
076f82
-                  case DT_DIR: case DT_LNK: case DT_UNKNOWN: break;
076f82
                   default: continue;
076f82
+                  case DT_DIR: break;
076f82
+                  case DT_LNK: case DT_UNKNOWN:
076f82
+                    /* The filesystem was too lazy to give us a hint,
076f82
+                       so we have to do it the hard way.  */
076f82
+                    if (__glibc_unlikely (dfd < 0 || flags & GLOB_ALTDIRFUNC))
076f82
+                      {
076f82
+                        size_t namelen = strlen (d.name);
076f82
+                        size_t need = dirlen + 1 + namelen + 1;
076f82
+                        if (s.length < need
076f82
+                            && !scratch_buffer_set_array_size (&s, need, 1))
076f82
+                          goto memory_error;
076f82
+                        char *p = mempcpy (s.data, directory, dirlen);
076f82
+                        *p = '/';
076f82
+                        p += p[-1] != '/';
076f82
+                        memcpy (p, d.name, namelen + 1);
076f82
+                        if (! is_dir (s.data, flags, pglob))
076f82
+                          continue;
076f82
+                      }
076f82
+                    else
076f82
+                      {
076f82
+                        struct_stat64 st64;
076f82
+                        if (! (GLOB_FSTATAT64 (dfd, d.name, &st64, 0) == 0
076f82
+                               && S_ISDIR (st64.st_mode)))
076f82
+                          continue;
076f82
+                      }
076f82
                   }
076f82
 
076f82
               if (fnmatch (pattern, d.name, fnm_flags) == 0)
076f82
@@ -1497,5 +1542,6 @@ glob_in_dir (const char *pattern, const char *directory, int flags,
076f82
       __set_errno (save);
076f82
     }
076f82
 
076f82
+  scratch_buffer_free (&s);
076f82
   return result;
076f82
 }
076f82
diff --git a/sysdeps/unix/sysv/linux/glob64-time64.c b/sysdeps/unix/sysv/linux/glob64-time64.c
076f82
index a465f70905e5a8a3..95efe4c4f4624967 100644
076f82
--- a/sysdeps/unix/sysv/linux/glob64-time64.c
076f82
+++ b/sysdeps/unix/sysv/linux/glob64-time64.c
076f82
@@ -37,6 +37,7 @@
076f82
 # define GLOB_LSTAT      gl_lstat
076f82
 # define GLOB_STAT64     __stat64_time64
076f82
 # define GLOB_LSTAT64    __lstat64_time64
076f82
+# define GLOB_FSTATAT64    __fstatat64_time64
076f82
 
076f82
 # define COMPILE_GLOB64	1
076f82