Class: Philiprehberger::Tar::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/tar/writer.rb

Overview

Writes tar archives in the standard 512-byte block format.

Constant Summary collapse

BLOCK_SIZE =
512

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Writer

Returns a new instance of Writer.

Parameters:

  • io (IO)

    the output stream



10
11
12
13
# File 'lib/philiprehberger/tar/writer.rb', line 10

def initialize(io)
  @io = io
  @entry_count = 0
end

Instance Attribute Details

#entry_countInteger (readonly)

Return the number of entries written so far.

Returns:

  • (Integer)


75
76
77
# File 'lib/philiprehberger/tar/writer.rb', line 75

def entry_count
  @entry_count
end

Instance Method Details

#add_file(path, name: nil, on_progress: nil, total: nil) ⇒ void

This method returns an undefined value.

Add a file from the filesystem.

Parameters:

  • path (String)

    path to the file

  • name (String, nil) (defaults to: nil)

    name in the archive (defaults to path basename)

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)

  • total (Integer, nil) (defaults to: nil)

    total entries for progress callback



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/philiprehberger/tar/writer.rb', line 22

def add_file(path, name: nil, on_progress: nil, total: nil)
  name ||= File.basename(path)

  if File.symlink?(path)
    target = File.readlink(path)
    add_symlink(name, target: target, on_progress: on_progress, total: total)
  else
    content = File.binread(path)
    mode = File.stat(path).mode & 0o7777
    write_entry(name, content, mode: mode)
    @entry_count += 1
    on_progress&.call(name, @entry_count, total)
  end
end

#add_string(name, content, mode: 0o644, on_progress: nil, total: nil) ⇒ void

This method returns an undefined value.

Add a file from a string.

Parameters:

  • name (String)

    filename in the archive

  • content (String)

    file content

  • mode (Integer) (defaults to: 0o644)

    file mode (default: 0644)

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)

  • total (Integer, nil) (defaults to: nil)

    total entries for progress callback



45
46
47
48
49
# File 'lib/philiprehberger/tar/writer.rb', line 45

def add_string(name, content, mode: 0o644, on_progress: nil, total: nil)
  write_entry(name, content, mode: mode)
  @entry_count += 1
  on_progress&.call(name, @entry_count, total)
end

This method returns an undefined value.

Add a symbolic link entry.

Parameters:

  • name (String)

    link name in the archive

  • target (String)

    link target path

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)

  • total (Integer, nil) (defaults to: nil)

    total entries for progress callback



58
59
60
61
62
63
# File 'lib/philiprehberger/tar/writer.rb', line 58

def add_symlink(name, target:, on_progress: nil, total: nil)
  header = build_header(name, 0, 0o777, '2', linkname: target)
  @io.write(header)
  @entry_count += 1
  on_progress&.call(name, @entry_count, total)
end

#closevoid

This method returns an undefined value.

Write the end-of-archive marker.



68
69
70
# File 'lib/philiprehberger/tar/writer.rb', line 68

def close
  @io.write("\0" * BLOCK_SIZE * 2)
end