Module: Snapsync::Btrfs
- Defined in:
- lib/snapsync/btrfs.rb
Defined Under Namespace
Classes: Error, UnexpectedBtrfsOutput
Class Method Summary collapse
- .btrfs_prog ⇒ Object
-
.find_new(subvolume_dir, last_gen, &block) ⇒ Object
Facade for ‘btrfs subvolume find-new’.
-
.generation_of(path) ⇒ Integer
Facade for finding the generation of a subvolume using ‘btrfs show’.
-
.popen(*args, mode: 'r', raise_on_error: true, **options) ⇒ Object
private
A IO.popen-like API to btrfs subcommands.
- .run(*args, **options) ⇒ Object
Class Method Details
.btrfs_prog ⇒ Object
23 24 25 |
# File 'lib/snapsync/btrfs.rb', line 23 def self.btrfs_prog ENV['BTRFS_PROG'] || 'btrfs' end |
.find_new(subvolume_dir, last_gen) {|a| ... } ⇒ Object .find_new(subvolume_dir, last_gen) ⇒ #each
Facade for ‘btrfs subvolume find-new’
It computes what changed between a reference generation of a subvolume, and that subvolume’s current state
97 98 99 100 |
# File 'lib/snapsync/btrfs.rb', line 97 def self.find_new(subvolume_dir, last_gen, &block) run('subvolume', 'find-new', subvolume_dir.to_s, last_gen.to_s). each_line(&block) end |
.generation_of(path) ⇒ Integer
Facade for finding the generation of a subvolume using ‘btrfs show’
75 76 77 78 79 80 81 82 |
# File 'lib/snapsync/btrfs.rb', line 75 def self.generation_of(path) info = Btrfs.run('subvolume', 'show', path.to_s) if info =~ /Generation[^:]*:\s+(\d+)/ Integer($1) else raise UnexpectedBtrfsOutput, "unexpected output for 'btrfs subvolume show', expected #{info} to contain a Generation: line" end end |
.popen(*args, mode: 'r', raise_on_error: true, **options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A IO.popen-like API to btrfs subcommands
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 56 57 58 59 60 61 62 63 |
# File 'lib/snapsync/btrfs.rb', line 30 def self.popen(*args, mode: 'r', raise_on_error: true, **) err_r, err_w = IO.pipe block_error, block_result = nil IO.popen([btrfs_prog, *args, err: err_w, **], mode) do |io| err_w.close begin block_result = yield(io) rescue Error raise rescue Exception => block_error end end if $?.success? && !block_error block_result elsif raise_on_error if block_error raise Error.new, block_error. 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., e.backtrace ensure err_r.close end |
.run(*args, **options) ⇒ Object
65 66 67 68 69 |
# File 'lib/snapsync/btrfs.rb', line 65 def self.run(*args, **) popen(*args, **) do |io| io.read.encode('utf-8', undef: :replace, invalid: :replace) end end |