ziperu 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +117 -0
- data/Cargo.lock +4 -1
- data/README.md +85 -44
- data/ext/ziperu/Cargo.toml +8 -1
- data/ext/ziperu/src/errors.rs +38 -0
- data/ext/ziperu/src/extract.rs +87 -699
- data/ext/ziperu/src/lib.rs +25 -0
- data/ext/ziperu/src/placer.rs +759 -0
- data/ext/ziperu/src/stream.rs +7 -1
- data/ext/ziperu/src/tar.rs +1454 -0
- data/ext/ziperu/src/tar_writer.rs +736 -0
- data/lib/ziperu/entry/source.rb +41 -6
- data/lib/ziperu/entry.rb +34 -46
- data/lib/ziperu/file/extraction.rb +16 -6
- data/lib/ziperu/file.rb +27 -8
- data/lib/ziperu/input_stream.rb +2 -0
- data/lib/ziperu/ioextras.rb +7 -0
- data/lib/ziperu/link_safety.rb +43 -0
- data/lib/ziperu/tar/extraction.rb +399 -0
- data/lib/ziperu/tar/writer.rb +393 -0
- data/lib/ziperu/tar.rb +264 -0
- data/lib/ziperu/version.rb +1 -1
- data/lib/ziperu.rb +2 -0
- metadata +8 -1
data/lib/ziperu/entry/source.rb
CHANGED
|
@@ -52,6 +52,11 @@ module Ziperu
|
|
|
52
52
|
stored&.native(offset)
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
+
# The password its owner was given for the archive, or nil.
|
|
56
|
+
def password
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
|
|
55
60
|
# The record's local-header extra fields, as stored.
|
|
56
61
|
def local_extra(offset)
|
|
57
62
|
stored.local_extra(offset)
|
|
@@ -69,8 +74,9 @@ module Ziperu
|
|
|
69
74
|
|
|
70
75
|
NONE = None.new.freeze
|
|
71
76
|
|
|
72
|
-
# A record of `native`, the archive `owner`
|
|
73
|
-
# for an entry
|
|
77
|
+
# A record of `native`, the archive `owner` has open: a Ziperu::File,
|
|
78
|
+
# a Reopener for an entry `File.foreach` yielded from a path, or nil
|
|
79
|
+
# for an entry read through an InputStream. `index` is nil
|
|
74
80
|
# for a record the native core cannot address by index (one of
|
|
75
81
|
# several under the same name), which is found by its offset.
|
|
76
82
|
class Archive
|
|
@@ -89,11 +95,14 @@ module Ziperu
|
|
|
89
95
|
self
|
|
90
96
|
end
|
|
91
97
|
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
def password
|
|
99
|
+
@owner.password if @owner.respond_to?(:password)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The native archive, reopened if its owner released it on close:
|
|
103
|
+
# rubyzip lets entries be read after `close`.
|
|
94
104
|
def native(offset)
|
|
95
|
-
if @native.closed? && @owner.
|
|
96
|
-
reopened = @owner.native_archive
|
|
105
|
+
if @native.closed? && (reopened = (@owner.native_archive if @owner.respond_to?(:native_archive)))
|
|
97
106
|
unless still_at_place?(reopened, offset)
|
|
98
107
|
where = @index ? "at index #{@index} of" : 'in'
|
|
99
108
|
raise EntryMovedError, "entry '#{@raw_name}' is no longer #{where} #{@owner.name}"
|
|
@@ -132,6 +141,32 @@ module Ziperu
|
|
|
132
141
|
end
|
|
133
142
|
end
|
|
134
143
|
|
|
144
|
+
# Stands in for a Ziperu::File as the owner of the entries that
|
|
145
|
+
# `File.foreach` yields: it has the password given for the archive,
|
|
146
|
+
# and for an archive at a path it reopens it, so the entries can be
|
|
147
|
+
# read once the walk is over (`foreach(path).find { ... }`, or
|
|
148
|
+
# entries collected in the block). One is shared by all the entries
|
|
149
|
+
# of a walk, and so is the archive it reopens.
|
|
150
|
+
class Reopener
|
|
151
|
+
attr_reader :name, :password
|
|
152
|
+
|
|
153
|
+
# `name` is the archive's path, or nil for one read from an IO,
|
|
154
|
+
# which cannot be opened again.
|
|
155
|
+
def initialize(name, settings, password = nil)
|
|
156
|
+
@name = name
|
|
157
|
+
@settings = settings
|
|
158
|
+
@password = password
|
|
159
|
+
@native = nil
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def native_archive
|
|
163
|
+
return if @name.nil?
|
|
164
|
+
|
|
165
|
+
@native = InputStream.open_archive(@name, @settings) if @native.nil? || @native.closed?
|
|
166
|
+
@native
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
135
170
|
# Data still outside the archive.
|
|
136
171
|
class Pending
|
|
137
172
|
include Defaults
|
data/lib/ziperu/entry.rb
CHANGED
|
@@ -9,6 +9,7 @@ module Ziperu
|
|
|
9
9
|
# consult the archive by index only for bytes.
|
|
10
10
|
class Entry
|
|
11
11
|
include Dirtyable
|
|
12
|
+
include LinkSafety
|
|
12
13
|
|
|
13
14
|
# Constant used to specify that the entry is stored (i.e., not compressed).
|
|
14
15
|
STORED = ::Ziperu::COMPRESSION_METHOD_STORE
|
|
@@ -266,12 +267,22 @@ module Ziperu
|
|
|
266
267
|
fstype version_made_by dos_date dos_time comment extra header_offset].freeze # :nodoc:
|
|
267
268
|
|
|
268
269
|
# Build an entry from one row (see ROW_FIELDS).
|
|
269
|
-
|
|
270
|
+
# `owner` is who reopens the archive for the entry once it is closed:
|
|
271
|
+
# `zipfile`, unless another is given (see Source::Reopener).
|
|
272
|
+
def self.from_row(archive, row, zipfile = nil, metadata_encoding: nil, settings: ::Ziperu, owner: zipfile) # :nodoc:
|
|
270
273
|
entry = allocate
|
|
271
|
-
entry.send(:init_from_row, archive, row, zipfile, metadata_encoding, settings)
|
|
274
|
+
entry.send(:init_from_row, archive, row, zipfile, metadata_encoding, settings, owner)
|
|
272
275
|
entry
|
|
273
276
|
end
|
|
274
277
|
|
|
278
|
+
# Raises for a `metadata_encoding:` Ruby does not know, which was once
|
|
279
|
+
# ignored: names then stayed binary and nothing said why.
|
|
280
|
+
def self.check_metadata_encoding!(encoding) # :nodoc:
|
|
281
|
+
::Encoding.find(encoding) unless encoding.nil?
|
|
282
|
+
rescue ::ArgumentError
|
|
283
|
+
raise ::ArgumentError, "unknown metadata_encoding: #{encoding.inspect}"
|
|
284
|
+
end
|
|
285
|
+
|
|
275
286
|
# Is this entry encrypted?
|
|
276
287
|
def encrypted?
|
|
277
288
|
gp_flags.anybits?(ENCRYPTED_GPFLAG)
|
|
@@ -497,7 +508,8 @@ module Ziperu
|
|
|
497
508
|
dest_dir = ::File.absolute_path(destination_directory || '.')
|
|
498
509
|
extract_path = ::File.absolute_path(::File.join(dest_dir, entry_path))
|
|
499
510
|
|
|
500
|
-
|
|
511
|
+
inside = extract_path.start_with?(dest_dir + ::File::SEPARATOR) || dest_dir == ::File::SEPARATOR
|
|
512
|
+
unless inside && !IOExtras.windows_stream_name?(entry_path)
|
|
501
513
|
warn "WARNING: skipped extracting '#{@name}' to '#{extract_path}' as unsafe."
|
|
502
514
|
return self
|
|
503
515
|
end
|
|
@@ -597,9 +609,13 @@ module Ziperu
|
|
|
597
609
|
end
|
|
598
610
|
|
|
599
611
|
# The decrypter for this entry when the owning `Ziperu::File` was
|
|
600
|
-
# opened with a password
|
|
612
|
+
# opened with a password, or `File.foreach` was given one; nil
|
|
613
|
+
# otherwise, or for a plain entry.
|
|
601
614
|
def default_decrypter # :nodoc:
|
|
602
|
-
return unless encrypted?
|
|
615
|
+
return unless encrypted?
|
|
616
|
+
|
|
617
|
+
pw = @zipfile.kind_of?(::Ziperu::File) ? @zipfile.password : @source.password
|
|
618
|
+
return unless pw
|
|
603
619
|
|
|
604
620
|
aes? ? AESDecrypter.new(pw) : TraditionalDecrypter.new(pw)
|
|
605
621
|
end
|
|
@@ -839,7 +855,8 @@ module Ziperu
|
|
|
839
855
|
# one: in a single native call when the entry is in an archive and its
|
|
840
856
|
# declared size is to be trusted, through a stream otherwise.
|
|
841
857
|
def read_all # :nodoc:
|
|
842
|
-
if !pending_data? && @source.stored? && !encrypted? && @settings.validate_entry_sizes
|
|
858
|
+
if !pending_data? && @source.stored? && !encrypted? && @settings.validate_entry_sizes &&
|
|
859
|
+
READABLE_COMPRESSION_METHODS.include?(compression_method)
|
|
843
860
|
get_raw_input_string
|
|
844
861
|
else
|
|
845
862
|
get_input_stream(&:read)
|
|
@@ -911,7 +928,15 @@ module Ziperu
|
|
|
911
928
|
::FileUtils.rm_f(staged) unless done
|
|
912
929
|
end
|
|
913
930
|
else
|
|
914
|
-
|
|
931
|
+
# Nothing was there, so an extraction that fails takes away what
|
|
932
|
+
# it had written rather than leave a file cut short.
|
|
933
|
+
done = false
|
|
934
|
+
begin
|
|
935
|
+
::File.open(dest_path, WRITE_NO_FOLLOW, binmode: true) { |os| write_data_to(os) }
|
|
936
|
+
done = true
|
|
937
|
+
ensure
|
|
938
|
+
::FileUtils.rm_f(dest_path) unless done
|
|
939
|
+
end
|
|
915
940
|
end
|
|
916
941
|
|
|
917
942
|
set_extra_attributes_on_path(dest_path)
|
|
@@ -985,43 +1010,6 @@ module Ziperu
|
|
|
985
1010
|
warn "WARNING: could not create symlink '#{dest_path}': #{e.message}"
|
|
986
1011
|
end
|
|
987
1012
|
|
|
988
|
-
# Why a link to `target` at `dest_path` must not be made under
|
|
989
|
-
# `dest_dir`, or nil when it may.
|
|
990
|
-
def symlink_refusal(target, dest_path, dest_dir)
|
|
991
|
-
resolved = ::File.expand_path(target, ::File.dirname(dest_path))
|
|
992
|
-
unless resolved.start_with?(dest_dir + ::File::SEPARATOR) || resolved == dest_dir
|
|
993
|
-
return 'it points outside the destination'
|
|
994
|
-
end
|
|
995
|
-
|
|
996
|
-
# The check above is lexical. A `..` after a name is not: through a
|
|
997
|
-
# link, `b/..` is the parent of wherever `b` points, so `c -> b/..`
|
|
998
|
-
# with `b -> .` is really the destination's parent. Leading `..`s
|
|
999
|
-
# only climb real directories (the ones on the way were checked).
|
|
1000
|
-
parts = target.split(%r{[/\\]}).reject { |part| part.empty? || part == '.' }
|
|
1001
|
-
"a '..' after a name could lead through a link" if parts.drop_while { |part| part == '..' }.include?('..')
|
|
1002
|
-
end
|
|
1003
|
-
public :symlink_refusal # :nodoc:
|
|
1004
|
-
|
|
1005
|
-
# The first link already on disk below `dest_dir` on the way to `path`,
|
|
1006
|
-
# and at `path` itself when `leaf`, or nil. A junction counts (Ruby's
|
|
1007
|
-
# lstat reports one as a link). The walk stops at the first component
|
|
1008
|
-
# that is missing or not a directory: the callers deal with those.
|
|
1009
|
-
def link_on_the_way(dest_dir, path, leaf: true)
|
|
1010
|
-
parts = path.delete_prefix(dest_dir).split('/').reject(&:empty?)
|
|
1011
|
-
parts.pop unless leaf
|
|
1012
|
-
walked = dest_dir
|
|
1013
|
-
parts.each do |part|
|
|
1014
|
-
walked = ::File.join(walked, part)
|
|
1015
|
-
stat = ::File.lstat(walked)
|
|
1016
|
-
return walked if stat.symlink?
|
|
1017
|
-
return nil unless stat.directory?
|
|
1018
|
-
rescue Errno::ENOENT, Errno::ENOTDIR
|
|
1019
|
-
return nil
|
|
1020
|
-
end
|
|
1021
|
-
nil
|
|
1022
|
-
end
|
|
1023
|
-
public :link_on_the_way # :nodoc:
|
|
1024
|
-
|
|
1025
1013
|
def set_unix_attributes_on_path(dest_path) # :nodoc:
|
|
1026
1014
|
# Ignore setuid/setgid bits by default. Honour if @restore_ownership.
|
|
1027
1015
|
unix_perms_mask = @restore_ownership ? PERMISSION_BITS : SAFE_PERMISSION_BITS
|
|
@@ -1064,7 +1052,7 @@ module Ziperu
|
|
|
1064
1052
|
raise EntryNameError if name.length > MAX_FIELD_LENGTH
|
|
1065
1053
|
end
|
|
1066
1054
|
|
|
1067
|
-
def init_from_row(archive, row, zipfile, metadata_encoding, settings)
|
|
1055
|
+
def init_from_row(archive, row, zipfile, metadata_encoding, settings, owner)
|
|
1068
1056
|
index, name, size, compressed_size, crc, method, flags, external_attributes, fstype, _version,
|
|
1069
1057
|
dos_date, dos_time, comment, extra, header_offset = row
|
|
1070
1058
|
@settings = settings
|
|
@@ -1080,7 +1068,7 @@ module Ziperu
|
|
|
1080
1068
|
# the archive knows the entry by.
|
|
1081
1069
|
@gp_flags = flags
|
|
1082
1070
|
@original_name = name
|
|
1083
|
-
@source = archive ? Source::Archive.new(archive,
|
|
1071
|
+
@source = archive ? Source::Archive.new(archive, owner, index, @original_name) : Source::NONE
|
|
1084
1072
|
# Most entries have no comment and many no extra fields: those share
|
|
1085
1073
|
# one empty String rather than carrying their own.
|
|
1086
1074
|
@extra = nil
|
|
@@ -44,6 +44,8 @@ module Ziperu
|
|
|
44
44
|
# itself (`unzip -j`); `sanitize: :windows` rewrites names that Windows
|
|
45
45
|
# refuses (`<>:"|?*`, control characters, a trailing dot or space,
|
|
46
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
|
+
|
|
47
49
|
def extract_all(destination_directory = '.', symlinks: :skip, progress: nil, threads: 1,
|
|
48
50
|
on_exists: nil, exclude: nil, junk_paths: false, sanitize: nil, &block)
|
|
49
51
|
targets = ExtractionPlan.new(@cdir.writable_entries, exclude: exclude, junk_paths: junk_paths,
|
|
@@ -118,6 +120,16 @@ module Ziperu
|
|
|
118
120
|
end
|
|
119
121
|
end
|
|
120
122
|
|
|
123
|
+
# Whether `name` matches one of `patterns` (what `exclude:` was given).
|
|
124
|
+
def self.excluded?(patterns, name)
|
|
125
|
+
Array(patterns).any? do |pattern|
|
|
126
|
+
case pattern
|
|
127
|
+
when ::Regexp then pattern.match?(name)
|
|
128
|
+
else ::File.fnmatch?(pattern.to_s, name, ::File::FNM_EXTGLOB | ::File::FNM_DOTMATCH)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
121
133
|
# `entries` are the archive's writable entries (of several records
|
|
122
134
|
# under one name only the last, the one rubyzip would know).
|
|
123
135
|
def initialize(entries, exclude: nil, junk_paths: false, sanitize: nil)
|
|
@@ -188,12 +200,7 @@ module Ziperu
|
|
|
188
200
|
# (`File.fnmatch` with `*` crossing `/`, as `unzip -x` has it) or a
|
|
189
201
|
# Regexp.
|
|
190
202
|
def excluded?(name)
|
|
191
|
-
|
|
192
|
-
case pattern
|
|
193
|
-
when ::Regexp then pattern.match?(name)
|
|
194
|
-
else ::File.fnmatch?(pattern.to_s, name, ::File::FNM_EXTGLOB | ::File::FNM_DOTMATCH)
|
|
195
|
-
end
|
|
196
|
-
end
|
|
203
|
+
self.class.excluded?(@exclude, name)
|
|
197
204
|
end
|
|
198
205
|
|
|
199
206
|
# The path under the destination an entry goes to.
|
|
@@ -262,6 +269,9 @@ module Ziperu
|
|
|
262
269
|
return false unless entry.instance_of?(::Ziperu::Entry) && entry.index && entry.backed_by?(native)
|
|
263
270
|
# An encrypted entry goes through the Ruby path, which has the password.
|
|
264
271
|
return false if entry.encrypted?
|
|
272
|
+
# So does one in a method that cannot be read, for the error rubyzip
|
|
273
|
+
# raises (CompressionMethodError).
|
|
274
|
+
return false unless READABLE_COMPRESSION_METHODS.include?(entry.compression_method)
|
|
265
275
|
return false unless entry.file? || entry.directory?
|
|
266
276
|
|
|
267
277
|
IOExtras.native_text?(entry.name)
|
data/lib/ziperu/file.rb
CHANGED
|
@@ -61,6 +61,7 @@ module Ziperu
|
|
|
61
61
|
# encoding flag (an archive made on Windows in Japan, say:
|
|
62
62
|
# "Shift_JIS"); they are transcoded to UTF-8. Without it such names
|
|
63
63
|
# are binary Strings, as in rubyzip.
|
|
64
|
+
Entry.check_metadata_encoding!(metadata_encoding)
|
|
64
65
|
@metadata_encoding = metadata_encoding
|
|
65
66
|
|
|
66
67
|
# On commit, runs of files added from disk are compressed on this
|
|
@@ -208,19 +209,27 @@ module Ziperu
|
|
|
208
209
|
# building the whole listing first: the way to walk an archive with
|
|
209
210
|
# hundreds of thousands of entries, or to stop at the first match.
|
|
210
211
|
# Each entry can be read (`get_input_stream`, `extract`) while the
|
|
211
|
-
# block runs; the archive is closed when `foreach` returns.
|
|
212
|
-
#
|
|
213
|
-
|
|
214
|
-
|
|
212
|
+
# block runs; the archive is closed when `foreach` returns. An entry
|
|
213
|
+
# of an archive at a path can still be read afterwards, as rubyzip
|
|
214
|
+
# allows: the archive is reopened for it. `password:` is the one the
|
|
215
|
+
# entries are read with, as with `open`. Without a block, returns an
|
|
216
|
+
# Enumerator.
|
|
217
|
+
def foreach(path_or_io, metadata_encoding: nil, password: nil, **settings, &block)
|
|
218
|
+
unless block
|
|
219
|
+
return enum_for(:foreach, path_or_io, metadata_encoding: metadata_encoding, password: password, **settings)
|
|
220
|
+
end
|
|
215
221
|
|
|
222
|
+
Entry.check_metadata_encoding!(metadata_encoding)
|
|
216
223
|
unknown = settings.dup
|
|
217
224
|
settings = Settings.take!(unknown)
|
|
218
225
|
raise ArgumentError, "unknown keyword: #{unknown.keys.map(&:inspect).join(', ')}" unless unknown.empty?
|
|
219
226
|
|
|
220
227
|
native = InputStream.open_archive(path_or_io, settings)
|
|
228
|
+
owner = Entry::Source::Reopener.new((path_or_io if InputStream.path_like?(path_or_io)), settings, password)
|
|
221
229
|
begin
|
|
222
230
|
native.each_entry_row do |row|
|
|
223
|
-
yield Entry.from_row(native, row, nil, metadata_encoding: metadata_encoding, settings: settings
|
|
231
|
+
yield Entry.from_row(native, row, nil, metadata_encoding: metadata_encoding, settings: settings,
|
|
232
|
+
owner: owner)
|
|
224
233
|
end
|
|
225
234
|
ensure
|
|
226
235
|
native.close
|
|
@@ -244,10 +253,13 @@ module Ziperu
|
|
|
244
253
|
end
|
|
245
254
|
|
|
246
255
|
# Extracts every entry of the archive at `zip_file_name` into
|
|
247
|
-
# `destination_directory`.
|
|
256
|
+
# `destination_directory`. Takes the options of `#extract_all`; any
|
|
257
|
+
# other is for opening the archive (`password:`, the limits and the
|
|
258
|
+
# rest of what `open` takes).
|
|
248
259
|
def extract_all(zip_file_name, destination_directory = '.', **options, &block)
|
|
249
|
-
|
|
250
|
-
|
|
260
|
+
how, open_options = options.partition { |name, _| EXTRACT_ALL_OPTIONS.include?(name) }.map(&:to_h)
|
|
261
|
+
Ziperu::File.open(zip_file_name, **open_options) do |zf|
|
|
262
|
+
zf.extract_all(destination_directory, **how, &block)
|
|
251
263
|
end
|
|
252
264
|
end
|
|
253
265
|
end
|
|
@@ -300,6 +312,13 @@ module Ziperu
|
|
|
300
312
|
@name
|
|
301
313
|
end
|
|
302
314
|
|
|
315
|
+
# Short, where the default lists every entry: half a megabyte for an
|
|
316
|
+
# archive of 5,000, in irb, a log line or an error message.
|
|
317
|
+
def inspect
|
|
318
|
+
state = [('read-only' if @read_only), ('uncommitted changes' if @dirty)].compact
|
|
319
|
+
"#<#{self.class.name}:#{@name} entries=#{@cdir.size}#{state.map { |s| " (#{s})" }.join}>"
|
|
320
|
+
end
|
|
321
|
+
|
|
303
322
|
# Returns a string containing the contents of the specified entry
|
|
304
323
|
def read(entry)
|
|
305
324
|
get_entry(entry).read_all
|
data/lib/ziperu/input_stream.rb
CHANGED
|
@@ -44,6 +44,7 @@ module Ziperu
|
|
|
44
44
|
enforce_size: nil, metadata_encoding: nil, streaming: false, **opts)
|
|
45
45
|
@settings = Settings.take!(opts)
|
|
46
46
|
enforce_size = @settings.validate_entry_sizes if enforce_size.nil?
|
|
47
|
+
Entry.check_metadata_encoding!(metadata_encoding)
|
|
47
48
|
@metadata_encoding = metadata_encoding
|
|
48
49
|
# rubyzip 2.x's `new(io, offset, decrypter)`.
|
|
49
50
|
offset, decrypter = Compat.positional(legacy, %i[offset decrypter],
|
|
@@ -99,6 +100,7 @@ module Ziperu
|
|
|
99
100
|
@next_index = offset.zero? || @archive.nil? ? 0 : index_at_offset(offset)
|
|
100
101
|
return unless @next_index.nil?
|
|
101
102
|
|
|
103
|
+
@archive.close
|
|
102
104
|
raise Error, "no entry starts at offset #{offset} (Zip local header magic not found)"
|
|
103
105
|
end
|
|
104
106
|
|
data/lib/ziperu/ioextras.rb
CHANGED
|
@@ -38,6 +38,13 @@ module Ziperu
|
|
|
38
38
|
obj.respond_to?(:to_path) ? obj.to_path : obj.to_s
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
# Whether Windows would take `name`, a path below a destination,
|
|
42
|
+
# for something other than a file there: with a colon it is a
|
|
43
|
+
# stream of a file ("a.txt:x" writes into `a.txt`) or a drive.
|
|
44
|
+
def windows_stream_name?(name)
|
|
45
|
+
::Ziperu::RUNNING_ON_WINDOWS && name.to_s.include?(':')
|
|
46
|
+
end
|
|
47
|
+
|
|
41
48
|
# Whether the native core can take `str` as a name or path: it wants
|
|
42
49
|
# valid UTF-8.
|
|
43
50
|
def native_text?(str)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ziperu
|
|
4
|
+
# The checks made before a link is created under a destination, and
|
|
5
|
+
# before anything is put where a link on disk could redirect it. Shared
|
|
6
|
+
# by zip and tar extraction.
|
|
7
|
+
module LinkSafety # :nodoc:
|
|
8
|
+
# Why a link to `target` at `dest_path` must not be made under
|
|
9
|
+
# `dest_dir`, or nil when it may.
|
|
10
|
+
def symlink_refusal(target, dest_path, dest_dir)
|
|
11
|
+
resolved = ::File.expand_path(target, ::File.dirname(dest_path))
|
|
12
|
+
unless resolved.start_with?(dest_dir + ::File::SEPARATOR) || resolved == dest_dir
|
|
13
|
+
return 'it points outside the destination'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# The check above is lexical. A `..` after a name is not: through a
|
|
17
|
+
# link, `b/..` is the parent of wherever `b` points, so `c -> b/..`
|
|
18
|
+
# with `b -> .` is really the destination's parent. Leading `..`s
|
|
19
|
+
# only climb real directories (the ones on the way were checked).
|
|
20
|
+
parts = target.split(%r{[/\\]}).reject { |part| part.empty? || part == '.' }
|
|
21
|
+
"a '..' after a name could lead through a link" if parts.drop_while { |part| part == '..' }.include?('..')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The first link already on disk below `dest_dir` on the way to `path`,
|
|
25
|
+
# and at `path` itself when `leaf`, or nil. A junction counts (Ruby's
|
|
26
|
+
# lstat reports one as a link). The walk stops at the first component
|
|
27
|
+
# that is missing or not a directory: the callers deal with those.
|
|
28
|
+
def link_on_the_way(dest_dir, path, leaf: true)
|
|
29
|
+
parts = path.delete_prefix(dest_dir).split('/').reject(&:empty?)
|
|
30
|
+
parts.pop unless leaf
|
|
31
|
+
walked = dest_dir
|
|
32
|
+
parts.each do |part|
|
|
33
|
+
walked = ::File.join(walked, part)
|
|
34
|
+
stat = ::File.lstat(walked)
|
|
35
|
+
return walked if stat.symlink?
|
|
36
|
+
return nil unless stat.directory?
|
|
37
|
+
rescue Errno::ENOENT, Errno::ENOTDIR
|
|
38
|
+
return nil
|
|
39
|
+
end
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|