2 # find.rb: the Find module for processing all files under a given directory.
6 # The +Find+ module supports the top-down traversal of a set of file paths.
8 # For example, to total the size of all files under your home directory,
9 # ignoring anything in a "dot" directory (e.g. $HOME/.ssh):
15 # Find.find(ENV["HOME"]) do |path|
16 # if FileTest.directory?(path)
17 # if File.basename(path)[0] == ?.
18 # Find.prune # Don't look any further into this directory.
23 # total_size += FileTest.size(path)
30 # Calls the associated block with the name of every file and directory listed
31 # as arguments, then recursively on their subdirectories, and so on.
33 # See the +Find+ module documentation for an example.
35 def find(*paths) # :yield: path
36 block_given? or return enum_for(__method__, *paths)
38 paths.collect!{|d| raise Errno::ENOENT unless File.exist?(d); d.dup}
39 while file = paths.shift
42 next unless File.exist? file
44 if File.lstat(file).directory? then
48 next if f == "." or f == ".."
49 if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
51 elsif file == "/" then
54 f = File.join(file, f)
56 paths.unshift f.untaint
62 rescue Errno::ENOENT, Errno::EACCES
69 # Skips the current file or directory, restarting the loop with the next
70 # entry. If the current file is a directory, that directory will not be
71 # recursively entered. Meaningful only within the block associated with
74 # See the +Find+ module documentation for an example.
80 module_function :find, :prune