minitar 1.0.2 → 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 +326 -0
- data/CODE_OF_CONDUCT.md +167 -0
- data/CONTRIBUTING.md +312 -0
- data/CONTRIBUTORS.md +27 -0
- data/LICENCE.md +42 -0
- data/Manifest.txt +31 -8
- data/README.md +82 -0
- data/Rakefile +74 -20
- data/SECURITY.md +54 -0
- data/lib/minitar/input.rb +242 -189
- data/lib/minitar/output.rb +72 -64
- data/lib/minitar/pax_header.rb +111 -0
- data/lib/minitar/posix_header.rb +276 -239
- data/lib/minitar/reader.rb +226 -231
- data/lib/minitar/version.rb +5 -0
- data/lib/minitar/writer.rb +246 -299
- data/lib/minitar.rb +134 -115
- data/licenses/bsdl.txt +20 -0
- data/licenses/dco.txt +34 -0
- data/licenses/ruby.txt +52 -0
- data/test/fixtures/issue_46.tar.gz +0 -0
- data/test/fixtures/issue_62.tar.gz +0 -0
- data/test/fixtures/tar_input.tgz +0 -0
- data/test/fixtures/test_input_non_strict_octal.tgz +0 -0
- data/test/fixtures/test_input_relative.tgz +0 -0
- data/test/fixtures/test_input_space_octal.tgz +0 -0
- data/test/fixtures/test_minitar.tar.gz +0 -0
- data/test/minitest_helper.rb +12 -1
- data/test/support/minitar_test_helpers/fixtures.rb +38 -0
- data/test/support/minitar_test_helpers/header.rb +130 -0
- data/test/support/minitar_test_helpers/tarball.rb +324 -0
- data/test/support/minitar_test_helpers.rb +36 -0
- data/test/test_filename_boundary_conditions.rb +74 -0
- data/test/test_gnu_tar_compatibility.rb +92 -0
- data/test/test_integration_pack_unpack_cycle.rb +38 -0
- data/test/test_issue_46.rb +5 -23
- data/test/test_issue_62.rb +50 -0
- data/test/test_minitar.rb +168 -39
- data/test/test_path_normalization.rb +170 -0
- data/test/test_pax_header.rb +104 -0
- data/test/test_pax_support.rb +66 -0
- data/test/test_tar_header.rb +289 -75
- data/test/test_tar_input.rb +14 -61
- data/test/test_tar_output.rb +7 -9
- data/test/test_tar_reader.rb +17 -18
- data/test/test_tar_writer.rb +105 -126
- data/test/test_windows_extraction.rb +121 -0
- metadata +108 -95
- data/Code-of-Conduct.md +0 -128
- data/Contributing.md +0 -94
- data/History.md +0 -236
- data/Licence.md +0 -15
- data/README.rdoc +0 -92
- data/docs/bsdl.txt +0 -19
- data/docs/ruby.txt +0 -56
- data/test/support/tar_test_helpers.rb +0 -134
data/lib/minitar/writer.rb
CHANGED
|
@@ -1,340 +1,287 @@
|
|
|
1
|
-
|
|
2
|
-
# The class that writes a tar format archive to a data stream.
|
|
3
|
-
class Writer
|
|
4
|
-
# The exception raised when the user attempts to write more data to a
|
|
5
|
-
# BoundedWriteStream than has been allocated.
|
|
6
|
-
WriteBoundaryOverflow = Class.new(StandardError)
|
|
7
|
-
|
|
8
|
-
# A stream wrapper that can only be written to. Any attempt to read
|
|
9
|
-
# from this restricted stream will result in a NameError being thrown.
|
|
10
|
-
class WriteOnlyStream
|
|
11
|
-
def initialize(io)
|
|
12
|
-
@io = io
|
|
13
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
14
2
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
# The class that writes a tar format archive to a data stream.
|
|
4
|
+
class Minitar::Writer
|
|
5
|
+
# The exception raised when the user attempts to write more data to a BoundedWriteStream
|
|
6
|
+
# than has been allocated.
|
|
7
|
+
WriteBoundaryOverflow = Class.new(Minitar::Error)
|
|
19
8
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
case c
|
|
26
|
-
when :FileOverflow
|
|
27
|
-
warn "Writer::BoundedWriteStream::FileOverflow has been renamed " \
|
|
28
|
-
"to Writer::WriteBoundaryOverflow"
|
|
29
|
-
const_set :FileOverflow,
|
|
30
|
-
Minitar::Writer::WriteBoundaryOverflow
|
|
31
|
-
else
|
|
32
|
-
super
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# The maximum number of bytes that may be written to this data
|
|
37
|
-
# stream.
|
|
38
|
-
attr_reader :limit
|
|
39
|
-
# The current total number of bytes written to this data stream.
|
|
40
|
-
attr_reader :written
|
|
41
|
-
|
|
42
|
-
def initialize(io, limit)
|
|
43
|
-
@io = io
|
|
44
|
-
@limit = limit
|
|
45
|
-
@written = 0
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def write(data)
|
|
49
|
-
size = data.bytesize
|
|
50
|
-
raise WriteBoundaryOverflow if (size + @written) > @limit
|
|
51
|
-
@io.write(data)
|
|
52
|
-
@written += size
|
|
53
|
-
size
|
|
54
|
-
end
|
|
9
|
+
# A stream wrapper that can only be written to. Any attempt to read from this restricted
|
|
10
|
+
# stream will result in a NameError being thrown.
|
|
11
|
+
class WriteOnlyStream
|
|
12
|
+
def initialize(io)
|
|
13
|
+
@io = io
|
|
55
14
|
end
|
|
56
15
|
|
|
57
|
-
|
|
16
|
+
def write(data) = @io.write(data)
|
|
17
|
+
end
|
|
58
18
|
|
|
59
|
-
|
|
60
|
-
case c
|
|
61
|
-
when :BoundedStream
|
|
62
|
-
warn "BoundedStream has been renamed to BoundedWriteStream"
|
|
63
|
-
const_set(:BoundedStream, BoundedWriteStream)
|
|
64
|
-
else
|
|
65
|
-
super
|
|
66
|
-
end
|
|
67
|
-
end
|
|
19
|
+
private_constant :WriteOnlyStream
|
|
68
20
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
#
|
|
72
|
-
|
|
73
|
-
# of
|
|
74
|
-
|
|
75
|
-
# call-seq:
|
|
76
|
-
# w = Minitar::Writer.open(STDOUT)
|
|
77
|
-
# w.add_file_simple('foo.txt', :size => 3)
|
|
78
|
-
# w.close
|
|
79
|
-
#
|
|
80
|
-
# Minitar::Writer.open(STDOUT) do |w|
|
|
81
|
-
# w.add_file_simple('foo.txt', :size => 3)
|
|
82
|
-
# end
|
|
83
|
-
def self.open(io) # :yields Writer:
|
|
84
|
-
writer = new(io)
|
|
85
|
-
return writer unless block_given?
|
|
86
|
-
|
|
87
|
-
# This exception context must remain, otherwise the stream closes on open
|
|
88
|
-
# even if a block is not given.
|
|
89
|
-
begin
|
|
90
|
-
yield writer
|
|
91
|
-
ensure
|
|
92
|
-
writer.close
|
|
93
|
-
end
|
|
94
|
-
end
|
|
21
|
+
# A WriteOnlyStream that also has a size limit.
|
|
22
|
+
class BoundedWriteStream < WriteOnlyStream
|
|
23
|
+
# The maximum number of bytes that may be written to this data stream.
|
|
24
|
+
attr_reader :limit
|
|
25
|
+
# The current total number of bytes written to this data stream.
|
|
26
|
+
attr_reader :written
|
|
95
27
|
|
|
96
|
-
|
|
97
|
-
def initialize(io)
|
|
28
|
+
def initialize(io, limit)
|
|
98
29
|
@io = io
|
|
99
|
-
@
|
|
30
|
+
@limit = limit
|
|
31
|
+
@written = 0
|
|
100
32
|
end
|
|
101
33
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
# Valid parameters to +opts+ are:
|
|
111
|
-
#
|
|
112
|
-
# <tt>:data</tt>:: Optional. The data to write to the archive.
|
|
113
|
-
# <tt>:mode</tt>:: The Unix file permissions mode value. If not
|
|
114
|
-
# provided, defaults to 0644.
|
|
115
|
-
# <tt>:size</tt>:: The size, in bytes. If <tt>:data</tt> is provided,
|
|
116
|
-
# this parameter may be ignored (if it is less than
|
|
117
|
-
# the size of the data provided) or used to add
|
|
118
|
-
# padding (if it is greater than the size of the data
|
|
119
|
-
# provided).
|
|
120
|
-
# <tt>:uid</tt>:: The Unix file owner user ID number.
|
|
121
|
-
# <tt>:gid</tt>:: The Unix file owner group ID number.
|
|
122
|
-
# <tt>:mtime</tt>:: File modification time, interpreted as an integer.
|
|
123
|
-
#
|
|
124
|
-
# An exception will be raised if the Writer is already closed, or if
|
|
125
|
-
# more data is written to the BoundedWriteStream than expected.
|
|
126
|
-
#
|
|
127
|
-
# call-seq:
|
|
128
|
-
# writer.add_file_simple('foo.txt', :data => "bar")
|
|
129
|
-
# writer.add_file_simple('foo.txt', :size => 3) do |w|
|
|
130
|
-
# w.write("bar")
|
|
131
|
-
# end
|
|
132
|
-
def add_file_simple(name, opts = {}) # :yields BoundedWriteStream:
|
|
133
|
-
raise ClosedStream if @closed
|
|
134
|
-
|
|
135
|
-
header = {
|
|
136
|
-
mode: opts.fetch(:mode, 0o644),
|
|
137
|
-
mtime: opts.fetch(:mtime, nil),
|
|
138
|
-
gid: opts.fetch(:gid, nil),
|
|
139
|
-
uid: opts.fetch(:uid, nil)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
data = opts.fetch(:data, nil)
|
|
143
|
-
size = opts.fetch(:size, nil)
|
|
144
|
-
|
|
145
|
-
if block_given?
|
|
146
|
-
if data
|
|
147
|
-
raise ArgumentError,
|
|
148
|
-
"Too much data (opts[:data] and block_given?)."
|
|
149
|
-
end
|
|
34
|
+
def write(data)
|
|
35
|
+
size = data.bytesize
|
|
36
|
+
raise WriteBoundaryOverflow if (size + @written) > @limit
|
|
37
|
+
@io.write(data)
|
|
38
|
+
@written += size
|
|
39
|
+
size
|
|
40
|
+
end
|
|
41
|
+
end
|
|
150
42
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
43
|
+
private_constant :BoundedWriteStream
|
|
44
|
+
|
|
45
|
+
# With no associated block, Writer::open is a synonym for Writer::new. If the optional
|
|
46
|
+
# code block is given, it will be passed the new _writer_ as an argument and the Writer
|
|
47
|
+
# object will automatically be closed when the block terminates. In this instance,
|
|
48
|
+
# Writer::open returns the value of the block.
|
|
49
|
+
#
|
|
50
|
+
# :call-seq:
|
|
51
|
+
# Minitar::Writer.open(io) -> writer
|
|
52
|
+
# Minitar::Writer.open(io) { |writer| block } -> obj
|
|
53
|
+
def self.open(io) # :yields: Writer
|
|
54
|
+
writer = new(io)
|
|
55
|
+
return writer unless block_given?
|
|
56
|
+
|
|
57
|
+
# This exception context must remain, otherwise the stream closes on open even if
|
|
58
|
+
# a block is not given.
|
|
59
|
+
begin
|
|
60
|
+
yield writer
|
|
61
|
+
ensure
|
|
62
|
+
writer.close
|
|
63
|
+
end
|
|
64
|
+
end
|
|
154
65
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
66
|
+
# Creates and returns a new Writer object.
|
|
67
|
+
def initialize(io)
|
|
68
|
+
@io = io
|
|
69
|
+
@closed = false
|
|
70
|
+
end
|
|
158
71
|
|
|
159
|
-
|
|
72
|
+
# Adds a file to the archive as `name`. The data can be provided in `opts[:data]` or
|
|
73
|
+
# provided to a BoundedWriteStream that is yielded to the provided block.
|
|
74
|
+
#
|
|
75
|
+
# If `opts[:data]` is provided, all other values to `opts` are optional. If the data is
|
|
76
|
+
# provided to the yielded BoundedWriteStream, `opts[:size]` must be provided.
|
|
77
|
+
#
|
|
78
|
+
# Valid parameters to `opts` are:
|
|
79
|
+
#
|
|
80
|
+
# - `:data` (_optional_): The data to write to the archive.
|
|
81
|
+
# - `:mode`: The Unix file permissions mode value. If not provided, defaults to `0o644`.
|
|
82
|
+
# - `:size`: The size, in bytes. If `:data` is provided, this parameter may be ignored
|
|
83
|
+
# (if it is less than the size of the data provided) or used to add padding
|
|
84
|
+
# (if it is greater than the size of the data provided).
|
|
85
|
+
# - `:uid`: The Unix file owner user ID number.
|
|
86
|
+
# - `:gid`: The Unix file owner group ID number.
|
|
87
|
+
# - `:mtime`: File modification time, interpreted as an integer.
|
|
88
|
+
#
|
|
89
|
+
# An exception will be raised if the Writer is already closed, or if more data is
|
|
90
|
+
# written to the BoundedWriteStream than expected.
|
|
91
|
+
#
|
|
92
|
+
# :call-seq:
|
|
93
|
+
# add_file_simple('foo.txt', :data => "bar") -> writer
|
|
94
|
+
# add_file_simple('foo.txt', :size => 3) { |w| block }
|
|
95
|
+
def add_file_simple(name, opts = {}) # :yields: BoundedWriteStream
|
|
96
|
+
raise Minitar::ClosedStream if @closed
|
|
97
|
+
|
|
98
|
+
header = {
|
|
99
|
+
mode: opts.fetch(:mode, 0o644),
|
|
100
|
+
mtime: opts.fetch(:mtime, nil),
|
|
101
|
+
gid: opts.fetch(:gid, nil),
|
|
102
|
+
uid: opts.fetch(:uid, nil)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
data = opts.fetch(:data, nil)
|
|
106
|
+
size = opts.fetch(:size, nil)
|
|
107
|
+
|
|
108
|
+
if block_given?
|
|
109
|
+
raise ArgumentError, "Too much data (opts[:data] and block_given?)." if data
|
|
110
|
+
raise ArgumentError, "No size provided" unless size
|
|
111
|
+
else
|
|
112
|
+
raise ArgumentError, "No data provided" unless data
|
|
113
|
+
|
|
114
|
+
bytes = data.bytesize
|
|
115
|
+
size = bytes if size.nil? || size < bytes
|
|
116
|
+
end
|
|
160
117
|
|
|
161
|
-
|
|
162
|
-
write_header(header, name, short_name, prefix, needs_long_name)
|
|
118
|
+
header[:size] = size
|
|
163
119
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
yield os
|
|
167
|
-
else
|
|
168
|
-
os.write(data)
|
|
169
|
-
end
|
|
120
|
+
short_name, prefix, needs_long_name = split_name(name)
|
|
121
|
+
write_header(header, name, short_name, prefix, needs_long_name)
|
|
170
122
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
123
|
+
os = BoundedWriteStream.new(@io, size)
|
|
124
|
+
if block_given?
|
|
125
|
+
yield os
|
|
126
|
+
else
|
|
127
|
+
os.write(data)
|
|
175
128
|
end
|
|
176
129
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
# Valid parameters to +opts+ are:
|
|
183
|
-
#
|
|
184
|
-
# <tt>:mode</tt>:: The Unix file permissions mode value. If not
|
|
185
|
-
# provided, defaults to 0644.
|
|
186
|
-
# <tt>:uid</tt>:: The Unix file owner user ID number.
|
|
187
|
-
# <tt>:gid</tt>:: The Unix file owner group ID number.
|
|
188
|
-
# <tt>:mtime</tt>:: File modification time, interpreted as an integer.
|
|
189
|
-
# <tt>:data</tt>:: Optional. The data to write to the archive.
|
|
190
|
-
#
|
|
191
|
-
# If <tt>opts[:data]</tt> is provided, this acts the same as
|
|
192
|
-
# #add_file_simple. Otherwise, the file's size will be determined from
|
|
193
|
-
# the amount of data written to the stream.
|
|
194
|
-
#
|
|
195
|
-
# For #add_file to be used without <tt>opts[:data]</tt>, the Writer
|
|
196
|
-
# must be wrapping a stream object that is seekable. Otherwise,
|
|
197
|
-
# #add_file_simple must be used.
|
|
198
|
-
#
|
|
199
|
-
# +opts+ may be modified during the writing of the file to the stream.
|
|
200
|
-
def add_file(name, opts = {}, &) # :yields WriteOnlyStream, +opts+:
|
|
201
|
-
raise ClosedStream if @closed
|
|
202
|
-
|
|
203
|
-
return add_file_simple(name, opts, &) if opts[:data]
|
|
204
|
-
|
|
205
|
-
unless Minitar.seekable?(@io)
|
|
206
|
-
raise Minitar::NonSeekableStream
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
short_name, prefix, needs_long_name = split_name(name)
|
|
130
|
+
min_padding = size - os.written
|
|
131
|
+
@io.write("\0" * min_padding)
|
|
132
|
+
remainder = (512 - (size % 512)) % 512
|
|
133
|
+
@io.write("\0" * remainder)
|
|
134
|
+
end
|
|
210
135
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
136
|
+
# Adds a file to the archive as `name`. The data can be provided in `opts[:data]` or
|
|
137
|
+
# provided to a yielded WriteOnlyStream. The size of the file will be determined from
|
|
138
|
+
# the amount of data written to the stream.
|
|
139
|
+
#
|
|
140
|
+
# Valid parameters to `opts` are:
|
|
141
|
+
#
|
|
142
|
+
# - `:data` (_optional_): The data to write to the archive.
|
|
143
|
+
# - `:mode`: The Unix file permissions mode value. If not provided, defaults to `0o644`.
|
|
144
|
+
# - `:uid`: The Unix file owner user ID number.
|
|
145
|
+
# - `:gid`: The Unix file owner group ID number.
|
|
146
|
+
# - `:mtime`: File modification time, interpreted as an integer.
|
|
147
|
+
#
|
|
148
|
+
# If `opts[:data]` is provided, this acts the same as #add_file_simple. Otherwise, the
|
|
149
|
+
# file's size will be determined from the amount of data written to the stream.
|
|
150
|
+
#
|
|
151
|
+
# For #add_file to be used without `opts[:data]`, the Writer must be wrapping a stream
|
|
152
|
+
# object that is seekable. Otherwise, #add_file_simple must be used.
|
|
153
|
+
#
|
|
154
|
+
# `opts` may be modified during the writing of the file to the stream.
|
|
155
|
+
def add_file(name, opts = {}, &) # :yields: WriteOnlyStream, `opts`
|
|
156
|
+
raise Minitar::ClosedStream if @closed
|
|
157
|
+
|
|
158
|
+
return add_file_simple(name, opts, &) if opts[:data]
|
|
159
|
+
|
|
160
|
+
raise Minitar::NonSeekableStream unless Minitar.seekable?(@io)
|
|
161
|
+
|
|
162
|
+
short_name, prefix, needs_long_name = split_name(name)
|
|
163
|
+
|
|
164
|
+
data_offset = needs_long_name ? 3 * 512 : 512
|
|
165
|
+
init_pos = @io.pos
|
|
166
|
+
@io.write("\0" * data_offset) # placeholder for the header
|
|
167
|
+
|
|
168
|
+
yield WriteOnlyStream.new(@io), opts
|
|
169
|
+
|
|
170
|
+
size = @io.pos - (init_pos + data_offset)
|
|
171
|
+
remainder = (512 - (size % 512)) % 512
|
|
172
|
+
@io.write("\0" * remainder)
|
|
173
|
+
|
|
174
|
+
final_pos, @io.pos = @io.pos, init_pos
|
|
175
|
+
|
|
176
|
+
header = {
|
|
177
|
+
mode: opts[:mode],
|
|
178
|
+
mtime: opts[:mtime],
|
|
179
|
+
size: size,
|
|
180
|
+
gid: opts[:gid],
|
|
181
|
+
uid: opts[:uid]
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
write_header(header, name, short_name, prefix, needs_long_name)
|
|
185
|
+
|
|
186
|
+
@io.pos = final_pos
|
|
187
|
+
end
|
|
214
188
|
|
|
215
|
-
|
|
189
|
+
# Creates a directory entry in the tar.
|
|
190
|
+
def mkdir(name, opts = {})
|
|
191
|
+
raise Minitar::ClosedStream if @closed
|
|
216
192
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
193
|
+
header = {
|
|
194
|
+
mode: opts[:mode],
|
|
195
|
+
typeflag: "5",
|
|
196
|
+
size: 0,
|
|
197
|
+
gid: opts[:gid],
|
|
198
|
+
uid: opts[:uid],
|
|
199
|
+
mtime: opts[:mtime]
|
|
200
|
+
}
|
|
220
201
|
|
|
221
|
-
|
|
202
|
+
short_name, prefix, needs_long_name = split_name(name)
|
|
203
|
+
write_header(header, name, short_name, prefix, needs_long_name)
|
|
222
204
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
mtime: opts[:mtime],
|
|
226
|
-
size: size,
|
|
227
|
-
gid: opts[:gid],
|
|
228
|
-
uid: opts[:uid]
|
|
229
|
-
}
|
|
205
|
+
nil
|
|
206
|
+
end
|
|
230
207
|
|
|
231
|
-
|
|
208
|
+
# Creates a symbolic link entry in the tar.
|
|
209
|
+
def symlink(name, link_target, opts = {})
|
|
210
|
+
raise Minitar::ClosedStream if @closed
|
|
211
|
+
raise Minitar::FileNameTooLong if link_target.size > 100
|
|
212
|
+
|
|
213
|
+
name, prefix = split_name(name)
|
|
214
|
+
header = {
|
|
215
|
+
name: name,
|
|
216
|
+
mode: opts[:mode],
|
|
217
|
+
typeflag: "2",
|
|
218
|
+
size: 0,
|
|
219
|
+
linkname: link_target,
|
|
220
|
+
gid: opts[:gid],
|
|
221
|
+
uid: opts[:uid],
|
|
222
|
+
mtime: opts[:mtime],
|
|
223
|
+
prefix: prefix
|
|
224
|
+
}
|
|
225
|
+
@io.write(Minitar::PosixHeader.new(header).to_s)
|
|
226
|
+
nil
|
|
227
|
+
end
|
|
232
228
|
|
|
233
|
-
|
|
234
|
-
|
|
229
|
+
# Passes the #flush method to the wrapped stream, used for buffered streams.
|
|
230
|
+
def flush
|
|
231
|
+
raise Minitar::ClosedStream if @closed
|
|
232
|
+
@io.flush if @io.respond_to?(:flush)
|
|
233
|
+
end
|
|
235
234
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
raise ClosedStream if @closed
|
|
239
|
-
|
|
240
|
-
header = {
|
|
241
|
-
mode: opts[:mode],
|
|
242
|
-
typeflag: "5",
|
|
243
|
-
size: 0,
|
|
244
|
-
gid: opts[:gid],
|
|
245
|
-
uid: opts[:uid],
|
|
246
|
-
mtime: opts[:mtime]
|
|
247
|
-
}
|
|
235
|
+
# Returns false if the writer is open.
|
|
236
|
+
def closed? = @closed
|
|
248
237
|
|
|
249
|
-
|
|
250
|
-
|
|
238
|
+
# Closes the Writer. This does not close the underlying wrapped output stream.
|
|
239
|
+
def close
|
|
240
|
+
return if @closed
|
|
241
|
+
@io.write("\0" * 1024)
|
|
242
|
+
@closed = true
|
|
243
|
+
end
|
|
251
244
|
|
|
252
|
-
|
|
253
|
-
end
|
|
245
|
+
private
|
|
254
246
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
name: name,
|
|
264
|
-
mode: opts[:mode],
|
|
265
|
-
typeflag: "2",
|
|
266
|
-
size: 0,
|
|
267
|
-
linkname: link_target,
|
|
268
|
-
gid: opts[:gid],
|
|
269
|
-
uid: opts[:uid],
|
|
270
|
-
mtime: opts[:mtime],
|
|
271
|
-
prefix: prefix
|
|
247
|
+
def write_header(header, long_name, short_name, prefix, needs_long_name)
|
|
248
|
+
if needs_long_name
|
|
249
|
+
long_name_header = {
|
|
250
|
+
prefix: "",
|
|
251
|
+
name: Minitar::PosixHeader::GNU_EXT_LONG_LINK,
|
|
252
|
+
typeflag: "L",
|
|
253
|
+
size: long_name.length + 1,
|
|
254
|
+
mode: 0
|
|
272
255
|
}
|
|
273
|
-
@io.write(PosixHeader.new(
|
|
274
|
-
|
|
256
|
+
@io.write(Minitar::PosixHeader.new(long_name_header).to_s)
|
|
257
|
+
@io.write(long_name)
|
|
258
|
+
@io.write("\0" * (512 - (long_name.length % 512)))
|
|
275
259
|
end
|
|
276
260
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
raise ClosedStream if @closed
|
|
281
|
-
@io.flush if @io.respond_to?(:flush)
|
|
282
|
-
end
|
|
261
|
+
new_header = header.merge({name: short_name, prefix: prefix})
|
|
262
|
+
@io.write(Minitar::PosixHeader.new(new_header).to_s)
|
|
263
|
+
end
|
|
283
264
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
265
|
+
def split_name(name)
|
|
266
|
+
if name.bytesize <= 100
|
|
267
|
+
prefix = ""
|
|
268
|
+
else
|
|
269
|
+
parts = name.split("/")
|
|
270
|
+
newname = parts.pop
|
|
288
271
|
|
|
289
|
-
|
|
290
|
-
# stream.
|
|
291
|
-
def close
|
|
292
|
-
return if @closed
|
|
293
|
-
@io.write("\0" * 1024)
|
|
294
|
-
@closed = true
|
|
295
|
-
end
|
|
272
|
+
nxt = ""
|
|
296
273
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
long_name_header = {
|
|
302
|
-
prefix: "",
|
|
303
|
-
name: PosixHeader::GNU_EXT_LONG_LINK,
|
|
304
|
-
typeflag: "L",
|
|
305
|
-
size: long_name.length + 1,
|
|
306
|
-
mode: 0
|
|
307
|
-
}
|
|
308
|
-
@io.write(PosixHeader.new(long_name_header))
|
|
309
|
-
@io.write(long_name)
|
|
310
|
-
@io.write("\0" * (512 - (long_name.length % 512)))
|
|
274
|
+
loop do
|
|
275
|
+
nxt = parts.pop || ""
|
|
276
|
+
break if newname.bytesize + 1 + nxt.bytesize >= 100
|
|
277
|
+
newname = "#{nxt}/#{newname}"
|
|
311
278
|
end
|
|
312
279
|
|
|
313
|
-
|
|
314
|
-
@io.write(PosixHeader.new(new_header))
|
|
315
|
-
end
|
|
316
|
-
|
|
317
|
-
def split_name(name)
|
|
318
|
-
if name.bytesize <= 100
|
|
319
|
-
prefix = ""
|
|
320
|
-
else
|
|
321
|
-
parts = name.split("/")
|
|
322
|
-
newname = parts.pop
|
|
280
|
+
prefix = (parts + [nxt]).join("/")
|
|
323
281
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
loop do
|
|
327
|
-
nxt = parts.pop || ""
|
|
328
|
-
break if newname.bytesize + 1 + nxt.bytesize >= 100
|
|
329
|
-
newname = "#{nxt}/#{newname}"
|
|
330
|
-
end
|
|
331
|
-
|
|
332
|
-
prefix = (parts + [nxt]).join("/")
|
|
333
|
-
|
|
334
|
-
name = newname
|
|
335
|
-
end
|
|
336
|
-
|
|
337
|
-
[name, prefix, name.bytesize > 100 || prefix.bytesize > 155]
|
|
282
|
+
name = newname
|
|
338
283
|
end
|
|
284
|
+
|
|
285
|
+
[name, prefix, name.bytesize > 100 || prefix.bytesize > 155]
|
|
339
286
|
end
|
|
340
287
|
end
|