Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does glob lstat matching entries?

Tags:

linux

glob

perl

Looking into behavior in this question, I was surprised to see that perl lstat()s every path matching a glob pattern:

$ mkdir dir
$ touch dir/{foo,bar,baz}.txt  
$ strace -e trace=lstat perl -E 'say $^V; <dir/b*>' 
v5.10.1
lstat("dir/baz.txt", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
lstat("dir/bar.txt", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0

I see the same behavior on my Linux system with glob(pattern) and <pattern>, and with later versions of perl.

My expectation was that the globbing would simply opendir/readdir under the hood, and that it would not need to inspect the actual pathnames it was searching.

What is the purpose of this lstat? Does it affect the glob()s return?

like image 802
pilcrow Avatar asked May 09 '13 12:05

pilcrow


1 Answers

This strange behavior has been noticed before on PerlMonks. It turns out that glob calls lstat to support its GLOB_MARK flag, which has the effect that:

Each pathname that is a directory that matches the pattern has a slash appended.

To find out whether a directory entry refers to a subdir, you need to stat it. This is apparently done even when the flag is not given.

like image 109
Fred Foo Avatar answered Oct 05 '22 22:10

Fred Foo