utils/popen: add safe argument to popen_read and popen_write
This commit is contained in:
parent
40d85ecb40
commit
88306d5f5d
@ -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 = {})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user