Skip to content
Kward Search API index

Class: Kward::NativeShell::Glob

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/shell/native_glob.rb

Overview

Path traversal uses the same bounded pattern matcher as case and trimming. Recursive ** never follows symlinked directories or visits hidden entries.

Constant Summary collapse

MAX_ENTRIES =
100_000
MAX_DEPTH =
100

Instance Method Summary collapse

Constructor Details

#initialize(pattern, cwd:, locale:, globstar: false) ⇒ Glob

Returns a new instance of Glob.



9
10
11
12
13
14
15
16
17
18
# File 'lib/kward/shell/native_glob.rb', line 9

def initialize(pattern, cwd:, locale:, globstar: false)
  @parts = pattern.split("/", -1)
  @absolute = pattern.start_with?("/")
  @parts.shift if @absolute
  @root = @absolute ? "/" : cwd
  @locale = locale
  @globstar = globstar
  @matches = []
  @visited = 0
end

Instance Method Details

#matches ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kward/shell/native_glob.rb', line 20

def matches
  root = @root
  prefix = +(@absolute ? "/" : "")
  index = 0
  if root.respond_to?(:access_path)
    # Resolve literal directory prefixes through the retained descriptor,
    # including ../patterns when the current directory has been removed.
    while index < @parts.length - 1 && !Pattern.new(@parts[index], locale: @locale).wildcards?
      prefix << @parts[index].gsub(/\\(.)/m, '\1') << "/"
      index += 1
    end
    opened = Directory.new(prefix, base: root) unless prefix.empty?
    root = (opened || root).access_path
  end
  walk(root, prefix, index, index)
  @matches.uniq.sort { |left, right| @locale.compare(left, right) }
rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EACCES
  []
ensure
  opened&.close
end