ziperu 0.3.1-aarch64-linux-gnu → 0.4.0-aarch64-linux-gnu

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.
@@ -0,0 +1,393 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Ziperu
6
+ module Tar # rubocop:disable Style/Documentation -- documented in tar.rb
7
+ # Writes a tar archive at `path_or_io` (a path, or an IO with `write`,
8
+ # which may be a pipe or a socket), yielding a Tar::Writer:
9
+ #
10
+ # Ziperu::Tar.create('backup.tar.zst') do |tar|
11
+ # tar.add('report.pdf', 'tmp/report.pdf') # a file from disk
12
+ # tar.add('upload.bin', io) # any IO
13
+ # tar.add_data('notes.txt', "written #{Time.now}\n")
14
+ # tar.add_recursive('public', prefix: 'site/')
15
+ # end
16
+ #
17
+ # `compression:` is `:none`, `:gzip`, `:zstd`, `:xz` or `:bzip2`. For a
18
+ # path it defaults to what the name ends in (`.tar.gz` and `.tgz`,
19
+ # `.tar.zst`, `.tar.xz`, `.tar.bz2` and their short forms), for an IO
20
+ # to `:none`. `level:` is the codec's own (gzip and xz 0 to 9, zstd 1
21
+ # to 22, bzip2 1 to 9).
22
+ #
23
+ # A path is written beside itself and renamed into place once the
24
+ # block returns, so a failure leaves what was there. An IO is left
25
+ # open. Returns `path_or_io`. Not in rubyzip.
26
+ def self.create(path_or_io, **options)
27
+ raise ArgumentError, 'a block that adds the entries is required' unless block_given?
28
+
29
+ writer = Writer.new(path_or_io, **options)
30
+ begin
31
+ yield writer
32
+ rescue Exception # rubocop:disable Lint/RescueException -- whatever it is, the half-written archive goes
33
+ writer.abandon
34
+ raise
35
+ end
36
+ writer.close
37
+ path_or_io
38
+ end
39
+
40
+ # Archives what is under `src_dir` into a new tar archive at
41
+ # `path_or_io`: `Ziperu::Tar.add_recursive('site.tar.gz', 'public')`.
42
+ # Takes the options of `create` and of `Writer#add_recursive`.
43
+ def self.add_recursive(path_or_io, src_dir, compression: nil, level: nil, **options)
44
+ create(path_or_io, compression: compression, level: level) { |tar| tar.add_recursive(src_dir, **options) }
45
+ end
46
+
47
+ # A tar archive as a body to stream (Rack's `each`): the block adds the
48
+ # entries, and the archive's bytes are yielded as they are made.
49
+ #
50
+ # [200, { 'content-type' => 'application/gzip' },
51
+ # Ziperu::Tar.body(compression: :gzip) { |tar| tar.add_data('a.csv', csv) }]
52
+ def self.body(**options, &block)
53
+ raise ArgumentError, 'a block that adds the entries is required' unless block
54
+
55
+ Enumerator.new { |yielder| create(::Ziperu::OutputStream::Yielder.new(yielder), **options, &block) }
56
+ end
57
+
58
+ # What `Tar.create` yields.
59
+ #
60
+ # Every entry's size goes ahead of its data, so it has to be known when
61
+ # the entry starts: a file's is its size on disk, an IO's is what it
62
+ # has left (`size - pos`), and an IO that cannot say is copied to a
63
+ # temporary file first.
64
+ #
65
+ # Entries are written as given: owners are not recorded (uid and gid
66
+ # 0, no names), times are whole seconds, and with
67
+ # `Ziperu.source_date_epoch` set no time is later than it, so the same
68
+ # input gives the same bytes.
69
+ class Writer
70
+ EXTENSIONS = {
71
+ /\.(tar\.gz|tgz|taz)\z/i => :gzip,
72
+ /\.(tar\.zst|tzst)\z/i => :zstd,
73
+ /\.(tar\.xz|txz)\z/i => :xz,
74
+ /\.(tar\.bz2|tbz2|tbz|tb2)\z/i => :bzip2
75
+ }.freeze
76
+ private_constant :EXTENSIONS
77
+
78
+ # The levels each codec takes.
79
+ LEVELS = { none: nil, gzip: 0..9, zstd: 1..22, xz: 0..9, bzip2: 1..9 }.freeze # :nodoc:
80
+
81
+ FILE = '0'.ord
82
+ HARDLINK = '1'.ord
83
+ SYMLINK = '2'.ord
84
+ DIRECTORY = '5'.ord
85
+ private_constant :FILE, :HARDLINK, :SYMLINK, :DIRECTORY
86
+
87
+ CHUNK = 1 << 16
88
+ PIECE = 1 << 20
89
+ private_constant :CHUNK, :PIECE
90
+
91
+ # See Tar.create. Without a block there, `close` ends the archive and
92
+ # `abandon` throws it away.
93
+ def initialize(path_or_io, compression: nil, level: nil, **options)
94
+ @settings = Settings.take!(options)
95
+ raise ArgumentError, "unknown keyword: #{options.keys.map(&:inspect).join(', ')}" unless options.empty?
96
+
97
+ to_io = !path_or_io.kind_of?(::Pathname) && path_or_io.respond_to?(:write)
98
+ @path = ::File.absolute_path(::File.path(path_or_io)) unless to_io
99
+ @compression = compression || (@path ? compression_of(@path) : :none)
100
+ check_compression(level)
101
+ @native =
102
+ if to_io
103
+ Native::TarWriter.open_io(path_or_io, @compression.to_s, level)
104
+ else
105
+ @staging = staging_path
106
+ Native::TarWriter.open_path(@staging, @compression.to_s, level)
107
+ end
108
+ @open = true
109
+ end
110
+
111
+ # `:none`, `:gzip`, `:zstd`, `:xz` or `:bzip2`.
112
+ attr_reader :compression
113
+
114
+ # Adds `source` under `name`: a path (a file, or a directory, which
115
+ # is added alone, without what is in it) or an IO with `read`.
116
+ # `mode:` and `mtime:` default to the file's own for a path, and to
117
+ # 0644 and now for an IO.
118
+ def add(name, source, mode: nil, mtime: nil)
119
+ if !source.kind_of?(::Pathname) && source.respond_to?(:read)
120
+ add_io(name, source, mode || 0o644, mtime)
121
+ else
122
+ add_path(name, ::File.path(source), mode, mtime)
123
+ end
124
+ self
125
+ end
126
+
127
+ # Adds a file named `name` holding `data`, a String.
128
+ def add_data(name, data, mode: 0o644, mtime: nil)
129
+ data = data.to_str
130
+ entry(file_name(name), FILE, data.bytesize, mode, mtime)
131
+ # In pieces: the native side copies what it is given.
132
+ (0...data.bytesize).step(PIECE) { |at| @native.write(data.byteslice(at, PIECE)) }
133
+ self
134
+ end
135
+
136
+ # Adds a directory entry.
137
+ def mkdir(name, mode: 0o755, mtime: nil)
138
+ entry("#{file_name(name)}/", DIRECTORY, 0, mode, mtime)
139
+ self
140
+ end
141
+
142
+ # Adds a symbolic link named `name` that points at `target`.
143
+ def symlink(name, target, mtime: nil)
144
+ entry(file_name(name), SYMLINK, 0, 0o777, mtime, link: target.to_s)
145
+ self
146
+ end
147
+
148
+ # Adds a hard link named `name` to `target`, the name of an entry
149
+ # already in the archive.
150
+ def link(name, target, mode: 0o644, mtime: nil)
151
+ entry(file_name(name), HARDLINK, 0, mode, mtime, link: file_name(target))
152
+ self
153
+ end
154
+
155
+ # Adds everything under `src_dir`, in name order, as `prefix` plus
156
+ # its path below `src_dir`.
157
+ #
158
+ # `symlinks:` is `:skip` (the default, with a warning per link) or
159
+ # `:store`, which records each link as a link; none is followed.
160
+ # `exclude:` lists globs or Regexps of names in the archive to leave
161
+ # out, a directory with what is under it. `progress:` is called with
162
+ # `(name, done, nil)` after each entry. Anything that is not a file,
163
+ # a directory or a link (a socket, a device) is skipped with a
164
+ # warning, and so is the archive being written, if it is in there.
165
+ def add_recursive(src_dir, prefix: '', symlinks: :skip, exclude: nil, progress: nil)
166
+ raise ArgumentError, "symlinks: takes :skip or :store, not #{symlinks.inspect}" \
167
+ unless %i[skip store].include?(symlinks)
168
+
169
+ src_dir = ::File.path(src_dir)
170
+ raise ArgumentError, "#{src_dir} is not a directory" unless ::File.directory?(src_dir)
171
+
172
+ walk = Walk.new(self, symlinks, exclude, progress, [@path, @staging].compact)
173
+ walk.directory(src_dir, prefix.to_s)
174
+ self
175
+ end
176
+
177
+ # Ends the archive; for a path, puts it in place.
178
+ def close
179
+ return unless @open
180
+
181
+ @open = false
182
+ begin
183
+ @native.close
184
+ rescue Exception # rubocop:disable Lint/RescueException -- see Tar.create
185
+ discard
186
+ raise
187
+ end
188
+ put_in_place if @staging
189
+ nil
190
+ end
191
+
192
+ # Stops without ending the archive. A path is left as it was before;
193
+ # what an IO was already given stays given.
194
+ def abandon
195
+ return unless @open
196
+
197
+ @open = false
198
+ @native.abandon
199
+ discard
200
+ nil
201
+ end
202
+
203
+ def closed?
204
+ !@open
205
+ end
206
+
207
+ private
208
+
209
+ def discard
210
+ ::File.unlink(@staging) if @staging && ::File.exist?(@staging)
211
+ rescue SystemCallError
212
+ nil
213
+ end
214
+
215
+ def put_in_place
216
+ ::File.rename(@staging, @path)
217
+ rescue SystemCallError
218
+ discard
219
+ raise
220
+ end
221
+
222
+ def compression_of(path)
223
+ EXTENSIONS.each { |pattern, compression| return compression if path.match?(pattern) }
224
+ :none
225
+ end
226
+
227
+ def check_compression(level)
228
+ unless LEVELS.key?(@compression)
229
+ raise ArgumentError,
230
+ "compression: takes #{LEVELS.keys.map(&:inspect).join(', ')}, not #{@compression.inspect}"
231
+ end
232
+ return if level.nil?
233
+
234
+ range = LEVELS[@compression] or raise ArgumentError, 'level: needs a compression to apply to'
235
+ return if level.kind_of?(::Integer) && range.cover?(level)
236
+
237
+ raise ArgumentError, "level: for #{@compression} is #{range.first} to #{range.last}, not #{level.inspect}"
238
+ end
239
+
240
+ # A name beside the archive's own, in the same directory so that the
241
+ # rename at the end cannot cross a file system.
242
+ def staging_path
243
+ dir, base = ::File.split(@path)
244
+ ::File.join(dir, ".#{base}.ziperu-#{::Process.pid}-#{::SecureRandom.hex(8)}")
245
+ end
246
+
247
+ # A name as the archive takes it: not empty, no NUL, and no slash at
248
+ # the end (which is how a directory is told).
249
+ def file_name(name)
250
+ name = name.to_s
251
+ raise ArgumentError, 'an entry needs a name' if name.empty? || name.delete('/').empty?
252
+ raise ArgumentError, 'an entry name cannot hold a NUL' if name.include?("\0")
253
+
254
+ name.end_with?('/') ? name.sub(%r{/+\z}, '') : name
255
+ end
256
+
257
+ # The seconds an entry is stamped with: no later than the
258
+ # reproducible-build epoch, when there is one.
259
+ def stamp(mtime)
260
+ seconds = (mtime || ::Time.now).to_i
261
+ epoch = @settings.source_date_epoch
262
+ epoch && (mtime.nil? || seconds > epoch) ? epoch : seconds
263
+ end
264
+
265
+ def entry(name, type, size, mode, mtime, link: '')
266
+ raise IOError, 'closed tar writer' unless @open
267
+ raise ArgumentError, 'a link needs a target' if [SYMLINK, HARDLINK].include?(type) && link.empty?
268
+
269
+ @native.entry(header(name, type, size, mode, mtime, link))
270
+ end
271
+
272
+ # What the native side takes for an entry.
273
+ def header(name, type, size, mode, mtime, link = '')
274
+ [name.b, link.b, type, size, Integer(mode) & 0o7777, stamp(mtime), 0, 0, '', '']
275
+ end
276
+
277
+ def add_path(name, path, mode, mtime)
278
+ stat = ::File.stat(path)
279
+ if stat.directory?
280
+ mkdir(name, mode: mode || stat.mode, mtime: mtime || stat.mtime)
281
+ elsif stat.file?
282
+ add_file(file_name(name), path, stat, mode, mtime)
283
+ else
284
+ raise ArgumentError, "#{path} is not a file or a directory"
285
+ end
286
+ end
287
+
288
+ public
289
+
290
+ # A file of known size, read by the native side, which opens it
291
+ # before it writes anything: one that cannot be read raises and the
292
+ # archive goes on without it. :nodoc:
293
+ def add_file(name, path, stat, mode = nil, mtime = nil)
294
+ raise IOError, 'closed tar writer' unless @open
295
+
296
+ given = @native.add_file(header(name, FILE, stat.size, mode || stat.mode, mtime || stat.mtime), path)
297
+ return if given == stat.size
298
+
299
+ warn "WARNING: '#{path}' shrank from #{stat.size} to #{given} bytes while it was read; " \
300
+ "'#{name}' is made up with zeros."
301
+ end
302
+
303
+ private
304
+
305
+ def add_io(name, io, mode, mtime)
306
+ size = remaining_in(io)
307
+ return spooled(io) { |file| add_io(name, file, mode, mtime) } if size.nil?
308
+
309
+ entry(file_name(name), FILE, size, mode, mtime)
310
+ left = size
311
+ while left.positive?
312
+ chunk = io.read([left, CHUNK].min)
313
+ break if chunk.nil? || chunk.empty?
314
+
315
+ @native.write(chunk)
316
+ left -= chunk.bytesize
317
+ end
318
+ end
319
+
320
+ # What `io` has left to read, when it can say.
321
+ def remaining_in(io)
322
+ return nil unless io.respond_to?(:size) && io.respond_to?(:pos)
323
+
324
+ left = io.size - io.pos
325
+ left.negative? ? nil : left
326
+ rescue SystemCallError, IOError, NotImplementedError, TypeError
327
+ nil
328
+ end
329
+
330
+ # Copies `io` to a temporary file, to learn how much it holds.
331
+ def spooled(io)
332
+ ::Tempfile.create('ziperu-tar') do |file|
333
+ file.binmode
334
+ # Not IO.copy_stream: that wants `read(length, buffer)`, which
335
+ # a response body may not have.
336
+ while (chunk = io.read(CHUNK)) && !chunk.empty?
337
+ file.write(chunk)
338
+ end
339
+ file.rewind
340
+ yield file
341
+ end
342
+ end
343
+
344
+ # One `add_recursive`.
345
+ class Walk # :nodoc:
346
+ def initialize(writer, symlinks, exclude, progress, own)
347
+ @writer = writer
348
+ @symlinks = symlinks
349
+ @exclude = exclude
350
+ @progress = progress
351
+ @own = own
352
+ @done = 0
353
+ end
354
+
355
+ def directory(dir, prefix)
356
+ ::Dir.children(dir).sort.each do |child|
357
+ name = "#{prefix}#{child}"
358
+ next if @exclude && ::Ziperu::File::ExtractionPlan.excluded?(@exclude, name)
359
+
360
+ add(::File.join(dir, child), name)
361
+ end
362
+ end
363
+
364
+ private
365
+
366
+ def add(path, name)
367
+ stat = ::File.lstat(path)
368
+ if stat.symlink?
369
+ return warn("WARNING: skipped symlink '#{path}'.") unless @symlinks == :store
370
+
371
+ @writer.symlink(name, ::File.readlink(path), mtime: stat.mtime)
372
+ elsif stat.directory?
373
+ @writer.mkdir(name, mode: stat.mode, mtime: stat.mtime)
374
+ report(name)
375
+ return directory(path, "#{name}/")
376
+ elsif stat.file?
377
+ return if @own.include?(::File.absolute_path(path))
378
+
379
+ @writer.add_file(name, path, stat)
380
+ else
381
+ return warn("WARNING: skipped '#{path}': not a file, a directory or a link.")
382
+ end
383
+ report(name)
384
+ end
385
+
386
+ def report(name)
387
+ @done += 1
388
+ @progress&.call(name, @done, nil)
389
+ end
390
+ end
391
+ end
392
+ end
393
+ end
data/lib/ziperu/tar.rb ADDED
@@ -0,0 +1,264 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ziperu
4
+ # Reading, extracting and writing tar archives: plain, or compressed
5
+ # with gzip, zstd, xz or bzip2. When reading, which it is comes from the
6
+ # archive's first bytes, not its name.
7
+ #
8
+ # Ziperu::Tar.extract_all('release.tar.gz', 'out')
9
+ # Ziperu::Tar.create('backup.tar.zst') { |tar| tar.add_recursive('data') }
10
+ #
11
+ # Ziperu::Tar.foreach('logs.tar.zst') do |entry|
12
+ # puts "#{entry.name} (#{entry.size} bytes)"
13
+ # process(entry.read) if entry.file?
14
+ # end
15
+ #
16
+ # A tar archive has no index: it is read from front to back, one entry
17
+ # at a time, in constant memory. That is also why it can be read from a
18
+ # pipe, a socket or a response body, anything with `read`.
19
+ #
20
+ # The limit settings apply as they do to a zip (`max_entries`,
21
+ # `max_entry_size`, `max_total_size`, `max_compression_ratio`, and
22
+ # `max_archive_size` for what is read from an IO), as keywords or as
23
+ # the `Ziperu.*` globals:
24
+ #
25
+ # Ziperu::Tar.foreach(upload, **Ziperu::Limits::UPLOAD) { |entry| ... }
26
+ #
27
+ # Sparse files and multi-volume archives are not read (Ziperu::Error).
28
+ # Not in rubyzip.
29
+ module Tar
30
+ # Yields each entry of the archive at `path_or_io` (a path, or an IO
31
+ # with `read`) in turn. An entry's data can be read only inside the
32
+ # block, before the next entry is reached. Without a block, returns
33
+ # an Enumerator.
34
+ def self.foreach(path_or_io, **options, &block)
35
+ return enum_for(:foreach, path_or_io, **options) unless block
36
+
37
+ Reader.open(path_or_io, **options) { |reader| reader.each(&block) }
38
+ nil
39
+ end
40
+
41
+ # One pass over a tar archive.
42
+ #
43
+ # Ziperu::Tar::Reader.open(io) do |tar|
44
+ # tar.compression # :none, :gzip, :zstd, :xz or :bzip2
45
+ # while (entry = tar.next_entry)
46
+ # ...
47
+ # end
48
+ # end
49
+ class Reader
50
+ include Enumerable
51
+
52
+ # How much `Entry#read` asks for at a time when reading to the end.
53
+ CHUNK = 1 << 20 # :nodoc:
54
+
55
+ # With a block, yields the reader and closes it afterwards.
56
+ def self.open(path_or_io, **options)
57
+ reader = new(path_or_io, **options)
58
+ return reader unless block_given?
59
+
60
+ begin
61
+ yield reader
62
+ ensure
63
+ reader.close
64
+ end
65
+ end
66
+
67
+ # `path_or_io` is a path (a String or anything with `to_path`) or an
68
+ # IO-like object with `read(length)`; it is read forward only, and
69
+ # is not closed. The keywords are the limit settings.
70
+ def initialize(path_or_io, **options)
71
+ @settings = Settings.take!(options)
72
+ raise ArgumentError, "unknown keyword: #{options.keys.map(&:inspect).join(', ')}" unless options.empty?
73
+
74
+ ratio = @settings.max_compression_ratio&.to_f
75
+ # A Pathname has a `read` of its own, which is not an IO's.
76
+ @native =
77
+ if !path_or_io.kind_of?(Pathname) && path_or_io.respond_to?(:read)
78
+ Native::TarReader.open_io(path_or_io, ratio, @settings.max_archive_size)
79
+ else
80
+ Native::TarReader.open_path(::File.path(path_or_io), ratio, nil)
81
+ end
82
+ @count = 0
83
+ @total = 0
84
+ @current = nil
85
+ end
86
+
87
+ # How the archive is compressed: `:none`, `:gzip`, `:zstd`, `:xz` or
88
+ # `:bzip2`.
89
+ def compression
90
+ @native.compression
91
+ end
92
+
93
+ # The next entry, or nil at the end of the archive. What was left
94
+ # unread of the entry before it is skipped.
95
+ def next_entry
96
+ @current = nil
97
+ fields = @native.next_entry or return nil
98
+
99
+ @count += 1
100
+ @total += fields[:size]
101
+ check_limits(fields[:size])
102
+ @current = Entry.new(self, fields)
103
+ end
104
+
105
+ # Yields each remaining entry.
106
+ def each
107
+ return enum_for(:each) unless block_given?
108
+
109
+ while (entry = next_entry)
110
+ yield entry
111
+ end
112
+ self
113
+ end
114
+
115
+ # What the extraction drives. :nodoc:
116
+ attr_reader :native, :settings
117
+
118
+ def close
119
+ @current = nil
120
+ @native.close
121
+ nil
122
+ end
123
+
124
+ # Up to `length` bytes of `entry`'s data, or nil at its end.
125
+ def read_from(entry, length) # :nodoc:
126
+ raise IOError, "entry '#{entry.name}' can no longer be read: the archive has moved past it" \
127
+ unless entry.equal?(@current)
128
+
129
+ @native.read(length)
130
+ end
131
+
132
+ private
133
+
134
+ def check_limits(size)
135
+ if (limit = @settings.max_entries) && @count > limit
136
+ raise LimitError.new('an entry count', @count, limit)
137
+ end
138
+ if (limit = @settings.max_entry_size) && size > limit
139
+ raise LimitError.new('an entry with a declared size', size, limit)
140
+ end
141
+ return unless (limit = @settings.max_total_size) && @total > limit
142
+
143
+ raise LimitError.new('a declared total size', @total, limit)
144
+ end
145
+ end
146
+
147
+ # One entry of a tar archive: what its header says, and its data while
148
+ # the reader stands at it.
149
+ class Entry
150
+ # The entry's name as the archive has it: UTF-8 when it is valid
151
+ # UTF-8, binary otherwise. Not checked or cleaned: it may be
152
+ # absolute or hold `..`.
153
+ attr_reader :name
154
+
155
+ # Bytes of data; zero for anything but a file.
156
+ attr_reader :size
157
+
158
+ # `:file`, `:directory`, `:symlink`, `:hardlink`,
159
+ # `:character_device`, `:block_device`, `:fifo` or `:other`.
160
+ attr_reader :type
161
+
162
+ # Permission bits (the low twelve of the mode field).
163
+ attr_reader :mode
164
+
165
+ # Where a symlink or hardlink points, as the archive has it; nil for
166
+ # anything else.
167
+ attr_reader :link_target
168
+
169
+ # Owner and group as recorded. Never applied by ziperu.
170
+ attr_reader :uid, :gid, :uname, :gname
171
+
172
+ def initialize(reader, fields) # :nodoc:
173
+ @reader = reader
174
+ @name = text(fields[:name])
175
+ @size = fields[:size]
176
+ @type = fields[:type]
177
+ @mode = fields[:mode]
178
+ @mtime_sec = fields[:mtime_sec]
179
+ @mtime_nsec = fields[:mtime_nsec]
180
+ @link_target = %i[symlink hardlink].include?(@type) ? text(fields[:linkname]) : nil
181
+ @uid = fields[:uid]
182
+ @gid = fields[:gid]
183
+ @uname = text(fields[:uname])
184
+ @gname = text(fields[:gname])
185
+ @pos = 0
186
+ end
187
+
188
+ # The modification time, to the nanosecond where the archive has it.
189
+ def mtime
190
+ ::Time.at(@mtime_sec, @mtime_nsec, :nsec)
191
+ end
192
+
193
+ def file?
194
+ @type == :file
195
+ end
196
+
197
+ def directory?
198
+ @type == :directory
199
+ end
200
+
201
+ def symlink?
202
+ @type == :symlink
203
+ end
204
+
205
+ def hardlink?
206
+ @type == :hardlink
207
+ end
208
+
209
+ # As IO#read: with no `length`, the rest of the data (an empty String
210
+ # at the end); with one, up to that many bytes, or nil at the end.
211
+ # Always binary.
212
+ def read(length = nil)
213
+ return read_rest if length.nil?
214
+ raise ArgumentError, "negative length #{length} given" if length.negative?
215
+ return +'' if length.zero?
216
+
217
+ chunk = @reader.read_from(self, length)
218
+ @pos += chunk.bytesize if chunk
219
+ chunk
220
+ end
221
+
222
+ # Yields the rest of the data in pieces of at most `size` bytes.
223
+ def each_chunk(size = Reader::CHUNK)
224
+ return enum_for(:each_chunk, size) unless block_given?
225
+
226
+ while (chunk = read(size))
227
+ yield chunk
228
+ end
229
+ self
230
+ end
231
+
232
+ # Bytes of the data read so far.
233
+ attr_reader :pos
234
+
235
+ def eof?
236
+ @pos >= @size
237
+ end
238
+
239
+ def inspect
240
+ "#<#{self.class.name} #{@name.inspect} #{@type} size=#{@size}>"
241
+ end
242
+
243
+ private
244
+
245
+ def read_rest
246
+ out = +''.b
247
+ while (chunk = @reader.read_from(self, Reader::CHUNK))
248
+ @pos += chunk.bytesize
249
+ out << chunk
250
+ end
251
+ out
252
+ end
253
+
254
+ # A name from a header: bytes, which are UTF-8 if they can be.
255
+ def text(bytes)
256
+ utf8 = bytes.dup.force_encoding(Encoding::UTF_8)
257
+ utf8.valid_encoding? ? utf8 : bytes
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ require_relative 'tar/extraction'
264
+ require_relative 'tar/writer'
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ziperu
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.0'
5
5
  # The rubyzip API this gem tracks.
6
6
  RUBYZIP_COMPAT_VERSION = '3.7'
7
7
  end
data/lib/ziperu.rb CHANGED
@@ -56,6 +56,7 @@ require_relative "ziperu/crypto"
56
56
  require_relative "ziperu/null_input_stream"
57
57
  require_relative "ziperu/input_stream"
58
58
  require_relative "ziperu/output_stream"
59
+ require_relative "ziperu/link_safety"
59
60
  require_relative "ziperu/entry"
60
61
  require_relative "ziperu/entry_set"
61
62
  require_relative "ziperu/streamable_stream"
@@ -72,6 +73,7 @@ require_relative "ziperu/file_split"
72
73
  require_relative "ziperu/file"
73
74
  require_relative "ziperu/verify_report"
74
75
  require_relative "ziperu/limits"
76
+ require_relative "ziperu/tar"
75
77
  require_relative "ziperu/size_estimator"
76
78
 
77
79
  # Drop-in replacement for rubyzip, backed by a Rust core.