Class: Gemvault::TarArchive

Inherits:
Object
  • Object
show all
Defined in:
lib/gemvault/tar_archive.rb

Overview

The tar file backing a Tarvault. Reads named entries and rewrites the whole archive atomically: tar has no index and the leading manifest entry changes size on every mutation, so mutation means a full rewrite.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ TarArchive

Returns a new instance of TarArchive.



11
12
13
# File 'lib/gemvault/tar_archive.rb', line 11

def initialize(path)
  @path = path
end

Instance Method Details

#gem_pairsObject



21
22
23
24
25
26
27
# File 'lib/gemvault/tar_archive.rb', line 21

def gem_pairs
  pairs = []
  each_entry do |entry|
    pairs << [entry.full_name, entry.read] unless entry.full_name == Manifest::FILENAME
  end
  pairs
end

#read(name) ⇒ Object



15
16
17
18
19
# File 'lib/gemvault/tar_archive.rb', line 15

def read(name)
  bytes = nil
  each_entry { |entry| bytes = entry.read if entry.full_name == name }
  bytes
end

#write(pairs) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gemvault/tar_archive.rb', line 29

def write(pairs)
  tmp = Tempfile.create(["tarvault", ".tar"], File.dirname(@path))
  write_entries(tmp, pairs)
  tmp.flush
  tmp.fsync
  tmp.close
  File.rename(tmp.path, @path)
rescue StandardError
  cleanup(tmp)
  raise
end