Module: Utils::Path::Helpers Private
Overview
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Path helpers available as private methods when this module is included.
Instance Method Summary collapse
- #child_of?(parent, child) ⇒ Boolean private
- #cp_path_sub(path, pattern, replacement, &_block) ⇒ void private
- #ensure_child_of!(parent, child, message:) ⇒ void private
- #ensure_writable(path, &_block) ⇒ void private
-
#relative_path_from(path, base) ⇒ Pathname
private
Pathname#relative_path_fromwithout itscleanpathcalls for clean absolute paths. - #resolved_path(path) ⇒ Pathname private
- #resolved_path_exists?(path) ⇒ Boolean private
- #rmdir_if_possible(path) ⇒ Boolean private
- #text_executable?(path) ⇒ Boolean private
Instance Method Details
#child_of?(parent, child) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
20 21 22 23 24 25 |
# File 'utils/path.rb', line 20 def child_of?(parent, child) parent_pathname = Pathname(parent). child_pathname = Pathname(child). child_pathname.ascend { |p| return true if p == parent_pathname } false end |
#cp_path_sub(path, pattern, replacement, &_block) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'utils/path.rb', line 45 def cp_path_sub(path, pattern, replacement, &_block) raise "#{path} does not exist" unless path.exist? pattern = pattern.to_s if pattern.is_a?(Pathname) replacement = replacement.to_s if replacement.is_a?(Pathname) dst = path.sub(pattern, replacement) raise "#{path} is the same file as #{dst}" if path == dst if path.directory? dst.mkpath else dst.dirname.mkpath dst = yield(path, dst) if block_given? FileUtils.cp(path, dst) end end |
#ensure_child_of!(parent, child, message:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
28 29 30 31 32 33 34 35 |
# File 'utils/path.rb', line 28 def ensure_child_of!(parent, child, message:) return if child_of?(resolve_symlinks(parent), resolve_symlinks(child)) raise rescue SystemCallError # Fail closed: an unresolvable symlink leaves containment unproven. raise end |
#ensure_writable(path, &_block) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'utils/path.rb', line 98 def ensure_writable(path, &_block) path = Pathname(path) saved_perms = nil unless path.writable? saved_perms = path.stat.mode FileUtils.chmod "u+rw", path.to_path end yield ensure path.chmod saved_perms if saved_perms end |
#relative_path_from(path, base) ⇒ Pathname
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Pathname#relative_path_from without its cleanpath calls for clean absolute paths.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'utils/path.rb', line 112 def relative_path_from(path, base) path_string = path.to_s base_string = base.to_s if File::FNM_SYSCASE.zero? && clean_absolute_path?(path_string) && clean_absolute_path?(base_string) base_prefix = "#{base_string}/" return Pathname(path_string.delete_prefix(base_prefix)) if path_string.start_with?(base_prefix) path_names = path_string.split("/").drop(1) base_names = base_string.split("/").drop(1) common = 0 common += 1 while common < path_names.length && path_names[common] == base_names[common] names = Array.new(base_names.length - common, "..") + path_names.drop(common) return Pathname(names.empty? ? "." : File.join(*names)) end path.relative_path_from(base) end |
#resolved_path(path) ⇒ Pathname
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
84 85 86 |
# File 'utils/path.rb', line 84 def resolved_path(path) path.symlink? ? path.dirname.join(path.readlink) : path end |
#resolved_path_exists?(path) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
89 90 91 92 93 94 95 |
# File 'utils/path.rb', line 89 def resolved_path_exists?(path) link = path.readlink rescue ArgumentError false else path.dirname.join(link).exist? end |
#rmdir_if_possible(path) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'utils/path.rb', line 64 def rmdir_if_possible(path) path.rmdir true rescue Errno::ENOTEMPTY if (ds_store = path/".DS_Store").exist? && path.children.one? ds_store.unlink retry else false end rescue Errno::EACCES, Errno::ENOENT, Errno::EBUSY, Errno::EPERM false end |
#text_executable?(path) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
79 80 81 |
# File 'utils/path.rb', line 79 def text_executable?(path) /\A#!\s*\S+/.match?(path.open("r") { |file| file.read(1024) }) end |