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 paths.collect!{|d| d.dup}
37 while file = paths.shift
40 next unless File.exist? file
42 if File.lstat(file).directory? then
46 next if f == "." or f == ".."
47 if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
49 elsif file == "/" then
52 f = File.join(file, f)
54 paths.unshift f.untaint
60 rescue Errno::ENOENT, Errno::EACCES
67 # Skips the current file or directory, restarting the loop with the next
68 # entry. If the current file is a directory, that directory will not be
69 # recursively entered. Meaningful only within the block associated with
72 # See the +Find+ module documentation for an example.
78 module_function :find, :prune