Module: AgentC::Tools::Paths

Defined in:
lib/agent_c/tools/paths.rb

Class Method Summary collapse

Class Method Details

.allowed?(dir, path) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/agent_c/tools/paths.rb', line 18

def allowed?(dir, path)
  child?(dir, path)
end

.child?(parent_dir, child_path) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/agent_c/tools/paths.rb', line 22

def child?(parent_dir, child_path)

  unless child_path.start_with?("/")
    child_path = File.join(parent_dir, child_path)
  end

  child = Pathname.new(child_path).expand_path
  parent = Pathname.new(parent_dir).expand_path

  relative = child.relative_path_from(parent)
  !relative.to_s.start_with?('..')
end

.relative_to_dir(dir, path) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/agent_c/tools/paths.rb', line 10

def relative_to_dir(dir, path)
  if path.start_with?(dir)
    path
  else
    File.join(dir, path)
  end
end