ziperu 0.2.3 → 0.3.1
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/Cargo.lock +2 -1
- data/README.md +35 -7
- data/ext/ziperu/Cargo.toml +9 -1
- data/ext/ziperu/src/archive.rs +246 -144
- data/ext/ziperu/src/entry_options.rs +1 -1
- data/ext/ziperu/src/errors.rs +39 -11
- data/ext/ziperu/src/extract.rs +332 -104
- data/ext/ziperu/src/gvl.rs +45 -26
- data/ext/ziperu/src/lib.rs +17 -0
- data/ext/ziperu/src/place.rs +379 -0
- data/ext/ziperu/src/rb.rs +14 -1
- data/ext/ziperu/src/reader.rs +100 -29
- data/ext/ziperu/src/sink.rs +2 -7
- data/ext/ziperu/src/source.rs +4 -7
- data/ext/ziperu/src/stream.rs +89 -11
- data/ext/ziperu/src/test_rng.rs +37 -0
- data/ext/ziperu/src/writer.rs +71 -25
- data/ext/ziperu/src/zipcrypto.rs +40 -12
- data/lib/zip.rb +3 -3
- 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 +8 -1
data/ext/ziperu/src/extract.rs
CHANGED
|
@@ -11,9 +11,17 @@
|
|
|
11
11
|
//! normalised lexically (as `File.absolute_path` does), and the result must
|
|
12
12
|
//! start with the destination plus a separator. Nothing is created for an
|
|
13
13
|
//! entry that fails it; Ruby prints the same warning it would have.
|
|
14
|
+
//!
|
|
15
|
+
//! Below the destination nothing is followed through a link already on
|
|
16
|
+
//! disk (a symlink, or on Windows a junction): every directory on the way
|
|
17
|
+
//! to an entry, and the entry's own path, must be a real directory or
|
|
18
|
+
//! file, or the entry is skipped with a warning. The destination itself is
|
|
19
|
+
//! taken as given, link or not. This is checked before each path is used;
|
|
20
|
+
//! on Linux, `place.rs` also opens each file's directory beneath the
|
|
21
|
+
//! destination so that the kernel refuses a link swapped in meanwhile.
|
|
14
22
|
|
|
15
23
|
use std::collections::HashSet;
|
|
16
|
-
use std::fs::{self, FileTimes
|
|
24
|
+
use std::fs::{self, FileTimes};
|
|
17
25
|
use std::io::{self, Read, Write};
|
|
18
26
|
use std::path::{Path, PathBuf};
|
|
19
27
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
@@ -27,6 +35,7 @@ use zip::ZipArchive;
|
|
|
27
35
|
|
|
28
36
|
use crate::errors::{io_error, read_error, zip_error, DESTINATION_EXISTS_ERROR, ENTRY_SIZE_ERROR};
|
|
29
37
|
use crate::gvl::maybe_without_gvl;
|
|
38
|
+
use crate::place::{is_link_error, Beneath};
|
|
30
39
|
use crate::reader::{open_local_entry, EntryInfo};
|
|
31
40
|
use crate::source::Source;
|
|
32
41
|
|
|
@@ -80,9 +89,7 @@ impl From<io::Error> for Failure {
|
|
|
80
89
|
.get_ref()
|
|
81
90
|
.and_then(|inner| inner.downcast_ref::<ZipError>())
|
|
82
91
|
.is_some_and(|z| matches!(z, ZipError::DecompressedSizeLimitExceeded { .. }))
|
|
83
|
-
|| (e
|
|
84
|
-
&& e.to_string()
|
|
85
|
-
.contains("larger than its declared uncompressed size"));
|
|
92
|
+
|| crate::errors::is_size_limit(&e);
|
|
86
93
|
if limit {
|
|
87
94
|
Failure::SizeLimit
|
|
88
95
|
} else {
|
|
@@ -95,14 +102,25 @@ impl From<io::Error> for Failure {
|
|
|
95
102
|
enum Notice {
|
|
96
103
|
/// The entry resolved outside the destination; nothing was written.
|
|
97
104
|
Unsafe(String),
|
|
105
|
+
/// A link on disk stands at this path on the way to the entry, or at
|
|
106
|
+
/// the entry's own path; nothing was written.
|
|
107
|
+
Link(String),
|
|
98
108
|
/// Without size enforcement, more bytes came out than declared.
|
|
99
109
|
Oversize,
|
|
100
110
|
}
|
|
101
111
|
|
|
112
|
+
/// How an extraction is to be done, as Ruby asks for it.
|
|
113
|
+
pub struct Options {
|
|
114
|
+
pub enforce_size: bool,
|
|
115
|
+
pub threads: usize,
|
|
116
|
+
/// 0 raise, 1 overwrite, 2 skip (see `OnExists`).
|
|
117
|
+
pub on_exists: u8,
|
|
118
|
+
}
|
|
119
|
+
|
|
102
120
|
/// Extracts `specs` under `dest` (an absolute path) and returns one
|
|
103
121
|
/// `[index, kind, path]` triple per entry that was skipped as unsafe
|
|
104
|
-
/// (`kind` `:unsafe`)
|
|
105
|
-
/// nil). An existing file raises `Ziperu::DestinationExistsError`, a header
|
|
122
|
+
/// (`kind` `:unsafe`), skipped for a link in the way (`:link`, with the
|
|
123
|
+
/// link's path) or came out larger than declared (`:oversize`, path nil). An existing file raises `Ziperu::DestinationExistsError`, a header
|
|
106
124
|
/// that understates the size `Ziperu::EntrySizeError`; either leaves the
|
|
107
125
|
/// entries before it extracted, as the Ruby loop would.
|
|
108
126
|
///
|
|
@@ -110,14 +128,6 @@ enum Notice {
|
|
|
110
128
|
/// GVL), the directories are made first, in order, and the files are
|
|
111
129
|
/// then shared out among that many threads, each reading through its own
|
|
112
130
|
/// handle on the archive.
|
|
113
|
-
/// How an extraction is to be done, as Ruby asks for it.
|
|
114
|
-
pub struct Options {
|
|
115
|
-
pub enforce_size: bool,
|
|
116
|
-
pub threads: usize,
|
|
117
|
-
/// 0 raise, 1 overwrite, 2 skip (see `OnExists`).
|
|
118
|
-
pub on_exists: u8,
|
|
119
|
-
}
|
|
120
|
-
|
|
121
131
|
pub fn extract_entries(
|
|
122
132
|
ruby: &Ruby,
|
|
123
133
|
archive: ZipArchive<Source>,
|
|
@@ -184,10 +194,12 @@ pub fn extract_entries(
|
|
|
184
194
|
notices.sort_by_key(|(index, _)| *index);
|
|
185
195
|
let unsafe_kind = ruby.to_symbol("unsafe");
|
|
186
196
|
let oversize_kind = ruby.to_symbol("oversize");
|
|
197
|
+
let link_kind = ruby.to_symbol("link");
|
|
187
198
|
let out = ruby.ary_new_capa(notices.len());
|
|
188
199
|
for (index, notice) in notices {
|
|
189
200
|
let triple: (usize, Symbol, Option<String>) = match notice {
|
|
190
201
|
Notice::Unsafe(path) => (index, unsafe_kind, Some(path)),
|
|
202
|
+
Notice::Link(path) => (index, link_kind, Some(path)),
|
|
191
203
|
Notice::Oversize => (index, oversize_kind, None),
|
|
192
204
|
};
|
|
193
205
|
out.push(triple)?;
|
|
@@ -275,7 +287,10 @@ fn extract_files(
|
|
|
275
287
|
.collect();
|
|
276
288
|
workers
|
|
277
289
|
.into_iter()
|
|
278
|
-
.map(|w|
|
|
290
|
+
.map(|w| {
|
|
291
|
+
w.join()
|
|
292
|
+
.unwrap_or_else(|panic| std::panic::resume_unwind(panic))
|
|
293
|
+
})
|
|
279
294
|
.collect()
|
|
280
295
|
});
|
|
281
296
|
let mut notices = std::mem::take(&mut extractor.notices);
|
|
@@ -335,9 +350,35 @@ enum OnExists {
|
|
|
335
350
|
Skip,
|
|
336
351
|
}
|
|
337
352
|
|
|
353
|
+
/// What stands at a path, looked at without following a link there.
|
|
354
|
+
enum OnDisk {
|
|
355
|
+
Missing,
|
|
356
|
+
Directory,
|
|
357
|
+
/// A symlink, or on Windows a junction or other name-surrogate
|
|
358
|
+
/// reparse point (what `FileType::is_symlink` counts).
|
|
359
|
+
Link,
|
|
360
|
+
/// A file, or anything else that is neither a directory nor a link.
|
|
361
|
+
Other,
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
fn on_disk(path: &Path) -> io::Result<OnDisk> {
|
|
365
|
+
match fs::symlink_metadata(path) {
|
|
366
|
+
Ok(meta) if meta.file_type().is_symlink() => Ok(OnDisk::Link),
|
|
367
|
+
Ok(meta) if meta.is_dir() => Ok(OnDisk::Directory),
|
|
368
|
+
Ok(_) => Ok(OnDisk::Other),
|
|
369
|
+
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(OnDisk::Missing),
|
|
370
|
+
Err(e) => Err(e),
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
338
374
|
struct Extractor<'a> {
|
|
339
375
|
archive: ZipArchive<Source>,
|
|
340
376
|
dest: &'a str,
|
|
377
|
+
/// `dest` as `resolve` has it, with one trailing separator: what every
|
|
378
|
+
/// entry path starts with.
|
|
379
|
+
prefix: String,
|
|
380
|
+
/// `dest` as `resolve` has it.
|
|
381
|
+
root: String,
|
|
341
382
|
enforce_size: bool,
|
|
342
383
|
on_exists: OnExists,
|
|
343
384
|
/// Directories known to exist, so each parent is created once.
|
|
@@ -345,6 +386,9 @@ struct Extractor<'a> {
|
|
|
345
386
|
/// Directories made for an entry, with the times and mode to give
|
|
346
387
|
/// them once their files are in.
|
|
347
388
|
stamp: Vec<(usize, PathBuf, Option<SystemTime>, Option<u32>)>,
|
|
389
|
+
/// The destination opened for kernel-checked lookups beneath it;
|
|
390
|
+
/// made once the destination exists.
|
|
391
|
+
beneath: Option<Beneath>,
|
|
348
392
|
/// The place in the batch of the spec being extracted.
|
|
349
393
|
at: usize,
|
|
350
394
|
buf: Vec<u8>,
|
|
@@ -358,21 +402,28 @@ impl<'a> Extractor<'a> {
|
|
|
358
402
|
enforce_size: bool,
|
|
359
403
|
on_exists: OnExists,
|
|
360
404
|
) -> Self {
|
|
405
|
+
let root = resolve(dest, "");
|
|
406
|
+
let prefix = if root.ends_with('/') {
|
|
407
|
+
root.clone()
|
|
408
|
+
} else {
|
|
409
|
+
format!("{root}/")
|
|
410
|
+
};
|
|
361
411
|
Self {
|
|
362
412
|
archive,
|
|
363
413
|
dest,
|
|
414
|
+
prefix,
|
|
415
|
+
root,
|
|
364
416
|
enforce_size,
|
|
365
417
|
on_exists,
|
|
366
418
|
made: HashSet::new(),
|
|
367
419
|
stamp: Vec::new(),
|
|
420
|
+
beneath: None,
|
|
368
421
|
at: 0,
|
|
369
422
|
buf: vec![0; CHUNK],
|
|
370
423
|
notices: Vec::new(),
|
|
371
424
|
}
|
|
372
425
|
}
|
|
373
426
|
|
|
374
|
-
/// Extracts `specs` in order; `offset` is the index of the first in
|
|
375
|
-
/// the whole batch, for naming the one that failed.
|
|
376
427
|
/// Extracts `specs`, each with its place in the whole batch, in
|
|
377
428
|
/// order; a failure names the place.
|
|
378
429
|
fn extract_each(&mut self, specs: &[(usize, &Spec)]) -> Result<(), (usize, Failure)> {
|
|
@@ -389,7 +440,6 @@ impl<'a> Extractor<'a> {
|
|
|
389
440
|
self.notices.push((spec.index, Notice::Unsafe(shown)));
|
|
390
441
|
return Ok(());
|
|
391
442
|
};
|
|
392
|
-
let path = PathBuf::from(path);
|
|
393
443
|
if spec.directory {
|
|
394
444
|
self.create_directory(&path, spec)
|
|
395
445
|
} else {
|
|
@@ -397,23 +447,110 @@ impl<'a> Extractor<'a> {
|
|
|
397
447
|
}
|
|
398
448
|
}
|
|
399
449
|
|
|
400
|
-
|
|
401
|
-
|
|
450
|
+
/// `dir` relative to the destination, `/`-separated; empty for the
|
|
451
|
+
/// destination itself.
|
|
452
|
+
fn below<'p>(&self, dir: &'p str) -> &'p str {
|
|
453
|
+
if dir == self.root || format!("{dir}/") == self.prefix {
|
|
454
|
+
return "";
|
|
455
|
+
}
|
|
456
|
+
dir.strip_prefix(self.prefix.as_str()).unwrap_or(dir)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/// The destination, opened on first use (it exists by then).
|
|
460
|
+
fn beneath(&mut self) -> &Beneath {
|
|
461
|
+
let root = &self.root;
|
|
462
|
+
self.beneath
|
|
463
|
+
.get_or_insert_with(|| Beneath::new(Path::new(root)))
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/// Makes sure every directory between the destination and `path` (not
|
|
467
|
+
/// `path` itself) is a real directory, creating the missing ones.
|
|
468
|
+
/// Returns false, having noted why if it is a link, when the entry is
|
|
469
|
+
/// to be left out.
|
|
470
|
+
fn prepare_parents(&mut self, path: &str, spec: &Spec) -> Result<bool, Failure> {
|
|
471
|
+
let Some((parent, _)) = path.rsplit_once('/') else {
|
|
472
|
+
return Ok(true);
|
|
473
|
+
};
|
|
474
|
+
if self.made.contains(Path::new(parent)) {
|
|
475
|
+
return Ok(true);
|
|
476
|
+
}
|
|
477
|
+
// The destination is taken as given, a link or not, and made if
|
|
478
|
+
// it is not there yet.
|
|
479
|
+
if !self.made.contains(Path::new(&self.root)) {
|
|
480
|
+
let root = PathBuf::from(&self.root);
|
|
481
|
+
fs::create_dir_all(&root).map_err(at(&root))?;
|
|
482
|
+
self.made.insert(root);
|
|
483
|
+
}
|
|
484
|
+
let Some(below) = format!("{parent}/")
|
|
485
|
+
.strip_prefix(&self.prefix)
|
|
486
|
+
.map(str::to_string)
|
|
487
|
+
else {
|
|
488
|
+
return Ok(true);
|
|
489
|
+
};
|
|
490
|
+
let mut walked = self.prefix.clone();
|
|
491
|
+
for component in below.split('/').filter(|c| !c.is_empty()) {
|
|
492
|
+
if !walked.ends_with('/') {
|
|
493
|
+
walked.push('/');
|
|
494
|
+
}
|
|
495
|
+
walked.push_str(component);
|
|
496
|
+
let step = PathBuf::from(&walked);
|
|
497
|
+
if self.made.contains(&step) {
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
match on_disk(&step).map_err(at(&step))? {
|
|
501
|
+
OnDisk::Directory => {}
|
|
502
|
+
OnDisk::Missing => match fs::create_dir(&step) {
|
|
503
|
+
Ok(()) => {}
|
|
504
|
+
// Another thread made it meanwhile.
|
|
505
|
+
Err(e)
|
|
506
|
+
if e.kind() == io::ErrorKind::AlreadyExists
|
|
507
|
+
&& matches!(on_disk(&step), Ok(OnDisk::Directory)) => {}
|
|
508
|
+
Err(e) => return Err(at(&step)(e)),
|
|
509
|
+
},
|
|
510
|
+
OnDisk::Link => {
|
|
511
|
+
self.notices.push((spec.index, Notice::Link(walked)));
|
|
512
|
+
return Ok(false);
|
|
513
|
+
}
|
|
514
|
+
// A file where a directory should be: the entry cannot go
|
|
515
|
+
// there. Skipped under `:skip`, an error otherwise (a
|
|
516
|
+
// directory entry, when there is one, has already had its
|
|
517
|
+
// say under `:overwrite`).
|
|
518
|
+
OnDisk::Other => {
|
|
519
|
+
return match self.on_exists {
|
|
520
|
+
OnExists::Skip => Ok(false),
|
|
521
|
+
_ => Err(Failure::Exists(walked)),
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
self.made.insert(step);
|
|
526
|
+
}
|
|
527
|
+
Ok(true)
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
fn create_directory(&mut self, path: &str, spec: &Spec) -> Result<(), Failure> {
|
|
531
|
+
let dir = PathBuf::from(path);
|
|
532
|
+
if self.made.contains(&dir) || !self.prepare_parents(path, spec)? {
|
|
402
533
|
return Ok(());
|
|
403
534
|
}
|
|
404
|
-
match
|
|
405
|
-
|
|
406
|
-
self.made.insert(
|
|
535
|
+
match on_disk(&dir).map_err(at(&dir))? {
|
|
536
|
+
OnDisk::Directory => {
|
|
537
|
+
self.made.insert(dir);
|
|
538
|
+
return Ok(());
|
|
539
|
+
}
|
|
540
|
+
OnDisk::Link => {
|
|
541
|
+
self.notices
|
|
542
|
+
.push((spec.index, Notice::Link(path.to_string())));
|
|
407
543
|
return Ok(());
|
|
408
544
|
}
|
|
409
|
-
|
|
410
|
-
OnExists::Raise => return Err(Failure::Exists(path_string(
|
|
545
|
+
OnDisk::Other => match self.on_exists {
|
|
546
|
+
OnExists::Raise => return Err(Failure::Exists(path_string(&dir))),
|
|
411
547
|
OnExists::Skip => return Ok(()),
|
|
412
|
-
OnExists::Overwrite => fs::remove_file(
|
|
548
|
+
OnExists::Overwrite => fs::remove_file(&dir).map_err(at(&dir))?,
|
|
413
549
|
},
|
|
414
|
-
|
|
550
|
+
OnDisk::Missing => {}
|
|
415
551
|
}
|
|
416
|
-
fs::
|
|
552
|
+
fs::create_dir(&dir).map_err(at(&dir))?;
|
|
553
|
+
let path = dir.as_path();
|
|
417
554
|
self.made.insert(path.to_path_buf());
|
|
418
555
|
if spec.mtime.is_some() || spec.mode.is_some() {
|
|
419
556
|
self.stamp
|
|
@@ -428,62 +565,103 @@ impl<'a> Extractor<'a> {
|
|
|
428
565
|
let mut stamp = std::mem::take(&mut self.stamp);
|
|
429
566
|
stamp.sort_by_key(|(_, path, _, _)| std::cmp::Reverse(path.components().count()));
|
|
430
567
|
for (i, path, mtime, mode) in stamp {
|
|
568
|
+
let below = self.below(&path_string(&path)).to_string();
|
|
431
569
|
let path = path.as_path();
|
|
570
|
+
// Through a handle opened beneath the destination where the
|
|
571
|
+
// OS can, so a link swapped in cannot redirect a chmod.
|
|
572
|
+
let dir = self
|
|
573
|
+
.beneath()
|
|
574
|
+
.directory_handle(path, &below)
|
|
575
|
+
.map_err(|e| (i, at(path)(e)))?;
|
|
432
576
|
if let Some(mtime) = mtime {
|
|
433
577
|
let times = FileTimes::new().set_accessed(mtime).set_modified(mtime);
|
|
434
|
-
|
|
435
|
-
.and_then(|dir| dir.set_times(times))
|
|
436
|
-
.map_err(|e| (i, at(path)(e)))?;
|
|
578
|
+
dir.set_times(times).map_err(|e| (i, at(path)(e)))?;
|
|
437
579
|
}
|
|
438
580
|
if let Some(mode) = mode {
|
|
439
|
-
|
|
581
|
+
set_directory_mode(&dir, path, mode).map_err(|e| (i, at(path)(e)))?;
|
|
440
582
|
}
|
|
441
583
|
}
|
|
442
584
|
Ok(())
|
|
443
585
|
}
|
|
444
586
|
|
|
445
|
-
fn create_file(&mut self,
|
|
446
|
-
if
|
|
447
|
-
|
|
448
|
-
// A file where the parent directory should be: the entry
|
|
449
|
-
// cannot go there. Skipped under `:skip`, an error
|
|
450
|
-
// otherwise (a directory entry, when there is one, has
|
|
451
|
-
// already had its say under `:overwrite`).
|
|
452
|
-
if fs::metadata(parent).is_ok_and(|m| !m.is_dir()) {
|
|
453
|
-
return match self.on_exists {
|
|
454
|
-
OnExists::Skip => Ok(()),
|
|
455
|
-
_ => Err(Failure::Exists(path_string(parent))),
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
fs::create_dir_all(parent).map_err(at(parent))?;
|
|
459
|
-
self.made.insert(parent.to_path_buf());
|
|
460
|
-
}
|
|
587
|
+
fn create_file(&mut self, name: &str, spec: &Spec) -> Result<(), Failure> {
|
|
588
|
+
if !self.prepare_parents(name, spec)? {
|
|
589
|
+
return Ok(());
|
|
461
590
|
}
|
|
462
|
-
let
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
591
|
+
let path = Path::new(name);
|
|
592
|
+
let replace = match (on_disk(path).map_err(at(path))?, self.on_exists) {
|
|
593
|
+
// Never written through, whatever the policy.
|
|
594
|
+
(OnDisk::Link, _) => {
|
|
595
|
+
self.notices
|
|
596
|
+
.push((spec.index, Notice::Link(name.to_string())));
|
|
597
|
+
return Ok(());
|
|
598
|
+
}
|
|
599
|
+
(OnDisk::Missing, _) => false,
|
|
600
|
+
(_, OnExists::Raise) => return Err(Failure::Exists(path_string(path))),
|
|
601
|
+
(_, OnExists::Skip) => return Ok(()),
|
|
468
602
|
// A directory in the way is not overwritten by a file.
|
|
469
|
-
OnExists::Overwrite
|
|
470
|
-
|
|
603
|
+
(OnDisk::Directory, OnExists::Overwrite) => {
|
|
604
|
+
return Err(Failure::Exists(path_string(path)))
|
|
605
|
+
}
|
|
606
|
+
(OnDisk::Other, OnExists::Overwrite) => true,
|
|
471
607
|
};
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
608
|
+
// The directory it goes in, opened beneath the destination where
|
|
609
|
+
// the OS can: a link swapped in since the walk is refused here.
|
|
610
|
+
let (parent, leaf) = name.rsplit_once('/').unwrap_or(("", name));
|
|
611
|
+
let parent = if parent.is_empty() { "/" } else { parent };
|
|
612
|
+
let below = self.below(parent).to_string();
|
|
613
|
+
let place = match self.beneath().place(Path::new(parent), &below) {
|
|
614
|
+
Ok(place) => place,
|
|
615
|
+
Err(e) if is_link_error(&e) => {
|
|
616
|
+
self.notices
|
|
617
|
+
.push((spec.index, Notice::Link(parent.to_string())));
|
|
618
|
+
return Ok(());
|
|
476
619
|
}
|
|
477
|
-
Err(e) => return Err(at(
|
|
620
|
+
Err(e) => return Err(at(Path::new(parent))(e)),
|
|
478
621
|
};
|
|
479
|
-
|
|
622
|
+
// A file being replaced is written beside it and renamed over it
|
|
623
|
+
// once complete, so a failure part-way leaves the original as it
|
|
624
|
+
// was; a new one is written in place.
|
|
625
|
+
let (target, mut file) = if replace {
|
|
626
|
+
place.staging(leaf).map_err(at(path))?
|
|
627
|
+
} else {
|
|
628
|
+
match place.create_new(leaf) {
|
|
629
|
+
Ok(f) => (leaf.to_string(), f),
|
|
630
|
+
Err(e) if is_link_error(&e) => {
|
|
631
|
+
self.notices
|
|
632
|
+
.push((spec.index, Notice::Link(name.to_string())));
|
|
633
|
+
return Ok(());
|
|
634
|
+
}
|
|
635
|
+
Err(e) if e.kind() == io::ErrorKind::AlreadyExists || path.exists() => {
|
|
636
|
+
return Err(Failure::Exists(path_string(path)));
|
|
637
|
+
}
|
|
638
|
+
Err(e) => return Err(at(path)(e)),
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
let outcome = self.copy_entry(spec.index, &mut file).and_then(|written| {
|
|
642
|
+
if let Some(mtime) = spec.mtime {
|
|
643
|
+
file.set_times(FileTimes::new().set_accessed(mtime).set_modified(mtime))
|
|
644
|
+
.map_err(at(path))?;
|
|
645
|
+
}
|
|
646
|
+
if let Some(mode) = spec.mode {
|
|
647
|
+
set_file_mode(&file, mode).map_err(at(path))?;
|
|
648
|
+
}
|
|
649
|
+
Ok(written)
|
|
650
|
+
});
|
|
651
|
+
drop(file);
|
|
652
|
+
let outcome = outcome.and_then(|written| {
|
|
653
|
+
if replace {
|
|
654
|
+
place.rename(&target, leaf).map_err(at(path))?;
|
|
655
|
+
}
|
|
656
|
+
Ok(written)
|
|
657
|
+
});
|
|
658
|
+
let written = match outcome {
|
|
480
659
|
Ok(n) => n,
|
|
481
660
|
Err(failure) => {
|
|
482
661
|
// Interrupted or failed part-way: a half-written file
|
|
483
662
|
// would pass for the entry, and stand in the way of a
|
|
484
|
-
// second run.
|
|
485
|
-
|
|
486
|
-
let _ = fs::remove_file(path);
|
|
663
|
+
// second run. What it replaces is still there.
|
|
664
|
+
let _ = place.remove(&target);
|
|
487
665
|
return Err(match failure {
|
|
488
666
|
// Reading the entry is the entry's problem; writing is
|
|
489
667
|
// the path's.
|
|
@@ -497,14 +675,6 @@ impl<'a> Extractor<'a> {
|
|
|
497
675
|
});
|
|
498
676
|
}
|
|
499
677
|
};
|
|
500
|
-
if let Some(mtime) = spec.mtime {
|
|
501
|
-
file.set_times(FileTimes::new().set_accessed(mtime).set_modified(mtime))
|
|
502
|
-
.map_err(at(path))?;
|
|
503
|
-
}
|
|
504
|
-
if let Some(mode) = spec.mode {
|
|
505
|
-
set_file_mode(&file, mode).map_err(at(path))?;
|
|
506
|
-
}
|
|
507
|
-
drop(file);
|
|
508
678
|
if !self.enforce_size {
|
|
509
679
|
let declared = self.archive.metadata().entry(spec.index)?.size();
|
|
510
680
|
if written > declared {
|
|
@@ -547,14 +717,7 @@ impl<'a> Extractor<'a> {
|
|
|
547
717
|
let info = {
|
|
548
718
|
let meta = self.archive.metadata();
|
|
549
719
|
let raw = meta.entry(index)?;
|
|
550
|
-
EntryInfo
|
|
551
|
-
index: Some(index),
|
|
552
|
-
header_start: raw.header_start(),
|
|
553
|
-
size: raw.size(),
|
|
554
|
-
compressed_size: raw.compressed_size(),
|
|
555
|
-
crc32: raw.crc32(),
|
|
556
|
-
method: Some(crate::archive::compression_code(raw.compression())),
|
|
557
|
-
}
|
|
720
|
+
EntryInfo::from_entry(index, &raw)
|
|
558
721
|
};
|
|
559
722
|
let mut source = self.archive.clone().into_inner();
|
|
560
723
|
let mut entry = open_local_entry(&mut source, &info, None, u64::MAX)?;
|
|
@@ -650,35 +813,16 @@ fn is_sep(b: u8) -> bool {
|
|
|
650
813
|
b == b'/' || b == b'\\'
|
|
651
814
|
}
|
|
652
815
|
|
|
653
|
-
/// A directory handle that `set_times` accepts: on Windows that means
|
|
654
|
-
/// backup semantics and write-attributes access, which a plain `open`
|
|
655
|
-
/// does not ask for.
|
|
656
|
-
#[cfg(windows)]
|
|
657
|
-
fn open_directory(path: &Path) -> io::Result<fs::File> {
|
|
658
|
-
use std::os::windows::fs::OpenOptionsExt;
|
|
659
|
-
const FILE_WRITE_ATTRIBUTES: u32 = 0x0100;
|
|
660
|
-
const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
|
|
661
|
-
OpenOptions::new()
|
|
662
|
-
.access_mode(FILE_WRITE_ATTRIBUTES)
|
|
663
|
-
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
|
|
664
|
-
.open(path)
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
#[cfg(not(windows))]
|
|
668
|
-
fn open_directory(path: &Path) -> io::Result<fs::File> {
|
|
669
|
-
fs::File::open(path)
|
|
670
|
-
}
|
|
671
|
-
|
|
672
816
|
#[cfg(unix)]
|
|
673
817
|
fn set_file_mode(file: &fs::File, mode: u32) -> io::Result<()> {
|
|
674
818
|
use std::os::unix::fs::PermissionsExt;
|
|
675
819
|
file.set_permissions(fs::Permissions::from_mode(mode))
|
|
676
820
|
}
|
|
677
821
|
|
|
822
|
+
/// Sets a directory's mode through its handle (fchmod).
|
|
678
823
|
#[cfg(unix)]
|
|
679
|
-
fn
|
|
680
|
-
|
|
681
|
-
fs::set_permissions(path, fs::Permissions::from_mode(mode))
|
|
824
|
+
fn set_directory_mode(dir: &fs::File, _path: &Path, mode: u32) -> io::Result<()> {
|
|
825
|
+
set_file_mode(dir, mode)
|
|
682
826
|
}
|
|
683
827
|
|
|
684
828
|
/// Ruby's `File.chmod` on Windows only knows the read-only attribute,
|
|
@@ -693,8 +837,10 @@ fn set_file_mode(file: &fs::File, mode: u32) -> io::Result<()> {
|
|
|
693
837
|
Ok(())
|
|
694
838
|
}
|
|
695
839
|
|
|
840
|
+
/// On Windows by path: the handle opened for times cannot change the
|
|
841
|
+
/// read-only attribute.
|
|
696
842
|
#[cfg(not(unix))]
|
|
697
|
-
fn
|
|
843
|
+
fn set_directory_mode(_dir: &fs::File, path: &Path, mode: u32) -> io::Result<()> {
|
|
698
844
|
if mode & 0o200 == 0 {
|
|
699
845
|
let mut perms = fs::metadata(path)?.permissions();
|
|
700
846
|
perms.set_readonly(true);
|
|
@@ -744,4 +890,86 @@ mod tests {
|
|
|
744
890
|
);
|
|
745
891
|
assert_eq!(safe_path("C:/", "x").as_deref(), Some("C:/x"));
|
|
746
892
|
}
|
|
893
|
+
|
|
894
|
+
/// A name made of the pieces that matter to the check: separators of
|
|
895
|
+
/// both kinds, `.`, `..`, empty components, drive letters and plain
|
|
896
|
+
/// names.
|
|
897
|
+
fn random_name(rng: &mut crate::test_rng::Rng) -> String {
|
|
898
|
+
const PIECES: &[&str] = &[
|
|
899
|
+
"a",
|
|
900
|
+
"b",
|
|
901
|
+
"dest",
|
|
902
|
+
"dest_evil",
|
|
903
|
+
"..",
|
|
904
|
+
".",
|
|
905
|
+
"",
|
|
906
|
+
"/",
|
|
907
|
+
"\\",
|
|
908
|
+
"C:",
|
|
909
|
+
"x.txt",
|
|
910
|
+
];
|
|
911
|
+
let len = rng.below(8);
|
|
912
|
+
let mut name = String::new();
|
|
913
|
+
for _ in 0..len {
|
|
914
|
+
name.push_str(rng.pick(PIECES));
|
|
915
|
+
name.push(*rng.pick(&['/', '\\', '/']));
|
|
916
|
+
}
|
|
917
|
+
name.push_str(rng.pick(PIECES));
|
|
918
|
+
name
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
#[test]
|
|
922
|
+
fn property_a_safe_path_is_strictly_inside_and_normal() {
|
|
923
|
+
const DESTS: &[&str] = &["/p/dest", "/p/dest/", "C:/p/dest", "//srv/share/dest", "/"];
|
|
924
|
+
let mut rng = crate::test_rng::Rng::new(1);
|
|
925
|
+
for case in 0..crate::test_rng::CASES {
|
|
926
|
+
let dest = *rng.pick(DESTS);
|
|
927
|
+
let name = random_name(&mut rng);
|
|
928
|
+
let resolved = resolve(dest, &name);
|
|
929
|
+
// Normalised: nothing a second pass would change.
|
|
930
|
+
assert_eq!(
|
|
931
|
+
resolve(&resolved, ""),
|
|
932
|
+
resolved,
|
|
933
|
+
"case {case}: {dest:?} + {name:?}"
|
|
934
|
+
);
|
|
935
|
+
assert!(!resolved.contains('\\'), "case {case}: {resolved:?}");
|
|
936
|
+
let Some(path) = safe_path(dest, &name) else {
|
|
937
|
+
continue;
|
|
938
|
+
};
|
|
939
|
+
assert_eq!(path, resolved);
|
|
940
|
+
if dest != "/" {
|
|
941
|
+
let root = resolve(dest, "");
|
|
942
|
+
let prefix = format!("{}/", root.trim_end_matches('/'));
|
|
943
|
+
assert!(
|
|
944
|
+
path.starts_with(&prefix),
|
|
945
|
+
"case {case}: {path:?} not under {prefix:?}"
|
|
946
|
+
);
|
|
947
|
+
assert!(
|
|
948
|
+
path.len() > prefix.len(),
|
|
949
|
+
"case {case}: the destination itself"
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
let below = &path[resolve(dest, "").len()..];
|
|
953
|
+
assert!(
|
|
954
|
+
below.split('/').all(|c| c != ".." && c != "."),
|
|
955
|
+
"case {case}: {path:?}"
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
#[test]
|
|
961
|
+
fn property_a_name_without_dot_dot_stays_inside() {
|
|
962
|
+
let mut rng = crate::test_rng::Rng::new(2);
|
|
963
|
+
for case in 0..crate::test_rng::CASES {
|
|
964
|
+
let name = random_name(&mut rng);
|
|
965
|
+
let climbs = name.split(['/', '\\']).any(|c| c == "..");
|
|
966
|
+
let empty = name.split(['/', '\\']).all(|c| c.is_empty() || c == ".");
|
|
967
|
+
if !climbs && !empty {
|
|
968
|
+
assert!(
|
|
969
|
+
safe_path("/p/dest", &name).is_some(),
|
|
970
|
+
"case {case}: {name:?}"
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}
|
|
747
975
|
}
|