Method: Chef::ChefFS::PathUtils.descendant_path

Defined in:
lib/chef/chef_fs/path_utils.rb

.descendant_path(path, ancestor) ⇒ Object

Given two general OS-dependent file paths, determines the relative path of the child with respect to the ancestor. Both child and ancestor must exist and be fully resolved - this is strictly a lexical comparison. No trailing slashes and other shenanigans are allowed.

TODO: Move this to util/path_helper.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/chef_fs/path_utils.rb', line 113

def self.descendant_path(path, ancestor)
  candidate_fragment = path[0, ancestor.length]
  return nil unless PathUtils.os_path_eq?(candidate_fragment, ancestor)

  if ancestor.length == path.length
    ""
  elsif /#{PathUtils::REGEXP_PATH_SEPARATOR}/.match?(path[ancestor.length, 1])
    path[ancestor.length + 1..-1]
  else
    nil
  end
end