minitar 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,111 +1,111 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Minitar
4
- # Implements the PAX Extended Header as a Ruby class. The header consists of following
5
- # format:
6
- #
7
- # <decimal-length><space><ascii-keyword>=<value><newline>
8
- #
9
- # Where:
10
- #
11
- # - +decimal-length+ is the total number of bytes for the PaxHeader record using ASCII
12
- # decimal values; this includes the terminal newline (0x10).
13
- # - +space+ is a single literal ASCII space (0x20).
14
- # - +ascii-keyword+ is a PAX Extended Header keyword, which may be any ASCII character
15
- # except newline (0x10) or equal sign (0x3D).
16
- # - +=+ is the literal ASCII equal sign (0x3D).
17
- # - +value+ is any series of bytes except newline (0x10).
18
- # - +newline+ is the literal ASCII newline (0x10).
19
- #
20
- # There are several keywords defined in the POSIX standard; some of them are supported
21
- # in this class, but may not be supported by Minitar as a whole.
22
- #
23
- # Primary support for PAX extended headers is for extracting size information for large
24
- # file support. Other features may be added in the future.
25
- class PaxHeader
26
- BLOCK_SIZE = 512
27
-
28
- attr_reader :attributes
29
-
30
- class << self
31
- # Creates a new PaxHeader from a data stream and posix header. Reads the PAX content
32
- # based on the size specified in the posix header.
33
- def from_stream(stream, posix_header)
34
- raise ArgumentError, "Header must be a PAX header" unless posix_header.pax_header?
35
-
36
- pax_block = (posix_header.size / BLOCK_SIZE.to_f).ceil * BLOCK_SIZE
37
- pax_content = stream.read(pax_block)
38
-
39
- raise Minitar::InvalidTarStream if pax_content.nil? || pax_content.bytesize < posix_header.size
40
-
41
- actual_content = pax_content[0, posix_header.size]
42
-
43
- from_data(actual_content)
44
- end
45
-
46
- # Creates a new PaxHeader from PAX content data.
47
- def from_data(content) = new(parse_content(content))
3
+ # Implements the PAX Extended Header as a Ruby class. The header consists of following
4
+ # format:
5
+ #
6
+ # ```
7
+ # <decimal-length><space><ascii-keyword>=<value><newline>
8
+ # ```
9
+ #
10
+ # Where:
11
+ #
12
+ # - `decimal-length`: the total number of bytes for the PaxHeader record using ASCII
13
+ # decimal values; this includes the terminal newline (0x10).
14
+ # - `space` is a single literal ASCII space (0x20).
15
+ # - `ascii-keyword` is a PAX Extended Header keyword, which may be any ASCII character
16
+ # except newline (0x10) or equal sign (0x3D).
17
+ # - `=` is the literal ASCII equal sign (0x3D).
18
+ # - `value` is any series of bytes except newline (0x10).
19
+ # - `newline` is the literal ASCII newline (0x10).
20
+ #
21
+ # There are several keywords defined in the POSIX standard; some of them are supported in
22
+ # this class, but may not be supported by Minitar as a whole.
23
+ #
24
+ # Primary support for PAX extended headers is for extracting size information for large
25
+ # file support. Other features may be added in the future.
26
+ class Minitar::PaxHeader
27
+ BLOCK_SIZE = 512
28
+
29
+ attr_reader :attributes
30
+
31
+ class << self
32
+ # Creates a new PaxHeader from a data stream and posix header. Reads the PAX content
33
+ # based on the size specified in the posix header.
34
+ def from_stream(stream, posix_header)
35
+ raise ArgumentError, "Header must be a PAX header" unless posix_header.pax_header?
36
+
37
+ pax_block = (posix_header.size / BLOCK_SIZE.to_f).ceil * BLOCK_SIZE
38
+ pax_content = stream.read(pax_block)
39
+
40
+ raise Minitar::InvalidTarStream if pax_content.nil? || pax_content.bytesize < posix_header.size
41
+
42
+ actual_content = pax_content[0, posix_header.size]
43
+
44
+ from_data(actual_content)
45
+ end
48
46
 
49
- private
47
+ # Creates a new PaxHeader from PAX content data.
48
+ def from_data(content) = new(parse_content(content))
50
49
 
51
- def parse_content(content)
52
- attributes = {}
53
- offset = 0
50
+ private
54
51
 
55
- while offset < content.bytesize
56
- space_pos = content.index(" ", offset)
57
- break unless space_pos
52
+ def parse_content(content)
53
+ attributes = {}
54
+ offset = 0
58
55
 
59
- length_str = content[offset, space_pos - offset]
56
+ while offset < content.bytesize
57
+ space_pos = content.index(" ", offset)
58
+ break unless space_pos
60
59
 
61
- unless length_str.match?(/\A\d+\z/)
62
- raise ArgumentError, "Invalid length format in PAX header: '#{length_str}'"
63
- end
60
+ length_str = content[offset, space_pos - offset]
64
61
 
65
- length = length_str.to_i
66
- if offset + length > content.bytesize
67
- raise ArgumentError, "Length beyond PAX header: '#{content[offset..]}'"
68
- end
69
- record = content[offset, length]
62
+ unless length_str.match?(/\A\d+\z/)
63
+ raise ArgumentError, "Invalid length format in PAX header: '#{length_str}'"
64
+ end
70
65
 
71
- keyword_value = record[(space_pos - offset + 1)..-2]
72
- if keyword_value.include?("=")
73
- keyword, value = keyword_value.split("=", 2)
74
- attributes[keyword] = value
75
- end
66
+ length = length_str.to_i
67
+ if offset + length > content.bytesize
68
+ raise ArgumentError, "Length beyond PAX header: '#{content[offset..]}'"
69
+ end
70
+ record = content[offset, length]
76
71
 
77
- offset += length
72
+ keyword_value = record[(space_pos - offset + 1)..-2]
73
+ if keyword_value.include?("=")
74
+ keyword, value = keyword_value.split("=", 2)
75
+ attributes[keyword] = value
78
76
  end
79
- attributes
77
+
78
+ offset += length
80
79
  end
80
+ attributes
81
81
  end
82
+ end
82
83
 
83
- # Creates a new PaxHeader from attributes hash.
84
- def initialize(attributes = {})
85
- @attributes = attributes.transform_keys(&:to_s)
86
- end
84
+ # Creates a new PaxHeader from attributes hash.
85
+ def initialize(attributes = {})
86
+ @attributes = attributes.transform_keys(&:to_s)
87
+ end
87
88
 
88
- # The size value from PAX attributes
89
- def size = @attributes["size"]&.to_i
90
-
91
- # The path value from PAX attributes
92
- def path = @attributes["path"]
93
-
94
- # The mtime value from PAX attributes
95
- def mtime = @attributes["mtime"]&.to_f
96
-
97
- # Returns a string representation of the PAX header content.
98
- def to_s
99
- @attributes.map do |keyword, value|
100
- keyword_value = " #{keyword}=#{value}\n"
101
- record = keyword_value
102
- begin
103
- length = record.bytesize
104
- length_str = length.to_s
105
- record = "#{length_str}#{keyword_value}"
106
- end while record.size != length # standard:disable Lint/Loop
107
- record
108
- end.join
109
- end
89
+ # The size value from PAX attributes
90
+ def size = @attributes["size"]&.to_i
91
+
92
+ # The path value from PAX attributes
93
+ def path = @attributes["path"]
94
+
95
+ # The mtime value from PAX attributes
96
+ def mtime = @attributes["mtime"]&.to_f
97
+
98
+ # Returns a string representation of the PAX header content.
99
+ def to_s
100
+ @attributes.map do |keyword, value|
101
+ keyword_value = " #{keyword}=#{value}\n"
102
+ record = keyword_value
103
+ begin
104
+ length = record.bytesize
105
+ length_str = length.to_s
106
+ record = "#{length_str}#{keyword_value}"
107
+ end while record.size != length # standard:disable Lint/Loop
108
+ record
109
+ end.join
110
110
  end
111
111
  end