Module: Utils Private

Defined in:
utils.rb,
utils/ast.rb,
utils/git.rb,
utils/svn.rb,
utils/tar.rb,
utils/uid.rb,
utils/curl.rb,
utils/data.rb,
utils/fork.rb,
utils/gzip.rb,
utils/link.rb,
utils/path.rb,
utils/ruby.rb,
utils/text.rb,
utils/clang.rb,
utils/popen.rb,
utils/shell.rb,
utils/timer.rb,
utils/editor.rb,
utils/output.rb,
utils/socket.rb,
utils/bottles.rb,
utils/browser.rb,
utils/linkage.rb,
utils/service.rb,
utils/shebang.rb,
utils/analytics.rb,
utils/backtrace.rb,
utils/gem_setup.rb,
utils/inreplace.rb,
utils/profiling.rb,
utils/autoremove.rb,
utils/executable.rb,
utils/interrupts.rb,
utils/attestation.rb,
utils/brew_command.rb,
utils/portable_ruby.rb,
utils/git_repository.rb,
utils/shell_completion.rb,
utils/topological_hash.rb,
utils/ast.rbi

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.

Defined Under Namespace

Modules: AST, Analytics, Attestation, Backtrace, Bottles, BrewCommand, Browser, Clang, Curl, CycleTolerantTSort, Data, Editor, Executable, GemSetup, Git, Gzip, Inreplace, Interrupts, Link, Output, Path, PortableRuby, Profiling, Ruby, Service, Shebang, Shell, ShellCompletion, Svn, Tar, Text, Timer, UID, UNIXSocketExt Classes: ForkedChildChannel, TopologicalHash, UNIXServerExt

Class Method Summary collapse

Class Method Details

.binary_linked_to_library?(binary, library) ⇒ Boolean

This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
# File 'utils/linkage.rb', line 9

def self.binary_linked_to_library?(binary, library)
  library = library.to_s
  library = File.realpath(library) if library.start_with?(HOMEBREW_PREFIX.to_s)

  binary_path = BinaryPathname.wrap(binary)
  binary_path.dynamically_linked_libraries.any? do |dll|
    dll = File.realpath(dll) if dll.start_with?(HOMEBREW_PREFIX.to_s)
    dll == library
  end
end

.child_error_hash(error) ⇒ Hash{String => T.untyped}

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.

Parameters:

  • error (Exception)

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'utils/fork.rb', line 62

def self.child_error_hash(error)
  # `rewrite_child_error` and `Cask::Artifact` read these keys back.
  error_hash = {
    "json_class" => error.class.name,
    "m"          => error.message,
    "b"          => error.backtrace,
  }
  case error
  when BuildError
    error_hash["cmd"] = error.cmd
    error_hash["args"] = error.args
    error_hash["env"] = error.env
  when ErrorDuringExecution
    error_hash["cmd"] = error.cmd
    error_hash["status"] = if error.status.is_a?(Process::Status)
      {
        exitstatus: error.exitstatus,
        termsig:    error.termsig,
      }
    else
      error.status
    end
    error_hash["output"] = error.output
  end
  error_hash
end

.convert_to_string_or_symbol(string) ⇒ String, Symbol

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.

Converts a string starting with : to a symbol, otherwise returns the string itself.

convert_to_string_or_symbol(":example") # => :example
convert_to_string_or_symbol("example")  # => "example"

Parameters:

Returns:



182
183
184
185
186
# File 'utils.rb', line 182

def self.convert_to_string_or_symbol(string)
  return string.delete_prefix(":").to_sym if string.start_with?(":")

  string
end

.deconstantize(path) ⇒ String

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.

Removes the rightmost segment from the constant expression in the string.

deconstantize('Net::HTTP')   # => "Net"
deconstantize('::Net::HTTP') # => "::Net"
deconstantize('String')      # => ""
deconstantize('::String')    # => ""
deconstantize('')            # => ""

See also #demodulize.

Parameters:

Returns:

See Also:



17
18
19
# File 'utils.rb', line 17

def self.deconstantize(path)
  path.rpartition("::").first
end

.deep_compact_blank(obj, compact_zero: true, compact_false: true) ⇒ T.type_parameter(:U)?

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.

Parameters:

  • obj (T.all(T.type_parameter(:U), Object))
  • compact_zero (Boolean) (defaults to: true)
  • compact_false (Boolean) (defaults to: true)

Returns:

  • (T.type_parameter(:U), nil)


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'utils.rb', line 236

def self.deep_compact_blank(obj, compact_zero: true, compact_false: true)
  obj = case obj
  when Hash
    obj.transform_values { |v| deep_compact_blank(v, compact_zero:, compact_false:) }
       .compact
  when Array
    obj.each_with_object([]) do |v, compacted|
      value = deep_compact_blank(v, compact_zero:, compact_false:)
      compacted << value unless value.nil?
    end
  else
    obj
  end

  return if (compact_false || obj != false) &&
            (obj.blank? || (compact_zero && obj.is_a?(Numeric) && obj.zero?))

  obj
end

.deep_stringify_symbols(obj) ⇒ T.untyped

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.

Parameters:

  • obj (T.untyped)

Returns:

  • (T.untyped)


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'utils.rb', line 189

def self.deep_stringify_symbols(obj)
  case obj
  when String
    # Escape leading : or \ to avoid confusion with stringified symbols
    # ":foo" -> "\:foo"
    # "\foo" -> "\\foo"
    if obj.start_with?(":", "\\")
      "\\#{obj}"
    else
      obj
    end
  when Symbol
    ":#{obj}"
  when Hash
    obj.to_h { |k, v| [deep_stringify_symbols(k), deep_stringify_symbols(v)] }
  when Array
    obj.map { |v| deep_stringify_symbols(v) }
  else
    obj
  end
end

.deep_unstringify_symbols(obj) ⇒ T.untyped

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.

Parameters:

  • obj (T.untyped)

Returns:

  • (T.untyped)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'utils.rb', line 212

def self.deep_unstringify_symbols(obj)
  case obj
  when String
    if obj.start_with?("\\")
      obj[1..]
    elsif obj.start_with?(":")
      obj.delete_prefix(":").to_sym
    else
      obj
    end
  when Hash
    obj.to_h { |k, v| [deep_unstringify_symbols(k), deep_unstringify_symbols(v)] }
  when Array
    obj.map { |v| deep_unstringify_symbols(v) }
  else
    obj
  end
end

.demodulize(path) ⇒ String

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.

Removes the module part from the expression in the string.

demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
demodulize('Inflections')                           # => "Inflections"
demodulize('::Inflections')                         # => "Inflections"
demodulize('')                                      # => ""

See also #deconstantize.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if the provided path is nil

See Also:



33
34
35
36
37
# File 'utils.rb', line 33

def self.demodulize(path)
  raise ArgumentError, "No constant path provided" if path.nil?

  path.rpartition("::").last
end

.exponential_backoff_sleep(try, base: 2, &_blk) {|wait| ... } ⇒ 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.

Sleeps for an exponentially increasing wait (base ** try seconds), yielding the wait time first so callers can print a message before sleeping.

Parameters:

Yields:

  • (wait)


125
126
127
128
129
# File 'utils.rb', line 125

def self.exponential_backoff_sleep(try, base: 2, &_blk)
  wait = base.pow(try)
  yield wait if block_given?
  sleep wait
end

.forked_child_channelForkedChildChannel

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.

Returns:



48
49
50
51
52
# File 'utils/fork.rb', line 48

def self.forked_child_channel
  UNIXSocketExt.open(ENV.fetch("HOMEBREW_ERROR_PIPE")) do |socket|
    ForkedChildChannel.new(receive_forked_child_pipe(socket), receive_forked_child_pipe(socket))
  end
end

.forked_child_error_pipeIO

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.

Returns:

  • (IO)


41
42
43
44
45
# File 'utils/fork.rb', line 41

def self.forked_child_error_pipe
  UNIXSocketExt.open(ENV.fetch("HOMEBREW_ERROR_PIPE")) do |socket|
    receive_forked_child_pipe(socket)
  end
end

.full_name?(full_name) ⇒ 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.

Whether full_name is fully-qualified with a tap prefix, e.g. user/tap/name.

Parameters:

Returns:

  • (Boolean)


72
73
74
# File 'utils.rb', line 72

def self.full_name?(full_name)
  full_name.count("/") == 2
end

.fully_qualified_name(formula_or_cask) ⇒ String

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.

Returns the package name with its tap, including core taps, or its full name if tapless.

Parameters:

Returns:



53
54
55
56
57
58
59
60
# File 'utils.rb', line 53

def self.fully_qualified_name(formula_or_cask)
  tap = formula_or_cask.tap
  if tap && (tap.core_tap? || tap.core_cask_tap?)
    "#{tap.name}/#{formula_or_cask.full_name}"
  else
    formula_or_cask.full_name
  end
end

.git_branch(repo = Pathname.pwd, safe: true) ⇒ String?

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.

Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.

Parameters:

  • repo (String, Pathname) (defaults to: Pathname.pwd)
  • safe (Boolean) (defaults to: true)

Returns:



38
39
40
# File 'utils/git_repository.rb', line 38

def self.git_branch(repo = Pathname.pwd, safe: true)
  GitRepository.new(Pathname(repo)).branch_name(safe:)
end

.git_head(repo = Pathname.pwd, length: nil, safe: true) ⇒ String?

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.

Gets the full commit hash of the HEAD commit.

Parameters:

  • repo (String, Pathname) (defaults to: Pathname.pwd)
  • length (Integer, nil) (defaults to: nil)
  • safe (Boolean) (defaults to: true)

Returns:



13
14
15
16
17
# File 'utils/git_repository.rb', line 13

def self.git_head(repo = Pathname.pwd, length: nil, safe: true)
  return git_short_head(repo, length:) if length

  GitRepository.new(Pathname(repo)).head_ref(safe:)
end

.git_short_head(repo = Pathname.pwd, length: nil, safe: true) ⇒ String?

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.

Gets a short commit hash of the HEAD commit.

Parameters:

  • repo (String, Pathname) (defaults to: Pathname.pwd)
  • length (Integer, nil) (defaults to: nil)
  • safe (Boolean) (defaults to: true)

Returns:



27
28
29
# File 'utils/git_repository.rb', line 27

def self.git_short_head(repo = Pathname.pwd, length: nil, safe: true)
  GitRepository.new(Pathname(repo)).short_head_ref(length:, safe:)
end

.name_from_full_name(full_name) ⇒ String

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.

Parameters:

Returns:



40
41
42
43
44
# File 'utils.rb', line 40

def self.name_from_full_name(full_name)
  _, _, name = full_name.split("/", 3)

  name || full_name
end

.name_or_token(formula_or_cask) ⇒ String

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.

Parameters:

Returns:



47
48
49
# File 'utils.rb', line 47

def self.name_or_token(formula_or_cask)
  formula_or_cask.is_a?(Cask::Cask) ? formula_or_cask.token : formula_or_cask.name
end

.parallel_map(items, &block) ⇒ Array<T.type_parameter(:Result)>

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.

Maps items to block results with one thread per item, so that blocking waits (subprocesses, network requests) overlap instead of accumulating serially. Results keep the order of items. If multiple blocks raise, the exception re-raised by Thread#value is the earliest in items order (not necessarily the chronologically first failure) and other blocks may still run to completion. Only worthwhile when each block spends its time waiting: the GVL serializes Ruby execution.

Parameters:

  • items (Enumerable<T.type_parameter(:Item)>)
  • block (T.proc.params(item: T.type_parameter(:Item)).returns(T.type_parameter(:Result)))

Returns:

  • (Array<T.type_parameter(:Result)>)


90
91
92
93
94
95
96
97
98
99
# File 'utils.rb', line 90

def self.parallel_map(items, &block)
  threads = items.map do |item|
    Thread.new do
      # The exception is re-raised by `Thread#value`; don't also report it.
      Thread.current.report_on_exception = false
      yield(item)
    end
  end
  threads.map { |thread| T.cast(thread.value, T.type_parameter(:Result)) }
end

.parse_author!(author) ⇒ Hash

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.

Parameters:

Returns:

Raises:



132
133
134
135
136
137
138
139
140
141
# File 'utils.rb', line 132

def self.parse_author!(author)
  match_data = /^(?<name>[^<]+?)[ \t]*<(?<email>[^>]+?)>$/.match(author)
  if match_data
    name = match_data[:name]
    email = match_data[:email]
  end
  raise UsageError, "Unable to parse name and email." if name.nil? || email.nil?

  { name:, email: }
end

.pluralize(stem, count, plural: "s", singular: "", include_count: false) ⇒ String

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.

A lightweight alternative to ActiveSupport::Inflector.pluralize: Combines stem with the singular or plural suffix based on count. Adds a prefix of the count value if include_count is set to true.

Parameters:

  • stem (String)
  • count (Integer)
  • plural (String) (defaults to: "s")
  • singular (String) (defaults to: "")
  • include_count (Boolean) (defaults to: false)

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'utils.rb', line 107

def self.pluralize(stem, count, plural: "s", singular: "", include_count: false)
  case stem
  when "formula"
    plural = "e"
  when "dependency", "try"
    stem = stem.delete_suffix("y")
    plural = "ies"
    singular = "y"
  end

  prefix = include_count ? "#{count} " : ""
  suffix = (count == 1) ? singular : plural
  "#{prefix}#{stem}#{suffix}"
end

.popen(args, mode, options = {}, &_block) ⇒ T.type_parameter(:U), String

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.

Parameters:

Returns:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'utils/popen.rb', line 101

def self.popen(args, mode, options = {}, &_block)
  # `brew prof --vernier` uses this to avoid inheriting Vernier's active
  # native collector state through `IO.popen("-")` fork paths.
  if ENV["HOMEBREW_SPAWN_SYSTEM"] == "1"
    options[:err] = [:child, :out] if options[:err] == :out
    options[:err] ||= File::NULL unless ENV["HOMEBREW_STDERR"]
    IO.popen(args, mode, options) do |pipe|
      return pipe.read unless block_given?

      return yield pipe
    end
  end

  IO.popen("-", mode) do |pipe|
    if pipe
      return pipe.read unless block_given?

      yield pipe
    else
      options[:err] ||= File::NULL unless ENV["HOMEBREW_STDERR"]
      cmd = if args[0].is_a? Hash
        args[1]
      else
        args[0]
      end
      begin
        exec(*args, options)
      rescue Errno::ENOENT
        $stderr.puts "brew: command not found: #{cmd}" if options[:err] != :close
        exit! 127
      rescue SystemCallError => e
        if options[:err] != :close
          require "utils"
          $stderr.puts "brew: exec failed (#{Utils.demodulize(e.class.name)}): #{cmd}"
        end
        exit! 1
      end
    end
  end
end

.popen_read(*args, safe: false, **options, &block) ⇒ T.type_parameter(:U), String

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.

Parameters:

Returns:

Raises:



17
18
19
20
21
22
# File 'utils/popen.rb', line 17

def self.popen_read(*args, safe: false, **options, &block)
  output = popen(args, "rb", options, &block)
  return output if !safe || $CHILD_STATUS.success?

  raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, T.cast(output, String)]])
end

.popen_read_text(*args, **options) ⇒ String

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.

Parameters:

Returns:

Raises:

  • (TypeError)


30
31
32
33
34
35
# File 'utils/popen.rb', line 30

def self.popen_read_text(*args, **options)
  output = popen_read(*args, **options)
  raise TypeError, "Expected command output to be a String" unless output.is_a?(String)

  output.force_encoding(Encoding.default_external)
end

.popen_write(*args, safe: false, **options, &_block) ⇒ String

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.

Parameters:

Returns:

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'utils/popen.rb', line 57

def self.popen_write(*args, safe: false, **options, &_block)
  output = ""
  popen(args, "w+b", options) do |pipe|
    # Before we yield to the block, capture as much output as we can
    loop do
      output += pipe.read_nonblock(IO_DEFAULT_BUFFER_SIZE)
    rescue IO::WaitReadable, EOFError
      break
    end

    yield pipe
    pipe.close_write
    pipe.wait_readable

    # Capture the rest of the output
    output += pipe.read
    output.freeze
  end
  return output if !safe || $CHILD_STATUS.success?

  raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]])
end

.report_forked_child_error(error_pipe, error) ⇒ 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.

Parameters:



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'utils/fork.rb', line 90

def self.report_forked_child_error(error_pipe, error)
  require "json"

  coder = JSON::Coder.new do |object|
    case object
    when Exception then child_error_hash(object)
    else object
    end
  end

  error_pipe&.puts coder.dump(error)
  error_pipe&.close
end

.rewrite_child_error(child_error) ⇒ Exception

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.

Parameters:

Returns:

  • (Exception)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'utils/fork.rb', line 105

def self.rewrite_child_error(child_error)
  # The error class name comes from the forked child's serialised JSON.
  # rubocop:disable Sorbet/ConstantsFromStrings
  inner_class = Object.const_get(child_error["json_class"])
  # rubocop:enable Sorbet/ConstantsFromStrings
  error = if child_error["cmd"] && inner_class == ErrorDuringExecution
    ErrorDuringExecution.new(child_error["cmd"],
                             status: child_error["status"],
                             output: child_error["output"])
  elsif child_error["cmd"] && inner_class == BuildError
    # We fill `BuildError#formula` and `BuildError#options` in later,
    # when we rescue this in `FormulaInstaller#build`.
    BuildError.new(nil, child_error["cmd"], child_error["args"], child_error["env"])
  elsif inner_class == Interrupt
    Interrupt.new
  else
    # Everything other error in the child just becomes a RuntimeError.
    RuntimeError.new <<~EOS
      An exception occurred within a child process:
        #{inner_class}: #{child_error["m"]}
    EOS
  end

  error.set_backtrace child_error["b"]

  error
end

.safe_filename(basename) ⇒ String

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.

Parameters:

Returns:



172
173
174
# File 'utils.rb', line 172

def self.safe_filename(basename)
  basename.gsub(SAFE_FILENAME_REGEX, "")
end

.safe_filename?(basename) ⇒ 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.

Parameters:

Returns:

  • (Boolean)


167
168
169
# File 'utils.rb', line 167

def self.safe_filename?(basename)
  !SAFE_FILENAME_REGEX.match?(basename)
end

.safe_fork(directory: nil, yield_parent: false, child_message_handler: nil, &_blk) ⇒ 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.

When using this function, remember to call exec as soon as reasonably possible. This function does not protect against the pitfalls of what you can do pre-exec in a fork. See man fork for more information.

Parameters:

  • directory (String, nil) (defaults to: nil)
  • yield_parent (Boolean) (defaults to: false)
  • child_message_handler (T.proc.params(message: String).returns(T.nilable(String)), nil) (defaults to: nil)
  • _blk (T.proc.params(arg0: T.nilable(String)).void)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'utils/fork.rb', line 141

def self.safe_fork(directory: nil, yield_parent: false, child_message_handler: nil, &_blk)
  block = proc do |tmpdir|
    UNIXServerExt.open("#{tmpdir}/socket") do |server|
      error_read, error_write = IO.pipe
      response_read = T.let(nil, T.nilable(IO))
      response_write = T.let(nil, T.nilable(IO))
      response_read, response_write = IO.pipe if child_message_handler

      pid = fork do
        # bootsnap doesn't like these forked processes
        ENV["HOMEBREW_NO_BOOTSNAP"] = "1"
        error_pipe = server.path
        ENV["HOMEBREW_ERROR_PIPE"] = error_pipe
        if child_message_handler
          ENV["HOMEBREW_CHILD_MESSAGE_CHANNEL"] = "1"
        else
          ENV.delete("HOMEBREW_CHILD_MESSAGE_CHANNEL")
        end
        server.close
        error_read.close
        response_write&.close
        error_write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
        response_read&.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)

        Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid

        yield(error_pipe)
      # This could be any type of exception, so rescue them all.
      rescue Exception => e # rubocop:disable Lint/RescueException
        report_forked_child_error(error_write, e)

        exit!
      else
        exit!(true)
      end

      data = T.let(+"", String)
      reader = Thread.new do
        Thread.current.report_on_exception = false
        # Lines the handler answers are responded to on the response pipe;
        # anything else (e.g. a child error report) stays error data.
        error_read.each_line do |line|
          if child_message_handler && response_write && (response = child_message_handler.call(line))
            response_write.puts response
            response_write.flush
          else
            data << line
          end
        end
      end

      child_reaped = T.let(false, T::Boolean)
      begin
        yield(nil) if yield_parent

        begin
          socket = server.accept_nonblock
        rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
          retry unless Process.waitpid(pid, Process::WNOHANG)

          child_reaped = true
        else
          socket.send_io(error_write)
          socket.send_io(response_read) if response_read
          socket.close
        end
        error_write.close
        response_read&.close
        reader.value
        error_read.close
        response_write&.close
        unless socket.nil?
          Process.waitpid(pid)
          child_reaped = true
        end
      ensure
        # Stop the reader so closing its pipe cannot mask the original exception.
        reader.kill if $ERROR_INFO
        # Close the pipes before reaping: a child blocked waiting for a
        # response must see EOF and exit or `waitpid` would deadlock.
        [error_read, error_write, response_read, response_write].compact.each do |pipe|
          pipe.close unless pipe.closed?
        end
        begin
          Process.waitpid(pid) unless child_reaped
        rescue Errno::ECHILD
          nil
        end
        reader.join
      end

      # 130 is the exit status for a process interrupted via Ctrl-C.
      raise Interrupt if $CHILD_STATUS.exitstatus == 130
      raise Interrupt if $CHILD_STATUS.termsig == Signal.list["INT"]

      if data.present?
        error_hash = JSON.parse(data.lines.fetch(0))
        raise rewrite_child_error(error_hash)
      end

      raise ChildProcessError, $CHILD_STATUS unless $CHILD_STATUS.success?
    end
  end

  if directory
    block.call(directory)
  else
    Dir.mktmpdir("homebrew-fork", HOMEBREW_TEMP, &block)
  end
end

.safe_popen_read(*args, **options, &block) ⇒ T.type_parameter(:U), String

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.

Parameters:

Returns:



45
46
47
# File 'utils/popen.rb', line 45

def self.safe_popen_read(*args, **options, &block)
  popen_read(*args, safe: true, **options, &block)
end

.safe_popen_write(*args, **options, &block) ⇒ T.type_parameter(:U)

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.

Parameters:

Returns:

  • (T.type_parameter(:U))


88
89
90
# File 'utils/popen.rb', line 88

def self.safe_popen_write(*args, **options, &block)
  popen_write(*args, safe: true, **options, &block)
end

.tap_from_full_name(full_name) ⇒ String?

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.

Parameters:

Returns:



63
64
65
66
67
68
# File 'utils.rb', line 63

def self.tap_from_full_name(full_name)
  user, repository, name = full_name.split("/", 3)
  return unless name

  "#{user}/#{repository}"
end

.underscore(camel_cased_word) ⇒ String

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.

Makes an underscored, lowercase form from the expression in the string.

Changes '::' to '/' to convert namespaces to paths.

underscore('ActiveModel')         # => "active_model"
underscore('ActiveModel::Errors') # => "active_model/errors"

Parameters:

Returns:

See Also:



153
154
155
156
157
158
159
160
161
# File 'utils.rb', line 153

def self.underscore(camel_cased_word)
  return camel_cased_word.to_s unless /[A-Z-]|::/.match?(camel_cased_word)

  word = camel_cased_word.to_s.gsub("::", "/")
  word.gsub!(/[A-Z](?=[A-Z][a-z])|[a-z\d](?=[A-Z])/, '\0_')
  word.tr!("-", "_")
  word.downcase!
  word
end