ziperu 0.2.3-x86_64-linux-gnu → 0.3.1-x86_64-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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +190 -0
- data/README.md +35 -7
- data/lib/zip.rb +3 -3
- data/lib/ziperu/3.1/ziperu.so +0 -0
- data/lib/ziperu/3.2/ziperu.so +0 -0
- data/lib/ziperu/3.3/ziperu.so +0 -0
- data/lib/ziperu/3.4/ziperu.so +0 -0
- data/lib/ziperu/4.0/ziperu.so +0 -0
- data/lib/ziperu/central_directory.rb +4 -8
- data/lib/ziperu/constants.rb +8 -0
- data/lib/ziperu/crypto.rb +10 -2
- data/lib/ziperu/dos_time.rb +1 -1
- data/lib/ziperu/entry/source.rb +41 -6
- data/lib/ziperu/entry.rb +252 -112
- data/lib/ziperu/entry_set.rb +0 -5
- data/lib/ziperu/errors.rb +4 -2
- data/lib/ziperu/extra_field/zip64.rb +1 -3
- data/lib/ziperu/extra_field.rb +10 -10
- data/lib/ziperu/file/commit.rb +255 -0
- data/lib/ziperu/file/extraction.rb +285 -0
- data/lib/ziperu/file.rb +77 -405
- data/lib/ziperu/file_split.rb +1 -3
- data/lib/ziperu/filesystem/dir.rb +3 -9
- data/lib/ziperu/filesystem/file.rb +2 -6
- data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
- data/lib/ziperu/input_stream.rb +30 -32
- data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
- data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
- data/lib/ziperu/ioextras/fake_io.rb +1 -3
- data/lib/ziperu/ioextras.rb +6 -0
- data/lib/ziperu/limits.rb +48 -0
- data/lib/ziperu/null_input_stream.rb +1 -1
- data/lib/ziperu/output_stream.rb +118 -31
- data/lib/ziperu/pass_thru_decompressor.rb +1 -3
- data/lib/ziperu/rails_streaming.rb +5 -5
- data/lib/ziperu/remote_io.rb +181 -32
- data/lib/ziperu/size_estimator.rb +27 -19
- data/lib/ziperu/verify_report.rb +62 -0
- data/lib/ziperu/version.rb +2 -2
- data/lib/ziperu.rb +19 -17
- metadata +7 -2
|
@@ -42,9 +42,7 @@ module Ziperu
|
|
|
42
42
|
def parse(original_size, compressed_size, relative_header_offset = nil, disk_start_number = nil)
|
|
43
43
|
@original_size = extract(8, 'Q<') if original_size == 0xFFFFFFFF
|
|
44
44
|
@compressed_size = extract(8, 'Q<') if compressed_size == 0xFFFFFFFF
|
|
45
|
-
if relative_header_offset && relative_header_offset == 0xFFFFFFFF
|
|
46
|
-
@relative_header_offset = extract(8, 'Q<')
|
|
47
|
-
end
|
|
45
|
+
@relative_header_offset = extract(8, 'Q<') if relative_header_offset && relative_header_offset == 0xFFFFFFFF
|
|
48
46
|
@disk_start_number = extract(4, 'V') if disk_start_number && disk_start_number == 0xFFFF
|
|
49
47
|
@content = nil
|
|
50
48
|
[@original_size || original_size,
|
data/lib/ziperu/extra_field.rb
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
module Ziperu
|
|
6
6
|
class ExtraField < Hash # :nodoc:all
|
|
7
|
-
ID_MAP = {}
|
|
7
|
+
ID_MAP = {} # rubocop:disable Style/MutableConstant -- the registry the field classes add themselves to
|
|
8
8
|
|
|
9
9
|
def initialize(binstr = nil, local: false)
|
|
10
10
|
merge(binstr, local: local) if binstr
|
|
@@ -95,15 +95,15 @@ module Ziperu
|
|
|
95
95
|
end
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
98
|
+
require_relative 'extra_field/unknown'
|
|
99
|
+
require_relative 'extra_field/generic'
|
|
100
|
+
require_relative 'extra_field/universal_time'
|
|
101
|
+
require_relative 'extra_field/unicode_path'
|
|
102
|
+
require_relative 'extra_field/old_unix'
|
|
103
|
+
require_relative 'extra_field/unix'
|
|
104
|
+
require_relative 'extra_field/zip64'
|
|
105
|
+
require_relative 'extra_field/ntfs'
|
|
106
|
+
require_relative 'extra_field/aes'
|
|
107
107
|
|
|
108
108
|
# Copyright (C) 2002, 2003 Thomas Sondergaard
|
|
109
109
|
# rubyzip is free software; you can redistribute it and/or
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ziperu
|
|
4
|
+
# Committing: writing the archive out with its changes, by appending in
|
|
5
|
+
# place, rewriting beside it and renaming over it, or to another path or
|
|
6
|
+
# an IO.
|
|
7
|
+
class File
|
|
8
|
+
# Commits changes made since the previous commit to the zip archive: a
|
|
9
|
+
# new archive is written beside the old one and renamed over it.
|
|
10
|
+
def commit
|
|
11
|
+
return if name.kind_of?(StringIO) || !commit_required?
|
|
12
|
+
|
|
13
|
+
appended = write_changes
|
|
14
|
+
# Only now are the changes on disk; until here the entries keep
|
|
15
|
+
# their dirty state so a failed commit can be retried.
|
|
16
|
+
@cdir.each(&:clean_up)
|
|
17
|
+
if @write_to
|
|
18
|
+
# From here on this object works on the archive just written:
|
|
19
|
+
# what it holds now includes the changes, and a later commit
|
|
20
|
+
# updates it rather than starting again from the original (which
|
|
21
|
+
# would drop what this commit wrote).
|
|
22
|
+
@name = @write_to
|
|
23
|
+
@write_to = nil
|
|
24
|
+
end
|
|
25
|
+
initialize_cdir(@name) unless appended && refresh_after_append(appended)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Writes the archive out with its changes. Returns the entries added
|
|
29
|
+
# when they were appended in place, nil when the archive was written
|
|
30
|
+
# whole.
|
|
31
|
+
def write_changes
|
|
32
|
+
if @write_to
|
|
33
|
+
# The changes go to a new archive; the one opened is not touched,
|
|
34
|
+
# and keeps being what this object reads. It is written beside
|
|
35
|
+
# its path and renamed into place once complete, so a failure
|
|
36
|
+
# leaves whatever was at the path as it was.
|
|
37
|
+
dirname, basename = ::File.split(@write_to)
|
|
38
|
+
::Dir::Tmpname.create(basename, dirname) do |tmp_filename|
|
|
39
|
+
write_all_entries(tmp_filename)
|
|
40
|
+
::File.rename(tmp_filename, @write_to)
|
|
41
|
+
ensure
|
|
42
|
+
FileUtils.rm_f(tmp_filename)
|
|
43
|
+
end
|
|
44
|
+
nil
|
|
45
|
+
elsif (appended = appended_entries)
|
|
46
|
+
# Only additions: they go after the entries already there, which
|
|
47
|
+
# are left untouched (see Ziperu.append_in_place). A failure puts
|
|
48
|
+
# the archive back as it was.
|
|
49
|
+
::Ziperu::OutputStream.open(@name, append: true, **stream_options) do |zos|
|
|
50
|
+
zos.put_entries(appended, threads: @threads)
|
|
51
|
+
end
|
|
52
|
+
appended
|
|
53
|
+
else
|
|
54
|
+
learn_raw_copy_losses
|
|
55
|
+
on_success_replace do |tmp_file|
|
|
56
|
+
write_all_entries(tmp_file)
|
|
57
|
+
true
|
|
58
|
+
end
|
|
59
|
+
nil
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
private :write_changes
|
|
63
|
+
|
|
64
|
+
def write_all_entries(target)
|
|
65
|
+
::Ziperu::OutputStream.open(target, **stream_options) do |zos|
|
|
66
|
+
zos.put_entries(@cdir.writable_entries, threads: @threads)
|
|
67
|
+
zos.comment = comment
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
private :write_all_entries
|
|
71
|
+
|
|
72
|
+
# Before a rewrite: which entries a raw copy would lose fields of, asked
|
|
73
|
+
# of the native core for all of them at once rather than by reading
|
|
74
|
+
# each one's local header into Ruby (see Entry#loses_fields_on_raw_copy?).
|
|
75
|
+
def learn_raw_copy_losses
|
|
76
|
+
return unless @native
|
|
77
|
+
|
|
78
|
+
candidates = @cdir.entries.select do |e|
|
|
79
|
+
e.index && !e.pending_data? && e.backed_by?(@native) && e.restreamable?
|
|
80
|
+
end
|
|
81
|
+
return if candidates.empty?
|
|
82
|
+
|
|
83
|
+
lossy = @native.indices_with_extra_fields(candidates.map(&:index), Entry::RAW_COPY_LOSES_IDS)
|
|
84
|
+
lossy = lossy.to_h { |i| [i, true] }
|
|
85
|
+
candidates.each { |e| e.raw_copy_loses = lossy.key?(e.index) }
|
|
86
|
+
rescue Error
|
|
87
|
+
# Only a shortcut: should the archive no longer read as it did (the
|
|
88
|
+
# file changed under it, say), each entry is asked the long way.
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
private :learn_raw_copy_losses
|
|
92
|
+
|
|
93
|
+
# After an append: the entries already there are where they were, so
|
|
94
|
+
# they are kept (pointed at the archive reopened) and only the new
|
|
95
|
+
# records are read, rather than every entry built again. False, with
|
|
96
|
+
# nothing changed, when the archive does not hold what was expected;
|
|
97
|
+
# `initialize_cdir` then reads it all.
|
|
98
|
+
def refresh_after_append(appended)
|
|
99
|
+
held = @native.entry_count
|
|
100
|
+
native = InputStream.open_archive(@name, @settings)
|
|
101
|
+
unless native.entry_count == held + appended.size && !native.duplicate_names?
|
|
102
|
+
native.close
|
|
103
|
+
return false
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
fresh = native.entry_metas(held, appended.size).map do |meta|
|
|
107
|
+
Entry.from_meta(native, meta, self, metadata_encoding: @metadata_encoding, settings: @settings)
|
|
108
|
+
end
|
|
109
|
+
unless fresh.map { |e| e.name.b } == appended.map { |e| e.name.b }
|
|
110
|
+
native.close
|
|
111
|
+
return false
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
kept = @cdir.entries.first(held)
|
|
115
|
+
kept.each { |e| e.rebind_native(native) }
|
|
116
|
+
fresh.each { |e| @cdir << e }
|
|
117
|
+
@native.close
|
|
118
|
+
@native = native
|
|
119
|
+
@dirty = false
|
|
120
|
+
@file_permissions = ::File.stat(@name).mode
|
|
121
|
+
true
|
|
122
|
+
end
|
|
123
|
+
private :refresh_after_append
|
|
124
|
+
|
|
125
|
+
# The entries to append, when the pending changes are additions and
|
|
126
|
+
# nothing else: every entry the archive holds is still listed, in
|
|
127
|
+
# place, unchanged, and the comment is as it was. Nil otherwise, and
|
|
128
|
+
# the archive is rewritten.
|
|
129
|
+
def appended_entries
|
|
130
|
+
return nil if @create || @buffer || @native.nil? || @comment_changed || @settings.sort_entries
|
|
131
|
+
return nil unless @settings.append_in_place && appendable_in_place?
|
|
132
|
+
|
|
133
|
+
all = @cdir.entries
|
|
134
|
+
held = @native.entry_count
|
|
135
|
+
# The local headers there stay as they are; the crate writes the
|
|
136
|
+
# central directory again, which loses some fields, so those decide
|
|
137
|
+
# (without reading every local header, as `needs_restream?` would).
|
|
138
|
+
return nil if all.size <= held || all.first(held).each_with_index.any? do |e, i|
|
|
139
|
+
!(e.index == i && e.backed_by?(@native) && !e.dirty? && !e.changed? &&
|
|
140
|
+
!e.central_fields_lost_on_append? && !@cdir.shadowed?(e))
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
all.drop(held)
|
|
144
|
+
end
|
|
145
|
+
private :appended_entries
|
|
146
|
+
|
|
147
|
+
# Whether the archive's file can take an append: one that is read-only
|
|
148
|
+
# can still be replaced (the rewrite renames a new file over it), and
|
|
149
|
+
# one with other hard links must be, or the append would change what
|
|
150
|
+
# they hold too.
|
|
151
|
+
def appendable_in_place?
|
|
152
|
+
stat = ::File.stat(@name)
|
|
153
|
+
stat.file? && stat.writable? && stat.nlink <= 1
|
|
154
|
+
rescue SystemCallError
|
|
155
|
+
false
|
|
156
|
+
end
|
|
157
|
+
private :appendable_in_place?
|
|
158
|
+
|
|
159
|
+
# Writes the archive, with any changes, to `io` and returns it. `io`
|
|
160
|
+
# may be a pipe, socket or response body: entries are streamed to it
|
|
161
|
+
# as they are written, unless the archive holds ZipCrypto entries,
|
|
162
|
+
# whose headers are patched at the end; such an archive is built in
|
|
163
|
+
# memory first.
|
|
164
|
+
def write_buffer(io = ::StringIO.new)
|
|
165
|
+
return io unless commit_required?
|
|
166
|
+
|
|
167
|
+
# Rewriting the archive into the very IO it is read from would
|
|
168
|
+
# overwrite entries still to be copied; build it aside first.
|
|
169
|
+
if @buffer && io.equal?(@io) && !io.kind_of?(::StringIO)
|
|
170
|
+
built = write_buffer(::StringIO.new).string
|
|
171
|
+
io.rewind
|
|
172
|
+
io.write(built)
|
|
173
|
+
io.truncate(built.bytesize) if io.respond_to?(:truncate)
|
|
174
|
+
return io
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# ZipCrypto entries are copied with patched headers, which a stream
|
|
178
|
+
# (a pipe, say) cannot take: such an archive is built in memory first.
|
|
179
|
+
memory = @cdir.writable_entries.any?(&:zipcrypto?)
|
|
180
|
+
::Ziperu::OutputStream.write_buffer(io, memory: memory, **stream_options) do |zos|
|
|
181
|
+
@cdir.writable_entries.each { |e| e.write_to_zip_output_stream(zos) }
|
|
182
|
+
zos.comment = comment
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Returns true if any changes has been made to this archive since the
|
|
187
|
+
# previous commit
|
|
188
|
+
def commit_required?
|
|
189
|
+
@create || @dirty || @cdir.any?(&:dirty?)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
private
|
|
193
|
+
|
|
194
|
+
# Windows refuses to replace a file that is still open. A stream the
|
|
195
|
+
# caller forgot to close holds its own handle until the GC frees it, so
|
|
196
|
+
# on failure collect garbage once and try again.
|
|
197
|
+
def rename_over_original(tmp_filename)
|
|
198
|
+
::File.rename(tmp_filename, name)
|
|
199
|
+
rescue Errno::EACCES
|
|
200
|
+
raise unless ::Ziperu::RUNNING_ON_WINDOWS
|
|
201
|
+
|
|
202
|
+
GC.start
|
|
203
|
+
begin
|
|
204
|
+
::File.rename(tmp_filename, name)
|
|
205
|
+
rescue Errno::EACCES
|
|
206
|
+
# A directory (or anything but a file) at the path is not ours to
|
|
207
|
+
# move: the commit fails, as rubyzip's does.
|
|
208
|
+
raise unless ::File.file?(name)
|
|
209
|
+
|
|
210
|
+
# Something else (another Ziperu::File, say) still has the archive
|
|
211
|
+
# open. Renaming an open file is allowed where replacing one is
|
|
212
|
+
# not, so move the old archive aside first; that handle keeps
|
|
213
|
+
# reading the old bytes, as it would on Unix.
|
|
214
|
+
aside = "#{name}.#{Process.pid}.#{rand(1 << 32).to_s(36)}.old"
|
|
215
|
+
::File.rename(name, aside)
|
|
216
|
+
begin
|
|
217
|
+
::File.rename(tmp_filename, name)
|
|
218
|
+
rescue SystemCallError => e
|
|
219
|
+
# The original goes back, so a failed commit leaves an archive
|
|
220
|
+
# at the path.
|
|
221
|
+
begin
|
|
222
|
+
::File.rename(aside, name)
|
|
223
|
+
rescue SystemCallError
|
|
224
|
+
raise Error, "could not replace #{name} (#{e.message}), nor put it back: the original is at #{aside}"
|
|
225
|
+
end
|
|
226
|
+
raise
|
|
227
|
+
end
|
|
228
|
+
begin
|
|
229
|
+
::File.delete(aside)
|
|
230
|
+
rescue SystemCallError
|
|
231
|
+
warn "WARNING: could not remove '#{aside}' (still open elsewhere); delete it once it is closed."
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# What every stream this archive is written through is opened with.
|
|
237
|
+
def stream_options
|
|
238
|
+
{ suppress_extra_fields: @suppress_extra_fields, **Settings.overrides(@settings) }
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def on_success_replace
|
|
242
|
+
dirname, basename = ::File.split(name)
|
|
243
|
+
::Dir::Tmpname.create(basename, dirname) do |tmp_filename|
|
|
244
|
+
if yield tmp_filename
|
|
245
|
+
# The native handle must be gone before the rename on Windows.
|
|
246
|
+
release_native
|
|
247
|
+
rename_over_original(tmp_filename)
|
|
248
|
+
::File.chmod(@file_permissions, name) unless @create
|
|
249
|
+
end
|
|
250
|
+
ensure
|
|
251
|
+
FileUtils.rm_f(tmp_filename)
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ziperu
|
|
4
|
+
# Extraction: `extract`, `extract_all`, and the native fast path that
|
|
5
|
+
# `extract_all` hands most entries to.
|
|
6
|
+
class File
|
|
7
|
+
# Entries handed to the native extractor per call, so an interrupt is
|
|
8
|
+
# noticed between calls rather than after the whole archive.
|
|
9
|
+
NATIVE_EXTRACT_BATCH = 256
|
|
10
|
+
private_constant :NATIVE_EXTRACT_BATCH
|
|
11
|
+
|
|
12
|
+
# Extracts `entry` to a file at `entry_path`, with
|
|
13
|
+
# `destination_directory` as the base. The block (or Ziperu.on_exists_proc)
|
|
14
|
+
# decides whether an existing file is overwritten.
|
|
15
|
+
def extract(entry, entry_path = nil, destination_directory: '.', symlinks: :skip, on_exists: nil,
|
|
16
|
+
&block)
|
|
17
|
+
found_entry = get_entry(entry)
|
|
18
|
+
entry_path ||= found_entry.name
|
|
19
|
+
found_entry.extract(entry_path, destination_directory: destination_directory,
|
|
20
|
+
symlinks: symlinks, on_exists: on_exists, &block)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Extracts every entry in the archive into `destination_directory`,
|
|
24
|
+
# preserving the archive's directory structure. Symlink entries are
|
|
25
|
+
# skipped with a warning; see `Entry#extract` for the path-safety checks
|
|
26
|
+
# applied to every other entry.
|
|
27
|
+
#
|
|
28
|
+
# `symlinks:` is `:skip` (the default, with a warning per link, as in
|
|
29
|
+
# rubyzip 3) or `:create`, which creates links whose target stays inside
|
|
30
|
+
# `destination_directory`. `progress:` is called with
|
|
31
|
+
# `(entry, done, total)` after each entry.
|
|
32
|
+
# `threads: n` extracts the files on that many threads (the archive is
|
|
33
|
+
# read through one handle per thread); a file or String archive only,
|
|
34
|
+
# since an IO is driven from Ruby. Directories are made first, links
|
|
35
|
+
# last, as before. An error is raised for the first entry (in archive
|
|
36
|
+
# order) that failed, once every thread has stopped.
|
|
37
|
+
#
|
|
38
|
+
# The policies `unzip` has: `on_exists:` is `:raise` (the default),
|
|
39
|
+
# `:overwrite` (`unzip -o`) or `:skip` (`unzip -n`) for a path already
|
|
40
|
+
# taken, where rubyzip only offers a block; `exclude:` lists globs (or
|
|
41
|
+
# Regexps) of entry names to leave out (`unzip -x`), matched against
|
|
42
|
+
# the whole name, so `"*.log"` matches at any depth; `junk_paths: true`
|
|
43
|
+
# drops the directories and puts every file in `destination_directory`
|
|
44
|
+
# itself (`unzip -j`); `sanitize: :windows` rewrites names that Windows
|
|
45
|
+
# refuses (`<>:"|?*`, control characters, a trailing dot or space,
|
|
46
|
+
# `CON`, `NUL` and the like) so the archive extracts there at all.
|
|
47
|
+
EXTRACT_ALL_OPTIONS = %i[symlinks progress threads on_exists exclude junk_paths sanitize].freeze # :nodoc:
|
|
48
|
+
|
|
49
|
+
def extract_all(destination_directory = '.', symlinks: :skip, progress: nil, threads: 1,
|
|
50
|
+
on_exists: nil, exclude: nil, junk_paths: false, sanitize: nil, &block)
|
|
51
|
+
targets = ExtractionPlan.new(@cdir.writable_entries, exclude: exclude, junk_paths: junk_paths,
|
|
52
|
+
sanitize: sanitize).targets
|
|
53
|
+
report = progress_reporter(progress, targets.size)
|
|
54
|
+
|
|
55
|
+
# With no block to consult about paths already taken (the policy
|
|
56
|
+
# says, or Ziperu.on_exists_proc), the native core extracts every
|
|
57
|
+
# qualifying entry in one call; the rest take the per-entry path.
|
|
58
|
+
policy = on_exists || (@settings.on_exists_proc ? :overwrite : :raise)
|
|
59
|
+
slow = if block.nil? && !@restore_ownership
|
|
60
|
+
extract_all_native(targets, destination_directory, report, threads, policy)
|
|
61
|
+
else
|
|
62
|
+
targets
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
slow.each do |entry, target|
|
|
66
|
+
if entry.symlink? && symlinks != :create
|
|
67
|
+
warn "WARNING: skipped symlink '#{entry.name}' during extract_all."
|
|
68
|
+
else
|
|
69
|
+
entry.extract(target, destination_directory: destination_directory,
|
|
70
|
+
create_parent_directories: true,
|
|
71
|
+
symlinks: symlinks,
|
|
72
|
+
on_exists: on_exists, &block)
|
|
73
|
+
end
|
|
74
|
+
report.call(entry)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
self
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# What `extract_all(destination_directory, **options)` would do, entry
|
|
81
|
+
# by entry, without writing anything: one ExtractionPlan::Item per
|
|
82
|
+
# entry, in the order they would be extracted. Takes the options
|
|
83
|
+
# `extract_all` takes that change the outcome (`symlinks:`,
|
|
84
|
+
# `exclude:`, `junk_paths:`, `sanitize:`):
|
|
85
|
+
#
|
|
86
|
+
# plan = zf.extraction_plan("out", sanitize: :windows)
|
|
87
|
+
# plan.reject(&:extract?).each { |item| puts "#{item.entry.name}: #{item.status}" }
|
|
88
|
+
# plan.select(&:collides_with).map(&:path) # names that clash once sanitized
|
|
89
|
+
# plan.sum(&:declared_size) # declared bytes to be written
|
|
90
|
+
#
|
|
91
|
+
# The disk is only looked at: whether something is already at each
|
|
92
|
+
# path, and whether a link is on the way. Not in rubyzip.
|
|
93
|
+
def extraction_plan(destination_directory = '.', symlinks: :skip, exclude: nil, junk_paths: false,
|
|
94
|
+
sanitize: nil)
|
|
95
|
+
ExtractionPlan.new(@cdir.writable_entries, exclude: exclude, junk_paths: junk_paths, sanitize: sanitize)
|
|
96
|
+
.preview(destination_directory, symlinks: symlinks)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Which entries an `extract_all` writes, in what order, and to which
|
|
100
|
+
# name under the destination: decided from the archive and the options
|
|
101
|
+
# alone, before anything touches the disk.
|
|
102
|
+
class ExtractionPlan # :nodoc:
|
|
103
|
+
# One entry of `File#extraction_plan`:
|
|
104
|
+
#
|
|
105
|
+
# - `entry`, and `name`, the path under the destination it goes to
|
|
106
|
+
# (after `junk_paths:` and `sanitize:`); `path`, the absolute one;
|
|
107
|
+
# - `kind`: `:file`, `:directory` or `:symlink`; `declared_size`:
|
|
108
|
+
# the bytes the entry says it holds (0 for a directory or link);
|
|
109
|
+
# - `status`: `:extract`, `:unsafe` (it resolves outside the
|
|
110
|
+
# destination), `:through_link` (a link on disk is on the way, at
|
|
111
|
+
# `link`), `:symlink_skipped` (without `symlinks: :create`) or
|
|
112
|
+
# `:symlink_refused` (its target is not allowed, `reason` says why);
|
|
113
|
+
# - `exists`: something is already at `path` (`on_exists:` decides);
|
|
114
|
+
# - `collides_with`: an earlier entry that goes to the same path (on
|
|
115
|
+
# Windows, the same path in any case), or nil.
|
|
116
|
+
Item = Struct.new(:entry, :name, :path, :kind, :declared_size, :status, :exists, :collides_with, :link, :reason,
|
|
117
|
+
keyword_init: true) do
|
|
118
|
+
def extract?
|
|
119
|
+
status == :extract
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# `entries` are the archive's writable entries (of several records
|
|
124
|
+
# under one name only the last, the one rubyzip would know).
|
|
125
|
+
def initialize(entries, exclude: nil, junk_paths: false, sanitize: nil)
|
|
126
|
+
unless [nil, false, :windows].include?(sanitize)
|
|
127
|
+
raise ArgumentError, "sanitize: takes :windows, not #{sanitize.inspect}"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
@entries = entries
|
|
131
|
+
@exclude = exclude
|
|
132
|
+
@junk_paths = junk_paths
|
|
133
|
+
@sanitize = sanitize
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# `[entry, target]` pairs. Directories first, so files have
|
|
137
|
+
# somewhere to go; links last, so nothing is written through one.
|
|
138
|
+
def targets
|
|
139
|
+
dirs, rest = @entries.partition(&:directory?)
|
|
140
|
+
links, others = rest.partition(&:symlink?)
|
|
141
|
+
ordered = dirs + others + links
|
|
142
|
+
ordered = ordered.reject { |e| excluded?(e.name) } if @exclude
|
|
143
|
+
ordered = ordered.reject(&:directory?) if @junk_paths
|
|
144
|
+
ordered.map { |e| [e, target_for(e.name)] }
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The Items for extracting under `destination_directory`.
|
|
148
|
+
def preview(destination_directory, symlinks: :skip)
|
|
149
|
+
dest = ::File.absolute_path(destination_directory || '.')
|
|
150
|
+
seen = {}
|
|
151
|
+
targets.map do |entry, name|
|
|
152
|
+
path = ::File.absolute_path(::File.join(dest, name))
|
|
153
|
+
kind = kind_of(entry)
|
|
154
|
+
item = Item.new(entry: entry, name: name, path: path, kind: kind,
|
|
155
|
+
declared_size: kind == :file ? entry.size.to_i : 0)
|
|
156
|
+
item.status = status_of(item, dest, symlinks)
|
|
157
|
+
item.exists = ::File.symlink?(path) || ::File.exist?(path)
|
|
158
|
+
key = ::Ziperu::RUNNING_ON_WINDOWS ? path.downcase : path
|
|
159
|
+
item.collides_with = seen[key]&.entry unless kind == :directory && seen[key]&.kind == :directory
|
|
160
|
+
seen[key] ||= item
|
|
161
|
+
item
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
private
|
|
166
|
+
|
|
167
|
+
def kind_of(entry)
|
|
168
|
+
if entry.directory? then :directory
|
|
169
|
+
elsif entry.symlink? then :symlink
|
|
170
|
+
else :file
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# What `extract_all` would do with the item; fills in `link` or
|
|
175
|
+
# `reason` where they apply.
|
|
176
|
+
def status_of(item, dest, symlinks)
|
|
177
|
+
inside = item.path.start_with?(dest + ::File::SEPARATOR) || dest == ::File::SEPARATOR
|
|
178
|
+
return :unsafe unless inside
|
|
179
|
+
|
|
180
|
+
entry = item.entry
|
|
181
|
+
return :through_link if (item.link = entry.link_on_the_way(dest, item.path, leaf: item.kind != :symlink))
|
|
182
|
+
return :extract unless item.kind == :symlink
|
|
183
|
+
return :symlink_skipped unless symlinks == :create
|
|
184
|
+
|
|
185
|
+
item.reason = entry.symlink_refusal(entry.symlink_target, item.path, dest)
|
|
186
|
+
item.reason ? :symlink_refused : :extract
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Whether `name` matches one of the `exclude:` patterns: a glob
|
|
190
|
+
# (`File.fnmatch` with `*` crossing `/`, as `unzip -x` has it) or a
|
|
191
|
+
# Regexp.
|
|
192
|
+
def excluded?(name)
|
|
193
|
+
Array(@exclude).any? do |pattern|
|
|
194
|
+
case pattern
|
|
195
|
+
when ::Regexp then pattern.match?(name)
|
|
196
|
+
else ::File.fnmatch?(pattern.to_s, name, ::File::FNM_EXTGLOB | ::File::FNM_DOTMATCH)
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# The path under the destination an entry goes to.
|
|
202
|
+
def target_for(name)
|
|
203
|
+
name = ::File.basename(name) if @junk_paths
|
|
204
|
+
@sanitize == :windows ? ::Ziperu::File.sanitize_windows_name(name) : name
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
WINDOWS_RESERVED_NAMES = /\A(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|\z)/i # :nodoc:
|
|
209
|
+
|
|
210
|
+
# `name` as Windows will take it: in each component the characters it
|
|
211
|
+
# refuses (`<>:"|?*` and controls) become `_`, a trailing dot or space
|
|
212
|
+
# goes, and a reserved device name gets a leading `_`.
|
|
213
|
+
# Not in rubyzip.
|
|
214
|
+
def self.sanitize_windows_name(name)
|
|
215
|
+
trailing = name.end_with?('/') ? '/' : ''
|
|
216
|
+
parts = name.split('/').map do |part|
|
|
217
|
+
part = part.gsub(/[<>:"|?*\x00-\x1f]/, '_').sub(/[. ]+\z/, '')
|
|
218
|
+
part = "_#{part}" if part.match?(WINDOWS_RESERVED_NAMES)
|
|
219
|
+
part.empty? ? '_' : part
|
|
220
|
+
end
|
|
221
|
+
parts.join('/') + trailing
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
private
|
|
225
|
+
|
|
226
|
+
# Entries the native core cannot take are returned for the Ruby loop:
|
|
227
|
+
# symlinks (skipped there with a warning), encrypted entries, entries
|
|
228
|
+
# not read from this archive, and names it cannot represent.
|
|
229
|
+
def extract_all_native(targets, destination_directory, report, threads, on_exists)
|
|
230
|
+
native = native_archive
|
|
231
|
+
dest = ::File.absolute_path(destination_directory || '.')
|
|
232
|
+
code = Entry.on_exists_code(on_exists)
|
|
233
|
+
slow = []
|
|
234
|
+
specs = []
|
|
235
|
+
by_index = {}
|
|
236
|
+
targets.each do |entry, target|
|
|
237
|
+
if native_extractable?(entry, native)
|
|
238
|
+
specs << native_extract_spec(entry, target)
|
|
239
|
+
by_index[entry.index] = entry
|
|
240
|
+
else
|
|
241
|
+
slow << [entry, target]
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
specs.each_slice(NATIVE_EXTRACT_BATCH) do |batch|
|
|
246
|
+
native.extract_entries(dest, batch, @settings.validate_entry_sizes, threads, code).each do |index, kind, path|
|
|
247
|
+
entry = by_index[index]
|
|
248
|
+
case kind
|
|
249
|
+
when :unsafe
|
|
250
|
+
warn "WARNING: skipped extracting '#{entry.name}' to '#{path}' as unsafe."
|
|
251
|
+
when :link
|
|
252
|
+
warn "WARNING: skipped extracting '#{entry.name}' through the link '#{path}'."
|
|
253
|
+
when :oversize
|
|
254
|
+
warn "WARNING: #{EntrySizeError.new(entry).message}"
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
batch.each { |spec| report.call(spec[0]) }
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
slow
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def native_extractable?(entry, native)
|
|
264
|
+
return false unless entry.instance_of?(::Ziperu::Entry) && entry.index && entry.backed_by?(native)
|
|
265
|
+
# An encrypted entry goes through the Ruby path, which has the password.
|
|
266
|
+
return false if entry.encrypted?
|
|
267
|
+
# So does one in a method that cannot be read, for the error rubyzip
|
|
268
|
+
# raises (CompressionMethodError).
|
|
269
|
+
return false unless READABLE_COMPRESSION_METHODS.include?(entry.compression_method)
|
|
270
|
+
return false unless entry.file? || entry.directory?
|
|
271
|
+
|
|
272
|
+
IOExtras.native_text?(entry.name)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# What `Entry#extract` would do for the entry, as plain values:
|
|
276
|
+
# [entry, index, name, mtime seconds, mtime nanoseconds, unix mode, directory?].
|
|
277
|
+
def native_extract_spec(entry, target)
|
|
278
|
+
mtime = entry.time if @restore_times
|
|
279
|
+
if @restore_permissions && entry.fstype == FSTYPE_UNIX && entry.unix_perms
|
|
280
|
+
mode = entry.unix_perms & Entry::SAFE_PERMISSION_BITS
|
|
281
|
+
end
|
|
282
|
+
[entry, entry.index, target, mtime&.to_i, mtime&.nsec, mode, entry.directory?]
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
end
|