Class: Omnizip::Formats::Tar::Entry

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

Overview

TAR entry model

Represents a single entry (file, directory, link) in a TAR archive

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(name, options = {}) ⇒ Entry

Initialize a new TAR entry

Parameters:

  • name (String)

    Entry name/path

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

    Entry options



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/omnizip/formats/tar/entry.rb', line 20

def initialize(name, options = {})
  @name = name
  @mode = options[:mode] || 0o644
  @uid = options[:uid] || 0
  @gid = options[:gid] || 0
  @size = options[:size] || 0
  @mtime = options[:mtime] || Time.now
  @typeflag = options[:typeflag] || TYPE_REGULAR
  @linkname = options[:linkname] || ""
  @uname = options[:uname] || ""
  @gname = options[:gname] || ""
  @devmajor = options[:devmajor] || 0
  @devminor = options[:devminor] || 0
  @prefix = options[:prefix] || ""
  @data = nil
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/omnizip/formats/tar/entry.rb', line 14

def data
  @data
end

#devmajorObject

Returns the value of attribute devmajor.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def devmajor
  @devmajor
end

#devminorObject

Returns the value of attribute devminor.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def devminor
  @devminor
end

#gidObject

Returns the value of attribute gid.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def gid
  @gid
end

#gnameObject

Returns the value of attribute gname.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def gname
  @gname
end

#linknameObject

Returns the value of attribute linkname.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def linkname
  @linkname
end

#modeObject

Returns the value of attribute mode.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def mode
  @mode
end

#mtimeObject

Returns the value of attribute mtime.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def mtime
  @mtime
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def name
  @name
end

#prefixObject

Returns the value of attribute prefix.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def prefix
  @prefix
end

#sizeObject

Returns the value of attribute size.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def size
  @size
end

#typeflagObject

Returns the value of attribute typeflag.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def typeflag
  @typeflag
end

#uidObject

Returns the value of attribute uid.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def uid
  @uid
end

#unameObject

Returns the value of attribute uname.



12
13
14
# File 'lib/omnizip/formats/tar/entry.rb', line 12

def uname
  @uname
end

Class Method Details

.calculate_checksum(header) ⇒ Integer

Calculate checksum for TAR header

Parameters:

  • header (String)

    TAR header bytes

Returns:

  • (Integer)

    Checksum value



81
82
83
84
85
86
87
88
# File 'lib/omnizip/formats/tar/entry.rb', line 81

def self.calculate_checksum(header)
  # Replace checksum field with spaces for calculation
  checksum_header = header.dup
  checksum_header[CHECKSUM_OFFSET, CHECKSUM_SIZE] = " " * CHECKSUM_SIZE

  # Sum all bytes
  checksum_header.bytes.sum
end

Instance Method Details

#directory?Boolean

Check if entry is a directory

Returns:

  • (Boolean)

    true if directory



48
49
50
# File 'lib/omnizip/formats/tar/entry.rb', line 48

def directory?
  @typeflag == TYPE_DIRECTORY
end

#file?Boolean

Check if entry is a file

Returns:

  • (Boolean)

    true if regular file



55
56
57
# File 'lib/omnizip/formats/tar/entry.rb', line 55

def file?
  @typeflag == TYPE_REGULAR || @typeflag.nil? || @typeflag.empty?
end

#full_nameString

Get full entry name (prefix + name)

Returns:

  • (String)

    Full entry name



69
70
71
72
73
74
75
# File 'lib/omnizip/formats/tar/entry.rb', line 69

def full_name
  if @prefix && !@prefix.empty?
    File.join(@prefix, @name)
  else
    @name
  end
end

#symlink?Boolean

Check if entry is a symbolic link

Returns:

  • (Boolean)

    true if symbolic link



62
63
64
# File 'lib/omnizip/formats/tar/entry.rb', line 62

def symlink?
  @typeflag == TYPE_SYMLINK
end