Module: Snapsync::Btrfs

Defined in:
lib/snapsync/btrfs.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.popen(*args, mode: 'r', raise_on_error: true, **options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/snapsync/btrfs.rb', line 20

def self.popen(*args, mode: 'r', raise_on_error: true, **options)
    err_r, err_w = IO.pipe

    block_error, block_result = nil
    IO.popen(['btrfs', *args, err: err_w, **options], mode) do |io|
        err_w.close
        if block_given?
            begin
                block_result = yield(io)
            rescue Error
                raise
            rescue Exception => block_error
            end
        else io.read
        end
    end

    if $?.success? && !block_error
        block_result
    elsif raise_on_error
        if block_error
            raise Error.new, block_error.message
        else
            raise Error.new, "btrfs failed"
        end
    end

rescue Error => e
    prefix = args.join(" ")
    lines = err_r.readlines.map do |line|
        "#{prefix}: #{line.chomp}"
    end
    raise Error.new(e.error_lines + lines), e.message, e.backtrace

ensure err_r.close
end

.run(*args, **options) ⇒ Object



57
58
59
60
61
# File 'lib/snapsync/btrfs.rb', line 57

def self.run(*args, **options)
    popen(*args, **options) do |io|
        io.read
    end
end