Feature #22604 » find_lstat.patch
| lib/find.rb | ||
|---|---|---|
|
# Find.find('lib') {|entry| raise Errno::ENOENT }
|
||
|
# ```
|
||
|
#
|
||
|
# When keyword argument `lstat` is given as `true`,
|
||
|
# also passes the block the File::Stat that `Find` gets from File.lstat for each path,
|
||
|
# or `nil` if that call raised an ignored exception:
|
||
|
#
|
||
|
# ```ruby
|
||
|
# size = 0
|
||
|
# Find.find('bin', lstat: true) {|path, stat| size += stat.size if stat&.file? }
|
||
|
# ```
|
||
|
#
|
||
|
# With no block given, returns a new Enumerator.
|
||
|
def find(*paths, ignore_error: true) # :yield: path
|
||
|
block_given? or return enum_for(__method__, *paths, ignore_error: ignore_error)
|
||
|
def find(*paths, ignore_error: true, lstat: false) # :yield: path
|
||
|
block_given? or return enum_for(__method__, *paths, ignore_error: ignore_error, lstat: lstat)
|
||
|
fs_encoding = Encoding.find("filesystem")
|
||
| ... | ... | |
|
ps = [path]
|
||
|
while file = ps.shift
|
||
|
catch(:prune) do
|
||
|
yield file.dup
|
||
|
yield file.dup unless lstat
|
||
|
begin
|
||
|
s = File.lstat(file)
|
||
|
rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG, Errno::EINVAL
|
||
|
raise unless ignore_error
|
||
|
yield file.dup, nil if lstat
|
||
|
next
|
||
|
end
|
||
|
yield file.dup, s if lstat
|
||
|
if s.directory? then
|
||
|
begin
|
||
|
fs = Dir.children(file, encoding: enc)
|
||
| lib/pathname.rb | ||
|---|---|---|
|
# Pathname('lib').find {|entry| raise Errno::ENOENT }
|
||
|
# ```
|
||
|
#
|
||
|
# When keyword argument `lstat` is given as `true`,
|
||
|
# also passes the block the File::Stat for each path, as Find.find does.
|
||
|
#
|
||
|
# With no block given, returns a new Enumerator.
|
||
|
def find(ignore_error: true) # :yield: pathname
|
||
|
return to_enum(__method__, ignore_error: ignore_error) unless block_given?
|
||
|
def find(ignore_error: true, lstat: false) # :yield: pathname
|
||
|
return to_enum(__method__, ignore_error: ignore_error, lstat: lstat) unless block_given?
|
||
|
require 'find'
|
||
|
if @path == '.'
|
||
|
Find.find(@path, ignore_error: ignore_error) {|f| yield self.class.new(f.delete_prefix('./')) }
|
||
|
Find.find(@path, ignore_error: ignore_error, lstat: lstat) {|f, *s| yield self.class.new(f.delete_prefix('./')), *s }
|
||
|
else
|
||
|
Find.find(@path, ignore_error: ignore_error) {|f| yield self.class.new(f) }
|
||
|
Find.find(@path, ignore_error: ignore_error, lstat: lstat) {|f, *s| yield self.class.new(f), *s }
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| test/pathname/test_pathname.rb | ||
|---|---|---|
|
}
|
||
|
end
|
||
|
def test_find_lstat
|
||
|
with_tmpchdir('rubytest-pathname') {|dir|
|
||
|
open("a", "w") {}
|
||
|
Dir.mkdir("d")
|
||
|
open("d/x", "w") {}
|
||
|
a = []; Pathname(".").find(lstat: true) {|v, s| a << [v, s.ftype] }; a.sort!
|
||
|
assert_equal([[Pathname("."), "directory"], [Pathname("a"), "file"], [Pathname("d"), "directory"], [Pathname("d/x"), "file"]], a)
|
||
|
a = []; Pathname("d").find(lstat: true) {|v, s| a << [v, s.ftype] }; a.sort!
|
||
|
assert_equal([[Pathname("d"), "directory"], [Pathname("d/x"), "file"]], a)
|
||
|
a = Pathname("d").find(lstat: true).map {|v, s| [v, s.ftype] }.sort
|
||
|
assert_equal([[Pathname("d"), "directory"], [Pathname("d/x"), "file"]], a)
|
||
|
}
|
||
|
end
|
||
|
def assert_mode(val, mask, path, mesg = nil)
|
||
|
st = File.stat(path)
|
||
|
assert_equal(val.to_s(8), (st.mode & mask).to_s(8), st.inspect)
|
||
| test/test_find.rb | ||
|---|---|---|
|
}
|
||
|
end
|
||
|
def test_lstat
|
||
|
Dir.mktmpdir {|d|
|
||
|
File.open("#{d}/a", "w"){}
|
||
|
Dir.mkdir("#{d}/b")
|
||
|
File.open("#{d}/b/a", "w"){}
|
||
|
a = []
|
||
|
Find.find(d, lstat: true) {|f, s| a << [f, s.ftype] }
|
||
|
assert_equal([[d, "directory"], ["#{d}/a", "file"], ["#{d}/b", "directory"], ["#{d}/b/a", "file"]], a)
|
||
|
a = Find.find(d, lstat: true).map {|f, s| [f, s.ftype] }
|
||
|
assert_equal([[d, "directory"], ["#{d}/a", "file"], ["#{d}/b", "directory"], ["#{d}/b/a", "file"]], a)
|
||
|
}
|
||
|
end
|
||
|
def test_lstat_dont_follow_symlink
|
||
|
Dir.mktmpdir {|d|
|
||
|
Dir.mkdir("#{d}/b")
|
||
|
begin
|
||
|
File.symlink("#{d}/b", "#{d}/c")
|
||
|
rescue NotImplementedError, Errno::EACCES
|
||
|
omit "symlink is not supported."
|
||
|
end
|
||
|
a = []
|
||
|
Find.find(d, lstat: true) {|f, s| a << [f, s.ftype] }
|
||
|
assert_equal([[d, "directory"], ["#{d}/b", "directory"], ["#{d}/c", "link"]], a)
|
||
|
}
|
||
|
end
|
||
|
def test_lstat_prune
|
||
|
Dir.mktmpdir {|d|
|
||
|
File.open("#{d}/a", "w"){}
|
||
|
Dir.mkdir("#{d}/b")
|
||
|
File.open("#{d}/b/a", "w"){}
|
||
|
Dir.mkdir("#{d}/c")
|
||
|
a = []
|
||
|
Find.find(d, lstat: true) {|f, s|
|
||
|
a << f
|
||
|
Find.prune if f == "#{d}/b"
|
||
|
}
|
||
|
assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/c"], a)
|
||
|
}
|
||
|
end
|
||
|
def test_lstat_unsearchable_dir
|
||
|
omit "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
|
||
|
omit "skipped because root can read anything" if Process.uid == 0
|
||
|
Dir.mktmpdir {|d|
|
||
|
Dir.mkdir(dir = "#{d}/dir")
|
||
|
File.open(file = "#{dir}/foo", "w"){}
|
||
|
begin
|
||
|
File.chmod(0600, dir)
|
||
|
a = []
|
||
|
Find.find(d, lstat: true) {|f, s| a << [f, s&.ftype] }
|
||
|
assert_equal([[d, "directory"], [dir, "directory"], [file, nil]], a)
|
||
|
a = []
|
||
|
assert_raise_with_message(Errno::EACCES, /#{Regexp.quote(file)}/) do
|
||
|
Find.find(d, ignore_error: false, lstat: true) {|f, s| a << f }
|
||
|
end
|
||
|
assert_equal([d, dir], a)
|
||
|
ensure
|
||
|
File.chmod(0700, dir)
|
||
|
end
|
||
|
}
|
||
|
end
|
||
|
def test_encoding_ascii
|
||
|
Dir.mktmpdir {|d|
|
||
|
File.open("#{d}/a", "w"){}
|
||