diff --git a/Library/Homebrew/utils/popen.rb b/Library/Homebrew/utils/popen.rb index b721b3191c..c5546c6d93 100644 --- a/Library/Homebrew/utils/popen.rb +++ b/Library/Homebrew/utils/popen.rb @@ -5,21 +5,20 @@ module Utils IO_DEFAULT_BUFFER_SIZE = 4096 private_constant :IO_DEFAULT_BUFFER_SIZE - def self.popen_read(*args, **options, &block) - popen(args, "rb", options, &block) - end - - def self.safe_popen_read(*args, **options, &block) - output = popen_read(*args, **options, &block) - return output if $CHILD_STATUS.success? + 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, output]]) end - def self.popen_write(*args, **options) - popen(args, "w+b", options) do |pipe| - output = "" + def self.safe_popen_read(*args, **options, &block) + popen_read(*args, safe: true, **options, &block) + end + def self.popen_write(*args, safe: false, **options) + 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) @@ -35,13 +34,13 @@ module Utils output += pipe.read output.freeze end + return output if !safe || $CHILD_STATUS.success? + + raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) end def self.safe_popen_write(*args, **options, &block) - output = popen_write(*args, **options, &block) - return output if $CHILD_STATUS.success? - - raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) + popen_write(*args, safe: true, **options, &block) end def self.popen(args, mode, options = {})