如何使Ruby的Find.find遵循符号链接? [英] How to make Ruby's Find.find follow symlinks?
问题描述
我有一个文件层次结构,一些子目录是相对的符号链接。我使用 Ruby的 Find.find
来抓取这些dirs和找到一些特定的文件。但是它没有查找任何符号链接的目录(它跟随的是符号链接)。
查看源代码似乎问题是因为它使用 我怎样才能让 对于其他人,我最终使用 File.lstat(file).directory?
测试是否有某个目录。这会返回符号链接 false
,但是 File.stat.directory?
返回 true $ c
Find.find
遵循符号链接,短缺猴子修补它使用 File.stat
而不是 File.lstat
?路径名
pre $ def $ all_files_under(* paths)
paths .flatten!
paths.map! {| p | Pathname.new(p)}
files = paths.select {| p | p.file? }
(路径 - 文件).each do | dir |
档案<< all_files_under(dir.children)
end
files.flatten
end
I have a file hierarchy and some of the sub-directories are relative symlinks. I am using Ruby's Find.find
to crawl through these dirs and find some specific files. However it's not looking into any directory which is a symlink (it follows files which are symlinks).
Looking at the source code it seems the problem is because it's using File.lstat(file).directory?
to test if something is a directory. This returns false
for symlinks but File.stat.directory?
returns true
.
How can I make Find.find
follow symlinks, short of monkey patching it to use File.stat
instead of File.lstat
?
For anyone else watching, I ended up using Pathname
and the following recursive code:
def all_files_under(*paths)
paths.flatten!
paths.map! { |p| Pathname.new(p) }
files = paths.select { |p| p.file? }
(paths - files).each do |dir|
files << all_files_under(dir.children)
end
files.flatten
end
这篇关于如何使Ruby的Find.find遵循符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!