minitar 1.1.0 → 1.1.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +94 -53
- data/CODE_OF_CONDUCT.md +155 -116
- data/CONTRIBUTING.md +154 -115
- data/LICENCE.md +8 -5
- data/Manifest.txt +2 -2
- data/README.md +20 -8
- data/Rakefile +7 -8
- data/SECURITY.md +16 -26
- data/lib/minitar/input.rb +243 -203
- data/lib/minitar/output.rb +69 -73
- data/lib/minitar/pax_header.rb +94 -94
- data/lib/minitar/posix_header.rb +273 -275
- data/lib/minitar/reader.rb +222 -222
- data/lib/minitar/version.rb +1 -1
- data/lib/minitar/writer.rb +247 -262
- data/lib/minitar.rb +97 -74
- data/test/test_path_normalization.rb +170 -0
- data/test/test_windows_extraction.rb +121 -0
- metadata +32 -25
- data/docs/bsdl.txt +0 -19
- data/docs/ruby.txt +0 -56
data/lib/minitar/pax_header.rb
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
class
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
47
|
+
# Creates a new PaxHeader from PAX content data.
|
|
48
|
+
def from_data(content) = new(parse_content(content))
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
attributes = {}
|
|
53
|
-
offset = 0
|
|
50
|
+
private
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
def parse_content(content)
|
|
53
|
+
attributes = {}
|
|
54
|
+
offset = 0
|
|
58
55
|
|
|
59
|
-
|
|
56
|
+
while offset < content.bytesize
|
|
57
|
+
space_pos = content.index(" ", offset)
|
|
58
|
+
break unless space_pos
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
raise ArgumentError, "Invalid length format in PAX header: '#{length_str}'"
|
|
63
|
-
end
|
|
60
|
+
length_str = content[offset, space_pos - offset]
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
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
|
-
|
|
77
|
+
|
|
78
|
+
offset += length
|
|
80
79
|
end
|
|
80
|
+
attributes
|
|
81
81
|
end
|
|
82
|
+
end
|
|
82
83
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
# Creates a new PaxHeader from attributes hash.
|
|
85
|
+
def initialize(attributes = {})
|
|
86
|
+
@attributes = attributes.transform_keys(&:to_s)
|
|
87
|
+
end
|
|
87
88
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|