Class: DirR

Inherits:
Object
  • Object
show all
Defined in:
lib/dir_r.rb

Overview

Recursive walk through a directory tree There is also Find.find and Dir.glob as alternatives Symbolic links, pointing nowhere, are ignored Symbolic links to directories are ignored

We will process the linked to directory, if it is in this tree;
But we don't want to break out of this tree, and go wondering through the file system.

We also ignore special files (i.e. sockets, dev nodes, …)

Constant Summary collapse

VERSION =
'1.0.1'

Class Method Summary collapse

Class Method Details

.walk_dir(directory:, walk_sub_directories: true, ignore_symlinks: true, debug: false) {|String, String, Boolean| ... } ⇒ Object

Directory listing. Ignoring symbolic links

Parameters:

  • directory (String)

    directory to list

  • walk_sub_directories (Boolean) (defaults to: true)

    Recurse through the subdirectories

  • ignore_symlinks (Boolean) (defaults to: true)

    Skip over symbolic links to files

Yields:

  • (String, String, Boolean)

    Directory path, Filename, Symbolic Link true/false (only if ! ignore_symlinks)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dir_r.rb', line 17

def self.walk_dir(directory:, walk_sub_directories: true, ignore_symlinks: true, debug: false, &block)
  puts "In #{directory}" if debug
  Dir.open(directory).each do |filename|
    puts " Got: #{filename}" if debug
    if filename != '.' && filename != '..' #ignore parent, and current directories.
      qualified_filename =  "#{directory}/#{filename}"
      begin
        stat_record =  File.stat(qualified_filename)  #File end
        lstat_record =  File.lstat(qualified_filename)#Link end 
      rescue Errno::ENOENT
        puts "  Skipping #{qualified_filename}" if debug
        next # Link points nowhere, so we ignore it, rather than stop.
      end
      link = lstat_record.symlink? 
      if stat_record.directory?
        if !link && walk_sub_directories 
          #recurse through sub-directories.
          #don't follow links to directories though, as they could take us anywhere, including looping.
          walk_dir( directory: qualified_filename,
                    walk_sub_directories: walk_sub_directories, 
                    ignore_symlinks: ignore_symlinks, 
                    debug: debug,
                    &block
                  )
          puts "Back In #{directory}" if debug
        end
      elsif stat_record.file? 
        # Ordinary file and not a link, or we are accepting links
        if !ignore_symlinks
          yield directory, filename, link
        elsif !link
          yield directory, filename
        end
      end
    end
  end
end