ziperu 0.3.1-x86_64-linux-gnu → 0.4.0-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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c1c83b3f365169e23e5deb2f17fd91ed0899495ff0a1effc78610c06369df03
4
- data.tar.gz: 3a5ae29c4d163eedbe64067be5960a64ec6f9383231d5742d6a75524abf907a3
3
+ metadata.gz: 49844608bfb83de6351ba4c955619662c434a907c4d628af30fffa63c919210b
4
+ data.tar.gz: 5b21c6605456e099a0918b6233ed82d33e2bb02e7443aa2f95d80af35eb1f088
5
5
  SHA512:
6
- metadata.gz: 1bfc68f3c4c19659a6ce49e4494dad8ee5629337830bb11d4b0b53662560aa88cdf58af00798bf87317a0f6151ad5aa8985e6fc197dac04e0c8ff81dfebc0ccd
7
- data.tar.gz: ba6760aff9da62e8d575045e7ea00df5916152cd9e735d0b0966e48924bf9660ce36a39d75f8a4ac5f98d88172df9695851eba85e3f3e087981f2f46d68d3778
6
+ metadata.gz: f856fec7aee3ecc338d86078911c67cac0241ba38ec733f7acd9f08bdf8105ca3f949d0e9923ef30889ae01a7d361f9a235cc8831bb126f0600f80387d88df2b
7
+ data.tar.gz: 4e5b312c3235fbf9cf53516ca86b2adadd77e474700b431fc29382f4176df7c4f686f70135ea989114a8201dc87c5c13a8a4a1a34d2766d683156b2a7a357deb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,83 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0 (2026-11-21)
4
+
5
+ Added:
6
+
7
+ - `Ziperu::Tar.foreach` and `Ziperu::Tar::Reader` read tar archives:
8
+ plain, or compressed with gzip, zstd, xz or bzip2, told apart by their
9
+ first bytes and not by their name. An archive is read front to back,
10
+ an entry at a time, in constant memory, from a path or from any IO
11
+ with `read` (a pipe, a socket, a response body). An entry gives its
12
+ name, size, type, mode, time (to the nanosecond where the archive has
13
+ it), link target and owner, and its data through `read` and
14
+ `each_chunk`. v7, ustar, GNU and PAX archives are read, long names and
15
+ extended headers included; sparse files and multi-volume archives are
16
+ refused.
17
+ - `Ziperu::Tar.extract_all(path_or_io, dest)` extracts a tar archive
18
+ under the rules a zip is extracted by: a name that resolves outside
19
+ the destination is skipped with a warning, nothing is written through
20
+ a link already on disk, and times and permission bits are restored
21
+ but never setuid, setgid or an owner. Symlinks are skipped unless
22
+ `symlinks: :create`, and then made only when the target stays inside.
23
+ A hard link is made only to a file the same extraction wrote. Devices
24
+ and fifos are skipped. `on_exists:`, `exclude:`, `sanitize:` and
25
+ `progress:` are as for a zip; `strip_components:` drops leading
26
+ directories. Of two entries with one name the last wins, as `tar` has
27
+ it. Links are created after every file.
28
+ - `Ziperu.extract(path_or_io, dest)` extracts a zip or a tar archive,
29
+ told apart by the first bytes, with the options of whichever it is.
30
+ From a pipe a tar archive is streamed; a zip is read into memory
31
+ first, within `max_archive_size`.
32
+ - `Ziperu::Tar.create(path_or_io) { |tar| ... }` writes a tar archive,
33
+ plain or compressed with gzip, zstd, xz or bzip2 (from the file's name,
34
+ or `compression:`, with `level:`). `add` takes a file from disk or any
35
+ IO, `add_data` a String, and there are `mkdir`, `symlink`, `link` and
36
+ `add_recursive`; `Ziperu::Tar.add_recursive(archive, dir)` does a whole
37
+ directory and `Ziperu::Tar.body` gives a Rack body. What is written is
38
+ ustar, with a PAX extended header only for what ustar cannot hold (a
39
+ long or non-ASCII name, a size of 8 GiB or more). It goes to a pipe or
40
+ socket as readily as to a file, and a file is put in place only once
41
+ it is whole. Owners are not recorded, and with `source_date_epoch` the
42
+ same input gives the same bytes.
43
+ - [docs/tar.md](docs/tar.md) describes what is read and written and how
44
+ extraction behaves. CI now exchanges tar archives with GNU tar, bsdtar, Python's
45
+ tarfile and 7-Zip in both directions, and `rake fuzz:tar` fuzzes the
46
+ reader, the extractor and the writer against Python's tarfile.
47
+ - The limits hold for tar as for zip: `max_entries`, `max_entry_size`,
48
+ `max_total_size`, `max_archive_size` for what is read from an IO, and
49
+ `max_compression_ratio`, which is measured over the stream as it is
50
+ read and so stops a compressed bomb a few megabytes in, read or
51
+ skipped.
52
+ - A compressed tar archive is read to the end of its stream, where
53
+ gzip, xz and bzip2 keep their checksum: tar has none for an entry's
54
+ data. A long name or extended header over 1 MiB is refused, and an xz
55
+ stream that asks for more than 256 MiB to decode.
56
+
57
+ Fixed:
58
+
59
+ - On Windows an entry with a colon in its name was written as an
60
+ alternate data stream: `a.txt:x` went into `a.txt`, a file that could
61
+ have been in the destination before and was neither replaced nor
62
+ reported, whatever `on_exists:` said. And `C:x`, a path relative to
63
+ the current directory of drive C, was written there: outside the
64
+ destination. Such an entry is now skipped with a warning, as one that resolves outside the destination is;
65
+ `sanitize: :windows` rewrites the name and extracts it. This was true
66
+ of zip extraction in every release so far.
67
+
68
+ Internal:
69
+
70
+ - The native extractor is in two parts: `placer.rs` decides where an
71
+ entry goes and what is refused on the way (the path check, links on
72
+ disk, what is already there), and `extract.rs` reads zip entries into
73
+ the files it opens. Nothing changes in what is extracted or how.
74
+ - The memory growth check failed a nightly run on code that had passed
75
+ the day before. It sampled resident memory after a full GC, but memory
76
+ glibc had freed and not yet returned swung the samples between 68 and
77
+ 107 MiB. It now calls `malloc_trim` before each sample: the workload
78
+ then sits at 23.8 MiB for all 600 iterations, and the growth allowed
79
+ drops from 8 MiB to 3.
80
+
3
81
  ## 0.3.1 (2026-11-16)
4
82
 
5
83
  Fixed:
data/README.md CHANGED
@@ -19,8 +19,13 @@ the [`zip`](https://github.com/zip-rs/zip2) crate through
19
19
  [Magnus](https://github.com/matsadler/magnus). `require "zip"` aliases
20
20
  `Zip = Ziperu`, so existing code runs unchanged.
21
21
 
22
- ziperu 0.2.3 on GitHub's Linux runners (Ruby 3.4, median of 5), against
23
- rubyzip 3.7.0:
22
+ It also reads, extracts and writes tar archives (`.tar`, `.tar.gz`,
23
+ `.tar.zst`, `.tar.xz`, `.tar.bz2`), extracting under the same safety
24
+ rules, so one gem unpacks whatever was uploaded:
25
+ `Ziperu.extract(upload, "out")`.
26
+
27
+ Measured with ziperu 0.2.3 against rubyzip 3.7.0 on GitHub's Linux
28
+ runners (Ruby 3.4, median of 5 runs):
24
29
 
25
30
  | scenario | rubyzip | ziperu | speedup |
26
31
  |---|---:|---:|---:|
@@ -32,15 +37,17 @@ rubyzip 3.7.0:
32
37
  | append one entry to a 10000-entry archive | 1.16 s | 148 ms | 7.8x |
33
38
  | add 1000 files from disk (`threads: 4`) | 348 ms | 89 ms | 3.9x |
34
39
 
35
- macOS and Windows, every scenario, and which calls release the GVL (what
36
- Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
40
+ Results for macOS and Windows, and the rest of the scenarios, are in
41
+ [docs/benchmarks.md](docs/benchmarks.md). It also lists which calls
42
+ release the GVL, so other threads (other Puma requests, say) keep running.
37
43
 
38
- - **Precompiled** for Linux (glibc and musl), macOS and Windows, Ruby 3.1 to 4.0. No toolchain, no runtime dependencies.
44
+ - **Precompiled** for Linux (glibc and musl), macOS and Windows, on Ruby 3.3 to 4.0, with binaries for 3.1 and 3.2 as well. No toolchain, no runtime dependencies.
39
45
  - **Fast**: entries inflate and deflate in Rust with the GVL released, the central directory is parsed once, unchanged entries are copied byte for byte on commit, and a commit that only adds entries appends them in place.
40
46
  - **Reads everything**: Stored, Deflate, Deflate64, bzip2, zstd, LZMA, XZ, PPMd and the PKZIP 1.x methods; AES and ZipCrypto. Writes Deflate, Stored, bzip2, zstd, XZ, PPMd, AES and ZipCrypto.
47
+ - **Tar as well**: `Ziperu::Tar` reads and extracts v7, ustar, GNU and PAX archives, plain or compressed with gzip, zstd, xz or bzip2, from a path or any IO including a pipe, and writes them too. See [docs/tar.md](docs/tar.md).
41
48
  - **Safe by default**: no entry inflates past its declared size, extraction skips names that resolve outside the destination and never writes through a link already on disk, symlinks are opt-in, and limits on entry count, entry size, total size and compression ratio can be set up front. [SECURITY.md](SECURITY.md) has the details.
42
49
  - **Friendlier than rubyzip where it costs nothing**: a password on the archive (`File.open(path, password: ...)`), `add` from an IO, Pathname arguments, `File#verify`, progress callbacks, and errors that fail at the call site.
43
- - **Checked against the field**: rubyzip's own test suite, differential fuzzing against Python's `zipfile` and rubyzip, interoperability runs with Info-ZIP, 7-Zip and Python, and the test suites of gems built on rubyzip (roo, caxlsx, rubyXL, docx, write_xlsx, sablon, gepub) run with ziperu in its place.
50
+ - **Checked against the field**: rubyzip's own test suite, differential fuzzing against Python's `zipfile` and rubyzip, interoperability runs with Info-ZIP, 7-Zip, Python, GNU tar and bsdtar, and the test suites of gems built on rubyzip (roo, caxlsx, rubyXL, docx, write_xlsx, sablon, gepub) run with ziperu in its place.
44
51
 
45
52
  ## Installation
46
53
 
@@ -56,9 +63,6 @@ Or on its own:
56
63
  gem install ziperu
57
64
  ```
58
65
 
59
- Precompiled gems ship for Linux, macOS and Windows; see
60
- [Platforms](#platforms) for the details.
61
-
62
66
  ## Quick start
63
67
 
64
68
  ```ruby
@@ -74,28 +78,10 @@ Ziperu::File.open("new.zip", create: true) do |zf|
74
78
  zf.add("report.pdf", "tmp/report.pdf")
75
79
  zf.get_output_stream("notes.txt") { |io| io.puts "written at #{Time.now}" }
76
80
  end
77
-
78
- Ziperu::OutputStream.write_buffer(socket) do |zos| # any IO; a pipe, socket or
79
- zos.put_next_entry("hello.txt") # response body is streamed,
80
- zos.write("hello") # nothing buffered to the end
81
- end
82
-
83
- body = Ziperu::OutputStream.body do |zos| # a Rack body: an Enumerator of
84
- zos.put_next_entry("export.csv") # the archive's bytes, produced
85
- zos.write(csv) # as the server consumes them
86
- end
87
- [200, { "content-type" => "application/zip" }, body]
88
-
89
- Ziperu::File.foreach("huge.zip") do |entry| # one entry at a time, constant
90
- puts entry.name if entry.size > 1 << 30 # memory, however many there are
91
- end
92
-
93
- Ziperu::File.open("secret.zip", password: "s3cret") do |zf| # AES or ZipCrypto
94
- zf.extract_all("out/")
95
- zf.add("upload.bin", StringIO.new(bytes)) # any IO, streamed on commit
96
- end
97
81
  ```
98
82
 
83
+ Streaming, passwords, limits and the rest are in the cookbook below.
84
+
99
85
  ## Cookbook
100
86
 
101
87
  Zip a directory, unzip into one:
@@ -109,7 +95,8 @@ Ziperu::File.open("site.zip") do |zf|
109
95
  end
110
96
  ```
111
97
 
112
- Build an archive in memory and hand it out, or take one in:
98
+ Build an archive in memory and hand it out, write one straight to an IO,
99
+ or take one in:
113
100
 
114
101
  ```ruby
115
102
  bytes = Ziperu::OutputStream.write_buffer do |zos|
@@ -117,26 +104,45 @@ bytes = Ziperu::OutputStream.write_buffer do |zos|
117
104
  zos.write(csv)
118
105
  end.string
119
106
 
107
+ Ziperu::OutputStream.write_buffer(socket) do |zos| # a pipe or socket is streamed,
108
+ zos.put_next_entry("report.csv") # nothing buffered to the end
109
+ zos.write(csv)
110
+ end
111
+
120
112
  Ziperu::File.open_buffer(params[:upload].read) do |zf|
121
113
  zf.each { |e| puts "#{e.name}: #{e.size} bytes" }
122
114
  end
123
115
  ```
124
116
 
125
- Stream a zip download without a temp file (Rack; in Rails, write to
126
- `response.stream` inside `ActionController::Live` the same way):
117
+ Stream a zip download without a temp file. `OutputStream.body` returns a
118
+ Rack body: an Enumerator of the archive's bytes, produced as the server
119
+ consumes them.
127
120
 
128
121
  ```ruby
122
+ require "ziperu/rails_streaming"
123
+
129
124
  body = Ziperu::OutputStream.body do |zos|
130
125
  attachments.each do |att|
131
126
  zos.put_next_entry(att.filename)
132
127
  att.download { |chunk| zos.write(chunk) }
133
128
  end
134
129
  end
135
- [200, Ziperu::RailsStreaming.headers("attachments.zip"), body] # require "ziperu/rails_streaming"
130
+ [200, Ziperu::RailsStreaming.headers("attachments.zip"), body]
131
+ ```
136
132
 
137
- class ExportsController < ApplicationController # in Rails: headers, body, no Live
133
+ In a Rails controller, `zip_stream` sets the headers and the body, with
134
+ no need for `ActionController::Live`:
135
+
136
+ ```ruby
137
+ class ExportsController < ApplicationController
138
138
  include Ziperu::RailsStreaming
139
- def show = zip_stream("export.zip") { |zos| zos.put_next_entry("a.csv"); zos.write(csv) }
139
+
140
+ def show
141
+ zip_stream("export.zip") do |zos|
142
+ zos.put_next_entry("a.csv")
143
+ zos.write(csv)
144
+ end
145
+ end
140
146
  end
141
147
  ```
142
148
 
@@ -147,6 +153,7 @@ changed archive elsewhere:
147
153
  ```ruby
148
154
  Ziperu::File.open("backup.zip") do |zf|
149
155
  zf.add("logs/today.log", "/var/log/app/today.log")
156
+ zf.add("upload.bin", StringIO.new(bytes)) # any IO, streamed on commit
150
157
  zf.get_output_stream("manifest.json") { |io| io.write(manifest.to_json) }
151
158
  end
152
159
 
@@ -154,7 +161,9 @@ Ziperu::File.open("backup.zip", write: "backup-2.zip") { |zf| zf.remove("old.log
154
161
  Ziperu::File.open("backup.zip", write: false) { |zf| zf.read("manifest.json") } # read-only
155
162
  ```
156
163
 
157
- Pick one entry out of a large archive, or read it all as a stream:
164
+ Pick one entry out of a large archive, or read it all as a stream.
165
+ `foreach` yields one entry at a time in constant memory, however many
166
+ there are:
158
167
 
159
168
  ```ruby
160
169
  entry = Ziperu::File.foreach("huge.zip").find { |e| e.name.end_with?(".sig") }
@@ -264,6 +273,41 @@ Ziperu::File.open(upload) do |zip|
264
273
  end
265
274
  ```
266
275
 
276
+ Unpack whatever was uploaded, a zip or a tar archive, compressed or not.
277
+ `Ziperu.extract` tells which from the first bytes. A tar archive is read
278
+ front to back, so it can come from a pipe or a response body:
279
+
280
+ ```ruby
281
+ Ziperu.extract(upload, "out", **Ziperu::Limits::UPLOAD)
282
+
283
+ Ziperu::Tar.extract_all("release.tar.gz", "src", strip_components: 1)
284
+
285
+ Ziperu::Tar.foreach("logs.tar.zst") do |entry| # one entry at a time
286
+ process(entry.name, entry.read) if entry.file?
287
+ end
288
+ ```
289
+
290
+ Tar extraction follows the rules zip extraction does, and adds its own
291
+ for hard links and devices; [docs/tar.md](docs/tar.md) has them, and
292
+ what is read.
293
+
294
+ Write a tar archive, to a file or to anything with `write`. The
295
+ compression follows the file's name, or `compression:`:
296
+
297
+ ```ruby
298
+ Ziperu::Tar.add_recursive("site.tar.gz", "public")
299
+
300
+ Ziperu::Tar.create("backup.tar.zst", level: 10) do |tar|
301
+ tar.add("report.pdf", "tmp/report.pdf") # a file from disk
302
+ tar.add("upload.bin", StringIO.new(bytes)) # any IO
303
+ tar.add_data("manifest.json", manifest.to_json)
304
+ tar.add_recursive("var/log/app", prefix: "logs/")
305
+ end
306
+
307
+ [200, { "content-type" => "application/gzip" },
308
+ Ziperu::Tar.body(compression: :gzip) { |tar| tar.add_data("report.csv", csv) }]
309
+ ```
310
+
267
311
  ## Migrating from rubyzip
268
312
 
269
313
  Change the Gemfile line and keep `require "zip"`: the `Zip` constant and
@@ -300,14 +344,6 @@ flagged UTF-8 come back as UTF-8 strings; duplicate names in one
300
344
  methods), is in [docs/migrating.md](docs/migrating.md). What rubyzip's own
301
345
  test suite makes of ziperu is in [COMPAT.md](COMPAT.md).
302
346
 
303
- ## Why
304
-
305
- rubyzip is pure Ruby and does per-entry work in Ruby objects. ziperu
306
- parses the central directory once in Rust and hands Ruby a flat metadata
307
- dump, reads entries in large chunks, copies unchanged entries byte for
308
- byte on `commit`, and releases the GVL while inflating from a file or
309
- String buffer and while deflating to a file, so other Ruby threads keep running.
310
-
311
347
  ## Platforms
312
348
 
313
349
  - **Ruby 3.3 to 4.0** are supported and tested on Linux, macOS and Windows.
@@ -324,6 +360,7 @@ bundle install
324
360
  bundle exec rake compile test # build the extension, run the suite
325
361
  bundle exec rake compat # rubyzip's own test suite against ziperu
326
362
  bundle exec rake fuzz # differential fuzzing (TIME=180 for three minutes)
363
+ bundle exec rake fuzz:tar # the same for tar, against Python's tarfile
327
364
  bundle exec rake bench # the table above
328
365
  ```
329
366
 
@@ -337,6 +374,10 @@ run, the release pipeline and the code layout are described in
337
374
  - [Changelog](CHANGELOG.md)
338
375
  - [Migrating from rubyzip](docs/migrating.md): every deliberate difference, and what ziperu adds
339
376
  - [rubyzip compatibility report](COMPAT.md)
377
+ - [Tar archives](docs/tar.md): what is read and written, how extraction behaves, the limits
378
+ - [Benchmarks](docs/benchmarks.md): all three platforms, and which calls release the GVL
379
+ - [Security](SECURITY.md): what extraction and reading guard against
380
+ - [Development](docs/development.md): building, the fuzzer, the release pipeline
340
381
 
341
382
  ## Contributing
342
383
 
Binary file
Binary file
Binary file
Binary file
Binary file
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
@@ -507,7 +508,8 @@ module Ziperu
507
508
  dest_dir = ::File.absolute_path(destination_directory || '.')
508
509
  extract_path = ::File.absolute_path(::File.join(dest_dir, entry_path))
509
510
 
510
- unless extract_path.start_with?(dest_dir + ::File::SEPARATOR) || dest_dir == ::File::SEPARATOR
511
+ inside = extract_path.start_with?(dest_dir + ::File::SEPARATOR) || dest_dir == ::File::SEPARATOR
512
+ unless inside && !IOExtras.windows_stream_name?(entry_path)
511
513
  warn "WARNING: skipped extracting '#{@name}' to '#{extract_path}' as unsafe."
512
514
  return self
513
515
  end
@@ -1008,43 +1010,6 @@ module Ziperu
1008
1010
  warn "WARNING: could not create symlink '#{dest_path}': #{e.message}"
1009
1011
  end
1010
1012
 
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
-
1048
1013
  def set_unix_attributes_on_path(dest_path) # :nodoc:
1049
1014
  # Ignore setuid/setgid bits by default. Honour if @restore_ownership.
1050
1015
  unix_perms_mask = @restore_ownership ? PERMISSION_BITS : SAFE_PERMISSION_BITS
@@ -120,6 +120,16 @@ module Ziperu
120
120
  end
121
121
  end
122
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
+
123
133
  # `entries` are the archive's writable entries (of several records
124
134
  # under one name only the last, the one rubyzip would know).
125
135
  def initialize(entries, exclude: nil, junk_paths: false, sanitize: nil)
@@ -190,12 +200,7 @@ module Ziperu
190
200
  # (`File.fnmatch` with `*` crossing `/`, as `unzip -x` has it) or a
191
201
  # Regexp.
192
202
  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
203
+ self.class.excluded?(@exclude, name)
199
204
  end
200
205
 
201
206
  # The path under the destination an entry goes to.
@@ -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