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
|
IO_DEFAULT_BUFFER_SIZE = 4096
|
||||||
private_constant :IO_DEFAULT_BUFFER_SIZE
|
private_constant :IO_DEFAULT_BUFFER_SIZE
|
||||||
|
|
||||||
def self.popen_read(*args, **options, &block)
|
def self.popen_read(*args, safe: false, **options, &block)
|
||||||
popen(args, "rb", options, &block)
|
output = popen(args, "rb", options, &block)
|
||||||
end
|
return output if !safe || $CHILD_STATUS.success?
|
||||||
|
|
||||||
def self.safe_popen_read(*args, **options, &block)
|
|
||||||
output = popen_read(*args, **options, &block)
|
|
||||||
return output if $CHILD_STATUS.success?
|
|
||||||
|
|
||||||
raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]])
|
raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]])
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.popen_write(*args, **options)
|
def self.safe_popen_read(*args, **options, &block)
|
||||||
popen(args, "w+b", options) do |pipe|
|
popen_read(*args, safe: true, **options, &block)
|
||||||
output = ""
|
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
|
# Before we yield to the block, capture as much output as we can
|
||||||
loop do
|
loop do
|
||||||
output += pipe.read_nonblock(IO_DEFAULT_BUFFER_SIZE)
|
output += pipe.read_nonblock(IO_DEFAULT_BUFFER_SIZE)
|
||||||
@ -35,13 +34,13 @@ module Utils
|
|||||||
output += pipe.read
|
output += pipe.read
|
||||||
output.freeze
|
output.freeze
|
||||||
end
|
end
|
||||||
|
return output if !safe || $CHILD_STATUS.success?
|
||||||
|
|
||||||
|
raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]])
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.safe_popen_write(*args, **options, &block)
|
def self.safe_popen_write(*args, **options, &block)
|
||||||
output = popen_write(*args, **options, &block)
|
popen_write(*args, safe: true, **options, &block)
|
||||||
return output if $CHILD_STATUS.success?
|
|
||||||
|
|
||||||
raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.popen(args, mode, options = {})
|
def self.popen(args, mode, options = {})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user