computer-transfer 0.3.2

Packs and unpacks file trees for transfers between the host and the computer.
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
//! Writes a tar stream made by `pack` to disk.

use std::{
    collections::{HashMap, hash_map::Entry},
    fs,
    io::{self, BufWriter, Read},
    path::{Path, PathBuf},
    time::{Duration, UNIX_EPOCH},
};

use computer_protocol::{SKIP_REPORT_ENTRY, SkipList};
use tar::{Archive, EntryType};
use uuid::Uuid;

use crate::{
    Failure, Progress,
    rules::{self, Action, Kind, PathProblem, Platform, Wanted},
};

const MAX_REPORT_BYTES: u64 = 1024 * 1024;
const WRITE_BUFFER: usize = 128 * 1024;

/// What was written and where.
#[derive(Debug)]
pub struct Unpacked {
    /// Final path of the file or folder that was created or merged into.
    pub path: PathBuf,
    pub progress: Progress,
    /// Entries left out here, followed by the ones the sender left out.
    pub skipped: SkipList,
}

/// Removes its file when dropped before [`Temp::place`].
struct Temp(Option<PathBuf>);

impl Temp {
    fn new(dir: &Path) -> Self {
        Self(Some(dir.join(format!(
            ".computer-use-transfer-{}.tmp",
            Uuid::new_v4().simple()
        ))))
    }

    fn path(&self) -> &Path {
        self.0
            .as_deref()
            .expect("a temporary file has a path until it is placed")
    }

    fn place(mut self, to: &Path) -> io::Result<()> {
        fs::rename(self.path(), to)?;
        self.0 = None;
        Ok(())
    }
}

impl Drop for Temp {
    fn drop(&mut self) {
        if let Some(path) = self.0.take() {
            let _ = fs::remove_file(path);
        }
    }
}

/// Counts the bytes that pass through, so progress is right when a copy stops halfway.
struct Counted<'a, R> {
    inner: R,
    bytes: &'a mut u64,
}

impl<R: Read> Read for Counted<'_, R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.inner.read(buf)?;
        *self.bytes += n as u64;
        Ok(n)
    }
}

fn kind_at(path: &Path) -> Option<Kind> {
    let meta = fs::symlink_metadata(path).ok()?;
    let kind = meta.file_type();
    Some(if kind.is_symlink() {
        Kind::Link
    } else if kind.is_dir() {
        Kind::Folder
    } else if kind.is_file() {
        Kind::File
    } else {
        Kind::Other
    })
}

fn followed_kind(path: &Path) -> Option<Kind> {
    let meta = fs::metadata(path).ok()?;
    Some(if meta.is_dir() {
        Kind::Folder
    } else if meta.is_file() {
        Kind::File
    } else {
        Kind::Other
    })
}

/// Tells before any data is sent whether a root named `name` can land at `dest`.
///
/// Applies the same rules as [`unpack`] does with the first entry, and returns the final path.
///
/// # Errors
///
/// Refuses with a message naming the path that is in the way, or the name that cannot exist here.
pub fn check_destination(
    dest: &Path,
    name: &str,
    folder: bool,
    overwrite: bool,
    platform: Platform,
) -> Result<PathBuf, String> {
    if let Some(problem) = rules::name_problem(name, platform) {
        return Err(format!("{name} cannot be created here: {problem}"));
    }
    let wanted = if folder { Wanted::Folder } else { Wanted::File };
    rules::destination(dest, followed_kind(dest), name, wanted, overwrite, kind_at)
        .map(|found| found.path)
}

/// Writes the archive read from `reader` to `dest`, following the destination rule of `cp -r`.
///
/// The root entry decides everything before anything is written: the final path, the refusal
/// when it exists and `overwrite` is off, and the refusal of a file against a folder. The same
/// checks run again for every entry of a folder, so a merge replaces same-named files and keeps
/// the others. Files are written under a temporary name and renamed into place, and an
/// unfinished temporary file is removed on every failure.
///
/// Entries with an absolute path or `..` stop the transfer. An entry whose path goes through a
/// symbolic link or a file is refused, so links made by the archive cannot lead later entries
/// outside the destination. Names `platform` cannot hold, and links it cannot create, are
/// skipped and listed.
///
/// # Errors
///
/// Fails with the reason and how far the transfer got.
pub fn unpack<R: Read>(
    reader: R,
    dest: &Path,
    overwrite: bool,
    platform: Platform,
) -> Result<Unpacked, Failure> {
    let mut unpacker = Unpacker {
        platform,
        overwrite,
        root: PathBuf::new(),
        progress: Progress::default(),
        skipped: SkipList::default(),
        sender_skipped: SkipList::default(),
        skipped_dirs: Vec::new(),
        checked_parent: None,
        saw_end: false,
        written: HashMap::new(),
    };
    match unpacker.run(reader, dest) {
        Ok(()) => {
            let Unpacker {
                root,
                progress,
                mut skipped,
                sender_skipped,
                ..
            } = unpacker;
            skipped.merge(sender_skipped);
            Ok(Unpacked {
                path: root,
                progress,
                skipped,
            })
        }
        Err(message) => Err(Failure::new(message, unpacker.progress)),
    }
}

struct Unpacker {
    platform: Platform,
    overwrite: bool,
    root: PathBuf,
    progress: Progress,
    skipped: SkipList,
    sender_skipped: SkipList,
    /// Folders that were skipped, so their contents are skipped without a report each.
    skipped_dirs: Vec<PathBuf>,
    /// Folder whose path was last checked for links, which most next entries share.
    checked_parent: Option<PathBuf>,
    /// Whether the sender's closing report entry arrived.
    saw_end: bool,
    /// On systems that ignore case, the lowercased path of every entry written, with the name it had.
    written: HashMap<String, String>,
}

fn read_error(error: &io::Error) -> String {
    format!("reading the transfer failed: {error}")
}

impl Unpacker {
    fn run<R: Read>(&mut self, reader: R, dest: &Path) -> Result<(), String> {
        let mut archive = Archive::new(reader);
        let mut entries = archive.entries().map_err(|e| read_error(&e))?;
        let mut first = entries
            .next()
            .ok_or("the transfer held no data")?
            .map_err(|e| read_error(&e))?;
        let name = self.root_name(&first)?;
        let wanted = match first.header().entry_type() {
            EntryType::Directory => Wanted::Folder,
            EntryType::Regular | EntryType::Continuous => Wanted::File,
            other => {
                return Err(format!(
                    "the transfer starts with an unexpected entry ({other:?})"
                ));
            }
        };
        let found = rules::destination(
            dest,
            followed_kind(dest),
            &name,
            wanted,
            self.overwrite,
            kind_at,
        )?;
        self.root = found.path;
        if let Some(parent) = self.root.parent() {
            fs::create_dir_all(parent).map_err(|e| io_message(&e, parent))?;
        }
        let root = self.root.clone();
        if wanted == Wanted::Folder {
            self.make_dir(&root, found.action)?;
        } else {
            self.write_file(&mut first, &root)?;
        }

        for entry in entries {
            let mut entry = entry.map_err(|e| read_error(&e))?;
            self.entry(&mut entry, &name, wanted)?;
        }
        if !self.saw_end {
            return Err(
                "the transfer ended before the sender finished, so files may be missing".to_owned(),
            );
        }
        Ok(())
    }

    fn root_name<R: Read>(&self, entry: &tar::Entry<'_, R>) -> Result<String, String> {
        let path = entry.path().map_err(|e| read_error(&e))?;
        let names = rules::inside(&path).map_err(|_| "the transfer starts with a bad path")?;
        let [name] = names.as_slice() else {
            return Err("the transfer must start with one file or folder".to_owned());
        };
        let name = name
            .to_str()
            .ok_or("the name of the first entry is not valid UTF-8")?;
        match rules::name_problem(name, self.platform) {
            Some(problem) => Err(format!("{name} cannot be created here: {problem}")),
            None => Ok(name.to_owned()),
        }
    }

    fn entry<R: Read>(
        &mut self,
        entry: &mut tar::Entry<'_, R>,
        root_name: &str,
        root: Wanted,
    ) -> Result<(), String> {
        let raw = entry.path().map_err(|e| read_error(&e))?.into_owned();
        if raw == Path::new(SKIP_REPORT_ENTRY) {
            return self.read_report(entry);
        }
        let kind = entry.header().entry_type();
        if matches!(kind, EntryType::XGlobalHeader) {
            return Ok(());
        }
        if root == Wanted::File {
            return Err("the transfer holds more than one file where one was expected".to_owned());
        }
        let names = rules::inside(&raw).map_err(|problem| {
            let why = match problem {
                PathProblem::Absolute => "is an absolute path",
                PathProblem::ParentDir => "contains ..",
                PathProblem::Empty => "is empty",
            };
            format!(
                "the transfer holds an entry whose path {why}: {}",
                raw.display()
            )
        })?;
        if names[0] != root_name {
            return Err(format!(
                "the transfer holds an entry outside its root: {}",
                raw.display()
            ));
        }
        let mut rel = PathBuf::new();
        for name in &names[1..] {
            rel.push(name);
        }
        if rel.as_os_str().is_empty() {
            return Ok(());
        }
        if self.skipped_dirs.iter().any(|dir| rel.starts_with(dir)) {
            return Ok(());
        }
        if let Some(problem) = self.name_problem(&rel) {
            if kind == EntryType::Directory {
                self.skipped_dirs.push(rel.clone());
            }
            self.skipped.push(rel.display().to_string(), problem);
            return Ok(());
        }
        if let Some(first) = self.case_clash(&rel) {
            if kind == EntryType::Directory {
                self.skipped_dirs.push(rel.clone());
            }
            self.skipped.push(
                rel.display().to_string(),
                format!(
                    "it differs only by case from {first}, and this system treats them as one name"
                ),
            );
            return Ok(());
        }
        let target = self.prepare_parent(&rel)?;
        match kind {
            EntryType::Directory => {
                let action = rules::check_existing(
                    &target,
                    kind_at(&target),
                    Wanted::Folder,
                    self.overwrite,
                )?;
                self.make_dir(&target, action)
            }
            EntryType::Regular | EntryType::Continuous => self.write_file(entry, &target),
            EntryType::Symlink => self.write_link(entry, &rel, &target),
            other => {
                self.skipped.push(
                    rel.display().to_string(),
                    format!("it is not a file, folder, or link ({other:?})"),
                );
                Ok(())
            }
        }
    }

    fn read_report<R: Read>(&mut self, entry: &mut tar::Entry<'_, R>) -> Result<(), String> {
        let mut json = Vec::new();
        entry
            .take(MAX_REPORT_BYTES)
            .read_to_end(&mut json)
            .map_err(|e| read_error(&e))?;
        if let Ok(list) = serde_json::from_slice::<SkipList>(&json) {
            self.sender_skipped = list;
        }
        self.saw_end = true;
        Ok(())
    }

    /// The earlier entry that `rel` collides with on a system that ignores case, remembering `rel` otherwise.
    fn case_clash(&mut self, rel: &Path) -> Option<String> {
        if !self.platform.ignores_case() {
            return None;
        }
        let shown = rel.display().to_string();
        match self.written.entry(shown.to_lowercase()) {
            Entry::Occupied(known) if *known.get() != shown => Some(known.get().clone()),
            Entry::Occupied(_) => None,
            Entry::Vacant(slot) => {
                slot.insert(shown);
                None
            }
        }
    }

    fn name_problem(&self, rel: &Path) -> Option<String> {
        for part in rel.components() {
            let name = part.as_os_str();
            let Some(name) = name.to_str() else {
                return Some("the name is not valid UTF-8".to_owned());
            };
            if let Some(problem) = rules::name_problem(name, self.platform) {
                return Some(problem);
            }
        }
        None
    }

    /// The path for `rel` under the root, after making sure every folder above it is a real folder.
    fn prepare_parent(&mut self, rel: &Path) -> Result<PathBuf, String> {
        let target = self.root.join(rel);
        let Some(parent_rel) = rel.parent().filter(|p| !p.as_os_str().is_empty()) else {
            return Ok(target);
        };
        let parent = self.root.join(parent_rel);
        if self.checked_parent.as_deref() == Some(parent.as_path()) {
            return Ok(target);
        }
        let mut current = self.root.clone();
        for part in parent_rel.components() {
            current.push(part);
            match fs::symlink_metadata(&current) {
                Ok(meta) if meta.is_dir() => {}
                Ok(meta) if meta.is_symlink() => {
                    return Err(format!(
                        "{} is a symbolic link, so entries under it are refused",
                        current.display()
                    ));
                }
                Ok(_) => {
                    return Err(format!(
                        "{} is a file where a folder is needed",
                        current.display()
                    ));
                }
                Err(error) if error.kind() == io::ErrorKind::NotFound => {
                    fs::create_dir(&current).map_err(|e| io_message(&e, &current))?;
                    self.progress.folders += 1;
                }
                Err(error) => return Err(io_message(&error, &current)),
            }
        }
        self.checked_parent = Some(parent);
        Ok(target)
    }

    fn make_dir(&mut self, path: &Path, action: Action) -> Result<(), String> {
        if action != Action::Merge {
            fs::create_dir_all(path).map_err(|e| io_message(&e, path))?;
        }
        self.progress.folders += 1;
        Ok(())
    }

    fn write_file<R: Read>(
        &mut self,
        entry: &mut tar::Entry<'_, R>,
        target: &Path,
    ) -> Result<(), String> {
        rules::check_existing(target, kind_at(target), Wanted::File, self.overwrite)?;
        let parent = target
            .parent()
            .ok_or_else(|| format!("{} has no folder to write in", target.display()))?;
        let temp = Temp::new(parent);
        let mode = entry.header().mode().unwrap_or(0o644);
        let modified = entry.header().mtime().ok();
        let file = fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(temp.path())
            .map_err(|e| io_message(&e, target))?;
        let mut bytes = 0;
        let copied = {
            let mut out = BufWriter::with_capacity(WRITE_BUFFER, file);
            let mut input = Counted {
                inner: &mut *entry,
                bytes: &mut bytes,
            };
            io::copy(&mut input, &mut out)
                .and_then(|_| out.into_inner().map_err(io::IntoInnerError::into_error))
        };
        self.progress.bytes += bytes;
        let file = copied.map_err(|e| format!("writing {} failed: {e}", target.display()))?;
        if bytes != entry.size() {
            return Err(format!(
                "the transfer ended in the middle of {}",
                target.display()
            ));
        }
        set_mode(&file, mode).map_err(|e| io_message(&e, target))?;
        if let Some(secs) = modified.filter(|secs| *secs > 0) {
            let _ = file.set_modified(UNIX_EPOCH + Duration::from_secs(secs));
        }
        drop(file);
        temp.place(target).map_err(|e| io_message(&e, target))?;
        self.progress.files += 1;
        Ok(())
    }

    fn write_link<R: Read>(
        &mut self,
        entry: &tar::Entry<'_, R>,
        rel: &Path,
        target: &Path,
    ) -> Result<(), String> {
        let link = entry
            .link_name()
            .map_err(|e| read_error(&e))?
            .ok_or("a link in the transfer has no target")?
            .into_owned();
        let action = rules::check_existing(target, kind_at(target), Wanted::Link, self.overwrite)?;
        if action == Action::Replace {
            fs::remove_file(target).map_err(|e| io_message(&e, target))?;
        }
        match make_link(&link, target) {
            Ok(()) => self.progress.files += 1,
            Err(error) if self.platform == Platform::Windows => self.skipped.push(
                rel.display().to_string(),
                format!("Windows would not create the link: {error}"),
            ),
            Err(error) => return Err(io_message(&error, target)),
        }
        Ok(())
    }
}

fn io_message(error: &io::Error, path: &Path) -> String {
    format!("{}: {error}", path.display())
}

#[cfg(unix)]
fn set_mode(file: &fs::File, mode: u32) -> io::Result<()> {
    use std::os::unix::fs::PermissionsExt;
    file.set_permissions(fs::Permissions::from_mode(mode & 0o777))
}

#[cfg(not(unix))]
#[expect(
    clippy::unnecessary_wraps,
    reason = "matches the signature of the Unix version"
)]
fn set_mode(_file: &fs::File, _mode: u32) -> io::Result<()> {
    Ok(())
}

#[cfg(unix)]
fn make_link(link: &Path, at: &Path) -> io::Result<()> {
    std::os::unix::fs::symlink(link, at)
}

#[cfg(windows)]
fn make_link(link: &Path, at: &Path) -> io::Result<()> {
    let points_at_folder = at.parent().is_some_and(|parent| parent.join(link).is_dir());
    if points_at_folder {
        std::os::windows::fs::symlink_dir(link, at)
    } else {
        std::os::windows::fs::symlink_file(link, at)
    }
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use tar::{Builder, Header};

    use super::*;
    use crate::pack::pack;

    struct Dir(PathBuf);

    impl Dir {
        fn new() -> Self {
            let path = std::env::temp_dir().join(format!("computer-transfer-{}", Uuid::new_v4()));
            fs::create_dir_all(&path).expect("temp dir is creatable");
            Self(fs::canonicalize(path).expect("temp dir exists"))
        }

        fn join(&self, name: &str) -> PathBuf {
            self.0.join(name)
        }
    }

    impl Drop for Dir {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    fn packed(source: &Path) -> Vec<u8> {
        let mut out = Vec::new();
        pack(source, &mut out, Platform::current()).unwrap();
        out
    }

    fn put(dest: &Path, archive: &[u8], overwrite: bool) -> Result<Unpacked, Failure> {
        unpack(Cursor::new(archive), dest, overwrite, Platform::current())
    }

    fn raw_archive(entries: &[(&str, EntryType, &[u8], Option<&str>)]) -> Vec<u8> {
        let mut builder = Builder::new(Vec::new());
        for (path, kind, data, link) in entries {
            let mut header = Header::new_gnu();
            header.as_old_mut().name[..path.len()].copy_from_slice(path.as_bytes());
            if let Some(link) = link {
                header.as_old_mut().linkname[..link.len()].copy_from_slice(link.as_bytes());
            }
            header.set_entry_type(*kind);
            header.set_mode(0o644);
            header.set_size(data.len() as u64);
            header.set_cksum();
            builder.append(&header, *data).unwrap();
        }
        if !entries.iter().any(|(path, ..)| *path == SKIP_REPORT_ENTRY) {
            let empty = serde_json::to_vec(&SkipList::default()).unwrap();
            let mut header = Header::new_gnu();
            header.as_old_mut().name[..SKIP_REPORT_ENTRY.len()]
                .copy_from_slice(SKIP_REPORT_ENTRY.as_bytes());
            header.set_size(empty.len() as u64);
            header.set_cksum();
            builder.append(&header, empty.as_slice()).unwrap();
        }
        builder.into_inner().unwrap()
    }

    fn files_under(dir: &Path) -> Vec<String> {
        let mut found = Vec::new();
        let mut stack = vec![dir.to_path_buf()];
        while let Some(current) = stack.pop() {
            for entry in fs::read_dir(&current).unwrap() {
                let path = entry.unwrap().path();
                let shown = path.strip_prefix(dir).unwrap().to_string_lossy();
                found.push(shown.replace('\\', "/"));
                if fs::symlink_metadata(&path).unwrap().is_dir() {
                    stack.push(path);
                }
            }
        }
        found.sort();
        found
    }

    fn source_tree(base: &Dir) -> PathBuf {
        let root = base.join("tree");
        fs::create_dir_all(root.join("empty")).unwrap();
        fs::create_dir_all(root.join("deep/er")).unwrap();
        let binary: Vec<u8> = (0..=255u8).cycle().take(300_000).collect();
        fs::write(root.join("blob.bin"), &binary).unwrap();
        fs::write(root.join("deep/er/note.txt"), "héllo\n").unwrap();
        root
    }

    #[test]
    fn a_folder_arrives_with_its_bytes_and_empty_folders() {
        let base = Dir::new();
        let root = source_tree(&base);
        let out = Dir::new();
        let done = put(&out.0, &packed(&root), false).unwrap();

        assert_eq!(done.path, out.join("tree"));
        assert_eq!(
            done.progress,
            Progress {
                files: 2,
                folders: 4,
                bytes: 300_000 + "héllo\n".len() as u64
            }
        );
        assert_eq!(
            fs::read(out.join("tree/blob.bin")).unwrap(),
            fs::read(root.join("blob.bin")).unwrap()
        );
        assert_eq!(
            fs::read_to_string(out.join("tree/deep/er/note.txt")).unwrap(),
            "héllo\n"
        );
        assert!(out.join("tree/empty").is_dir());
        assert!(done.skipped.is_empty());
    }

    #[test]
    fn a_file_goes_inside_an_existing_folder_or_takes_the_new_name() {
        let base = Dir::new();
        let file = base.join("report.txt");
        fs::write(&file, "data").unwrap();
        let archive = packed(&file);

        let out = Dir::new();
        let inside = put(&out.0, &archive, false).unwrap();
        assert_eq!(inside.path, out.join("report.txt"));

        let renamed = put(&out.join("new/name.md"), &archive, false).unwrap();
        assert_eq!(renamed.path, out.join("new/name.md"));
        assert_eq!(fs::read_to_string(out.join("new/name.md")).unwrap(), "data");
    }

    #[test]
    fn an_existing_folder_is_refused_by_name_and_nothing_in_it_is_touched() {
        let base = Dir::new();
        let archive = packed(&source_tree(&base));
        let out = Dir::new();
        fs::create_dir(out.join("tree")).unwrap();
        fs::write(out.join("tree/keep.txt"), "mine").unwrap();
        fs::write(out.join("tree/blob.bin"), "old").unwrap();

        let error = put(&out.0, &archive, false).unwrap_err();
        assert_eq!(
            files_under(&out.0),
            ["tree", "tree/blob.bin", "tree/keep.txt"]
        );
        assert_eq!(
            fs::read_to_string(out.join("tree/blob.bin")).unwrap(),
            "old"
        );
        assert!(
            error.message.contains("tree") && error.message.contains("overwrite"),
            "{error}"
        );
        assert_eq!(error.progress, Progress::default());
    }

    #[test]
    fn overwrite_merges_a_folder_replacing_same_named_files_and_keeping_others() {
        let base = Dir::new();
        let archive = packed(&source_tree(&base));
        let out = Dir::new();
        fs::create_dir_all(out.join("tree/deep")).unwrap();
        fs::write(out.join("tree/keep.txt"), "mine").unwrap();
        fs::write(out.join("tree/blob.bin"), "old").unwrap();

        put(&out.0, &archive, true).unwrap();
        assert_eq!(
            fs::read_to_string(out.join("tree/keep.txt")).unwrap(),
            "mine"
        );
        assert_eq!(fs::read(out.join("tree/blob.bin")).unwrap().len(), 300_000);
        assert!(out.join("tree/deep/er/note.txt").is_file());
        assert!(
            !files_under(&out.0)
                .iter()
                .any(|name| name.contains(".computer-use-transfer-"))
        );
    }

    #[test]
    fn a_file_never_replaces_a_folder_during_a_merge() {
        let base = Dir::new();
        let archive = packed(&source_tree(&base));
        let out = Dir::new();
        fs::create_dir_all(out.join("tree/blob.bin/inner")).unwrap();

        let error = put(&out.0, &archive, true).unwrap_err();
        assert!(error.message.contains("is a folder"), "{error}");
        assert!(out.join("tree/blob.bin/inner").is_dir());
    }

    #[test]
    fn a_folder_cannot_replace_a_file_at_the_root() {
        let base = Dir::new();
        let archive = packed(&source_tree(&base));
        let out = Dir::new();
        fs::write(out.join("target"), "file").unwrap();
        let error = put(&out.join("target"), &archive, true).unwrap_err();
        assert!(error.message.contains("not a folder"), "{error}");
        assert_eq!(fs::read_to_string(out.join("target")).unwrap(), "file");
    }

    #[test]
    fn a_cut_off_stream_leaves_no_half_file_and_no_temporary_file() {
        let base = Dir::new();
        let archive = packed(&source_tree(&base));
        let cut = &archive[..archive.len() / 2];
        let out = Dir::new();

        let error = put(&out.0, cut, false).unwrap_err();
        assert!(
            !files_under(&out.0)
                .iter()
                .any(|name| name.contains(".computer-use-transfer-"))
        );
        assert!(!out.join("tree/blob.bin").exists());
        assert!(error.progress.bytes < 300_000, "{error:?}");
    }

    #[test]
    fn absolute_and_parent_paths_are_refused() {
        for path in ["tree/../../escape", "/etc/escape"] {
            let archive = raw_archive(&[
                ("tree/", EntryType::Directory, b"", None),
                (path, EntryType::Regular, b"x", None),
            ]);
            let out = Dir::new();
            let error = put(&out.join("dest"), &archive, false).unwrap_err();
            assert!(
                error.message.contains("..") || error.message.contains("absolute"),
                "{path}: {error}"
            );
            assert_eq!(files_under(&out.0), ["dest"]);
        }
    }

    #[cfg(unix)]
    #[test]
    fn an_entry_cannot_go_through_a_link_made_earlier_in_the_archive() {
        let outside = Dir::new();
        let archive = raw_archive(&[
            ("tree/", EntryType::Directory, b"", None),
            (
                "tree/out",
                EntryType::Symlink,
                b"",
                Some(outside.0.to_str().unwrap()),
            ),
            ("tree/out/pwned", EntryType::Regular, b"x", None),
        ]);
        let out = Dir::new();
        let error = put(&out.0, &archive, false).unwrap_err();
        assert!(error.message.contains("symbolic link"), "{error}");
        assert_eq!(files_under(&outside.0), Vec::<String>::new());
    }

    #[cfg(unix)]
    #[test]
    fn executables_links_and_pipes_are_handled_like_cp_would() {
        use std::os::unix::fs::PermissionsExt;

        let base = Dir::new();
        let root = source_tree(&base);
        fs::write(root.join("run.sh"), "#!/bin/sh\n").unwrap();
        fs::set_permissions(root.join("run.sh"), fs::Permissions::from_mode(0o755)).unwrap();
        std::os::unix::fs::symlink("blob.bin", root.join("link")).unwrap();
        std::os::unix::fs::symlink("/nonexistent/absolute", root.join("dangling")).unwrap();
        let fifo = std::process::Command::new("mkfifo")
            .arg(root.join("pipe"))
            .status()
            .unwrap();
        assert!(fifo.success());

        let out = Dir::new();
        let done = put(&out.0, &packed(&root), false).unwrap();
        let mode = |name: &str| fs::metadata(out.join(name)).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode("tree/run.sh"), 0o755);
        assert_eq!(mode("tree/blob.bin"), 0o644);
        assert_eq!(
            fs::read_link(out.join("tree/link")).unwrap(),
            Path::new("blob.bin")
        );
        assert_eq!(
            fs::read_link(out.join("tree/dangling")).unwrap(),
            Path::new("/nonexistent/absolute")
        );
        assert!(!out.join("tree/pipe").exists());
        assert_eq!(done.skipped.entries.len(), 1);
        assert_eq!(done.skipped.entries[0].path, "tree/pipe");

        let again = put(&out.0, &packed(&root), true).unwrap();
        assert_eq!(again.progress.files, done.progress.files);
        let refused = put(&out.0, &packed(&root), false).unwrap_err();
        assert!(refused.message.contains("already exists"), "{refused}");
    }

    #[test]
    fn names_the_target_cannot_hold_are_skipped_with_their_contents_and_listed() {
        let archive = raw_archive(&[
            ("tree/", EntryType::Directory, b"", None),
            ("tree/fine.txt", EntryType::Regular, b"ok", None),
            ("tree/a:b.txt", EntryType::Regular, b"x", None),
            ("tree/CON/", EntryType::Directory, b"", None),
            ("tree/CON/inner.txt", EntryType::Regular, b"x", None),
        ]);
        let out = Dir::new();
        let done = unpack(Cursor::new(archive), &out.0, false, Platform::Windows).unwrap();
        assert_eq!(files_under(&out.0), ["tree", "tree/fine.txt"]);
        let skipped: Vec<_> = done
            .skipped
            .entries
            .iter()
            .map(|s| s.path.as_str())
            .collect();
        assert_eq!(skipped, ["a:b.txt", "CON"]);
    }

    #[test]
    fn the_senders_skips_follow_the_receivers_own() {
        let mut sent = SkipList::default();
        sent.push("tree/pipe", "it is a pipe");
        let report = serde_json::to_vec(&sent).unwrap();
        let archive = raw_archive(&[
            ("tree/", EntryType::Directory, b"", None),
            ("tree/a:b", EntryType::Regular, b"x", None),
            (SKIP_REPORT_ENTRY, EntryType::Regular, &report, None),
        ]);
        let out = Dir::new();
        let done = unpack(Cursor::new(archive), &out.0, false, Platform::Windows).unwrap();
        let skipped: Vec<_> = done
            .skipped
            .entries
            .iter()
            .map(|s| s.path.as_str())
            .collect();
        assert_eq!(skipped, ["a:b", "tree/pipe"]);
    }

    #[test]
    fn a_stream_cut_exactly_between_entries_is_not_a_finished_transfer() {
        let base = Dir::new();
        let archive = packed(&source_tree(&base));
        let report_and_end = 512 + 512 + 1024;
        let cut = &archive[..archive.len() - report_and_end];
        let out = Dir::new();

        let error = put(&out.0, cut, false).unwrap_err();
        assert!(error.message.contains("ended before"), "{error}");
        assert_eq!(
            error.progress.files, 2,
            "everything before the cut was written"
        );
        assert!(put(&out.join("again"), &archive, false).is_ok());
    }

    #[test]
    fn names_that_differ_only_by_case_are_skipped_where_case_is_ignored() {
        let archive = raw_archive(&[
            ("tree/", EntryType::Directory, b"", None),
            ("tree/A.txt", EntryType::Regular, b"first", None),
            ("tree/a.txt", EntryType::Regular, b"second", None),
            ("tree/Dir/", EntryType::Directory, b"", None),
            ("tree/dir/", EntryType::Directory, b"", None),
            ("tree/dir/inner", EntryType::Regular, b"x", None),
        ]);
        let out = Dir::new();
        let done = unpack(Cursor::new(archive), &out.0, true, Platform::Windows).unwrap();
        assert_eq!(files_under(&out.0), ["tree", "tree/A.txt", "tree/Dir"]);
        assert_eq!(fs::read_to_string(out.join("tree/A.txt")).unwrap(), "first");
        let skipped: Vec<_> = done
            .skipped
            .entries
            .iter()
            .map(|s| {
                (
                    s.path.as_str(),
                    s.reason.contains("differs only by case from"),
                )
            })
            .collect();
        assert_eq!(skipped, [("a.txt", true), ("dir", true)]);
    }

    #[test]
    fn check_destination_applies_the_root_rules_before_any_data_moves() {
        let out = Dir::new();
        fs::create_dir(out.join("taken")).unwrap();
        fs::write(out.join("file"), "x").unwrap();
        let check = |dest: &str, name: &str, folder, overwrite| {
            check_destination(&out.join(dest), name, folder, overwrite, Platform::Unix)
        };
        assert_eq!(
            check(".", "new", true, false),
            Ok(out.join(".").join("new"))
        );
        let error = check(".", "taken", true, false).unwrap_err();
        assert!(error.contains("already exists"), "{error}");
        assert!(check(".", "taken", true, true).is_ok());
        assert!(
            check("file", "x", true, true)
                .unwrap_err()
                .contains("not a folder")
        );
        let windows = check_destination(&out.0, "a:b", false, false, Platform::Windows);
        assert!(windows.unwrap_err().contains("cannot be created"));
    }
}