Class: Omnizip::Formats::Tar::Writer
- Inherits:
-
Object
- Object
- Omnizip::Formats::Tar::Writer
- Includes:
- Constants
- Defined in:
- lib/omnizip/formats/tar/writer.rb
Overview
TAR archive writer
Creates TAR archives
Constant Summary
Constants included from Constants
Constants::BLOCK_SIZE, Constants::CHECKSUM_OFFSET, Constants::CHECKSUM_SIZE, Constants::DEVMAJOR_OFFSET, Constants::DEVMAJOR_SIZE, Constants::DEVMINOR_OFFSET, Constants::DEVMINOR_SIZE, Constants::GID_OFFSET, Constants::GID_SIZE, Constants::GNAME_OFFSET, Constants::GNAME_SIZE, Constants::HEADER_SIZE, Constants::LINKNAME_OFFSET, Constants::LINKNAME_SIZE, Constants::MAGIC_OFFSET, Constants::MAGIC_SIZE, Constants::MAX_FILE_SIZE, Constants::MODE_OFFSET, Constants::MODE_SIZE, Constants::MTIME_OFFSET, Constants::MTIME_SIZE, Constants::NAME_OFFSET, Constants::NAME_SIZE, Constants::PREFIX_OFFSET, Constants::PREFIX_SIZE, Constants::SIZE_OFFSET, Constants::SIZE_SIZE, Constants::TYPEFLAG_OFFSET, Constants::TYPEFLAG_SIZE, Constants::TYPE_BLOCK_DEVICE, Constants::TYPE_CHAR_DEVICE, Constants::TYPE_CONTIGUOUS, Constants::TYPE_DIRECTORY, Constants::TYPE_EXTENDED, Constants::TYPE_FIFO, Constants::TYPE_GLOBAL_EXTENDED, Constants::TYPE_GNU_LONGLINK, Constants::TYPE_GNU_LONGNAME, Constants::TYPE_HARD_LINK, Constants::TYPE_REGULAR, Constants::TYPE_SYMLINK, Constants::UID_OFFSET, Constants::UID_SIZE, Constants::UNAME_OFFSET, Constants::UNAME_SIZE, Constants::USTAR_MAGIC, Constants::USTAR_VERSION, Constants::VERSION_OFFSET, Constants::VERSION_SIZE
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Class Method Summary collapse
-
.create(file_path) {|Writer| ... } ⇒ Object
Create TAR archive with block syntax.
Instance Method Summary collapse
-
#add(entry_name, source_path = nil, options = {}) ⇒ Object
Add a file to the TAR archive.
-
#add_data(entry_name, data, options = {}) ⇒ Object
Add raw entry data.
-
#add_directory(entry_name, source_path = nil, options = {}) ⇒ Object
Add a directory entry.
-
#add_file(entry_name, source_path, options = {}) ⇒ Object
Add a file entry.
-
#add_symlink(entry_name, source_path, options = {}) ⇒ Object
Add a symbolic link entry.
-
#close ⇒ Object
Close the TAR archive.
-
#initialize(file_path) ⇒ Writer
constructor
Initialize TAR writer.
Constructor Details
#initialize(file_path) ⇒ Writer
Initialize TAR writer
17 18 19 20 21 |
# File 'lib/omnizip/formats/tar/writer.rb', line 17 def initialize(file_path) @file_path = file_path @file = nil @closed = false end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
12 13 14 |
# File 'lib/omnizip/formats/tar/writer.rb', line 12 def file_path @file_path end |
Class Method Details
.create(file_path) {|Writer| ... } ⇒ Object
Create TAR archive with block syntax
174 175 176 177 178 179 |
# File 'lib/omnizip/formats/tar/writer.rb', line 174 def self.create(file_path) writer = new(file_path) yield writer if block_given? writer.close writer end |
Instance Method Details
#add(entry_name, source_path = nil, options = {}) ⇒ Object
Add a file to the TAR archive
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/omnizip/formats/tar/writer.rb', line 28 def add(entry_name, source_path = nil, = {}) source_path ||= entry_name unless File.exist?(source_path) raise ArgumentError, "File not found: #{source_path}" end open_file unless @file if File.directory?(source_path) add_directory(entry_name, source_path, ) elsif File.symlink?(source_path) add_symlink(entry_name, source_path, ) else add_file(entry_name, source_path, ) end end |
#add_data(entry_name, data, options = {}) ⇒ Object
Add raw entry data
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/omnizip/formats/tar/writer.rb', line 140 def add_data(entry_name, data, = {}) open_file unless @file entry = Entry.new(entry_name, { mode: [:mode] || 0o644, uid: [:uid] || 0, gid: [:gid] || 0, size: data.bytesize, mtime: [:mtime] || Time.now, typeflag: TYPE_REGULAR, uname: [:uname] || "", gname: [:gname] || "", }) write_entry(entry, data) end |
#add_directory(entry_name, source_path = nil, options = {}) ⇒ Object
Add a directory entry
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/omnizip/formats/tar/writer.rb', line 76 def add_directory(entry_name, source_path = nil, = {}) open_file unless @file # Ensure directory name ends with / entry_name = "#{entry_name}/" unless entry_name.end_with?("/") if source_path && File.exist?(source_path) stat = File.stat(source_path) mode = stat.mode & 0o777 uid = stat.uid gid = stat.gid mtime = stat.mtime else mode = 0o755 uid = 0 gid = 0 mtime = Time.now end entry = Entry.new(entry_name, { mode: [:mode] || mode, uid: [:uid] || uid, gid: [:gid] || gid, size: 0, mtime: [:mtime] || mtime, typeflag: TYPE_DIRECTORY, uname: [:uname] || "", gname: [:gname] || "", }) write_entry(entry, nil) end |
#add_file(entry_name, source_path, options = {}) ⇒ Object
Add a file entry
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/omnizip/formats/tar/writer.rb', line 51 def add_file(entry_name, source_path, = {}) open_file unless @file stat = File.stat(source_path) data = File.binread(source_path) entry = Entry.new(entry_name, { mode: [:mode] || (stat.mode & 0o777), uid: [:uid] || stat.uid, gid: [:gid] || stat.gid, size: data.bytesize, mtime: [:mtime] || stat.mtime, typeflag: TYPE_REGULAR, uname: [:uname] || "", gname: [:gname] || "", }) write_entry(entry, data) end |
#add_symlink(entry_name, source_path, options = {}) ⇒ Object
Add a symbolic link entry
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/omnizip/formats/tar/writer.rb', line 114 def add_symlink(entry_name, source_path, = {}) open_file unless @file linkname = File.readlink(source_path) stat = File.lstat(source_path) entry = Entry.new(entry_name, { mode: [:mode] || 0o777, uid: [:uid] || stat.uid, gid: [:gid] || stat.gid, size: 0, mtime: [:mtime] || stat.mtime, typeflag: TYPE_SYMLINK, linkname: linkname, uname: [:uname] || "", gname: [:gname] || "", }) write_entry(entry, nil) end |
#close ⇒ Object
Close the TAR archive
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/omnizip/formats/tar/writer.rb', line 158 def close return if @closed if @file # Write two zero blocks to mark end of archive @file.write("\0" * BLOCK_SIZE * 2) @file.close end @closed = true end |