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

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



45
46
47
48
49
50
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
# File 'lib/omnizip/formats/xar/entry.rb', line 45

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.



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

def archived_checksum
  @archived_checksum
end

#archived_checksum_styleObject

Returns the value of attribute archived_checksum_style.



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

def archived_checksum_style
  @archived_checksum_style
end

#atimeObject

Returns the value of attribute atime.



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

def atime
  @atime
end

#ctimeObject

Returns the value of attribute ctime.



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

def ctime
  @ctime
end

#dataObject

Returns the value of attribute data.



22
23
24
# File 'lib/omnizip/formats/xar/entry.rb', line 22

def data
  @data
end

#data_encodingObject

Returns the value of attribute data_encoding.



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

def data_encoding
  @data_encoding
end

#data_lengthObject

Returns the value of attribute data_length.



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

def data_length
  @data_length
end

#data_offsetObject

Returns the value of attribute data_offset.



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

def data_offset
  @data_offset
end

#data_sizeObject

Returns the value of attribute data_size.



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

def data_size
  @data_size
end

#device_majorObject

Returns the value of attribute device_major.



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

def device_major
  @device_major
end

#device_minorObject

Returns the value of attribute device_minor.



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

def device_minor
  @device_minor
end

#eaObject

Returns the value of attribute ea.



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

def ea
  @ea
end

#extracted_checksumObject

Returns the value of attribute extracted_checksum.



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

def extracted_checksum
  @extracted_checksum
end

#extracted_checksum_styleObject

Returns the value of attribute extracted_checksum_style.



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

def extracted_checksum_style
  @extracted_checksum_style
end

#flagsObject

Returns the value of attribute flags.



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

def flags
  @flags
end

#gidObject

Returns the value of attribute gid.



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

def gid
  @gid
end

#groupObject

Returns the value of attribute group.



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

def group
  @group
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#inoObject

Returns the value of attribute ino.



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

def ino
  @ino
end

Returns the value of attribute link_target.



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

def link_target
  @link_target
end

Returns the value of attribute link_type.



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

def link_type
  @link_type
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#mtimeObject

Returns the value of attribute mtime.



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

def mtime
  @mtime
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Returns the value of attribute nlink.



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

def nlink
  @nlink
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#uidObject

Returns the value of attribute uid.



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

def uid
  @uid
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.type_from_mode(mode) ⇒ String

Get file type from mode

Returns:

  • (String)

    XAR type string



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/omnizip/formats/xar/entry.rb', line 149

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



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

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

#directory?Boolean

Check if entry is a directory

Returns:

  • (Boolean)

    true if directory



100
101
102
# File 'lib/omnizip/formats/xar/entry.rb', line 100

def directory?
  @type == TYPE_DIRECTORY
end

#fifo?Boolean

Check if entry is a FIFO

Returns:

  • (Boolean)

    true if FIFO



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

def fifo?
  @type == TYPE_FIFO
end

#file?Boolean

Check if entry is a regular file

Returns:

  • (Boolean)

    true if regular file



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

def file?
  @type == TYPE_FILE
end

#hardlink?Boolean

Check if entry is a hard link

Returns:

  • (Boolean)

    true if hard link



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

def hardlink?
  @type == TYPE_HARDLINK
end

#socket?Boolean

Check if entry is a socket

Returns:

  • (Boolean)

    true if socket



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

def socket?
  @type == TYPE_SOCKET
end

#symlink?Boolean

Check if entry is a symbolic link

Returns:

  • (Boolean)

    true if symbolic link



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

def symlink?
  @type == TYPE_SYMLINK
end

#to_hHash

Convert entry to hash for TOC generation

Returns:

  • (Hash)

    Entry as hash



165
166
167
168
169
170
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
# File 'lib/omnizip/formats/xar/entry.rb', line 165

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