Class: Gemvault::Tarball

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

Overview

The tar file backing a Tarvault. Reads named members 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) ⇒ Tarball

Returns a new instance of Tarball.



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

def initialize(path)
  @path = Pathname(path)
end

Instance Method Details

#entriesObject



19
20
21
# File 'lib/gemvault/tarball.rb', line 19

def entries
  each_entry.map { |member| ArchiveEntry.new(name: member.full_name, bytes: member.read) }
end

#namesObject

The member names alone. Reads tar headers without any member's bytes, so a caller asking what kind of vault this is does not load the whole file.



25
26
27
# File 'lib/gemvault/tarball.rb', line 25

def names
  each_entry.map(&:full_name)
end

#read(name) ⇒ Object



15
16
17
# File 'lib/gemvault/tarball.rb', line 15

def read(name)
  entries.find { |entry| entry.name == name }&.bytes
end

#write(entries) ⇒ Object



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

def write(entries)
  Tempfile.create(["tarvault", ".tar"], @path.dirname) do |tmp|
    write_entries(io: tmp, entries:)
    tmp.flush
    tmp.fsync
    tmp.close
    Pathname(tmp.path).rename(@path)
  end
end