Class: MemTar

Inherits:
Object
  • Object
show all
Defined in:
lib/memtar.rb,
lib/memtar/version.rb

Overview

In-memory tar archive writer.

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemTar

Returns a new instance of MemTar.



6
7
8
# File 'lib/memtar.rb', line 6

def initialize
  @io = StringIO.new
end

Class Method Details

.opts_of_stat(stat) ⇒ Object



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

def self.opts_of_stat stat
  uid, gid = [stat.uid, stat.gid]
  {
    mode: stat.mode & 0777,
    uid: uid, gid: gid,
    uname: Etc.getpwuid(uid).name,
    gname: Etc.getgrgid(gid).name,
    mtime: stat.mtime
  }
end

Instance Method Details

#add_existing_file(path, file) ⇒ Object



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

def add_existing_file path, file
  add_file path, file.read, MemTar.opts_of_stat(file.stat)
end

#add_file(path, content, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/memtar.rb', line 10

def add_file path, content, opts = {}
  return add_existing_file path, content if content.is_a?(File) && opts.empty?
  fail ArgumentError, "handling of paths over 100 bytes long not implemented" if path.size > 100

  size = content.size
  @io.write header opts.merge size: size, name: path
  @io.write content
  @io.write "\0" * (512 - size % 512)
end


20
21
22
23
# File 'lib/memtar.rb', line 20

def add_symlink path, target
  fail ArgumentError, "handling of paths over 100 bytes long not implemented" if path.size > 100
  @io.write header typeflag: '2', size: 0, mode: 0777, name: path, linkname: target
end

#defaultObject



48
49
50
51
52
53
54
55
56
# File 'lib/memtar.rb', line 48

def default
  @default ||= {
    mode: 0644,
    uid: 0, gid: 0,
    uname: 'wheel', gname: 'wheel',
    mtime: Time.now,
    prefix: ''
  }
end

#header(opts) ⇒ Object



44
45
46
# File 'lib/memtar.rb', line 44

def header opts
  Archive::Tar::PosixHeader.new(default.merge opts).to_s
end

#to_sObject



40
41
42
# File 'lib/memtar.rb', line 40

def to_s
  @io.string + "\0" * 1024
end