From: DJ Delorie via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org
Subject: glob vs '*/' vs GLOB_ONLYDIR vs xfs
Date: Wed, 04 Aug 2021 23:24:32 -0400 [thread overview]
Message-ID: <xny29go9ov.fsf@greed.delorie.com> (raw)
in posix/glob.c we have this comment:
/* POSIX requires all slashes to be matched. This means that with
a trailing slash we must match only directories. */
if (pattern[0] && pattern[strlen (pattern) - 1] == '/')
flags |= GLOB_ONLYDIR;
In "man glob" we see:
GLOB_ONLYDIR
This is a _hint_ to glob() that the caller is interested
only in directories that match the pattern.
So, for starters, we're relying on "a hint" to implement a "must".
If you're running glob on an XFS filesystem, readdir() doesn't
reliably fill in d_type, and the "hint" fails:
#include <stdlib.h>
#include <glob.h>
main(int argc, char **argv) {
glob_t g;
int i;
glob (argv[1], 0, NULL, &g);
for (i=0; i<g.gl_pathc; i++)
printf("glob[%d] = `%s'\n", i, g.gl_pathv[i]);
return 0;
}
$ ls -l
total 4
-rw-r--r-- 1 dj games 56 Aug 4 22:45 Makefile
drwxr-xr-x 2 dj games 10 Aug 4 22:44 hellod/
-rw-r--r-- 1 dj games 0 Aug 4 22:44 hellof
$ pwd
/greed/dj/gnu/make-4.3/dj
$ ~/src/globtest 'hello*/'
glob[0] = `hellod/'
glob[1] = `hellof'
$ ~/src/globtest 'hello*'
glob[0] = `hellod'
glob[1] = `hellof'
If we instead run this on a different filesystem type (tmpfs in this
case), it works as I expect:
$ ls -l
total 4
-rw-r--r-- 1 dj games 56 Aug 4 22:45 Makefile
drwxr-xr-x 2 dj games 6 Aug 4 22:44 hellod/
-rw-r--r-- 1 dj games 0 Aug 4 22:44 hellof
$ pwd
/tmp/make-4.3/dj
$ ~/src/globtest 'hello*/'
glob[0] = `hellod/'
$ ~/src/globtest 'hello*'
glob[0] = `hellod'
glob[1] = `hellof'
Is this a bug in glob(), or a misunderstanding of its documentation?
next reply other threads:[~2021-08-05 3:24 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-05 3:24 DJ Delorie via Libc-alpha [this message]
2021-08-05 8:10 ` glob vs '*/' vs GLOB_ONLYDIR vs xfs Paul Eggert
2021-08-05 18:10 ` DJ Delorie via Libc-alpha
2021-08-06 21:47 ` Paul Eggert
2021-08-06 22:03 ` DJ Delorie via Libc-alpha
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/libc/involved.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xny29go9ov.fsf@greed.delorie.com \
--to=libc-alpha@sourceware.org \
--cc=dj@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).