bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [patch] glob: resolve DT_UNKNOWN via is_dir
@ 2021-11-17  3:45 DJ Delorie
  2021-11-17 11:12 ` Florian Weimer
  2022-03-11 21:43 ` [patch v2] " DJ Delorie
  0 siblings, 2 replies; 11+ messages in thread
From: DJ Delorie @ 2021-11-17  3:45 UTC (permalink / raw)
  To: bug-gnulib


The DT_* values returned by getdents (readdir) are only hints and
not required.  In fact, some Linux filesystems return DT_UNKNOWN
for most entries, regardless of actual type.  This causes make
to mis-match patterns with a trailing slash (via GLOB_ONLYDIR)
(see make's functions/wildcard test case).  Thus, this patch
detects that case and uses is_dir() to make the type known enough
for proper operation.

Performance in non-DT_UNKNOWN cases is not affected.

The lack of DT_* is a well known issue on older XFS installations
(for example, RHEL 7 and 8, Fedora 28) but can be recreated by
creating an XFS filesystem with flags that mimic older behavior:

$ fallocate -l 10G /xfs.fs
$ mkfs.xfs -n ftype=0 -m crc=0 -f /xfs.fs
$ mkdir /xfs
$ mount -o loop /xfs.fs /xfs

[tested by importing this change to glibc, and using that to run make's testsuite]

diff --git a/lib/glob.c b/lib/glob.c
index 22c459574..d0521bb4a 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -1381,7 +1381,26 @@ glob_in_dir (const char *pattern, const char *directory, int flags,
               if (flags & GLOB_ONLYDIR)
                 switch (readdir_result_type (d))
                   {
-                  case DT_DIR: case DT_LNK: case DT_UNKNOWN: break;
+                  case DT_DIR: case DT_LNK: break;
+		  case DT_UNKNOWN:
+		    {
+		      /* The filesystem was too lazy to give us a hint,
+			 so we have to do it the hard way.  */
+		      char *fullpath, *p;
+		      bool isdir;
+		      fullpath = malloc (strlen (directory) + strlen (d.name) + 2);
+		      if (fullpath == NULL)
+			/* This matches old behavior wrt DT_UNKNOWN.  */
+			break;
+		      p = stpcpy (fullpath, directory);
+		      *p++ = '/';
+		      strcpy (p, d.name);
+		      isdir = is_dir (fullpath);
+		      free (fullpath);
+		      if (isdir)
+			break;
+		      continue;
+		    }
                   default: continue;
                   }
 



^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-05-14 20:35 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-17  3:45 [patch] glob: resolve DT_UNKNOWN via is_dir DJ Delorie
2021-11-17 11:12 ` Florian Weimer
2021-11-17 21:59   ` DJ Delorie
2021-11-17 22:14     ` Florian Weimer
2021-11-17 22:38       ` DJ Delorie
2022-03-11 21:43 ` [patch v2] " DJ Delorie
2022-03-12  0:37   ` Paul Eggert
2022-03-23  4:34     ` DJ Delorie
2022-03-23 17:35       ` Paul Eggert
2022-03-25 19:18         ` DJ Delorie
2022-05-14 20:34         ` Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).