exarch-core 0.9.0

Memory-safe archive extraction library with security validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//! Request values: one type per operation, optional inputs as setters.
//!
//! Every operation (extract, list, verify, extract into a sink, create) is
//! described by a request that holds the configuration it runs under, the
//! options of that operation and an optional [`ProgressCallback`]. A terminal
//! method runs it. Adding an optional input to an operation is a setter on its
//! request, not another function per combination.
//!
//! The plain functions ([`extract_archive`](crate::extract_archive),
//! [`list_archive`](crate::list_archive), ...) remain for the common case; they
//! are requests with the defaults.

use std::io::Cursor;
use std::path::Path;
use std::path::PathBuf;

use crate::ArchiveReader;
use crate::ArchiveWriter;
use crate::DecodeReuse;
use crate::EntrySink;
use crate::ExtractionOptions;
use crate::ExtractionReport;
use crate::ListOptions;
use crate::NoopProgress;
use crate::ProgressCallback;
use crate::Result;
use crate::SecurityConfig;
use crate::SinkOptions;
use crate::VerificationOptions;
use crate::api::extract_staged;
use crate::api::extract_to_sink_reusing;
use crate::api::extract_validated;
use crate::api::resolve_output_format;
use crate::closed_enum::closed_enum;
use crate::creation::CreationConfig;
use crate::creation::CreationReport;
use crate::creation::progress::check_cancelled;
use crate::creation::staged::StagedOutput;
use crate::creation::tar::write_tar;
use crate::creation::zip::warn_on_extraction_ratio;
use crate::creation::zip::write_zip;
use crate::formats::detect::CreatableFormat;
use crate::formats::detect::CreationFamily;
use crate::inspection::ArchiveManifest;
use crate::inspection::VerificationReport;
use crate::inspection::list::ListPass;
use crate::listed::ListedArchive;

/// The optional progress callback of a request; a missing one is a no-op.
struct OptionalProgress<'a>(Option<&'a mut dyn ProgressCallback>);

impl std::fmt::Debug for OptionalProgress<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(if self.0.is_some() { "Some(..)" } else { "None" })
    }
}

impl OptionalProgress<'_> {
    fn run<R>(self, operation: impl FnOnce(&mut dyn ProgressCallback) -> R) -> R {
        match self.0 {
            Some(progress) => operation(progress),
            None => operation(&mut NoopProgress),
        }
    }
}

/// Extracts an archive into a directory.
///
/// Holds the [`SecurityConfig`] the extraction runs under, the
/// [`ExtractionOptions`] and an optional [`ProgressCallback`]. Every entry
/// passes the validation pipeline before it is written.
///
/// # Errors
///
/// [`extract`](Self::extract) returns an error if:
/// - the archive cannot be opened
/// - the archive format is unsupported
/// - security validation fails
/// - an I/O operation fails
/// - the atomic staging directory cannot be created or renamed into place
///
/// # Examples
///
/// ```no_run
/// use exarch_core::ExtractRequest;
/// use exarch_core::ExtractionOptions;
/// use exarch_core::NoopProgress;
/// use exarch_core::SecurityConfig;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = SecurityConfig::default();
/// let mut progress = NoopProgress;
/// let report = ExtractRequest::new(&config)
///     .with_options(ExtractionOptions::default().with_atomic(true))
///     .with_progress(&mut progress)
///     .extract("archive.tar.gz", "/tmp/output")?;
/// println!("Extracted {} files", report.files_extracted);
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct ExtractRequest<'a> {
    config: &'a SecurityConfig,
    options: ExtractionOptions,
    progress: OptionalProgress<'a>,
}

impl<'a> ExtractRequest<'a> {
    /// An extraction under `config` with default options and no progress
    /// callback.
    #[must_use]
    pub fn new(config: &'a SecurityConfig) -> Self {
        Self {
            config,
            options: ExtractionOptions::default(),
            progress: OptionalProgress(None),
        }
    }

    /// Sets the extraction options.
    #[must_use]
    pub fn with_options(mut self, options: ExtractionOptions) -> Self {
        self.options = options;
        self
    }

    /// Reports progress to `progress`.
    #[must_use]
    pub fn with_progress(mut self, progress: &'a mut dyn ProgressCallback) -> Self {
        self.progress = OptionalProgress(Some(progress));
        self
    }

    /// Extracts `archive` into `output_dir`; the format is detected.
    ///
    /// # Errors
    ///
    /// See [`ExtractRequest`].
    pub fn extract(
        self,
        archive: impl AsRef<Path>,
        output_dir: impl AsRef<Path>,
    ) -> Result<ExtractionReport> {
        let Self {
            config,
            options,
            progress,
        } = self;
        progress.run(|progress| {
            extract_staged(output_dir.as_ref(), &options, |dir| {
                extract_validated(archive.as_ref(), dir, config, &options, progress)
            })
        })
    }
}

/// Lists the contents of an archive without extracting it.
///
/// Holds the [`SecurityConfig`] whose quotas and path rules the listing
/// enforces, the [`ListOptions`] and an optional [`ProgressCallback`], which
/// sees one `on_entry_start` / `on_entry_complete` pair per listed entry
/// (`total` is unknown for TAR) and `on_complete` once after the manifest was
/// built. Nothing is written to disk.
///
/// # Errors
///
/// [`list`](Self::list) and [`list_reader`](Self::list_reader) return an error
/// if:
/// - the archive cannot be opened or its format is unsupported
/// - the archive is corrupted
/// - a quota is exceeded (file count, total size, single file size)
/// - declared compressed sizes exceed the archive length
///
/// A reader source is neither opened nor detected, so a malformed archive fails
/// as it would from a file. `on_complete` is not called on failure.
///
/// # Examples
///
/// ```no_run
/// use exarch_core::ListRequest;
/// use exarch_core::SecurityConfig;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = SecurityConfig::default();
/// let manifest = ListRequest::new(&config).list("archive.tar.gz")?;
/// println!("{} entries", manifest.total_entries());
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct ListRequest<'a> {
    config: &'a SecurityConfig,
    options: ListOptions,
    progress: OptionalProgress<'a>,
}

impl<'a> ListRequest<'a> {
    /// A listing under `config` with default options and no progress callback.
    #[must_use]
    pub fn new(config: &'a SecurityConfig) -> Self {
        Self {
            config,
            options: ListOptions::default(),
            progress: OptionalProgress(None),
        }
    }

    /// Sets the listing options.
    #[must_use]
    pub const fn with_options(mut self, options: ListOptions) -> Self {
        self.options = options;
        self
    }

    /// Reports progress to `progress`.
    #[must_use]
    pub fn with_progress(mut self, progress: &'a mut dyn ProgressCallback) -> Self {
        self.progress = OptionalProgress(Some(progress));
        self
    }

    /// Lists the archive file at `archive`.
    ///
    /// # Errors
    ///
    /// See [`ListRequest`].
    pub fn list(self, archive: impl AsRef<Path>) -> Result<ArchiveManifest> {
        let Self {
            config,
            options,
            progress,
        } = self;
        progress.run(|progress| {
            crate::inspection::list_archive_with_progress(archive, config, options, progress)
        })
    }

    /// Lists an archive read from `reader`.
    ///
    /// # Errors
    ///
    /// See [`ListRequest`].
    pub fn list_reader(self, reader: ArchiveReader<'_>) -> Result<ArchiveManifest> {
        let Self {
            config,
            options,
            progress,
        } = self;
        progress.run(|progress| {
            let manifest = reader.list(
                config,
                &mut ListPass::strict_with(options.stored_targets).with_progress(progress),
            )?;
            progress.on_complete();
            Ok(manifest)
        })
    }

    /// Lists the archive at `archive` and keeps what extraction can reuse.
    ///
    /// `config` is the policy the **extraction** will run under. The list pass
    /// itself runs under the same quotas, path rules, allowlist and decoder
    /// limits with the flags that only matter once bytes are written (symlinks,
    /// hardlinks, world-writable files, preserved permissions) turned off, so
    /// that a listing never admits more than the extraction would. The
    /// returned [`ListedArchive`] extracts under `config`, so what was listed
    /// is what is extracted.
    ///
    /// `reuse` says where a ZIP archive keeps the output of its structural
    /// check for the extraction; for a compressed TAR the decoder output is
    /// spooled instead. See [`ListedArchive`].
    ///
    /// The progress callback, if any, sees the list pass's entry events;
    /// `on_complete` is left to the extraction.
    ///
    /// # Errors
    ///
    /// See [`ListRequest`].
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::DecodeReuse;
    /// use exarch_core::ListRequest;
    /// use exarch_core::SecurityConfig;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let dir = tempfile::tempdir()?;
    /// # let archive = dir.path().join("a.tar");
    /// # let mut tar = tar::Builder::new(Vec::new());
    /// # let mut header = tar::Header::new_gnu();
    /// # header.set_size(2);
    /// # header.set_mode(0o644);
    /// # tar.append_data(&mut header, "a.txt", &b"hi"[..])?;
    /// # std::fs::write(&archive, tar.into_inner()?)?;
    /// let config = SecurityConfig::default();
    /// let listed = ListRequest::new(&config).for_extraction(&archive, DecodeReuse::default())?;
    /// assert_eq!(listed.manifest().total_entries(), 1);
    /// # Ok(())
    /// # }
    /// ```
    pub fn for_extraction(
        self,
        archive: impl AsRef<Path>,
        reuse: DecodeReuse,
    ) -> Result<ListedArchive> {
        let Self {
            config,
            options,
            progress,
        } = self;
        progress.run(|progress| {
            ListedArchive::list(archive.as_ref(), config, options, reuse, Some(progress))
        })
    }
}

/// Verifies an archive's security without extracting it.
///
/// Holds the [`SecurityConfig`] to validate against, the
/// [`VerificationOptions`] (whether to also check payload integrity) and an
/// optional [`ProgressCallback`], which sees one `on_entry_start` /
/// `on_entry_complete` pair per scanned entry and `on_complete` once when a
/// report is returned, including one whose status is `Fail`.
///
/// # Errors
///
/// [`verify`](Self::verify) and [`verify_reader`](Self::verify_reader) return
/// an error if:
/// - the archive cannot be opened or its format is unsupported
/// - the archive structure cannot be read, or declared compressed sizes exceed
///   the archive length
/// - an entry name is a path traversal: the listing step rejects it with
///   [`ArchiveError::PathTraversal`](crate::ArchiveError::PathTraversal) before
///   a report exists
/// - integrity is requested and a TAR entry's content fails to decode
///
/// Other security violations are reported in
/// [`VerificationReport::issues`]. `on_complete` is not called on failure.
///
/// # Examples
///
/// ```no_run
/// use exarch_core::SecurityConfig;
/// use exarch_core::VerificationStatus;
/// use exarch_core::VerifyRequest;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = SecurityConfig::default();
/// let report = VerifyRequest::new(&config).verify("archive.zip")?;
/// assert_eq!(report.status(), VerificationStatus::Pass);
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct VerifyRequest<'a> {
    config: &'a SecurityConfig,
    options: VerificationOptions,
    progress: OptionalProgress<'a>,
}

impl<'a> VerifyRequest<'a> {
    /// A verification under `config` with default options and no progress
    /// callback.
    #[must_use]
    pub fn new(config: &'a SecurityConfig) -> Self {
        Self {
            config,
            options: VerificationOptions::default(),
            progress: OptionalProgress(None),
        }
    }

    /// Sets the verification options.
    #[must_use]
    pub const fn with_options(mut self, options: VerificationOptions) -> Self {
        self.options = options;
        self
    }

    /// Reports progress to `progress`.
    #[must_use]
    pub fn with_progress(mut self, progress: &'a mut dyn ProgressCallback) -> Self {
        self.progress = OptionalProgress(Some(progress));
        self
    }

    /// Verifies the archive file at `archive`.
    ///
    /// # Errors
    ///
    /// See [`VerifyRequest`].
    pub fn verify(self, archive: impl AsRef<Path>) -> Result<VerificationReport> {
        let Self {
            config,
            options,
            progress,
        } = self;
        progress.run(|progress| {
            crate::inspection::verify_archive_with_progress(archive, config, options, progress)
        })
    }

    /// Verifies an archive read from `reader`; nothing is written and no
    /// filesystem path is needed.
    ///
    /// # Errors
    ///
    /// See [`VerifyRequest`].
    pub fn verify_reader(self, reader: ArchiveReader<'_>) -> Result<VerificationReport> {
        let Self {
            config,
            options,
            progress,
        } = self;
        progress.run(|progress| {
            let report = reader.verify(config, options, progress)?;
            progress.on_complete();
            Ok(report)
        })
    }
}

/// Extracts an archive read from a reader into an [`EntrySink`].
///
/// Every entry passes the validation pipeline of [`ExtractRequest`] and is then
/// delivered to the sink instead of being written under a directory. See the
/// [`sink`](crate::sink) module for what the sink may rely on and what it must
/// do. The optional [`ProgressCallback`] sees the events of a directory
/// extraction.
///
/// A 7z source is read in two phases: everything the file table can refuse
/// (admission, ratios, quotas, link policy, paths, name lengths) is refused
/// before the sink sees an entry, then the entries are decoded and delivered in
/// archive order.
///
/// # Errors
///
/// [`extract`](Self::extract) returns the errors of [`ExtractRequest`], plus
/// any error the sink returns: [`ArchiveError::Io`](crate::ArchiveError::Io) if
/// no entry was delivered yet,
/// [`ArchiveError::PartialExtraction`](crate::ArchiveError::PartialExtraction)
/// wrapping it afterwards. A sink that reports [`SinkFull`](crate::SinkFull)
/// ends the extraction with a
/// [`QuotaResource::SinkCapacity`](crate::QuotaResource::SinkCapacity) quota
/// error.
///
/// # Examples
///
/// ```
/// use std::io;
///
/// use exarch_core::ArchiveReader;
/// use exarch_core::MemorySink;
/// use exarch_core::SecurityConfig;
/// use exarch_core::SinkExtractRequest;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut builder = tar::Builder::new(Vec::new());
/// let mut header = tar::Header::new_gnu();
/// header.set_size(2);
/// header.set_mode(0o644);
/// builder.append_data(&mut header, "a.txt", &b"hi"[..])?;
/// let bytes = builder.into_inner()?;
///
/// let mut sink = MemorySink::default();
/// let config = SecurityConfig::default();
/// let report = SinkExtractRequest::new(&config)
///     .extract(ArchiveReader::tar(io::Cursor::new(bytes), None), &mut sink)?;
/// assert_eq!(report.files_extracted, 1);
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct SinkExtractRequest<'a> {
    config: &'a SecurityConfig,
    options: SinkOptions,
    progress: OptionalProgress<'a>,
}

impl<'a> SinkExtractRequest<'a> {
    /// An extraction into a sink under `config` with default options and no
    /// progress callback.
    #[must_use]
    pub fn new(config: &'a SecurityConfig) -> Self {
        Self {
            config,
            options: SinkOptions::default(),
            progress: OptionalProgress(None),
        }
    }

    /// Sets the sink extraction options.
    #[must_use]
    pub const fn with_options(mut self, options: SinkOptions) -> Self {
        self.options = options;
        self
    }

    /// Reports progress to `progress`.
    #[must_use]
    pub fn with_progress(mut self, progress: &'a mut dyn ProgressCallback) -> Self {
        self.progress = OptionalProgress(Some(progress));
        self
    }

    /// Extracts `reader` into `sink`.
    ///
    /// # Errors
    ///
    /// See [`SinkExtractRequest`].
    pub fn extract<S: EntrySink>(
        self,
        reader: ArchiveReader<'_>,
        sink: &mut S,
    ) -> Result<ExtractionReport> {
        let Self {
            config,
            options,
            progress,
        } = self;
        progress
            .run(|progress| extract_to_sink_reusing(reader, sink, config, options, progress, None))
    }
}

/// Whether creating an output file may replace one that already exists.
///
/// There is no default: the caller states which it means.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum OutputPolicy {
    /// Refuse with
    /// [`ArchiveError::OutputExists`](crate::ArchiveError::OutputExists)
    /// when anything is at the path.
    CreateNew,
    /// Replace a regular file, or a symlink at the path (the link itself, not
    /// its destination), atomically when the archive is complete.
    Replace,
}

closed_enum! {
    /// Whether a created archive is flushed to stable storage before it is
    /// reported as written.
    ///
    /// `Synced` (the default) flushes the archive file and then its parent
    /// directory entry, so a published archive survives a crash or power
    /// loss.
    ///
    /// `Unsynced` skips both flushes and is faster, with these hazards:
    ///
    /// - After a crash or power loss a [`Replace`](OutputPolicy::Replace)
    ///   target can be empty or truncated, and a
    ///   [`CreateNew`](OutputPolicy::CreateNew) file can be empty.
    /// - Write-back errors (network filesystems, quotas, delayed-allocation
    ///   `ENOSPC`) are not reported, so an `Ok` result does not mean the bytes
    ///   reached the disk.
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::Durability;
    /// use exarch_core::OutputFile;
    ///
    /// assert_eq!(Durability::default(), Durability::Synced);
    /// let out = OutputFile::replace("scratch.tar").with_durability(Durability::Unsynced);
    /// assert_eq!(out.durability(), Durability::Unsynced);
    /// ```
    #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
    #[non_exhaustive]
    pub enum Durability {
        /// Flush the file and its directory entry before reporting success
        #[default]
        Synced => "synced",

        /// Skip the flushes; see the type documentation for the hazards
        Unsynced => "unsynced",
    }
}

/// How the archive format of an [`OutputFile`] is chosen.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum OutputFormat {
    /// From the file extension. The ZIP-based extensions that need structure
    /// exarch does not produce (`.apk`, `.whl`, `.epub`, ...) are refused.
    #[default]
    FromExtension,
    /// The given format, whatever the extension is. This is how a ZIP-based
    /// extension is written as a plain ZIP.
    Explicit(CreatableFormat),
}

/// The archive file a creation writes: its path, whether it may replace an
/// existing file and how its format is chosen.
///
/// The archive is staged in a temporary file in the target's directory (which
/// must be writable) and renamed into place only when it is complete, so a
/// failed creation leaves the path as it was. The output must be absent, a
/// regular file, or a symlink to one (or dangling); a directory, a FIFO or a
/// device is refused with
/// [`InvalidConfiguration`](crate::ArchiveError::InvalidConfiguration).
///
/// Replacing keeps the permission bits of the file that is replaced (a symlink
/// is resolved first), never wider than before and without setuid, setgid or
/// sticky bits; a new file gets `0o666` filtered by the umask. Owner, group,
/// ACLs and extended attributes of a replaced file are not preserved: the new
/// file belongs to the creating user and to the group the filesystem gives a
/// new file in that directory (the directory's group on macOS and BSD), so a
/// file that was shared through its group can become readable by another group
/// under the same mode bits.
///
/// The staged file and the target are never archived, even when a source
/// directory contains them. They are recognized by file name and canonical
/// parent directory, so an alias of the output directory that canonicalization
/// does not resolve (a bind or `nullfs` mount of it inside a source tree) is
/// not recognized and the output may be archived into itself. Do not put such
/// a mount under a source.
///
/// A [`ProgressCallback`] that reports
/// [`is_cancelled`](ProgressCallback::is_cancelled) stops the creation: nothing
/// is published and a previous output stays as it was. After the archive is
/// published the parent directory is synced on a best-effort basis (Unix).
///
/// # Examples
///
/// ```
/// use exarch_core::OutputFile;
/// use exarch_core::OutputFormat;
/// use exarch_core::OutputPolicy;
/// use exarch_core::formats::detect::CreatableFormat;
///
/// let out = OutputFile::replace("backup.tar.gz");
/// assert_eq!(out.policy(), OutputPolicy::Replace);
/// assert_eq!(out.format(), OutputFormat::FromExtension);
///
/// let out = OutputFile::create_new("bundle.apk").with_format(CreatableFormat::Zip);
/// assert_eq!(out.format(), OutputFormat::Explicit(CreatableFormat::Zip));
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutputFile {
    path: PathBuf,
    policy: OutputPolicy,
    format: OutputFormat,
    durability: Durability,
}

impl OutputFile {
    /// An output at `path` that must not exist yet.
    #[must_use]
    pub fn create_new(path: impl Into<PathBuf>) -> Self {
        Self::at(path.into(), OutputPolicy::CreateNew)
    }

    /// An output at `path` that replaces an existing file.
    #[must_use]
    pub fn replace(path: impl Into<PathBuf>) -> Self {
        Self::at(path.into(), OutputPolicy::Replace)
    }

    const fn at(path: PathBuf, policy: OutputPolicy) -> Self {
        Self {
            path,
            policy,
            format: OutputFormat::FromExtension,
            durability: Durability::Synced,
        }
    }

    /// Writes `format` instead of the one the extension names.
    #[must_use]
    pub const fn with_format(mut self, format: CreatableFormat) -> Self {
        self.format = OutputFormat::Explicit(format);
        self
    }

    /// Sets whether the archive is flushed to stable storage before success is
    /// reported.
    #[must_use]
    pub const fn with_durability(mut self, durability: Durability) -> Self {
        self.durability = durability;
        self
    }

    /// The output path.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Whether an existing file may be replaced.
    #[must_use]
    pub const fn policy(&self) -> OutputPolicy {
        self.policy
    }

    /// How the format is chosen.
    #[must_use]
    pub const fn format(&self) -> OutputFormat {
        self.format
    }

    /// Whether the archive is flushed to stable storage.
    #[must_use]
    pub const fn durability(&self) -> Durability {
        self.durability
    }
}

/// Creates an archive from files and directories.
///
/// Holds the [`CreationConfig`] and an optional [`ProgressCallback`]; the
/// destination is chosen by the terminal method: a file
/// ([`create`](Self::create)), a writer ([`write`](Self::write)) or memory
/// ([`to_bytes`](Self::to_bytes)).
///
/// # Errors
///
/// All terminals return an error if:
/// - a source does not exist or cannot be read
/// - the configuration is invalid
/// - writing the archive fails
///
/// [`create`](Self::create) also returns an error if the output is refused (see
/// [`OutputFile`]) or its format cannot be determined. Sources are validated
/// before the output is published, so a failed creation leaves no archive.
///
/// # Examples
///
/// ```
/// use exarch_core::CreateRequest;
/// use exarch_core::CreationConfig;
/// use exarch_core::OutputFile;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let dir = tempfile::tempdir()?;
/// std::fs::write(dir.path().join("a.txt"), b"hello")?;
///
/// let config = CreationConfig::default();
/// let output = dir.path().join("out.tar.gz");
/// let report = CreateRequest::new(&config).create(
///     &OutputFile::create_new(&output),
///     &[dir.path().join("a.txt")],
/// )?;
/// assert_eq!(report.files_added, 1);
/// assert!(output.exists());
///
/// let missing = dir.path().join("missing.tar.gz");
/// assert!(
///     CreateRequest::new(&config)
///         .create(
///             &OutputFile::create_new(&missing),
///             &[dir.path().join("nope")]
///         )
///         .is_err()
/// );
/// assert!(!missing.exists());
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct CreateRequest<'a> {
    config: &'a CreationConfig,
    progress: OptionalProgress<'a>,
}

impl<'a> CreateRequest<'a> {
    /// A creation under `config` with no progress callback.
    #[must_use]
    pub const fn new(config: &'a CreationConfig) -> Self {
        Self {
            config,
            progress: OptionalProgress(None),
        }
    }

    /// Reports progress to `progress`.
    #[must_use]
    pub fn with_progress(mut self, progress: &'a mut dyn ProgressCallback) -> Self {
        self.progress = OptionalProgress(Some(progress));
        self
    }

    /// Creates the archive at `output` from `sources`.
    ///
    /// `CreationReport::bytes_compressed` is the size of the archive file.
    ///
    /// # Errors
    ///
    /// See [`CreateRequest`].
    pub fn create<P: AsRef<Path>>(
        self,
        output: &OutputFile,
        sources: &[P],
    ) -> Result<CreationReport> {
        let Self { config, progress } = self;
        let format = resolve_output_format(output)?;
        let mut staged = StagedOutput::begin(output.path(), output.policy())?;
        let report = progress.run(|progress| -> Result<CreationReport> {
            let report = write_staged(&mut staged, format, sources, config, progress)?;
            check_cancelled(progress)?;
            Ok(report)
        })?;
        staged.commit(output.durability())?;
        Ok(report)
    }

    /// Creates the archive from `sources` into `writer`.
    ///
    /// The writer fixes the format. `CreationReport::bytes_compressed` is the
    /// number of bytes written to it.
    ///
    /// # Errors
    ///
    /// See [`CreateRequest`].
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::ArchiveWriter;
    /// use exarch_core::CreateRequest;
    /// use exarch_core::CreationConfig;
    /// use exarch_core::formats::CompressionCodec;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let src = tempfile::tempdir()?;
    /// std::fs::write(src.path().join("a.txt"), b"hello")?;
    ///
    /// let mut buf = Vec::new();
    /// let config = CreationConfig::default();
    /// let report = CreateRequest::new(&config).write(
    ///     ArchiveWriter::tar(&mut buf, Some(CompressionCodec::Gzip)),
    ///     &[src.path()],
    /// )?;
    /// assert_eq!(report.bytes_compressed, buf.len() as u64);
    /// # Ok(())
    /// # }
    /// ```
    pub fn write<P: AsRef<Path>>(
        self,
        writer: ArchiveWriter<'_>,
        sources: &[P],
    ) -> Result<CreationReport> {
        let Self { config, progress } = self;
        progress.run(|progress| writer.write(sources, config, progress))
    }

    /// Creates an archive of `format` from `sources` and returns its bytes.
    ///
    /// The whole archive is held in memory, so use [`create`](Self::create) for
    /// large ones. Because the finished ZIP can be read back, a ZIP result
    /// carries the same warning as [`create`](Self::create) when default
    /// extraction would refuse an entry as a zip bomb.
    ///
    /// # Errors
    ///
    /// See [`CreateRequest`].
    ///
    /// # Examples
    ///
    /// ```
    /// use exarch_core::CreateRequest;
    /// use exarch_core::CreationConfig;
    /// use exarch_core::formats::detect::CreatableFormat;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let src = tempfile::tempdir()?;
    /// std::fs::write(src.path().join("a.txt"), b"hello")?;
    ///
    /// let config = CreationConfig::default();
    /// let (bytes, report) =
    ///     CreateRequest::new(&config).to_bytes(CreatableFormat::TarGz, &[src.path()])?;
    /// assert_eq!(report.bytes_compressed, bytes.len() as u64);
    /// # Ok(())
    /// # }
    /// ```
    pub fn to_bytes<P: AsRef<Path>>(
        self,
        format: CreatableFormat,
        sources: &[P],
    ) -> Result<(Vec<u8>, CreationReport)> {
        let mut out = Cursor::new(Vec::new());
        let mut report = self.write(ArchiveWriter::of_type(&mut out, format), sources)?;
        if format == CreatableFormat::Zip {
            warn_on_extraction_ratio(&mut out, &mut report);
        }
        Ok((out.into_inner(), report))
    }
}

/// Writes the archive of `format` into the staged file, which a ZIP can be
/// read back from for the extraction-ratio warning.
fn write_staged<P: AsRef<Path>>(
    staged: &mut StagedOutput,
    format: CreatableFormat,
    sources: &[P],
    config: &CreationConfig,
    progress: &mut dyn ProgressCallback,
) -> Result<CreationReport> {
    let (file, excluded) = staged.parts();
    match format.family() {
        CreationFamily::Tar(codec) => write_tar(file, codec, sources, config, excluded, progress),
        CreationFamily::Zip => {
            let (mut report, file) = write_zip(file, sources, config, excluded, progress)?;
            warn_on_extraction_ratio(file, &mut report);
            Ok(report)
        }
    }
}