acorn-lib 0.3.2

ACORN library
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
//! Archive creation and extraction utilities.
use crate::{
    io::{files_all, remove_any, standard_project_folder, ApiResult},
    util::constants::app::{ARCHIVE_INFERENCE_BYTES, MAX_ARCHIVE_ENTRIES, MAX_ARCHIVE_EXPANDED_BYTES},
};
use acorn_core::{
    prelude::Component,
    util::{MimeType, StringConversion},
};
use acorn_host::fs::SafePath;
use alloc::collections::BTreeSet;
use color_eyre::{eyre::eyre, Report};
use core::{
    fmt,
    iter::once,
    sync::atomic::{AtomicU64, Ordering},
};
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use sevenz_rust2::{ArchiveEntry, ArchiveReader, ArchiveWriter, Password};
use std::{
    fs::{create_dir_all, read_dir, remove_dir_all, remove_file, rename, DirEntry, File, OpenOptions},
    io::{self, Cursor, ErrorKind, Read, Write},
    path::{Path, PathBuf},
    process,
};
use tar::{Archive as TarArchive, Builder as TarBuilder};
use zip::{write::SimpleFileOptions, ZipArchive, ZipWriter};
use zstd::stream::read::Decoder as ZstdDecoder;

/// Archive creation operations implemented by archive candidates
pub trait ArchiveCreation {
    /// Creates a 7z archive
    fn archive_7z(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Creates an uncompressed TAR archive
    fn archive_tar(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Creates a gzip-compressed TAR archive
    fn archive_tar_gzip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Creates a ZIP archive
    fn archive_zip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
}
trait ArchiveEntryExt {
    fn extract(&self, reader: &mut dyn Read, root: &Path) -> Result<bool, sevenz_rust2::Error>;
    fn validate(&self, seen: Vec<SafePath>) -> ApiResult<Vec<SafePath>>;
}
/// Archive extraction operations implemented by archive candidates
pub trait ArchiveExtraction {
    /// Extracts a 7z archive
    fn extract_7z(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Extracts an uncompressed TAR archive
    fn extract_tar(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Extracts a gzip-compressed TAR archive
    fn extract_tar_gzip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Extracts a Zstandard-compressed TAR archive.
    fn extract_tar_zstd(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Extracts a ZIP archive
    fn extract_zip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
}
/// Operations supported by archive MIME types.
pub trait ArchiveFormat {
    /// Creates an archive from `source`.
    fn archive(&self, candidate: ArchiveCandidate, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
    /// Extracts an archive into `destination`.
    fn extract(&self, candidate: ArchiveCandidate, destination: Option<PathBuf>) -> ApiResult<PathBuf>;
}
#[derive(Debug)]
enum ArchiveError {
    DestinationInsideSource,
    DestinationInvalid,
    DestinationMissingFilename,
    DuplicatePath(PathBuf),
    EntryExpandedSizeLimit,
    ExpandedSizeLimit,
    ExpandedSizeOverflow,
    FormatInference(PathBuf),
    InspectArchive { path: PathBuf, reason: Box<str> },
    InspectSourceChild { path: PathBuf, reason: Box<str> },
    OutputInvalid,
    PublishOutput { path: PathBuf, reason: Box<str> },
    ReadArchive { path: PathBuf, reason: Box<str> },
    ReplaceDestination(Box<str>),
    ResolveSource(Box<str>),
    SevenZip { operation: &'static str, reason: Box<str> },

    SourceLink(PathBuf),
    StageOutput { path: PathBuf, reason: Box<str> },
    TooManyEntries,
    UnsafeZipPath(Box<str>),
    UnsupportedFormat(MimeType),
    UnsupportedTarEntry,
    ZipLink(PathBuf),
}
/// Source content for one deterministic ZIP entry.
#[derive(Clone, Debug)]
pub enum ZipEntrySource {
    /// In-memory entry content.
    Bytes(Vec<u8>),
    /// Regular file entry content.
    File(PathBuf),
}
/// A local path that may be created as or extracted from an archive.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ArchiveCandidate(PathBuf);
/// One validated entry in a deterministic ZIP archive.
#[derive(Clone, Debug)]
pub struct DeterministicZipEntry {
    destination: SafePath,
    mode: u32,
    source: ZipEntrySource,
}
/// An archive file written beside its final destination and published only once complete.
#[derive(Debug)]
struct StagedArchive {
    staging: PathBuf,
    output: PathBuf,
}
/// An extraction directory owned by the caller of `extract` and published only once complete.
#[derive(Debug)]
struct StagedExtraction {
    staging: PathBuf,
    output: PathBuf,
}
impl ArchiveCandidate {
    /// Creates an archive using the selected MIME type.
    pub fn archive(self, destination: Option<PathBuf>, mime_type: MimeType) -> ApiResult<PathBuf> {
        mime_type.archive(self, destination)
    }
    /// Extracts this archive, inferring its format when no override is supplied.
    pub fn extract(self, destination: Option<PathBuf>, archive_format: Option<MimeType>) -> ApiResult<PathBuf> {
        archive_format
            .map_or_else(|| self.infer(), Ok)
            .and_then(|format| format.extract(self, destination))
    }
    fn append_tar<W: Write>(self, writer: W) -> ApiResult<()> {
        self.relative_children().and_then(|children| {
            children
                .into_iter()
                .try_fold(TarBuilder::new(writer), |mut archive, (path, relative)| {
                    archive.append_path_with_name(path, relative).map(|()| archive).map_err(Into::into)
                })
                .and_then(|mut archive| archive.finish().map_err(Into::into))
        })
    }
    fn infer(&self) -> ApiResult<MimeType> {
        File::open(&self.0)
            .map_err(|why| ArchiveError::ReadArchive {
                path: self.0.clone(),
                reason: why.to_string().into(),
            })
            .and_then(|file| {
                let mut header = Vec::new();
                file.take(ARCHIVE_INFERENCE_BYTES)
                    .read_to_end(&mut header)
                    .map(|_| header)
                    .map_err(|why| ArchiveError::InspectArchive {
                        path: self.0.clone(),
                        reason: why.to_string().into(),
                    })
            })
            .map_err(Into::into)
            .and_then(|header| MimeType::infer(&header).ok_or_else(|| ArchiveError::FormatInference(self.0.clone()).into()))
    }
    fn prepare_output(&self, destination: Option<PathBuf>, format: MimeType) -> ApiResult<StagedArchive> {
        let output = destination.unwrap_or_else(|| self.0.with_extension(format.file_type()));
        self.0
            .canonicalize()
            .map_err(|why| Report::from(ArchiveError::ResolveSource(why.to_string().into())))
            .and_then(|root| {
                let parent = output
                    .parent()
                    .filter(|path| !path.as_os_str().is_empty())
                    .unwrap_or_else(|| Path::new("."));
                parent
                    .canonicalize()
                    .map_err(Into::into)
                    .and_then(|parent| {
                        output
                            .file_name()
                            .map(|name| parent.join(name))
                            .ok_or_else(|| ArchiveError::DestinationMissingFilename.into())
                    })
                    .map(|output| (root, output))
            })
            .and_then(|(root, resolved)| match resolved.starts_with(&root) {
                | true => Err(ArchiveError::DestinationInsideSource.into()),
                | false => StagedArchive::stage(&resolved),
            })
    }
    fn relative_children(&self) -> ApiResult<Vec<(PathBuf, SafePath)>> {
        self.0
            .canonicalize()
            .map_err(|why| Report::from(ArchiveError::ResolveSource(why.to_string().into())))
            .and_then(|root| {
                files_all(root.clone(), None::<Vec<String>>)
                    .into_iter()
                    .map(|path| {
                        path.symlink_metadata()
                            .map_err(|why| {
                                Report::from(ArchiveError::InspectSourceChild {
                                    path: path.clone(),
                                    reason: why.to_string().into(),
                                })
                            })
                            .and_then(|metadata| match metadata.file_type().is_symlink() {
                                | true => Err(ArchiveError::SourceLink(path.clone()).into()),
                                | false => path.canonicalize().map_err(Into::into).and_then(|absolute| {
                                    absolute
                                        .strip_prefix(&root)
                                        .map(Path::to_path_buf)
                                        .map_err(Into::into)
                                        .and_then(|relative| SafePath::new(relative).map_err(Into::into))
                                        .map(|relative| (absolute, relative))
                                }),
                            })
                    })
                    .collect()
            })
    }
}
impl ArchiveCreation for ArchiveCandidate {
    fn archive_7z(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        self.prepare_output(destination, MimeType::SevenZip).and_then(|staged| {
            let written = staged.open().and_then(|file| {
                ArchiveWriter::new(file)
                    .map_err(|why| {
                        Report::from(ArchiveError::SevenZip {
                            operation: "create 7z archive",
                            reason: why.to_string().into(),
                        })
                    })
                    .and_then(|writer| {
                        self.relative_children().and_then(|children| {
                            children
                                .into_iter()
                                .try_fold(writer, |mut writer, (path, relative)| {
                                    let entry = ArchiveEntry::from_path(&path, relative.as_path().to_portable_path());
                                    path.is_file()
                                        .then(|| File::open(&path))
                                        .transpose()
                                        .map_err(Into::into)
                                        .and_then(|reader| match writer.push_archive_entry(entry, reader) {
                                            | Ok(_) => Ok(writer),
                                            | Err(why) => Err(ArchiveError::SevenZip {
                                                operation: "add 7z entry",
                                                reason: why.to_string().into(),
                                            }
                                            .into()),
                                        })
                                })
                                .and_then(|writer| {
                                    writer.finish().map_err(|why| {
                                        ArchiveError::SevenZip {
                                            operation: "finish 7z archive",
                                            reason: why.to_string().into(),
                                        }
                                        .into()
                                    })
                                })
                        })
                    })
                    .map(|_| ())
            });
            written.and_then(|()| staged.publish())
        })
    }
    fn archive_tar(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        self.prepare_output(destination, MimeType::Tar)
            .and_then(|staged| staged.open().and_then(|file| self.append_tar(file)).and_then(|()| staged.publish()))
    }
    fn archive_tar_gzip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        self.prepare_output(destination, MimeType::Gzip).and_then(|staged| {
            staged
                .open()
                .and_then(|file| self.append_tar(GzEncoder::new(file, Compression::default())))
                .and_then(|()| staged.publish())
        })
    }
    fn archive_zip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        self.prepare_output(destination, MimeType::Zip).and_then(|staged| {
            let written = staged.open().and_then(|file| {
                let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
                self.relative_children()
                    .and_then(|children| {
                        children
                            .into_iter()
                            .try_fold(ZipWriter::new(file), |mut writer, (path, relative)| match path.is_dir() {
                                | true => writer.add_directory_from_path(relative, options).map(|()| writer).map_err(Into::into),
                                | false => writer
                                    .start_file_from_path(relative, options)
                                    .map_err(Into::into)
                                    .and_then(|()| File::open(path).map_err(Into::into))
                                    .and_then(|mut input| io::copy(&mut input, &mut writer).map_err(Into::into))
                                    .map(|_| writer),
                            })
                    })
                    .and_then(|writer| writer.finish().map_err(Into::into))
                    .map(|_| ())
            });
            written.and_then(|()| staged.publish())
        })
    }
}
impl ArchiveExtraction for ArchiveCandidate {
    fn extract_7z(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        StagedExtraction::stage(destination).and_then(|staged| {
            let root = staged.root().to_path_buf();
            File::open(&self.0)
                .map_err(Into::into)
                .and_then(|file| {
                    ArchiveReader::new(file, Password::empty()).map_err(|why| {
                        ArchiveError::SevenZip {
                            operation: "read 7z archive",
                            reason: why.to_string().into(),
                        }
                        .into()
                    })
                })
                .and_then(|mut archive| {
                    let validation = {
                        let entries = archive.archive().files.as_slice();
                        let size = entries.iter().try_fold(0_u64, |total, entry| {
                            total.checked_add(entry.size()).ok_or_else(|| ArchiveError::ExpandedSizeOverflow.into())
                        });
                        match (entries.len() > MAX_ARCHIVE_ENTRIES, size) {
                            | (true, _) => Err(ArchiveError::TooManyEntries.into()),
                            | (_, Ok(size)) if size > MAX_ARCHIVE_EXPANDED_BYTES => Err(ArchiveError::ExpandedSizeLimit.into()),
                            | (_, Err(why)) => Err(why),
                            | _ => entries.iter().try_fold(Vec::new(), |seen, entry| entry.validate(seen)).and_then(|paths| {
                                paths
                                    .iter()
                                    .try_for_each(|relative| relative.materialize_under(&root).map(|_| ()).map_err(Into::into))
                            }),
                        }
                    };
                    validation.and_then(|()| {
                        archive.for_each_entries(|entry, reader| entry.extract(reader, &root)).map_err(|why| {
                            ArchiveError::SevenZip {
                                operation: "extract 7z archive",
                                reason: why.to_string().into(),
                            }
                            .into()
                        })
                    })
                })
                .map(|_| root)
                .and_then(|_| staged.publish())
        })
    }
    fn extract_tar(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        StagedExtraction::stage(destination).and_then(|staged| {
            File::open(self.0)
                .map_err(Into::into)
                .and_then(|file| extract_tar_reader(file, &staged))
                .and_then(|_| staged.publish())
        })
    }
    fn extract_tar_gzip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        StagedExtraction::stage(destination).and_then(|staged| {
            File::open(self.0)
                .map(GzDecoder::new)
                .map_err(Into::into)
                .and_then(|decoder| extract_tar_reader(decoder, &staged))
                .and_then(|_| staged.publish())
        })
    }
    fn extract_tar_zstd(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        StagedExtraction::stage(destination).and_then(|staged| {
            File::open(self.0)
                .map_err(Into::into)
                .and_then(|file| ZstdDecoder::new(file).map_err(Into::into))
                .and_then(|decoder| extract_tar_reader(decoder, &staged))
                .and_then(|_| staged.publish())
        })
    }
    fn extract_zip(self, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        StagedExtraction::stage(destination).and_then(|staged| {
            let root = staged.root().to_path_buf();
            File::open(&self.0)
                .map_err(Into::into)
                .and_then(|file| ZipArchive::new(file).map_err(Into::into))
                .and_then(|mut archive| match archive.len() > MAX_ARCHIVE_ENTRIES {
                    | true => Err(ArchiveError::TooManyEntries.into()),
                    | false => (0..archive.len()).try_fold((Vec::new(), 0_u64), |(seen, total), index| {
                        archive.by_index(index).map_err(Into::into).and_then(|mut entry| {
                            entry
                                .enclosed_name()
                                .ok_or_else(|| ArchiveError::UnsafeZipPath(entry.name().into()).into())
                                .and_then(|path| SafePath::new(path).map_err(Into::into))
                                .and_then(|relative| validate_entry(&root, seen, relative, entry.size()))
                                .and_then(|(seen, target)| {
                                    total
                                        .checked_add(entry.size())
                                        .ok_or_else(|| ArchiveError::ExpandedSizeOverflow.into())
                                        .and_then(|total| match total > MAX_ARCHIVE_EXPANDED_BYTES {
                                            | true => Err(ArchiveError::ExpandedSizeLimit.into()),
                                            | false => Ok((seen, target, total)),
                                        })
                                })
                                .and_then(|(seen, target, total)| {
                                    let mode = entry.unix_mode().unwrap_or_default() & 0o170000;
                                    match mode == 0o120000 {
                                        | true => Err(ArchiveError::ZipLink(target).into()),
                                        | false => {
                                            let is_directory = entry.is_dir();
                                            write_entry(&mut entry, &target, is_directory).map(|()| (seen, total))
                                        }
                                    }
                                })
                        })
                    }),
                })
                .map(|_| root)
                .and_then(|_| staged.publish())
        })
    }
}
impl From<&Path> for ArchiveCandidate {
    fn from(value: &Path) -> Self {
        Self(value.to_path_buf())
    }
}
impl From<PathBuf> for ArchiveCandidate {
    fn from(value: PathBuf) -> Self {
        Self(value)
    }
}
impl ArchiveEntryExt for ArchiveEntry {
    fn extract(&self, reader: &mut dyn Read, root: &Path) -> Result<bool, sevenz_rust2::Error> {
        SafePath::new(self.name())
            .map_err(Report::from)
            .and_then(|relative| relative.materialize_under(root).map(|target| (relative, target)).map_err(Into::into))
            .and_then(|(relative, target)| write_entry(reader, &target, self.is_directory()).map(|()| relative))
            .map(|_| true)
            .map_err(|why| sevenz_rust2::Error::Other(why.to_string().into()))
    }
    fn validate(&self, seen: Vec<SafePath>) -> ApiResult<Vec<SafePath>> {
        SafePath::new(self.name())
            .map_err(Into::into)
            .and_then(|relative| match seen.contains(&relative) {
                | true => Err(ArchiveError::DuplicatePath(relative.into_path_buf()).into()),
                | false => Ok(seen.into_iter().chain(once(relative)).collect()),
            })
    }
}
impl core::error::Error for ArchiveError {}
impl fmt::Display for ArchiveError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            | Self::DestinationInsideSource => write!(formatter, "Archive destination cannot be inside its source directory"),
            | Self::DestinationInvalid => write!(formatter, "Archive destination is not a regular directory"),
            | Self::DestinationMissingFilename => write!(formatter, "Archive destination must name a file"),
            | Self::DuplicatePath(path) => write!(formatter, "Duplicate archive path: {}", path.display()),
            | Self::EntryExpandedSizeLimit => write!(formatter, "Archive entry expands beyond the supported size limit"),
            | Self::ExpandedSizeLimit => write!(formatter, "Archive expands beyond the supported size limit"),
            | Self::ExpandedSizeOverflow => write!(formatter, "Archive expanded size overflow"),
            | Self::FormatInference(path) => write!(formatter, "Unable to infer archive format for {}", path.display()),
            | Self::InspectArchive { path, reason } => write!(formatter, "Failed to inspect archive {} — {reason}", path.display()),
            | Self::InspectSourceChild { path, reason } => {
                write!(formatter, "Failed to inspect archive source child {} — {reason}", path.display())
            }
            | Self::OutputInvalid => write!(formatter, "Archive destination exists and is not a regular file"),
            | Self::PublishOutput { path, reason } => write!(formatter, "Failed to publish {} — {reason}", path.display()),
            | Self::ReadArchive { path, reason } => write!(formatter, "Failed to read archive {} — {reason}", path.display()),
            | Self::ReplaceDestination(reason) => write!(formatter, "Failed to replace archive destination — {reason}"),
            | Self::ResolveSource(reason) => write!(formatter, "Failed to resolve archive source — {reason}"),
            | Self::SevenZip { operation, reason } => write!(formatter, "Failed to {operation} — {reason}"),
            | Self::SourceLink(path) => write!(formatter, "Archive source links are not supported: {}", path.display()),
            | Self::StageOutput { path, reason } => write!(formatter, "Failed to stage archive beside {} — {reason}", path.display()),
            | Self::TooManyEntries => write!(formatter, "Archive contains too many entries"),
            | Self::UnsafeZipPath(path) => write!(formatter, "Unsafe ZIP path: {path}"),
            | Self::UnsupportedFormat(format) => write!(formatter, "Unsupported archive format: {format}"),
            | Self::UnsupportedTarEntry => write!(formatter, "Unsupported TAR entry"),
            | Self::ZipLink(path) => write!(formatter, "ZIP links are not supported: {}", path.display()),
        }
    }
}
impl DeterministicZipEntry {
    /// Create a deterministic ZIP with fixed metadata and explicit safe entry names.
    pub fn create(path: &Path, entries: &[Self]) -> ApiResult<PathBuf> {
        let unique = entries.iter().map(|entry| entry.destination.as_path()).collect::<BTreeSet<_>>();
        match (unique.len() == entries.len(), entries.iter().all(|entry| entry.mode & !0o777 == 0)) {
            | (false, _) => Err(eyre!("Deterministic ZIP contains duplicate entry names")),
            | (_, false) => Err(eyre!("Deterministic ZIP entry mode must contain permission bits only")),
            | (true, true) => {
                let mut entries = entries.iter().collect::<Vec<_>>();
                entries.sort_by(|left, right| left.destination.cmp(&right.destination));
                OpenOptions::new()
                    .write(true)
                    .create_new(true)
                    .open(path)
                    .map_err(Report::from)
                    .and_then(|file| entries.into_iter().try_fold(ZipWriter::new(file), |archive, entry| entry.write(archive)))
                    .and_then(|archive| archive.finish().map_err(Report::from))
                    .map(|_| path.to_path_buf())
            }
        }
    }
    /// Create an entry from in-memory content.
    pub fn from_bytes(destination: impl Into<PathBuf>, content: Vec<u8>, mode: u32) -> ApiResult<Self> {
        SafePath::new(destination.into())
            .map(|destination| Self {
                destination,
                mode,
                source: ZipEntrySource::Bytes(content),
            })
            .map_err(Into::into)
    }
    /// Create an entry from a regular file.
    pub fn from_file(destination: impl Into<PathBuf>, source: PathBuf, mode: u32) -> ApiResult<Self> {
        source
            .symlink_metadata()
            .map_err(Into::into)
            .and_then(|metadata| match metadata.file_type().is_symlink() || !metadata.is_file() {
                | true => Err(eyre!("ZIP entry source must be a regular file: {}", source.display())),
                | false => SafePath::new(destination.into())
                    .map(|destination| Self {
                        destination,
                        mode,
                        source: ZipEntrySource::File(source),
                    })
                    .map_err(Into::into),
            })
    }
    fn write(&self, mut archive: ZipWriter<File>) -> ApiResult<ZipWriter<File>> {
        let options = SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored)
            .last_modified_time(zip::DateTime::default())
            .unix_permissions(self.mode);
        archive
            .start_file_from_path(self.destination.as_path(), options)
            .map_err(Report::from)
            .and_then(|()| match &self.source {
                | ZipEntrySource::Bytes(content) => archive.write_all(content).map(|()| archive).map_err(Report::from),
                | ZipEntrySource::File(path) => File::open(path)
                    .map_err(Report::from)
                    .and_then(|mut file| io::copy(&mut file, &mut archive).map_err(Report::from))
                    .map(|_| archive),
            })
    }
}
impl ArchiveFormat for MimeType {
    fn archive(&self, source: ArchiveCandidate, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        match self {
            | Self::Gzip => source.archive_tar_gzip(destination),
            | Self::SevenZip => source.archive_7z(destination),
            | Self::Tar => source.archive_tar(destination),
            | Self::Zip => source.archive_zip(destination),
            | _ => Err(ArchiveError::UnsupportedFormat(self.clone()).into()),
        }
    }
    fn extract(&self, source: ArchiveCandidate, destination: Option<PathBuf>) -> ApiResult<PathBuf> {
        match self {
            | Self::Gzip => source.extract_tar_gzip(destination),
            | Self::SevenZip => source.extract_7z(destination),
            | Self::Tar => source.extract_tar(destination),
            | Self::Zip => source.extract_zip(destination),
            | Self::Zstd => source.extract_tar_zstd(destination),
            | _ => Err(ArchiveError::UnsupportedFormat(self.clone()).into()),
        }
    }
}
impl Drop for StagedArchive {
    fn drop(&mut self) {
        let _ = remove_file(&self.staging);
    }
}

impl StagedArchive {
    /// Reserve an exclusive staging file beside `output` without disturbing an existing archive.
    fn stage(output: &Path) -> ApiResult<Self> {
        let output = match output.symlink_metadata() {
            | Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => Err(ArchiveError::OutputInvalid.into()),
            | Ok(_) => Ok(output.to_path_buf()),
            | Err(why) if why.kind() == ErrorKind::NotFound => Ok(output.to_path_buf()),
            | Err(why) => Err(why.into()),
        };
        output.and_then(|output| {
            let staging = scratch_sibling(&output, "acorn-archive");
            reserve_scratch(&staging, |path| OpenOptions::new().write(true).create_new(true).open(path).map(|_| ()))
                .map(|()| Self { staging, output })
        })
    }
    /// Open the staging file for writing.
    fn open(&self) -> ApiResult<File> {
        OpenOptions::new().write(true).truncate(true).open(&self.staging).map_err(|why| {
            ArchiveError::StageOutput {
                path: self.staging.clone(),
                reason: why.to_string().into(),
            }
            .into()
        })
    }
    /// Replace the final destination with the completed staging file.
    fn publish(self) -> ApiResult<PathBuf> {
        remove_any(&self.output)
            .map_err(|why: std::io::Error| Report::new(ArchiveError::ReplaceDestination(why.to_string().into())))
            .and_then(|()| {
                rename(&self.staging, &self.output).map_err(|why| {
                    ArchiveError::PublishOutput {
                        path: self.output.clone(),
                        reason: why.to_string().into(),
                    }
                    .into()
                })
            })
            .map(|()| self.output.clone())
    }
}

impl Drop for StagedExtraction {
    fn drop(&mut self) {
        let _ = remove_dir_all(&self.staging);
    }
}

impl StagedExtraction {
    /// Reserve an exclusive staging directory beside the requested destination.
    ///
    /// An absent destination defaults to a standard project folder so callers that do not choose an output location still stage beside a predictable directory.
    fn stage(destination: Option<PathBuf>) -> ApiResult<Self> {
        let output = destination.unwrap_or_else(|| standard_project_folder("extract", None));
        Self::try_from(output)
    }
    /// Return the directory entries are extracted into.
    fn root(&self) -> &Path {
        &self.staging
    }
    /// Move validated entries into the destination and discard the staging directory.
    fn publish(self) -> ApiResult<PathBuf> {
        create_dir_all(&self.output)
            .map_err(Into::into)
            .and_then(|()| publish_children(&self.staging, &self.output))
            .map(|()| self.output.clone())
    }
}

impl TryFrom<&Path> for StagedExtraction {
    type Error = Report;
    /// Reserve an exclusive staging directory beside `output`.
    fn try_from(output: &Path) -> Result<Self, Self::Error> {
        Self::try_from(output.to_path_buf())
    }
}

impl TryFrom<PathBuf> for StagedExtraction {
    type Error = Report;
    /// Reserve an exclusive staging directory beside `output` so the destination stays untouched until publication.
    fn try_from(output: PathBuf) -> Result<Self, Self::Error> {
        let output = match output.symlink_metadata() {
            | Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_dir() => Err(ArchiveError::DestinationInvalid.into()),
            | Ok(_) => Ok(output),
            | Err(why) if why.kind() == ErrorKind::NotFound => Ok(output),
            | Err(why) => Err(why.into()),
        };
        output.and_then(|output| {
            let parent = SafePath::parent_or_current(&output);
            let staging = scratch_sibling(&output, "acorn-extract");
            create_dir_all(parent)
                .map_err(Into::into)
                .and_then(|()| reserve_scratch(&staging, |path| std::fs::create_dir(path)))
                .map(|()| Self { staging, output })
        })
    }
}

/// Creates an archive using the selected MIME type.
pub fn archive(source: PathBuf, destination: Option<PathBuf>, mime_type: MimeType) -> ApiResult<PathBuf> {
    ArchiveCandidate::from(source).archive(destination, mime_type)
}
/// Extracts an archive, inferring its format from a bounded content header when no override is supplied.
pub fn extract(source: PathBuf, destination: Option<PathBuf>, archive_format: Option<MimeType>) -> ApiResult<PathBuf> {
    ArchiveCandidate::from(source).extract(destination, archive_format)
}
/// Extract every TAR entry from `reader` into `staged`, returning the directory they were written to.
fn extract_tar_reader<R: Read>(reader: R, staged: &StagedExtraction) -> ApiResult<PathBuf> {
    let root = staged.root().to_path_buf();
    TarArchive::new(reader)
        .entries()
        .map_err(Into::into)
        .and_then(|entries| {
            entries
                .enumerate()
                .try_fold((Vec::new(), 0_u64), |(seen, total), (index, entry)| match index >= MAX_ARCHIVE_ENTRIES {
                    | true => Err(ArchiveError::TooManyEntries.into()),
                    | false => entry.map_err(Into::into).and_then(|mut entry| {
                        entry
                            .path()
                            .map_err(Into::into)
                            .and_then(|path| {
                                let is_directory = entry.header().entry_type().is_dir();
                                let is_file = entry.header().entry_type().is_file();
                                let root_entry = path.components().all(|component| matches!(component, Component::CurDir));
                                match (root_entry, is_directory, is_file) {
                                    | (true, true, _) => Ok((seen, root.clone(), true, total)),
                                    | (false, true, _) | (false, _, true) => SafePath::new(path.as_ref())
                                        .map_err(Into::into)
                                        .and_then(|relative| validate_entry(&root, seen, relative, entry.size()))
                                        .and_then(|(seen, target)| {
                                            total
                                                .checked_add(entry.size())
                                                .ok_or_else(|| ArchiveError::ExpandedSizeOverflow.into())
                                                .and_then(|total| match total > MAX_ARCHIVE_EXPANDED_BYTES {
                                                    | true => Err(ArchiveError::ExpandedSizeLimit.into()),
                                                    | false => Ok((seen, target, is_directory, total)),
                                                })
                                        }),
                                    | _ => Err(ArchiveError::UnsupportedTarEntry.into()),
                                }
                            })
                            .and_then(|(seen, target, is_directory, total)| write_entry(&mut entry, &target, is_directory).map(|()| (seen, total)))
                    }),
                })
        })
        .map(|_| root)
}
/// Reserve a unique sibling path so concurrent work never shares a staging location.
fn scratch_sibling(destination: &Path, marker: &str) -> PathBuf {
    static NEXT: AtomicU64 = AtomicU64::new(0);
    let parent = SafePath::parent_or_current(destination);
    let ordinal = NEXT.fetch_add(1, Ordering::Relaxed);
    parent.join(format!(".{marker}-{pid}-{ordinal}", pid = process::id()))
}
/// Reserve a staging path by creating it exclusively, so an existing scratch file is never reused.
fn reserve_scratch(staging: &Path, create: impl Fn(&Path) -> std::io::Result<()>) -> ApiResult<()> {
    match create(staging) {
        | Ok(()) => Ok(()),
        | Err(why) => Err(ArchiveError::StageOutput {
            path: staging.to_path_buf(),
            reason: why.to_string().into(),
        }
        .into()),
    }
}
/// Merge staged content into an existing destination, replacing only the entries it supplies.
fn publish_children(from: &Path, to: &Path) -> ApiResult<()> {
    read_dir(from).map_err(Into::into).and_then(|entries| {
        entries
            .map(|entry| entry.map_err(Report::from).and_then(|entry| publish_entry(&entry, to)))
            .collect::<ApiResult<Vec<_>>>()
            .map(|_| ())
    })
}
/// Publish one staged entry, replacing whatever the destination already holds at that path.
fn publish_entry(entry: &DirEntry, to: &Path) -> ApiResult<()> {
    let target = to.join(entry.file_name());
    let source = entry.path();
    validate_publish_target(&target).and_then(|()| {
        entry.file_type().map_err(Into::into).and_then(|kind| match kind.is_dir() {
            | true => match target.symlink_metadata() {
                | Ok(metadata) if !metadata.is_dir() => remove_any(&target).and_then(|()| create_dir_all(&target).map_err(Into::into)),
                | Ok(_) => Ok(()),
                | Err(why) if why.kind() == ErrorKind::NotFound => create_dir_all(&target).map_err(Into::into),
                | Err(why) => Err(why.into()),
            }
            .and_then(|()| publish_children(&source, &target)),
            | false => remove_any(&target).and_then(|()| {
                rename(&source, &target).map_err(|why| {
                    ArchiveError::PublishOutput {
                        path: target,
                        reason: why.to_string().into(),
                    }
                    .into()
                })
            }),
        })
    })
}
fn validate_entry(root: &Path, seen: Vec<SafePath>, relative: SafePath, size: u64) -> ApiResult<(Vec<SafePath>, PathBuf)> {
    match size > MAX_ARCHIVE_EXPANDED_BYTES {
        | true => Err(ArchiveError::EntryExpandedSizeLimit.into()),
        | false if seen.contains(&relative) => Err(ArchiveError::DuplicatePath(relative.into_path_buf()).into()),
        | false => relative
            .materialize_under(root)
            .map(|target| (seen.into_iter().chain(once(relative)).collect(), target))
            .map_err(Into::into),
    }
}
fn validate_publish_target(target: &Path) -> ApiResult<()> {
    match target.symlink_metadata() {
        | Ok(metadata) if metadata.file_type().is_symlink() => Err(ArchiveError::DestinationInvalid.into()),
        | Ok(_) => Ok(()),
        | Err(why) if why.kind() == ErrorKind::NotFound => Ok(()),
        | Err(why) => Err(why.into()),
    }
}
fn write_entry(reader: &mut dyn Read, target: &Path, is_directory: bool) -> ApiResult<()> {
    match is_directory {
        | true => create_dir_all(target).map_err(Into::into),
        | false => target
            .parent()
            .map_or(Ok(()), |parent| create_dir_all(parent).map_err(Into::into))
            .and_then(|()| OpenOptions::new().write(true).create_new(true).open(target).map_err(Into::into))
            .and_then(|mut output| io::copy(reader, &mut output).map(|_| ()).map_err(Into::into)),
    }
}
/// Create an in-memory ZIP archive from validated relative entry names and byte content
pub fn zip_entries(entries: &[(&str, &[u8])]) -> ApiResult<Vec<u8>> {
    let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
    entries
        .iter()
        .try_fold(
            (ZipWriter::new(Cursor::new(Vec::new())), Vec::<SafePath>::new()),
            |(mut archive, seen), (name, content)| {
                SafePath::new(name)
                    .map_err(|_| Report::from(ArchiveError::UnsafeZipPath((*name).into())))
                    .and_then(|relative| match seen.contains(&relative) {
                        | true => Err(ArchiveError::DuplicatePath(relative.into_path_buf()).into()),
                        | false => archive
                            .start_file(relative.as_path().to_portable_path(), options)
                            .and_then(|()| archive.write_all(content).map_err(Into::into))
                            .map(|()| (archive, seen.into_iter().chain(once(relative)).collect()))
                            .map_err(Into::into),
                    })
            },
        )
        .and_then(|(archive, _)| archive.finish().map_err(Into::into))
        .map(Cursor::into_inner)
}