Class: Philiprehberger::Tar::Writer
- Inherits:
-
Object
- Object
- Philiprehberger::Tar::Writer
- 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
-
#entry_count ⇒ Integer
readonly
Return the number of entries written so far.
Instance Method Summary collapse
-
#add_file(path, name: nil, on_progress: nil, total: nil) ⇒ void
Add a file from the filesystem.
-
#add_string(name, content, mode: 0o644, on_progress: nil, total: nil) ⇒ void
Add a file from a string.
-
#add_symlink(name, target:, on_progress: nil, total: nil) ⇒ void
Add a symbolic link entry.
-
#close ⇒ void
Write the end-of-archive marker.
-
#initialize(io) ⇒ Writer
constructor
A new instance of Writer.
Constructor Details
#initialize(io) ⇒ Writer
Returns a new instance of Writer.
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_count ⇒ Integer (readonly)
Return the number of entries written so far.
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.
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.
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 |
#add_symlink(name, target:, on_progress: nil, total: nil) ⇒ void
This method returns an undefined value.
Add a symbolic link entry.
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 |
#close ⇒ void
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 |