Class: Omnizip::Formats::Tar::Writer

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Writer

Initialize TAR writer

Parameters:

  • file_path (String)

    Path to output TAR archive



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_pathObject (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

Parameters:

  • file_path (String)

    Path to output TAR archive

Yields:

  • (Writer)

    Writer instance



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

Parameters:

  • entry_name (String)

    Name/path in archive

  • source_path (String) (defaults to: nil)

    Source file path

  • options (Hash) (defaults to: {})

    Entry options



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, options = {})
  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, options)
  elsif File.symlink?(source_path)
    add_symlink(entry_name, source_path, options)
  else
    add_file(entry_name, source_path, options)
  end
end

#add_data(entry_name, data, options = {}) ⇒ Object

Add raw entry data

Parameters:

  • entry_name (String)

    Entry name

  • data (String)

    Entry data

  • options (Hash) (defaults to: {})

    Entry options



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, options = {})
  open_file unless @file

  entry = Entry.new(entry_name, {
                      mode: options[:mode] || 0o644,
                      uid: options[:uid] || 0,
                      gid: options[:gid] || 0,
                      size: data.bytesize,
                      mtime: options[:mtime] || Time.now,
                      typeflag: TYPE_REGULAR,
                      uname: options[:uname] || "",
                      gname: options[:gname] || "",
                    })

  write_entry(entry, data)
end

#add_directory(entry_name, source_path = nil, options = {}) ⇒ Object

Add a directory entry

Parameters:

  • entry_name (String)

    Directory name in archive

  • source_path (String) (defaults to: nil)

    Source directory path (optional)

  • options (Hash) (defaults to: {})

    Entry options



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, options = {})
  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: options[:mode] || mode,
                      uid: options[:uid] || uid,
                      gid: options[:gid] || gid,
                      size: 0,
                      mtime: options[:mtime] || mtime,
                      typeflag: TYPE_DIRECTORY,
                      uname: options[:uname] || "",
                      gname: options[:gname] || "",
                    })

  write_entry(entry, nil)
end

#add_file(entry_name, source_path, options = {}) ⇒ Object

Add a file entry

Parameters:

  • entry_name (String)

    Name in archive

  • source_path (String)

    Source file path

  • options (Hash) (defaults to: {})

    Entry options



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, options = {})
  open_file unless @file

  stat = File.stat(source_path)
  data = File.binread(source_path)

  entry = Entry.new(entry_name, {
                      mode: options[:mode] || (stat.mode & 0o777),
                      uid: options[:uid] || stat.uid,
                      gid: options[:gid] || stat.gid,
                      size: data.bytesize,
                      mtime: options[:mtime] || stat.mtime,
                      typeflag: TYPE_REGULAR,
                      uname: options[:uname] || "",
                      gname: options[:gname] || "",
                    })

  write_entry(entry, data)
end

Add a symbolic link entry

Parameters:

  • entry_name (String)

    Link name in archive

  • source_path (String)

    Source symlink path

  • options (Hash) (defaults to: {})

    Entry options



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, options = {})
  open_file unless @file

  linkname = File.readlink(source_path)
  stat = File.lstat(source_path)

  entry = Entry.new(entry_name, {
                      mode: options[:mode] || 0o777,
                      uid: options[:uid] || stat.uid,
                      gid: options[:gid] || stat.gid,
                      size: 0,
                      mtime: options[:mtime] || stat.mtime,
                      typeflag: TYPE_SYMLINK,
                      linkname: linkname,
                      uname: options[:uname] || "",
                      gname: options[:gname] || "",
                    })

  write_entry(entry, nil)
end

#closeObject

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