Class: Omnizip::Formats::Xar::Entry

Inherits:
Object
  • Object
show all
Includes:
Entry, Constants
Defined in:
lib/omnizip/formats/xar/entry.rb

Overview

XAR file entry model

Represents a single file/directory/link in a XAR archive. Each entry has metadata and optional data.

Defined Under Namespace

Classes: ExtendedAttribute

Constant Summary

Constants included from Constants

Constants::CHECKSUM_ALGORITHMS, Constants::CHECKSUM_NAMES, Constants::CHECKSUM_SIZES, Constants::CKSUM_MD5, Constants::CKSUM_NONE, Constants::CKSUM_OTHER, Constants::CKSUM_SHA1, Constants::COMPRESSION_BZIP2, Constants::COMPRESSION_GZIP, Constants::COMPRESSION_LZMA, Constants::COMPRESSION_MIME_TYPES, Constants::COMPRESSION_NONE, Constants::COMPRESSION_XZ, Constants::DEFAULT_COMPRESSION, Constants::DEFAULT_COMPRESSION_LEVEL, Constants::DEFAULT_FILE_CHECKSUM, Constants::DEFAULT_TOC_CHECKSUM, Constants::HEADER_CKSUM_ALG_OFFSET, Constants::HEADER_MAGIC_OFFSET, Constants::HEADER_SIZE, Constants::HEADER_SIZE_EX, Constants::HEADER_SIZE_OFFSET, Constants::HEADER_TOC_COMPRESSED_OFFSET, Constants::HEADER_TOC_UNCOMPRESSED_OFFSET, Constants::HEADER_VERSION_OFFSET, Constants::MAGIC, Constants::MAGIC_BYTES, Constants::MAX_PATH_LENGTH, Constants::MIME_TYPE_TO_COMPRESSION, Constants::TOC_XML_DECLARATION, Constants::TYPE_BLOCK, Constants::TYPE_CHAR, Constants::TYPE_DIRECTORY, Constants::TYPE_FIFO, Constants::TYPE_FILE, Constants::TYPE_HARDLINK, Constants::TYPE_SOCKET, Constants::TYPE_SYMLINK, Constants::XAR_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Entry

Initialize entry

Parameters:

  • name (String)

    Entry name/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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/omnizip/formats/xar/entry.rb', line 51

def initialize(name, options = {})
  @id = options[:id]
  @name = name
  @type = options[:type] || TYPE_FILE
  @mode = options[:mode] || 0o644
  @uid = options[:uid] || 0
  @gid = options[:gid] || 0
  @user = options[:user] || ""
  @group = options[:group] || ""
  @size = options[:size] || 0
  @ctime = options[:ctime]
  @mtime = options[:mtime]
  @atime = options[:atime]

  # Data properties
  @data_offset = options[:data_offset] || 0
  @data_length = options[:data_length] || 0
  @data_size = options[:data_size] || 0
  @data_encoding = options[:data_encoding] || COMPRESSION_NONE

  # Checksums
  @archived_checksum = options[:archived_checksum]
  @archived_checksum_style = options[:archived_checksum_style]
  @extracted_checksum = options[:extracted_checksum]
  @extracted_checksum_style = options[:extracted_checksum_style]

  # Links
  @link_type = options[:link_type]
  @link_target = options[:link_target]

  # Devices
  @device_major = options[:device_major]
  @device_minor = options[:device_minor]

  # Other metadata
  @ino = options[:ino]
  @nlink = options[:nlink] || 1
  @flags = options[:flags]
  @ea = options[:ea] || [] # Extended attributes

  @data = nil
end

Instance Attribute Details

#archived_checksumObject

Returns the value of attribute archived_checksum.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def archived_checksum
  @archived_checksum
end

#archived_checksum_styleObject

Returns the value of attribute archived_checksum_style.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def archived_checksum_style
  @archived_checksum_style
end

#atimeObject

Returns the value of attribute atime.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def atime
  @atime
end

#ctimeObject

Returns the value of attribute ctime.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def ctime
  @ctime
end

#dataObject

Returns the value of attribute data.



28
29
30
# File 'lib/omnizip/formats/xar/entry.rb', line 28

def data
  @data
end

#data_encodingObject

Returns the value of attribute data_encoding.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def data_encoding
  @data_encoding
end

#data_lengthObject

Returns the value of attribute data_length.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def data_length
  @data_length
end

#data_offsetObject

Returns the value of attribute data_offset.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def data_offset
  @data_offset
end

#data_sizeObject

Returns the value of attribute data_size.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def data_size
  @data_size
end

#device_majorObject

Returns the value of attribute device_major.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def device_major
  @device_major
end

#device_minorObject

Returns the value of attribute device_minor.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def device_minor
  @device_minor
end

#eaObject

Returns the value of attribute ea.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def ea
  @ea
end

#extracted_checksumObject

Returns the value of attribute extracted_checksum.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def extracted_checksum
  @extracted_checksum
end

#extracted_checksum_styleObject

Returns the value of attribute extracted_checksum_style.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def extracted_checksum_style
  @extracted_checksum_style
end

#flagsObject

Returns the value of attribute flags.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def flags
  @flags
end

#gidObject

Returns the value of attribute gid.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def gid
  @gid
end

#groupObject

Returns the value of attribute group.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def group
  @group
end

#idObject

Returns the value of attribute id.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def id
  @id
end

#inoObject

Returns the value of attribute ino.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def ino
  @ino
end

Returns the value of attribute link_target.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def link_target
  @link_target
end

Returns the value of attribute link_type.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def link_type
  @link_type
end

#modeObject

Returns the value of attribute mode.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def mode
  @mode
end

#mtimeObject

Returns the value of attribute mtime.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def mtime
  @mtime
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def name
  @name
end

Returns the value of attribute nlink.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def nlink
  @nlink
end

#sizeObject

Returns the value of attribute size.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def size
  @size
end

#typeObject

Returns the value of attribute type.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def type
  @type
end

#uidObject

Returns the value of attribute uid.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def uid
  @uid
end

#userObject

Returns the value of attribute user.



19
20
21
# File 'lib/omnizip/formats/xar/entry.rb', line 19

def user
  @user
end

Class Method Details

.type_from_mode(mode) ⇒ String

Get file type from mode

Returns:

  • (String)

    XAR type string



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/omnizip/formats/xar/entry.rb', line 155

def self.type_from_mode(mode)
  case mode & 0o170000
  when 0o040000 then TYPE_DIRECTORY
  when 0o100000 then TYPE_FILE
  when 0o120000 then TYPE_SYMLINK
  when 0o060000 then TYPE_BLOCK
  when 0o020000 then TYPE_CHAR
  when 0o010000 then TYPE_FIFO
  when 0o140000 then TYPE_SOCKET
  else TYPE_FILE
  end
end

Instance Method Details

#device?Boolean

Check if entry is a device

Returns:

  • (Boolean)

    true if block or character device



134
135
136
# File 'lib/omnizip/formats/xar/entry.rb', line 134

def device?
  @type == TYPE_BLOCK || @type == TYPE_CHAR
end

#directory?Boolean

Check if entry is a directory

Returns:

  • (Boolean)

    true if directory



106
107
108
# File 'lib/omnizip/formats/xar/entry.rb', line 106

def directory?
  @type == TYPE_DIRECTORY
end

#entry_directory?Boolean

Returns:

  • (Boolean)


15
# File 'lib/omnizip/formats/xar/entry.rb', line 15

def entry_directory? = type == :directory

#entry_mtimeObject



17
# File 'lib/omnizip/formats/xar/entry.rb', line 17

def entry_mtime = mtime

#entry_nameObject



14
# File 'lib/omnizip/formats/xar/entry.rb', line 14

def entry_name = name

#entry_sizeObject



16
# File 'lib/omnizip/formats/xar/entry.rb', line 16

def entry_size = size

#fifo?Boolean

Check if entry is a FIFO

Returns:

  • (Boolean)

    true if FIFO



141
142
143
# File 'lib/omnizip/formats/xar/entry.rb', line 141

def fifo?
  @type == TYPE_FIFO
end

#file?Boolean

Check if entry is a regular file

Returns:

  • (Boolean)

    true if regular file



113
114
115
# File 'lib/omnizip/formats/xar/entry.rb', line 113

def file?
  @type == TYPE_FILE
end

#hardlink?Boolean

Check if entry is a hard link

Returns:

  • (Boolean)

    true if hard link



127
128
129
# File 'lib/omnizip/formats/xar/entry.rb', line 127

def hardlink?
  @type == TYPE_HARDLINK
end

#socket?Boolean

Check if entry is a socket

Returns:

  • (Boolean)

    true if socket



148
149
150
# File 'lib/omnizip/formats/xar/entry.rb', line 148

def socket?
  @type == TYPE_SOCKET
end

#symlink?Boolean

Check if entry is a symbolic link

Returns:

  • (Boolean)

    true if symbolic link



120
121
122
# File 'lib/omnizip/formats/xar/entry.rb', line 120

def symlink?
  @type == TYPE_SYMLINK
end

#to_hHash

Convert entry to hash for TOC generation

Returns:

  • (Hash)

    Entry as hash



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/omnizip/formats/xar/entry.rb', line 171

def to_h
  hash = {
    id: @id,
    name: @name,
    type: @type,
  }

  hash[:mode] = format("0%03o", @mode) if @mode
  hash[:uid] = @uid if @uid
  hash[:gid] = @gid if @gid
  hash[:user] = @user unless @user.to_s.empty?
  hash[:group] = @group unless @group.to_s.empty?
  hash[:size] = @size if @size&.positive?

  # Timestamps
  hash[:ctime] = format_timestamp(@ctime) if @ctime
  hash[:mtime] = format_timestamp(@mtime) if @mtime
  hash[:atime] = format_timestamp(@atime) if @atime

  # Data section
  if @data_size&.positive? || file?
    data_hash = {}
    data_hash[:offset] = @data_offset
    data_hash[:size] = @data_length if @data_length&.positive?
    data_hash[:length] = @data_size if @data_size&.positive?

    if @data_encoding && @data_encoding != COMPRESSION_NONE
      data_hash[:encoding] =
        COMPRESSION_MIME_TYPES[@data_encoding] || @data_encoding
    end

    if @archived_checksum
      data_hash[:archived_checksum] =
        @archived_checksum
    end
    if @archived_checksum_style
      data_hash[:archived_checksum_style] =
        @archived_checksum_style
    end
    if @extracted_checksum
      data_hash[:extracted_checksum] =
        @extracted_checksum
    end
    if @extracted_checksum_style
      data_hash[:extracted_checksum_style] =
        @extracted_checksum_style
    end

    hash[:data] = data_hash
  end

  # Links
  if @link_target
    hash[:link] = { type: @link_type, target: @link_target }
  end

  # Devices
  if device?
    device_hash = {}
    device_hash[:major] = @device_major if @device_major
    device_hash[:minor] = @device_minor if @device_minor
    hash[:device] = device_hash
  end

  # Extended attributes
  if @ea&.any?
    hash[:ea] = @ea.map do |attr|
      ea_hash = { name: attr.name }
      ea_hash[:offset] = attr.data_offset
      ea_hash[:size] = attr.data_length
      ea_hash[:length] = attr.data_size
      ea_hash
    end
  end

  hash
end