lib/treevisitor/directory_walker.rb in treevisitor-0.2.2 vs lib/treevisitor/directory_walker.rb in treevisitor-0.2.3

- old
+ new

@@ -14,11 +14,11 @@ # @yieldparam [optional, types, ...] argname description # @yieldreturn [optional, types, ...] description # # @overload initialize(dirname) # @param [String] dirname the root of the tree (top level directory) - # + # # @overload initialize(dirname, options) # @param [Hash] options # @option options [String,Regex, Array<String,Regexp>] :ignore list of ignore pattern # @option options [String] :ignore_dir # @option options [String] :ignore_file @@ -257,28 +257,44 @@ # # recurse on other directories # def process_directory(dirname) - @visitor.enter_node(dirname) # return if ignore_dir?( dirname ) begin - Dir.entries(dirname).sort.each { |basename| + entries = Dir.entries(dirname).sort + rescue Errno::EACCES => e + $stderr.puts e + @visitor.cannot_enter_node(dirname, e) + return + rescue Errno::EPERM => e + $stderr.puts e + @visitor.cannot_enter_node(dirname, e) + return + end + + + @visitor.enter_node(dirname) + entries.each { |basename| + begin next if basename == "." or basename == ".." # ignore always "." and ".." pathname = File.join(dirname, basename) if File.directory?(pathname) process_directory(pathname) unless ignore_dir?(basename) else if !!@visit_file && match?(basename) && !ignore_file?(basename) @visitor.visit_leaf(pathname) end end + rescue Errno::EACCES => e + $stderr.puts e + rescue Errno::EPERM + $stderr.puts e + end } - rescue Errno::EACCES => e - $stderr.puts e - end + @visitor.exit_node(dirname) end end end