Method: Net::SFTP::Operations::Dir#glob

Defined in:
lib/net/sftp/operations/dir.rb

#glob(path, pattern, flags = 0) ⇒ Object

Works as ::Dir.glob, matching (possibly recursively) all directory entries under path against pattern. If a block is given, matches will be yielded to the block as they are found; otherwise, they will be returned in an array when the method finishes.

Because working over an SFTP connection is always going to be slower than working purely locally, don’t expect this method to perform with the same level of alacrity that ::Dir.glob does; it will work best for shallow directory hierarchies with relatively few directories, though it should be able to handle modest numbers of files in each directory.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/net/sftp/operations/dir.rb', line 58

def glob(path, pattern, flags=0)
  flags |= ::File::FNM_PATHNAME
  path = path.chop if path[-1,1] == "/"

  results = [] unless block_given?
  queue = entries(path).reject { |e| e.name == "." || e.name == ".." }
  while queue.any?
    entry = queue.shift

    if entry.directory? && !%w(. ..).include?(::File.basename(entry.name))
      queue += entries("#{path}/#{entry.name}").map do |e|
        e.name.replace("#{entry.name}/#{e.name}")
        e
      end
    end

    if ::File.fnmatch(pattern, entry.name, flags)
      if block_given?
        yield entry
      else
        results << entry
      end
    end
  end

  return results unless block_given?
end