Class: Minitar::PaxHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/minitar/pax_header.rb

Overview

Implements the PAX Extended Header as a Ruby class. The header consists of following format:

<decimal-length><space><ascii-keyword>=<value><newline>

Where:

  • decimal-length is the total number of bytes for the PaxHeader record using ASCII decimal values; this includes the terminal newline (0x10).

  • space is a single literal ASCII space (0x20).

  • ascii-keyword is a PAX Extended Header keyword, which may be any ASCII character except newline (0x10) or equal sign (0x3D).

  • = is the literal ASCII equal sign (0x3D).

  • value is any series of bytes except newline (0x10).

  • newline is the literal ASCII newline (0x10).

There are several keywords defined in the POSIX standard; some of them are supported in this class, but may not be supported by Minitar as a whole.

Primary support for PAX extended headers is for extracting size information for large file support. Other features may be added in the future.

Constant Summary collapse

BLOCK_SIZE =
512

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ PaxHeader

Creates a new PaxHeader from attributes hash.



84
85
86
# File 'lib/minitar/pax_header.rb', line 84

def initialize(attributes = {})
  @attributes = attributes.transform_keys(&:to_s)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



28
29
30
# File 'lib/minitar/pax_header.rb', line 28

def attributes
  @attributes
end

Class Method Details

.from_data(content) ⇒ Object

Creates a new PaxHeader from PAX content data.



47
# File 'lib/minitar/pax_header.rb', line 47

def from_data(content) = new(parse_content(content))

.from_stream(stream, posix_header) ⇒ Object

Creates a new PaxHeader from a data stream and posix header. Reads the PAX content based on the size specified in the posix header.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minitar/pax_header.rb', line 33

def from_stream(stream, posix_header)
  raise ArgumentError, "Header must be a PAX header" unless posix_header.pax_header?

  pax_block = (posix_header.size / BLOCK_SIZE.to_f).ceil * BLOCK_SIZE
  pax_content = stream.read(pax_block)

  raise Minitar::InvalidTarStream if pax_content.nil? || pax_content.bytesize < posix_header.size

  actual_content = pax_content[0, posix_header.size]

  from_data(actual_content)
end

Instance Method Details

#mtimeObject

The mtime value from PAX attributes



95
# File 'lib/minitar/pax_header.rb', line 95

def mtime = @attributes["mtime"]&.to_f

#pathObject

The path value from PAX attributes



92
# File 'lib/minitar/pax_header.rb', line 92

def path = @attributes["path"]

#sizeObject

The size value from PAX attributes



89
# File 'lib/minitar/pax_header.rb', line 89

def size = @attributes["size"]&.to_i

#to_sObject

Returns a string representation of the PAX header content.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/minitar/pax_header.rb', line 98

def to_s
  @attributes.map do |keyword, value|
    keyword_value = " #{keyword}=#{value}\n"
    record = keyword_value
    begin
      length = record.bytesize
      length_str = length.to_s
      record = "#{length_str}#{keyword_value}"
    end while record.size != length # standard:disable Lint/Loop
    record
  end.join
end