module Walk

Constants

CURRENT_DIR
PARENT_DIR
VERSION

Public Class Methods

walk(root, topdown: true, followlinks: false, &block) click to toggle source
# File lib/walk.rb, line 11
def self.walk(root, topdown: true, followlinks: false, &block)

  if block_given?
    inner_walk(root, topdown, followlinks, &block)
  else
    Enumerator.new do |enum|
      inner_walk(root, topdown, followlinks) do |path, dirs, files|
        enum << [path, dirs, files]
      end
    end      
  end

end

Private Class Methods

inner_walk(root, topdown, followlinks) { |root, dirs, files| ... } click to toggle source
# File lib/walk.rb, line 26
def self.inner_walk(root, topdown, followlinks, &block)

  dirs = []
  files = []
  path_to_explore = []

  begin
    Dir.entries(root).each do |entry|
      
      next if entry==CURRENT_DIR or entry==PARENT_DIR

      fullpath = File.join(root, entry)
      is_file, is_dir, go_down = inspect_entry(fullpath, followlinks)
      files << entry if is_file
      dirs << entry if is_dir
      path_to_explore << fullpath if go_down

    end        
  rescue Errno::ENOENT => e
    # current dir disappeared since parent scan, it's not an error
    return
  end

  yield  root, dirs, files  if topdown

  path_to_explore.each do |fullpath|
    inner_walk(fullpath, topdown, followlinks, &block)
  end

  yield  root, dirs, files  unless topdown

end
inspect_entry(fullpath, followlinks) click to toggle source
# File lib/walk.rb, line 60
def self.inspect_entry(fullpath, followlinks)

  is_file = is_dir = go_down = false

  if File.file?(fullpath)
    is_file = true
  elsif File.directory?(fullpath)
    is_dir = true
    if (followlinks or not File.symlink?(fullpath)) and
      File.readable?(fullpath)
      go_down = true
    end
  end

  return is_file, is_dir, go_down

rescue Errno::ENOENT => e
  return nil, nil, nil
end