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
data/lib/ziperu/entry.rb
CHANGED
|
@@ -24,8 +24,24 @@ module Ziperu
|
|
|
24
24
|
XZ = ::Ziperu::COMPRESSION_METHOD_XZ
|
|
25
25
|
PPMD = ::Ziperu::COMPRESSION_METHOD_PPMD
|
|
26
26
|
|
|
27
|
-
#
|
|
28
|
-
|
|
27
|
+
# General-purpose flag bits: the entry is encrypted; its sizes and CRC
|
|
28
|
+
# follow the data (it was written in streaming mode); its name and
|
|
29
|
+
# comment are UTF-8 (the language encoding flag, EFS).
|
|
30
|
+
ENCRYPTED_GPFLAG = 0b1 # :nodoc:
|
|
31
|
+
DATA_DESCRIPTOR_GPFLAG = 0b1000 # :nodoc:
|
|
32
|
+
EFS = 0b100000000000 # :nodoc:
|
|
33
|
+
|
|
34
|
+
# The permission bits of a Unix mode, and those kept when ownership is
|
|
35
|
+
# not being restored (setuid and setgid are dropped).
|
|
36
|
+
PERMISSION_BITS = 0o7777 # :nodoc:
|
|
37
|
+
SAFE_PERMISSION_BITS = 0o1777 # :nodoc:
|
|
38
|
+
|
|
39
|
+
# How `create_file` opens its file: as 'wb', but where the OS can, not
|
|
40
|
+
# through a link put at the path after it was checked.
|
|
41
|
+
WRITE_NO_FOLLOW = ::File::WRONLY | ::File::CREAT | ::File::TRUNC |
|
|
42
|
+
(defined?(::File::NOFOLLOW) ? ::File::NOFOLLOW : 0) # :nodoc:
|
|
43
|
+
# The same for a replacement written beside the file: always a new one.
|
|
44
|
+
STAGING_FLAGS = (WRITE_NO_FOLLOW & ~::File::TRUNC) | ::File::EXCL # :nodoc:
|
|
29
45
|
|
|
30
46
|
# Compression level flags (bits 1 and 2 of the gp flags).
|
|
31
47
|
COMPRESSION_LEVEL_SUPERFAST_GPFLAG = 0b110 # :nodoc:
|
|
@@ -104,7 +120,7 @@ module Ziperu
|
|
|
104
120
|
# The central directory's extra fields, parsed on first use: most
|
|
105
121
|
# entries of a large archive are only ever listed.
|
|
106
122
|
def parsed_extra
|
|
107
|
-
@extra ||= ExtraField.new(@extra_raw)
|
|
123
|
+
@extra ||= ExtraField.new(@extra_raw) # rubocop:disable Naming/MemoizedInstanceVariableName
|
|
108
124
|
end
|
|
109
125
|
private :parsed_extra
|
|
110
126
|
|
|
@@ -136,10 +152,18 @@ module Ziperu
|
|
|
136
152
|
# Ziperu::File has released it (rubyzip lets entries be read after `close`).
|
|
137
153
|
def archive # :nodoc:
|
|
138
154
|
@source.native(@local_header_offset)
|
|
139
|
-
end
|
|
155
|
+
end # :nodoc:
|
|
140
156
|
|
|
141
157
|
attr_writer :size # :nodoc:
|
|
142
158
|
|
|
159
|
+
# What the data of an entry read from a stream comes to: unknown (a
|
|
160
|
+
# nil size) until it has been read, when its header leaves that to the
|
|
161
|
+
# descriptor. Not an edit, so the entry is not marked dirty.
|
|
162
|
+
def learn_data(size, crc) # :nodoc:
|
|
163
|
+
@size = size
|
|
164
|
+
@crc = crc
|
|
165
|
+
end
|
|
166
|
+
|
|
143
167
|
# Reads from `native` from now on, which holds the entry's record at
|
|
144
168
|
# the same index (see Ziperu::File#refresh_after_append).
|
|
145
169
|
def rebind_native(native) # :nodoc:
|
|
@@ -164,8 +188,8 @@ module Ziperu
|
|
|
164
188
|
|
|
165
189
|
def comment=(comment)
|
|
166
190
|
comment = comment.to_s
|
|
167
|
-
if comment.bytesize >
|
|
168
|
-
raise Error, "entry comment is #{comment.bytesize} bytes; the limit is
|
|
191
|
+
if comment.bytesize > MAX_FIELD_LENGTH
|
|
192
|
+
raise Error, "entry comment is #{comment.bytesize} bytes; the limit is #{MAX_FIELD_LENGTH}"
|
|
169
193
|
end
|
|
170
194
|
|
|
171
195
|
@comment = comment
|
|
@@ -231,14 +255,36 @@ module Ziperu
|
|
|
231
255
|
# Build an entry from the metadata Hash the native core produces for
|
|
232
256
|
# one central-directory record.
|
|
233
257
|
def self.from_meta(archive, meta, zipfile = nil, metadata_encoding: nil, settings: ::Ziperu) # :nodoc:
|
|
258
|
+
from_row(archive, meta.values_at(*ROW_FIELDS), zipfile, metadata_encoding: metadata_encoding,
|
|
259
|
+
settings: settings)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# The fields of a row from `Native::Archive#each_entry_row`, in order:
|
|
263
|
+
# what `from_meta`'s Hash holds under these keys. In a row an empty
|
|
264
|
+
# comment or extra field may be nil.
|
|
265
|
+
ROW_FIELDS = %i[index name size compressed_size crc compression_method flags external_attributes
|
|
266
|
+
fstype version_made_by dos_date dos_time comment extra header_offset].freeze # :nodoc:
|
|
267
|
+
|
|
268
|
+
# Build an entry from one row (see ROW_FIELDS).
|
|
269
|
+
# `owner` is who reopens the archive for the entry once it is closed:
|
|
270
|
+
# `zipfile`, unless another is given (see Source::Reopener).
|
|
271
|
+
def self.from_row(archive, row, zipfile = nil, metadata_encoding: nil, settings: ::Ziperu, owner: zipfile) # :nodoc:
|
|
234
272
|
entry = allocate
|
|
235
|
-
entry.send(:
|
|
273
|
+
entry.send(:init_from_row, archive, row, zipfile, metadata_encoding, settings, owner)
|
|
236
274
|
entry
|
|
237
275
|
end
|
|
238
276
|
|
|
277
|
+
# Raises for a `metadata_encoding:` Ruby does not know, which was once
|
|
278
|
+
# ignored: names then stayed binary and nothing said why.
|
|
279
|
+
def self.check_metadata_encoding!(encoding) # :nodoc:
|
|
280
|
+
::Encoding.find(encoding) unless encoding.nil?
|
|
281
|
+
rescue ::ArgumentError
|
|
282
|
+
raise ::ArgumentError, "unknown metadata_encoding: #{encoding.inspect}"
|
|
283
|
+
end
|
|
284
|
+
|
|
239
285
|
# Is this entry encrypted?
|
|
240
286
|
def encrypted?
|
|
241
|
-
gp_flags
|
|
287
|
+
gp_flags.anybits?(ENCRYPTED_GPFLAG)
|
|
242
288
|
end
|
|
243
289
|
|
|
244
290
|
# Encrypted with the traditional PKWARE cipher (anything but AES).
|
|
@@ -250,7 +296,7 @@ module Ziperu
|
|
|
250
296
|
# Only informational here: reading goes through the central directory,
|
|
251
297
|
# so an incomplete local header does not stop `get_input_stream`.
|
|
252
298
|
def incomplete? # :nodoc:
|
|
253
|
-
(
|
|
299
|
+
gp_flags.anybits?(DATA_DESCRIPTOR_GPFLAG) && (crc == 0 || size == 0 || compressed_size == 0)
|
|
254
300
|
end
|
|
255
301
|
|
|
256
302
|
# Was a size given, or is it still unknown (as for an entry that is
|
|
@@ -310,13 +356,11 @@ module Ziperu
|
|
|
310
356
|
|
|
311
357
|
def time=(value, component: :mtime)
|
|
312
358
|
mark_dirty!(:time=)
|
|
313
|
-
unless extra.member?(:universaltime) || extra.member?(:ntfs)
|
|
314
|
-
extra.create(:universaltime)
|
|
315
|
-
end
|
|
359
|
+
extra.create(:universaltime) unless extra.member?(:universaltime) || extra.member?(:ntfs)
|
|
316
360
|
|
|
317
361
|
value = DOSTime.from_time(value)
|
|
318
|
-
|
|
319
|
-
(extra[:universaltime] || extra[:ntfs]).send(
|
|
362
|
+
setter = component.to_s.end_with?('=') ? component : "#{component}="
|
|
363
|
+
(extra[:universaltime] || extra[:ntfs]).send(setter, value)
|
|
320
364
|
@time = value if component == :mtime
|
|
321
365
|
end
|
|
322
366
|
|
|
@@ -436,6 +480,13 @@ module Ziperu
|
|
|
436
480
|
# NB: The caller is responsible for making sure `destination_directory` is
|
|
437
481
|
# safe, if it is passed.
|
|
438
482
|
#
|
|
483
|
+
# Below `destination_directory` nothing is written through a link
|
|
484
|
+
# already on disk (a symlink, or a junction on Windows): an entry with
|
|
485
|
+
# one on the way, or at its own path, is skipped with a warning. The
|
|
486
|
+
# destination itself may be a link. The check is made just before the
|
|
487
|
+
# path is used, so it does not hold against a local user who swaps a
|
|
488
|
+
# directory for a link while the extraction runs.
|
|
489
|
+
#
|
|
439
490
|
# `create_parent_directories`, if true, creates any missing intermediate
|
|
440
491
|
# directories for a file entry before writing it. This defaults to
|
|
441
492
|
# false: a missing intermediate directory usually means an earlier
|
|
@@ -461,6 +512,13 @@ module Ziperu
|
|
|
461
512
|
return self
|
|
462
513
|
end
|
|
463
514
|
|
|
515
|
+
# A symlink entry replaces a link at its own path; nothing else is
|
|
516
|
+
# written through one.
|
|
517
|
+
if (link = link_on_the_way(dest_dir, extract_path, leaf: !symlink?))
|
|
518
|
+
warn "WARNING: skipped extracting '#{@name}' through the link '#{link}'."
|
|
519
|
+
return self
|
|
520
|
+
end
|
|
521
|
+
|
|
464
522
|
block ||= Entry.on_exists_block(on_exists, @settings)
|
|
465
523
|
|
|
466
524
|
raise "unknown file type #{inspect}" unless directory? || file? || symlink?
|
|
@@ -488,7 +546,7 @@ module Ziperu
|
|
|
488
546
|
end
|
|
489
547
|
end
|
|
490
548
|
|
|
491
|
-
# The native extractor's code for the policy
|
|
549
|
+
# The native extractor's code for the policy.
|
|
492
550
|
def self.on_exists_code(policy) # :nodoc:
|
|
493
551
|
{ raise: 0, overwrite: 1, skip: 2 }.fetch(policy) do
|
|
494
552
|
raise ArgumentError, "on_exists: takes :raise, :overwrite or :skip, not #{policy.inspect}"
|
|
@@ -518,21 +576,13 @@ module Ziperu
|
|
|
518
576
|
stat = file_stat(src_path)
|
|
519
577
|
@ftype = case stat.ftype
|
|
520
578
|
when 'file'
|
|
521
|
-
|
|
522
|
-
raise ArgumentError,
|
|
523
|
-
"entry name '#{@name}' indicates a directory entry, but " \
|
|
524
|
-
"'#{src_path}' is not a directory"
|
|
525
|
-
end
|
|
579
|
+
check_not_directory_name!(src_path)
|
|
526
580
|
:file
|
|
527
581
|
when 'directory'
|
|
528
582
|
@name += '/' unless name_is_directory?
|
|
529
583
|
:directory
|
|
530
584
|
when 'link'
|
|
531
|
-
|
|
532
|
-
raise ArgumentError,
|
|
533
|
-
"entry name '#{@name}' indicates a directory entry, but " \
|
|
534
|
-
"'#{src_path}' is not a directory"
|
|
535
|
-
end
|
|
585
|
+
check_not_directory_name!(src_path)
|
|
536
586
|
:symlink
|
|
537
587
|
else
|
|
538
588
|
raise "unknown file type: #{src_path.inspect} #{stat.inspect}"
|
|
@@ -557,9 +607,13 @@ module Ziperu
|
|
|
557
607
|
end
|
|
558
608
|
|
|
559
609
|
# The decrypter for this entry when the owning `Ziperu::File` was
|
|
560
|
-
# opened with a password
|
|
610
|
+
# opened with a password, or `File.foreach` was given one; nil
|
|
611
|
+
# otherwise, or for a plain entry.
|
|
561
612
|
def default_decrypter # :nodoc:
|
|
562
|
-
return unless encrypted?
|
|
613
|
+
return unless encrypted?
|
|
614
|
+
|
|
615
|
+
pw = @zipfile.kind_of?(::Ziperu::File) ? @zipfile.password : @source.password
|
|
616
|
+
return unless pw
|
|
563
617
|
|
|
564
618
|
aes? ? AESDecrypter.new(pw) : TraditionalDecrypter.new(pw)
|
|
565
619
|
end
|
|
@@ -590,18 +644,11 @@ module Ziperu
|
|
|
590
644
|
when :copy_encrypted
|
|
591
645
|
zip_output_stream.copy_encrypted_entry(self)
|
|
592
646
|
else
|
|
593
|
-
renamed =
|
|
647
|
+
renamed = renamed?
|
|
594
648
|
zip_output_stream.copy_raw_entry(self, (@name if renamed), touch: dirty? && !renamed)
|
|
595
649
|
end
|
|
596
650
|
end
|
|
597
651
|
|
|
598
|
-
# How the entry reaches an output stream: `:header` for a directory or
|
|
599
|
-
# a symlink (nothing to stream), `:restream` to decompress and write
|
|
600
|
-
# afresh (a new entry, or one edited beyond what a raw copy carries),
|
|
601
|
-
# `:copy_encrypted` for a ZipCrypto entry copied byte for byte under
|
|
602
|
-
# its own headers, `:copy_raw` otherwise. Raises, with the reason, for
|
|
603
|
-
# an entry that would have to be restreamed but cannot be (encrypted,
|
|
604
|
-
# or a compression method the writer lacks).
|
|
605
652
|
# The file on disk a regular file entry added from a path is written
|
|
606
653
|
# from, when the native core can open it by name; nil otherwise.
|
|
607
654
|
def native_source_path # :nodoc:
|
|
@@ -612,9 +659,23 @@ module Ziperu
|
|
|
612
659
|
|
|
613
660
|
path = path.to_path if path.respond_to?(:to_path)
|
|
614
661
|
path = path.to_s
|
|
615
|
-
path if
|
|
662
|
+
path if IOExtras.native_text?(path)
|
|
663
|
+
end
|
|
664
|
+
|
|
665
|
+
# Whether committing the entry is a raw copy of it as it is: no new
|
|
666
|
+
# name, nothing touched, not ZipCrypto (see `write_to_zip_output_stream`).
|
|
667
|
+
# Such entries are copied in runs (OutputStream#put_entries).
|
|
668
|
+
def plain_raw_copy? # :nodoc:
|
|
669
|
+
write_plan == :copy_raw && !dirty? && !renamed?
|
|
616
670
|
end
|
|
617
671
|
|
|
672
|
+
# How the entry reaches an output stream: `:header` for a directory or
|
|
673
|
+
# a symlink (nothing to stream), `:restream` to decompress and write
|
|
674
|
+
# afresh (a new entry, or one edited beyond what a raw copy carries),
|
|
675
|
+
# `:copy_encrypted` for a ZipCrypto entry copied byte for byte under
|
|
676
|
+
# its own headers, `:copy_raw` otherwise. Raises, with the reason, for
|
|
677
|
+
# an entry that would have to be restreamed but cannot be (encrypted,
|
|
678
|
+
# or a compression method the writer lacks).
|
|
618
679
|
def write_plan # :nodoc:
|
|
619
680
|
# A symlink's target goes into the header call, whether the link is
|
|
620
681
|
# new or a copy that has to be written afresh (an edit, or extra
|
|
@@ -655,15 +716,25 @@ module Ziperu
|
|
|
655
716
|
end
|
|
656
717
|
|
|
657
718
|
def loses_fields_on_raw_copy? # :nodoc:
|
|
658
|
-
|
|
719
|
+
return false unless restreamable?
|
|
720
|
+
# Asked of the whole archive at once before a commit (see
|
|
721
|
+
# File#learn_raw_copy_losses), for an entry whose fields are as read.
|
|
722
|
+
return @raw_copy_loses if !@raw_copy_loses.nil? && !@local_extra_merged
|
|
723
|
+
|
|
724
|
+
extra.keys.intersect?(RAW_COPY_LOSES)
|
|
659
725
|
end
|
|
660
726
|
|
|
727
|
+
# The header IDs of RAW_COPY_LOSES, for the native core.
|
|
728
|
+
RAW_COPY_LOSES_IDS = [0x5455, 0x000A, 0x5855, 0x7855].freeze # :nodoc:
|
|
729
|
+
|
|
730
|
+
attr_writer :raw_copy_loses # :nodoc:
|
|
731
|
+
|
|
661
732
|
# Whether appending to the archive, which leaves this entry's local
|
|
662
733
|
# header alone but has the crate write its central record again,
|
|
663
734
|
# would lose fields of that record.
|
|
664
735
|
def central_fields_lost_on_append? # :nodoc:
|
|
665
736
|
fields = central_extra
|
|
666
|
-
!fields.nil? && restreamable? &&
|
|
737
|
+
!fields.nil? && restreamable? && fields.keys.intersect?(RAW_COPY_LOSES)
|
|
667
738
|
end
|
|
668
739
|
|
|
669
740
|
# Whether the writer can decompress and rewrite the entry: it is not
|
|
@@ -725,10 +796,9 @@ module Ziperu
|
|
|
725
796
|
|
|
726
797
|
@unix_uid = stat.uid
|
|
727
798
|
@unix_gid = stat.gid
|
|
728
|
-
@unix_perms = stat.mode &
|
|
799
|
+
@unix_perms = stat.mode & PERMISSION_BITS
|
|
729
800
|
end
|
|
730
801
|
|
|
731
|
-
# Decompressed contents as a binary String, in one native call.
|
|
732
802
|
# Yields (or returns) an IO over the whole archive this entry is in,
|
|
733
803
|
# as rubyzip does: a fresh File for an archive on disk, the buffer
|
|
734
804
|
# itself (rewound) for one in memory.
|
|
@@ -766,8 +836,10 @@ module Ziperu
|
|
|
766
836
|
@source.local_extra(@local_header_offset).bytesize
|
|
767
837
|
end
|
|
768
838
|
|
|
839
|
+
# Decompressed contents as a binary String, in one native call.
|
|
769
840
|
def get_raw_input_string # :nodoc:
|
|
770
841
|
return ''.b if ftype == :directory
|
|
842
|
+
|
|
771
843
|
check_in_archive!(archive)
|
|
772
844
|
# A record without an index is read through the stream.
|
|
773
845
|
return open_input_stream(enforce_size: true, &:read) if index.nil?
|
|
@@ -781,7 +853,8 @@ module Ziperu
|
|
|
781
853
|
# one: in a single native call when the entry is in an archive and its
|
|
782
854
|
# declared size is to be trusted, through a stream otherwise.
|
|
783
855
|
def read_all # :nodoc:
|
|
784
|
-
if !pending_data? && @source.stored? && !encrypted? && @settings.validate_entry_sizes
|
|
856
|
+
if !pending_data? && @source.stored? && !encrypted? && @settings.validate_entry_sizes &&
|
|
857
|
+
READABLE_COMPRESSION_METHODS.include?(compression_method)
|
|
785
858
|
get_raw_input_string
|
|
786
859
|
else
|
|
787
860
|
get_input_stream(&:read)
|
|
@@ -794,6 +867,19 @@ module Ziperu
|
|
|
794
867
|
|
|
795
868
|
private
|
|
796
869
|
|
|
870
|
+
# Whether the entry's name is no longer the one the archive has it under.
|
|
871
|
+
def renamed?
|
|
872
|
+
@name.b != @original_name.b.tr('\\', '/')
|
|
873
|
+
end
|
|
874
|
+
|
|
875
|
+
def check_not_directory_name!(src_path)
|
|
876
|
+
return unless name_is_directory?
|
|
877
|
+
|
|
878
|
+
raise ArgumentError,
|
|
879
|
+
"entry name '#{@name}' indicates a directory entry, but " \
|
|
880
|
+
"'#{src_path}' is not a directory"
|
|
881
|
+
end
|
|
882
|
+
|
|
797
883
|
# Like get_input_stream, with `enforce_size: false` reading past a
|
|
798
884
|
# declared size that is too small (what rubyzip does when
|
|
799
885
|
# Ziperu.validate_entry_sizes is off) instead of stopping at it.
|
|
@@ -819,35 +905,67 @@ module Ziperu
|
|
|
819
905
|
end
|
|
820
906
|
end
|
|
821
907
|
|
|
822
|
-
def create_file(dest_path
|
|
823
|
-
|
|
908
|
+
def create_file(dest_path)
|
|
909
|
+
replace = ::File.exist?(dest_path)
|
|
910
|
+
if replace
|
|
824
911
|
decision = yield(self, dest_path)
|
|
825
912
|
return if decision == :skip
|
|
826
913
|
raise DestinationExistsError, dest_path unless decision
|
|
827
914
|
end
|
|
828
915
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
916
|
+
# A file being replaced is written beside it and renamed over it once
|
|
917
|
+
# complete, so a failure part-way leaves the original as it was.
|
|
918
|
+
if replace
|
|
919
|
+
staged = staging_path(dest_path)
|
|
920
|
+
done = false
|
|
921
|
+
begin
|
|
922
|
+
::File.open(staged, STAGING_FLAGS, binmode: true) { |os| write_data_to(os) }
|
|
923
|
+
::File.rename(staged, dest_path)
|
|
924
|
+
done = true
|
|
925
|
+
ensure
|
|
926
|
+
::FileUtils.rm_f(staged) unless done
|
|
927
|
+
end
|
|
928
|
+
else
|
|
929
|
+
# Nothing was there, so an extraction that fails takes away what
|
|
930
|
+
# it had written rather than leave a file cut short.
|
|
931
|
+
done = false
|
|
932
|
+
begin
|
|
933
|
+
::File.open(dest_path, WRITE_NO_FOLLOW, binmode: true) { |os| write_data_to(os) }
|
|
934
|
+
done = true
|
|
935
|
+
ensure
|
|
936
|
+
::FileUtils.rm_f(dest_path) unless done
|
|
845
937
|
end
|
|
846
938
|
end
|
|
847
939
|
|
|
848
940
|
set_extra_attributes_on_path(dest_path)
|
|
849
941
|
end
|
|
850
942
|
|
|
943
|
+
# A hidden name beside `path` for its replacement, on the same file
|
|
944
|
+
# system so the rename stays one step.
|
|
945
|
+
def staging_path(path)
|
|
946
|
+
::File.join(::File.dirname(path), ".#{::File.basename(path)}.ziperu-#{::Process.pid}-#{rand(1 << 32).to_s(16)}")
|
|
947
|
+
end
|
|
948
|
+
|
|
949
|
+
# The entry's data, decompressed, into `os`.
|
|
950
|
+
def write_data_to(os)
|
|
951
|
+
if pending_data?
|
|
952
|
+
# Added or replaced from disk or an IO and not yet committed:
|
|
953
|
+
# the data is that source's, not the archive's.
|
|
954
|
+
get_input_stream { |is| ::IO.copy_stream(is, os) }
|
|
955
|
+
else
|
|
956
|
+
open_input_stream(decrypter: default_decrypter) do |is|
|
|
957
|
+
bytes_written = 0
|
|
958
|
+
while (buf = is.sysread(IOExtras::CHUNK_SIZE))
|
|
959
|
+
os << buf
|
|
960
|
+
bytes_written += buf.bytesize
|
|
961
|
+
end
|
|
962
|
+
# With enforcement on, the native reader has already raised
|
|
963
|
+
# EntrySizeError; without it, warn as rubyzip does.
|
|
964
|
+
warn "WARNING: #{EntrySizeError.new(self).message}" if bytes_written > size
|
|
965
|
+
end
|
|
966
|
+
end
|
|
967
|
+
end
|
|
968
|
+
|
|
851
969
|
def create_directory(dest_path)
|
|
852
970
|
return if ::File.directory?(dest_path)
|
|
853
971
|
|
|
@@ -863,8 +981,9 @@ module Ziperu
|
|
|
863
981
|
set_extra_attributes_on_path(dest_path)
|
|
864
982
|
end
|
|
865
983
|
|
|
866
|
-
#
|
|
867
|
-
#
|
|
984
|
+
# Created only with `symlinks: :create`, and only when the target
|
|
985
|
+
# resolves inside the destination: a link pointing outside would let a
|
|
986
|
+
# later entry write through it.
|
|
868
987
|
def create_symlink(dest_path, dest_dir, mode)
|
|
869
988
|
unless mode == :create
|
|
870
989
|
warn "WARNING: skipped symlink '#{dest_path}'."
|
|
@@ -872,9 +991,8 @@ module Ziperu
|
|
|
872
991
|
end
|
|
873
992
|
|
|
874
993
|
target = symlink_target
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
warn "WARNING: skipped symlink '#{@name}' to '#{target}' as it points outside the destination."
|
|
994
|
+
if (why = symlink_refusal(target, dest_path, dest_dir))
|
|
995
|
+
warn "WARNING: skipped symlink '#{@name}' to '#{target}' as #{why}."
|
|
878
996
|
return
|
|
879
997
|
end
|
|
880
998
|
|
|
@@ -890,12 +1008,47 @@ module Ziperu
|
|
|
890
1008
|
warn "WARNING: could not create symlink '#{dest_path}': #{e.message}"
|
|
891
1009
|
end
|
|
892
1010
|
|
|
1011
|
+
# Why a link to `target` at `dest_path` must not be made under
|
|
1012
|
+
# `dest_dir`, or nil when it may.
|
|
1013
|
+
def symlink_refusal(target, dest_path, dest_dir)
|
|
1014
|
+
resolved = ::File.expand_path(target, ::File.dirname(dest_path))
|
|
1015
|
+
unless resolved.start_with?(dest_dir + ::File::SEPARATOR) || resolved == dest_dir
|
|
1016
|
+
return 'it points outside the destination'
|
|
1017
|
+
end
|
|
1018
|
+
|
|
1019
|
+
# The check above is lexical. A `..` after a name is not: through a
|
|
1020
|
+
# link, `b/..` is the parent of wherever `b` points, so `c -> b/..`
|
|
1021
|
+
# with `b -> .` is really the destination's parent. Leading `..`s
|
|
1022
|
+
# only climb real directories (the ones on the way were checked).
|
|
1023
|
+
parts = target.split(%r{[/\\]}).reject { |part| part.empty? || part == '.' }
|
|
1024
|
+
"a '..' after a name could lead through a link" if parts.drop_while { |part| part == '..' }.include?('..')
|
|
1025
|
+
end
|
|
1026
|
+
public :symlink_refusal # :nodoc:
|
|
1027
|
+
|
|
1028
|
+
# The first link already on disk below `dest_dir` on the way to `path`,
|
|
1029
|
+
# and at `path` itself when `leaf`, or nil. A junction counts (Ruby's
|
|
1030
|
+
# lstat reports one as a link). The walk stops at the first component
|
|
1031
|
+
# that is missing or not a directory: the callers deal with those.
|
|
1032
|
+
def link_on_the_way(dest_dir, path, leaf: true)
|
|
1033
|
+
parts = path.delete_prefix(dest_dir).split('/').reject(&:empty?)
|
|
1034
|
+
parts.pop unless leaf
|
|
1035
|
+
walked = dest_dir
|
|
1036
|
+
parts.each do |part|
|
|
1037
|
+
walked = ::File.join(walked, part)
|
|
1038
|
+
stat = ::File.lstat(walked)
|
|
1039
|
+
return walked if stat.symlink?
|
|
1040
|
+
return nil unless stat.directory?
|
|
1041
|
+
rescue Errno::ENOENT, Errno::ENOTDIR
|
|
1042
|
+
return nil
|
|
1043
|
+
end
|
|
1044
|
+
nil
|
|
1045
|
+
end
|
|
1046
|
+
public :link_on_the_way # :nodoc:
|
|
1047
|
+
|
|
893
1048
|
def set_unix_attributes_on_path(dest_path) # :nodoc:
|
|
894
1049
|
# Ignore setuid/setgid bits by default. Honour if @restore_ownership.
|
|
895
|
-
unix_perms_mask =
|
|
896
|
-
if @restore_permissions && @unix_perms
|
|
897
|
-
::FileUtils.chmod(@unix_perms & unix_perms_mask, dest_path)
|
|
898
|
-
end
|
|
1050
|
+
unix_perms_mask = @restore_ownership ? PERMISSION_BITS : SAFE_PERMISSION_BITS
|
|
1051
|
+
::FileUtils.chmod(@unix_perms & unix_perms_mask, dest_path) if @restore_permissions && @unix_perms
|
|
899
1052
|
return unless @restore_ownership && @unix_uid && @unix_gid && ::Process.egid == 0
|
|
900
1053
|
|
|
901
1054
|
::FileUtils.chown(@unix_uid, @unix_gid, dest_path)
|
|
@@ -917,15 +1070,8 @@ module Ziperu
|
|
|
917
1070
|
@internal_file_attributes = 1
|
|
918
1071
|
@external_file_attributes = 0
|
|
919
1072
|
|
|
920
|
-
@version_needed_to_extract = VERSION_NEEDED_TO_EXTRACT
|
|
921
|
-
@version = VERSION_MADE_BY
|
|
922
|
-
|
|
923
1073
|
@ftype = nil
|
|
924
|
-
@gp_flags = 0
|
|
925
|
-
if @settings.unicode_names
|
|
926
|
-
@gp_flags |= EFS
|
|
927
|
-
@version = 63
|
|
928
|
-
end
|
|
1074
|
+
@gp_flags = @settings.unicode_names ? EFS : 0
|
|
929
1075
|
@follow_symlinks = false
|
|
930
1076
|
|
|
931
1077
|
@restore_times = DEFAULT_RESTORE_OPTIONS[:restore_times]
|
|
@@ -938,10 +1084,12 @@ module Ziperu
|
|
|
938
1084
|
|
|
939
1085
|
def check_name(name) # :nodoc:
|
|
940
1086
|
raise EntryNameError, name if name.start_with?('/')
|
|
941
|
-
raise EntryNameError if name.length >
|
|
1087
|
+
raise EntryNameError if name.length > MAX_FIELD_LENGTH
|
|
942
1088
|
end
|
|
943
1089
|
|
|
944
|
-
def
|
|
1090
|
+
def init_from_row(archive, row, zipfile, metadata_encoding, settings, owner)
|
|
1091
|
+
index, name, size, compressed_size, crc, method, flags, external_attributes, fstype, _version,
|
|
1092
|
+
dos_date, dos_time, comment, extra, header_offset = row
|
|
945
1093
|
@settings = settings
|
|
946
1094
|
@dirty = false
|
|
947
1095
|
@zip64 = nil
|
|
@@ -953,29 +1101,26 @@ module Ziperu
|
|
|
953
1101
|
# they stay binary. A lookup finds a name in any encoding with the
|
|
954
1102
|
# same bytes. The raw name, before separators are normalised, is what
|
|
955
1103
|
# the archive knows the entry by.
|
|
956
|
-
@gp_flags =
|
|
957
|
-
@original_name =
|
|
958
|
-
@source = archive ? Source::Archive.new(archive,
|
|
959
|
-
@name = decode_stored_text(@original_name.dup, metadata_encoding, meta[:extra])
|
|
960
|
-
@name.tr!('\\', '/')
|
|
961
|
-
@size = meta[:size]
|
|
962
|
-
@compressed_size = meta[:compressed_size]
|
|
963
|
-
@crc = meta[:crc]
|
|
964
|
-
@compression_method = meta[:compression_method]
|
|
965
|
-
@compression_level = @settings.default_compression
|
|
966
|
-
@fstype = meta[:fstype]
|
|
967
|
-
@version = meta[:version_made_by]
|
|
968
|
-
@external_file_attributes = meta[:external_attributes]
|
|
969
|
-
set_time(meta[:dos_date], meta[:dos_time])
|
|
1104
|
+
@gp_flags = flags
|
|
1105
|
+
@original_name = name
|
|
1106
|
+
@source = archive ? Source::Archive.new(archive, owner, index, @original_name) : Source::NONE
|
|
970
1107
|
# Most entries have no comment and many no extra fields: those share
|
|
971
1108
|
# one empty String rather than carrying their own.
|
|
972
|
-
comment = meta[:comment]
|
|
973
|
-
@comment = comment.empty? ? '' : decode_stored_text(comment, metadata_encoding)
|
|
974
1109
|
@extra = nil
|
|
975
|
-
|
|
976
|
-
@extra_raw = extra.empty? ? '' : extra
|
|
1110
|
+
@extra_raw = extra.nil? || extra.empty? ? '' : extra
|
|
977
1111
|
@local_extra_merged = false
|
|
978
|
-
@
|
|
1112
|
+
@name = decode_stored_text(@original_name.dup, metadata_encoding, name: true)
|
|
1113
|
+
@name.tr!('\\', '/')
|
|
1114
|
+
@size = size
|
|
1115
|
+
@compressed_size = compressed_size
|
|
1116
|
+
@crc = crc
|
|
1117
|
+
@compression_method = method
|
|
1118
|
+
@compression_level = @settings.default_compression
|
|
1119
|
+
@fstype = fstype
|
|
1120
|
+
@external_file_attributes = external_attributes
|
|
1121
|
+
set_time(dos_date, dos_time)
|
|
1122
|
+
@comment = comment.nil? || comment.empty? ? '' : decode_stored_text(comment, metadata_encoding)
|
|
1123
|
+
@local_header_offset = header_offset
|
|
979
1124
|
set_ftype_from_c_dir_entry
|
|
980
1125
|
end
|
|
981
1126
|
|
|
@@ -983,16 +1128,16 @@ module Ziperu
|
|
|
983
1128
|
# `encoding` is the archive's `metadata_encoding:`, for a name or
|
|
984
1129
|
# comment stored without the language encoding flag: the bytes are
|
|
985
1130
|
# read in it and transcoded to UTF-8 (left as bytes when they do not
|
|
986
|
-
# decode).
|
|
987
|
-
#
|
|
988
|
-
def decode_stored_text(text, encoding = nil,
|
|
1131
|
+
# decode). For a name, Info-ZIP's Unicode Path extra field is checked
|
|
1132
|
+
# first, which carries the UTF-8 name outright.
|
|
1133
|
+
def decode_stored_text(text, encoding = nil, name: false) # :nodoc:
|
|
989
1134
|
forced = @settings.force_entry_names_encoding
|
|
990
1135
|
if forced
|
|
991
1136
|
text.force_encoding(forced)
|
|
992
1137
|
elsif (@gp_flags & EFS).nonzero?
|
|
993
1138
|
text.force_encoding(::Encoding::UTF_8)
|
|
994
1139
|
text.force_encoding(::Encoding::BINARY) unless text.valid_encoding?
|
|
995
|
-
elsif
|
|
1140
|
+
elsif name && (utf8 = unicode_path_name(text))
|
|
996
1141
|
return utf8
|
|
997
1142
|
elsif encoding
|
|
998
1143
|
return transcode(text, encoding)
|
|
@@ -1002,19 +1147,14 @@ module Ziperu
|
|
|
1002
1147
|
|
|
1003
1148
|
# The name the Unicode Path extra field carries for `stored`, if there
|
|
1004
1149
|
# is one and it is for this name (its CRC matches).
|
|
1005
|
-
|
|
1006
|
-
|
|
1150
|
+
# The extra fields are parsed for it now rather than on first use.
|
|
1151
|
+
def unicode_path_name(stored)
|
|
1152
|
+
return unless @extra_raw.include?(ExtraField::UnicodePath::HEADER_ID)
|
|
1007
1153
|
|
|
1008
|
-
field =
|
|
1154
|
+
field = parsed_extra[:unicodepath]
|
|
1009
1155
|
field.name.dup if field&.for?(stored)
|
|
1010
1156
|
end
|
|
1011
1157
|
|
|
1012
|
-
# Parses the central directory's extra fields now rather than on first
|
|
1013
|
-
# use (the parse is needed for the name).
|
|
1014
|
-
def parsed_extra_from(raw_extra)
|
|
1015
|
-
@extra ||= ExtraField.new(raw_extra)
|
|
1016
|
-
end
|
|
1017
|
-
|
|
1018
1158
|
def transcode(text, encoding)
|
|
1019
1159
|
text.force_encoding(encoding).encode(::Encoding::UTF_8)
|
|
1020
1160
|
rescue ::EncodingError, ::ArgumentError
|
|
@@ -1058,7 +1198,7 @@ module Ziperu
|
|
|
1058
1198
|
def set_ftype_from_c_dir_entry # :nodoc:
|
|
1059
1199
|
@ftype = case @fstype
|
|
1060
1200
|
when ::Ziperu::FSTYPE_UNIX
|
|
1061
|
-
@unix_perms = (@external_file_attributes >> 16) &
|
|
1201
|
+
@unix_perms = (@external_file_attributes >> 16) & PERMISSION_BITS
|
|
1062
1202
|
case (@external_file_attributes >> 28)
|
|
1063
1203
|
when ::Ziperu::FILE_TYPE_DIR
|
|
1064
1204
|
:directory
|
|
@@ -1074,7 +1214,7 @@ module Ziperu
|
|
|
1074
1214
|
# bits. The native writer stamps the host of the machine it
|
|
1075
1215
|
# runs on, so archives written here on Windows would lose
|
|
1076
1216
|
# their permissions on reread; a non-zero mode is kept.
|
|
1077
|
-
perms = (@external_file_attributes >> 16) &
|
|
1217
|
+
perms = (@external_file_attributes >> 16) & PERMISSION_BITS
|
|
1078
1218
|
@unix_perms = perms unless perms.zero?
|
|
1079
1219
|
name_is_directory? ? :directory : :file
|
|
1080
1220
|
end
|
data/lib/ziperu/entry_set.rb
CHANGED
|
@@ -45,7 +45,6 @@ module Ziperu
|
|
|
45
45
|
if at
|
|
46
46
|
@all[at] = entry
|
|
47
47
|
else
|
|
48
|
-
remove_from_all(old) if old
|
|
49
48
|
@all << entry
|
|
50
49
|
end
|
|
51
50
|
end
|
|
@@ -84,10 +83,6 @@ module Ziperu
|
|
|
84
83
|
entries.reject { |e| shadowed?(e) }
|
|
85
84
|
end
|
|
86
85
|
|
|
87
|
-
def each_writable(&block)
|
|
88
|
-
writable_entries.each(&block)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
86
|
def size
|
|
92
87
|
@all.size
|
|
93
88
|
end
|
data/lib/ziperu/errors.rb
CHANGED
|
@@ -94,6 +94,8 @@ module Ziperu
|
|
|
94
94
|
end
|
|
95
95
|
end
|
|
96
96
|
|
|
97
|
+
# The end-of-central-directory record declares a different number of
|
|
98
|
+
# entries than the central directory holds.
|
|
97
99
|
class EntryNumberMismatchError < Error
|
|
98
100
|
def initialize(size, size_in_bytes)
|
|
99
101
|
super()
|
|
@@ -154,13 +156,13 @@ module Ziperu
|
|
|
154
156
|
|
|
155
157
|
PASSWORD_REQUIRED_MESSAGE = 'A password is required to decode this zip file.'
|
|
156
158
|
|
|
157
|
-
# Raised by the Rust core where it lacks the domain object a rubyzip
|
|
158
|
-
# exception wants; the Ruby layer re-raises the rubyzip-shaped error.
|
|
159
159
|
# An entry object outlived a commit that moved its record; what it holds
|
|
160
160
|
# from the central directory is still good, its local header is gone.
|
|
161
161
|
class EntryMovedError < Error # :nodoc:
|
|
162
162
|
end
|
|
163
163
|
|
|
164
|
+
# Raised by the Rust core where it lacks the domain object a rubyzip
|
|
165
|
+
# exception wants; the Ruby layer re-raises the rubyzip-shaped error.
|
|
164
166
|
module Native
|
|
165
167
|
class SizeLimitError < Ziperu::Error; end
|
|
166
168
|
class PasswordError < Ziperu::Error; end
|