为什么glob lstat匹配条目? [英] Why does glob lstat matching entries?

查看:47
本文介绍了为什么glob lstat匹配条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题中研究行为时,我很惊讶地看到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

我在使用glob(pattern)<pattern>以及更高版本的perl的Linux系统上看到相同的行为.

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

我的期望是,遍历只会在幕后的opendir/readdir中进行,而无需检查正在搜索的实际路径名.

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.

lstat的目的是什么?它会影响glob()的返回吗?

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

推荐答案

此奇怪的行为已经之前已注意到在PerlMonks上.事实证明,glob调用lstat以支持其 GLOB_MARK 标志,其效果是:

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.

要查明目录条目是否引用子目录,您需要stat它.即使没有给出标志,显然也可以做到这一点.

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.

这篇关于为什么glob lstat匹配条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆