如何使Ruby的Find.find遵循符号链接? [英] How to make Ruby's Find.find follow symlinks?

查看:164
本文介绍了如何使Ruby的Find.find遵循符号链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件层次结构,一些子目录是相对的符号链接。我使用 Ruby的 Find.find 来抓取这些dirs和找到一些特定的文件。但是它没有查找任何符号链接的目录(它跟随的是符号链接)。

查看源代码似乎问题是因为它使用 File.lstat(file).directory?测试是否有某个目录。这会返回符号链接 false ,但是 File.stat.directory?返回 true

我怎样才能让 Find.find 遵循符号链接,短缺猴子修补它使用 File.stat 而不是 File.lstat

解决方案

对于其他人,我最终使用 路径名

code> 和下面的递归代码:

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屋!

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