1 # frozen_string_literal: true
3 # find.rb: the Find module for processing all files under a given directory.
7 # The +Find+ module supports the top-down traversal of a set of file paths.
9 # For example, to total the size of all files under your home directory,
10 # ignoring anything in a "dot" directory (e.g. $HOME/.ssh):
16 # Find.find(ENV["HOME"]) do |path|
17 # if FileTest.directory?(path)
18 # if File.basename(path).start_with?('.')
19 # Find.prune # Don't look any further into this directory.
24 # total_size += FileTest.size(path)
33 # Calls the associated block with the name of every file and directory listed
34 # as arguments, then recursively on their subdirectories, and so on.
36 # Returns an enumerator if no block is given.
38 # See the +Find+ module documentation for an example.
40 def find(*paths, ignore_error: true) # :yield: path
41 block_given? or return enum_for(__method__, *paths, ignore_error: ignore_error)
43 fs_encoding = Encoding.find("filesystem")
45 paths.collect!{|d| raise Errno::ENOENT, d unless File.exist?(d); d.dup}.each do |path|
46 path = path.to_path if path.respond_to? :to_path
47 enc = path.encoding == Encoding::US_ASCII ? fs_encoding : path.encoding
54 rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG, Errno::EINVAL
55 raise unless ignore_error
60 fs = Dir.children(file, encoding: enc)
61 rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG, Errno::EINVAL
62 raise unless ignore_error
67 f = File.join(file, f)
78 # Skips the current file or directory, restarting the loop with the next
79 # entry. If the current file is a directory, that directory will not be
80 # recursively entered. Meaningful only within the block associated with
83 # See the +Find+ module documentation for an example.
89 module_function :find, :prune