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.1.4"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemTar

Returns a new instance of MemTar.



8
9
10
# File 'lib/memtar.rb', line 8

def initialize
  @io = StringIO.new
end

Class Method Details

.opts_of_stat(stat) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/memtar.rb', line 36

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



27
28
29
30
31
32
33
34
# File 'lib/memtar.rb', line 27

def add_existing_file path, file
  stat = file.lstat
  if stat.symlink?
    add_symlink path, File.readlink(file.path)
  else
    add_file path, file.read, MemTar.opts_of_stat(stat)
  end
end

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



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

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.bytesize
  @io.write header opts.merge size: size, name: path
  @io.write content
  @io.write "\0" * ((512 - size % 512) % 512)
end


22
23
24
25
# File 'lib/memtar.rb', line 22

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



55
56
57
58
59
60
61
62
63
# File 'lib/memtar.rb', line 55

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

#header(opts) ⇒ Object



51
52
53
# File 'lib/memtar.rb', line 51

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

#to_sObject



47
48
49
# File 'lib/memtar.rb', line 47

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