mnml-rs 0.2.20

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! Background file transfers — copy / move / delete with progress.
//!
//! #files item 6. **User ask 2026-08-30:** "we might need somewhere to
//! show transfer info like progress and speed."
//!
//! This is also the GATE on everything bulk in the Files pane. The file
//! operations are synchronous on the render thread today: a
//! `copy_recursively` of a 4 GB directory freezes mnml and reports by
//! toast when it eventually finishes. That is unacceptable for a file
//! manager, so the worker has to exist before cross-pane drag or
//! multi-select operations on large sets.
//!
//! Shape follows the Sonos worker and the git loader — one worker thread
//! reporting over an mpsc channel, the render loop never blocking on the
//! filesystem.
//!
//! Speed and ETA are DERIVED from bytes + elapsed rather than stored, so
//! they cannot go stale while a transfer sits between progress messages.

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Sender;
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferKind {
    Copy,
    Move,
    Delete,
}

impl TransferKind {
    pub fn verb(self) -> &'static str {
        match self {
            TransferKind::Copy => "Copying",
            TransferKind::Move => "Moving",
            TransferKind::Delete => "Deleting",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransferState {
    /// Walking the sources to compute a byte total. Progress is not yet
    /// meaningful, so the UI shows an indeterminate state rather than a
    /// misleading 0%.
    Sizing,
    Running,
    Done,
    Failed(String),
    Cancelled,
}

impl TransferState {
    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            TransferState::Done | TransferState::Failed(_) | TransferState::Cancelled
        )
    }
}

/// What a worker sends back. Every variant carries the id because the
/// app multiplexes several transfers onto one receiver.
#[derive(Debug, Clone)]
pub enum TransferMsg {
    /// The sizing pass finished — totals are now known.
    Total {
        id: u64,
        bytes_total: u64,
        files_total: usize,
    },
    Progress {
        id: u64,
        bytes_done: u64,
        files_done: usize,
    },
    Done {
        id: u64,
        /// Entries left behind because they could not be read or had
        /// vanished. A transfer that quietly skipped files must say so.
        skipped: usize,
    },
    Failed {
        id: u64,
        err: String,
        /// Whether the partial destination was cleaned up — the same
        /// promise `Cancelled` makes. A hard failure is no different
        /// from the user's side.
        cleaned_up: bool,
    },
    Cancelled {
        id: u64,
        /// Whether the partial destination was cleaned up. A cancel that
        /// leaves debris must SAY so — silently leaving half a directory
        /// behind is worse than not offering cancel.
        cleaned_up: bool,
    },
}

#[derive(Debug)]
pub struct Transfer {
    pub id: u64,
    pub kind: TransferKind,
    pub sources: Vec<PathBuf>,
    /// Destination directory. `None` for a delete.
    pub dest: Option<PathBuf>,
    pub bytes_total: u64,
    pub bytes_done: u64,
    pub files_total: usize,
    pub files_done: usize,
    pub started_at: Instant,
    pub finished_at: Option<Instant>,
    pub state: TransferState,
    cancel_flag: Arc<AtomicBool>,
}

impl Transfer {
    pub fn new(
        id: u64,
        kind: TransferKind,
        sources: Vec<PathBuf>,
        dest: Option<PathBuf>,
        cancel_flag: Arc<AtomicBool>,
    ) -> Self {
        Transfer {
            id,
            kind,
            sources,
            dest,
            bytes_total: 0,
            bytes_done: 0,
            files_total: 0,
            files_done: 0,
            started_at: Instant::now(),
            finished_at: None,
            state: TransferState::Sizing,
            cancel_flag,
        }
    }

    /// Request cancellation. The worker checks the flag between files, so
    /// a single enormous file still finishes copying — cancelling mid-file
    /// would leave a truncated destination that looks complete.
    pub fn cancel(&self) {
        self.cancel_flag.store(true, Ordering::Relaxed);
    }

    pub fn is_cancelling(&self) -> bool {
        self.cancel_flag.load(Ordering::Relaxed) && !self.state.is_terminal()
    }

    /// 0..=100. Reports 100 for a finished transfer even when the byte
    /// total was wrong (a file that shrank between the sizing pass and
    /// the copy), because "Done at 97%" reads as a failure.
    pub fn percent(&self) -> u8 {
        if self.state == TransferState::Done {
            return 100;
        }
        if self.bytes_total == 0 {
            return 0;
        }
        let pct = (self.bytes_done as f64 / self.bytes_total as f64 * 100.0).round();
        pct.clamp(0.0, 100.0) as u8
    }

    /// Elapsed time — frozen at completion, so a finished transfer's
    /// average speed does not decay while it sits in the list.
    pub fn elapsed(&self) -> Duration {
        self.finished_at
            .unwrap_or_else(Instant::now)
            .saturating_duration_since(self.started_at)
    }

    /// Bytes per second, or `None` before there is enough signal to be
    /// honest about. A speed computed over the first few milliseconds is
    /// noise, and showing "1.4 GB/s" that immediately collapses is worse
    /// than showing nothing.
    pub fn speed_bytes_per_sec(&self) -> Option<f64> {
        let secs = self.elapsed().as_secs_f64();
        if secs < 0.25 || self.bytes_done == 0 {
            return None;
        }
        Some(self.bytes_done as f64 / secs)
    }

    pub fn eta(&self) -> Option<Duration> {
        if self.state.is_terminal() || self.bytes_total == 0 {
            return None;
        }
        let speed = self.speed_bytes_per_sec()?;
        if speed <= 0.0 {
            return None;
        }
        let remaining = self.bytes_total.saturating_sub(self.bytes_done);
        Some(Duration::from_secs_f64(remaining as f64 / speed))
    }

    /// Fold one worker message into this transfer. Returns true when the
    /// message moved it to a terminal state, so the caller can toast once.
    pub fn apply(&mut self, msg: &TransferMsg) -> bool {
        match msg {
            TransferMsg::Total {
                bytes_total,
                files_total,
                ..
            } => {
                self.bytes_total = *bytes_total;
                self.files_total = *files_total;
                // Sizing is over even if the total is zero (an empty
                // directory); leaving it in Sizing would stall the UI.
                self.state = TransferState::Running;
                false
            }
            TransferMsg::Progress {
                bytes_done,
                files_done,
                ..
            } => {
                self.bytes_done = *bytes_done;
                self.files_done = *files_done;
                false
            }
            TransferMsg::Done { .. } => {
                self.state = TransferState::Done;
                self.finished_at = Some(Instant::now());
                true
            }
            TransferMsg::Failed { err, .. } => {
                self.state = TransferState::Failed(err.clone());
                self.finished_at = Some(Instant::now());
                true
            }
            TransferMsg::Cancelled { .. } => {
                self.state = TransferState::Cancelled;
                self.finished_at = Some(Instant::now());
                true
            }
        }
    }
}

/// Total bytes and file count under `paths`, following the same rules the
/// copy will.
///
/// Symlinks are counted as one file of their own (link) size and never
/// followed — following them would double-count a link into the tree and
/// can loop forever on a cycle.
pub fn measure(paths: &[PathBuf], cancel: &AtomicBool) -> (u64, usize) {
    let mut bytes = 0u64;
    let mut files = 0usize;
    let mut stack: Vec<PathBuf> = paths.to_vec();
    while let Some(p) = stack.pop() {
        if cancel.load(Ordering::Relaxed) {
            break;
        }
        let Ok(md) = std::fs::symlink_metadata(&p) else {
            continue;
        };
        if md.is_dir() {
            if let Ok(rd) = std::fs::read_dir(&p) {
                for e in rd.flatten() {
                    stack.push(e.path());
                }
            }
        } else {
            bytes = bytes.saturating_add(md.len());
            files += 1;
        }
    }
    (bytes, files)
}

/// Format a byte count for a chip — always 3-4 visible characters plus a
/// unit, so the statusline chip keeps a constant width.
pub fn human_bytes(n: u64) -> String {
    // Through E: a u64 byte count reaches 16 EiB, and stopping at T
    // rendered that as "16777216T" — nine cells in a chip budgeted for
    // five. Caught by the width assertion below, not by inspection.
    const UNITS: [&str; 7] = ["B", "K", "M", "G", "T", "P", "E"];
    let mut v = n as f64;
    let mut u = 0;
    while v >= 1024.0 && u < UNITS.len() - 1 {
        v /= 1024.0;
        u += 1;
    }
    if u == 0 {
        format!("{n}{}", UNITS[0])
    } else if v < 10.0 {
        format!("{v:.1}{}", UNITS[u])
    } else {
        format!("{v:.0}{}", UNITS[u])
    }
}

/// `1m 04s` / `12s` / `2h 05m`. Deliberately never "0s" for a live
/// transfer — that reads as finished.
pub fn human_eta(d: Duration) -> String {
    let s = d.as_secs().max(1);
    if s < 60 {
        format!("{s}s")
    } else if s < 3600 {
        format!("{}m {:02}s", s / 60, s % 60)
    } else {
        format!("{}h {:02}m", s / 3600, (s % 3600) / 60)
    }
}

// ─── the worker ────────────────────────────────────────────────────────

/// How often to report progress. Every file would flood the channel on a
/// tree of small files and the render loop would spend its tick draining
/// it; a time-based gate keeps the message rate bounded regardless of
/// file size.
const PROGRESS_EVERY: Duration = Duration::from_millis(100);

struct Reporter {
    id: u64,
    tx: Sender<TransferMsg>,
    bytes_done: u64,
    files_done: usize,
    last_sent: Instant,
}

impl Reporter {
    fn tick(&mut self, force: bool) {
        if !force && self.last_sent.elapsed() < PROGRESS_EVERY {
            return;
        }
        self.last_sent = Instant::now();
        let _ = self.tx.send(TransferMsg::Progress {
            id: self.id,
            bytes_done: self.bytes_done,
            files_done: self.files_done,
        });
    }
}

/// Whether a copy ran to completion or stopped early on cancel.
///
/// This distinction is the whole reason the type exists. `copy_one` used
/// to return `Ok(())` on cancel, which is indistinguishable from success
/// — and the Move branch reads success as licence to delete the source.
/// A user cancelling a large cross-filesystem move therefore lost the
/// original while the destination held only part of the tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CopyOutcome {
    Completed,
    Stopped,
}

/// Run one transfer to completion on this thread. `spawn` wraps it; this
/// is separate so tests can drive it synchronously and assert on the
/// exact message sequence rather than racing a thread.
pub fn run(
    id: u64,
    kind: TransferKind,
    items: Vec<(PathBuf, PathBuf)>,
    cancel: Arc<AtomicBool>,
    tx: Sender<TransferMsg>,
) {
    // Per-source totals, not just an aggregate: the rename fast path
    // completes one source at a stroke and has to credit that source's
    // own size. Crediting the whole transfer's total (what it used to do)
    // showed 100% after the first of several sources, then counted past
    // the total as the rest copied.
    let per_source: Vec<(u64, usize)> = items
        .iter()
        .map(|(src, _)| measure(std::slice::from_ref(src), &cancel))
        .collect();
    let bytes_total: u64 = per_source.iter().map(|(b, _)| *b).sum();
    let files_total: usize = per_source.iter().map(|(_, f)| *f).sum();
    let _ = tx.send(TransferMsg::Total {
        id,
        bytes_total,
        files_total,
    });

    let mut rep = Reporter {
        id,
        tx: tx.clone(),
        bytes_done: 0,
        files_done: 0,
        last_sent: Instant::now(),
    };

    // Paths this transfer actually BROUGHT INTO EXISTENCE, youngest last.
    //
    // Recorded per entry as it is created, and only when it was not
    // already there. The earlier version pushed each top-level target
    // before writing it, without checking — so cancelling a paste over an
    // existing name deleted the user's original file, and cancelling a
    // merge into an existing folder called `remove_dir_all` on a tree
    // this transfer had only added to.
    let mut created: Vec<PathBuf> = Vec::new();
    // Entries skipped because they could not be read or had vanished. A
    // transfer that quietly leaves files behind must still say so.
    let mut skipped = 0usize;

    for ((src, target), (src_bytes, src_files)) in items.iter().zip(per_source.iter()) {
        if cancel.load(Ordering::Relaxed) {
            finish_cancelled(id, &created, &tx);
            return;
        }
        let res: Result<CopyOutcome, String> = match kind {
            TransferKind::Delete => delete_one(src, &mut rep, &cancel),
            TransferKind::Copy | TransferKind::Move => {
                let target_existed = target.exists();
                // A move within one filesystem is a rename — no bytes
                // cross the disk, so never fall back to copy+delete when
                // the cheap path is available.
                if kind == TransferKind::Move && std::fs::rename(src, target).is_ok() {
                    if !target_existed {
                        created.push(target.clone());
                    }
                    rep.bytes_done = rep.bytes_done.saturating_add(*src_bytes);
                    rep.files_done += *src_files;
                    // Forced: a rename finishes an entire source in one
                    // step, so the 100ms gate would otherwise swallow the
                    // only progress this source ever produces.
                    rep.tick(true);
                    Ok(CopyOutcome::Completed)
                } else {
                    match copy_one(src, target, &mut rep, &cancel, &mut created, &mut skipped) {
                        // A cross-filesystem move removes the source ONLY
                        // on a genuine full completion. `Stopped` means
                        // the user cancelled mid-tree and the destination
                        // is partial.
                        Ok(CopyOutcome::Completed) if kind == TransferKind::Move => {
                            remove_path(src).map(|_| CopyOutcome::Completed)
                        }
                        other => other,
                    }
                }
            }
        };
        match res {
            Err(e) => {
                // Failure cleans up too. Leaving debris silently is the
                // thing this module refuses to do on cancel; a hard
                // failure is no different from the user's side.
                let _ = tx.send(TransferMsg::Failed {
                    id,
                    err: e,
                    cleaned_up: cleanup(&created),
                });
                return;
            }
            Ok(CopyOutcome::Stopped) => {
                finish_cancelled(id, &created, &tx);
                return;
            }
            Ok(CopyOutcome::Completed) => {}
        }
    }

    if cancel.load(Ordering::Relaxed) {
        finish_cancelled(id, &created, &tx);
        return;
    }
    rep.tick(true);
    let _ = tx.send(TransferMsg::Done { id, skipped });
}

fn finish_cancelled(id: u64, created: &[PathBuf], tx: &Sender<TransferMsg>) {
    let cleaned = cleanup(created);
    let _ = tx.send(TransferMsg::Cancelled {
        id,
        cleaned_up: cleaned,
    });
}

pub fn spawn(
    id: u64,
    kind: TransferKind,
    items: Vec<(PathBuf, PathBuf)>,
    cancel: Arc<AtomicBool>,
    tx: Sender<TransferMsg>,
) {
    std::thread::spawn(move || run(id, kind, items, cancel, tx));
}

/// Remove the destinations this transfer created. Returns whether every
/// one went away — the caller has to be able to SAY when debris is left.
fn cleanup(created: &[PathBuf]) -> bool {
    let mut ok = true;
    // Youngest first: a directory is recorded before the entries written
    // into it, so reversing removes children before their parents and
    // `remove_dir` never meets a non-empty directory.
    for p in created.iter().rev() {
        if !p.exists() {
            continue;
        }
        if remove_path(p).is_err() {
            ok = false;
        }
    }
    ok
}

fn remove_path(p: &Path) -> Result<(), String> {
    let md = std::fs::symlink_metadata(p).map_err(|e| format!("stat {}: {e}", p.display()))?;
    if md.is_dir() {
        std::fs::remove_dir_all(p).map_err(|e| format!("rm -r {}: {e}", p.display()))
    } else {
        std::fs::remove_file(p).map_err(|e| format!("rm {}: {e}", p.display()))
    }
}

fn delete_one(src: &Path, rep: &mut Reporter, cancel: &AtomicBool) -> Result<CopyOutcome, String> {
    let (bytes, files) = measure(std::slice::from_ref(&src.to_path_buf()), cancel);
    remove_path(src)?;
    rep.bytes_done = rep.bytes_done.saturating_add(bytes);
    rep.files_done += files;
    rep.tick(false);
    Ok(CopyOutcome::Completed)
}

/// Copy `src` to `dst`, reporting bytes as they land.
///
/// Iterative rather than recursive, and it refuses to descend into its
/// own destination — the same self-copy guard `copy_recursively` carries,
/// for the same reason: without it the walk keeps finding what it just
/// wrote and the PROCESS ABORTS on a stack overflow. Reachable in one
/// keystroke by pasting a folder into itself.
///
/// Appends every path it brings into existence to `created`, and only
/// those — a directory that was already there is merged into, never
/// recorded, so a later cleanup cannot delete a tree this transfer merely
/// added to.
fn copy_one(
    src: &Path,
    dst: &Path,
    rep: &mut Reporter,
    cancel: &AtomicBool,
    created: &mut Vec<PathBuf>,
    skipped: &mut usize,
) -> Result<CopyOutcome, String> {
    if crate::app::util::is_self_or_descendant(src, dst) {
        return Err(format!(
            "cannot copy {} into itself",
            src.file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_else(|| src.display().to_string())
        ));
    }
    let mut stack = vec![(src.to_path_buf(), dst.to_path_buf())];
    while let Some((s, d)) = stack.pop() {
        if cancel.load(Ordering::Relaxed) {
            return Ok(CopyOutcome::Stopped);
        }
        // A source entry that has vanished since the sizing pass is
        // SKIPPED, matching `measure`, which does not count what it
        // cannot stat. Failing the whole transfer over a temp file that
        // another process cleaned up mid-copy is the wrong trade.
        let Ok(md) = std::fs::symlink_metadata(&s) else {
            *skipped += 1;
            continue;
        };
        if md.is_dir() {
            if !d.exists() {
                created.push(d.clone());
            }
            std::fs::create_dir_all(&d).map_err(|e| format!("mkdir {}: {e}", d.display()))?;
            let rd = std::fs::read_dir(&s).map_err(|e| format!("read_dir {}: {e}", s.display()))?;
            for e in rd {
                // An entry we cannot even read the name of is counted,
                // not silently dropped — `Done` carries the tally.
                let Ok(e) = e else {
                    *skipped += 1;
                    continue;
                };
                let Some(name) = e.path().file_name().map(|n| n.to_owned()) else {
                    *skipped += 1;
                    continue;
                };
                stack.push((e.path(), d.join(name)));
            }
        } else {
            if !d.exists() {
                created.push(d.clone());
            }
            if md.file_type().is_symlink() {
                copy_symlink(&s, &d)?;
            } else {
                std::fs::copy(&s, &d)
                    .map_err(|e| format!("copy {}{}: {e}", s.display(), d.display()))?;
            }
            rep.bytes_done = rep.bytes_done.saturating_add(md.len());
            rep.files_done += 1;
            rep.tick(false);
        }
    }
    Ok(CopyOutcome::Completed)
}

#[cfg(unix)]
fn copy_symlink(s: &Path, d: &Path) -> Result<(), String> {
    let target = std::fs::read_link(s).map_err(|e| format!("readlink {}: {e}", s.display()))?;
    std::os::unix::fs::symlink(target, d).map_err(|e| format!("symlink {}: {e}", d.display()))
}

#[cfg(not(unix))]
fn copy_symlink(s: &Path, d: &Path) -> Result<(), String> {
    // Windows symlink creation needs a privilege most users lack, so copy
    // the target's contents instead of failing the whole transfer.
    std::fs::copy(s, d)
        .map(|_| ())
        .map_err(|e| format!("copy {}{}: {e}", s.display(), d.display()))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn t(kind: TransferKind) -> Transfer {
        Transfer::new(
            1,
            kind,
            vec![PathBuf::from("/a")],
            Some(PathBuf::from("/b")),
            Arc::new(AtomicBool::new(false)),
        )
    }

    #[test]
    fn a_transfer_starts_in_sizing_so_the_ui_does_not_show_a_fake_zero_percent() {
        let x = t(TransferKind::Copy);
        assert_eq!(x.state, TransferState::Sizing);
        assert!(!x.state.is_terminal());
    }

    /// The sizing pass must end even when the total is zero — an empty
    /// directory would otherwise leave the transfer stuck in Sizing
    /// forever with no progress bar and no completion.
    #[test]
    fn an_empty_total_still_leaves_sizing() {
        let mut x = t(TransferKind::Copy);
        x.apply(&TransferMsg::Total {
            id: 1,
            bytes_total: 0,
            files_total: 0,
        });
        assert_eq!(x.state, TransferState::Running);
    }

    /// "Done at 97%" reads as a failure. A file that shrank between the
    /// sizing pass and the copy makes that easy to hit.
    #[test]
    fn a_finished_transfer_reports_100_even_if_the_byte_total_was_wrong() {
        let mut x = t(TransferKind::Copy);
        x.apply(&TransferMsg::Total {
            id: 1,
            bytes_total: 1000,
            files_total: 1,
        });
        x.apply(&TransferMsg::Progress {
            id: 1,
            bytes_done: 970,
            files_done: 1,
        });
        assert_eq!(x.percent(), 97);
        x.apply(&TransferMsg::Done { id: 1, skipped: 0 });
        assert_eq!(x.percent(), 100, "a completed transfer showed short");
    }

    #[test]
    fn percent_is_clamped_when_more_bytes_arrive_than_were_measured() {
        let mut x = t(TransferKind::Copy);
        x.apply(&TransferMsg::Total {
            id: 1,
            bytes_total: 100,
            files_total: 1,
        });
        x.apply(&TransferMsg::Progress {
            id: 1,
            bytes_done: 250,
            files_done: 1,
        });
        assert_eq!(x.percent(), 100, "a file that grew produced >100%");
    }

    /// Speed over the first few milliseconds is noise. Reporting it makes
    /// the chip flash an absurd number that immediately collapses.
    #[test]
    fn speed_is_withheld_until_there_is_enough_signal() {
        let mut x = t(TransferKind::Copy);
        x.apply(&TransferMsg::Progress {
            id: 1,
            bytes_done: 4096,
            files_done: 1,
        });
        assert!(
            x.speed_bytes_per_sec().is_none(),
            "reported a speed measured over a few milliseconds"
        );

        // Backdate the start so elapsed is real, and it must appear.
        x.started_at = Instant::now() - Duration::from_secs(2);
        let speed = x.speed_bytes_per_sec().expect("no speed after 2s");
        assert!(
            (speed - 2048.0).abs() < 100.0,
            "4096 bytes in ~2s should be ~2048 B/s, got {speed}"
        );
    }

    #[test]
    fn eta_divides_the_remainder_by_the_measured_speed() {
        let mut x = t(TransferKind::Copy);
        x.apply(&TransferMsg::Total {
            id: 1,
            bytes_total: 10_000,
            files_total: 1,
        });
        x.apply(&TransferMsg::Progress {
            id: 1,
            bytes_done: 2_000,
            files_done: 1,
        });
        x.started_at = Instant::now() - Duration::from_secs(2);
        // 2000 bytes in 2s = 1000 B/s; 8000 left ⇒ ~8s.
        let eta = x.eta().expect("no eta");
        assert!(
            (eta.as_secs_f64() - 8.0).abs() < 1.0,
            "eta was {eta:?}, expected ~8s"
        );
    }

    /// A finished transfer has no ETA, and its average speed must FREEZE
    /// rather than decay towards zero while it sits in the list.
    #[test]
    fn a_finished_transfer_has_no_eta_and_a_frozen_speed() {
        let mut x = t(TransferKind::Copy);
        x.apply(&TransferMsg::Progress {
            id: 1,
            bytes_done: 1_000,
            files_done: 1,
        });
        x.started_at = Instant::now() - Duration::from_secs(1);
        x.apply(&TransferMsg::Done { id: 1, skipped: 0 });
        assert!(x.eta().is_none(), "a finished transfer advertised an ETA");

        let first = x.elapsed();
        std::thread::sleep(Duration::from_millis(30));
        assert_eq!(
            first,
            x.elapsed(),
            "elapsed kept climbing after the transfer finished"
        );
    }

    #[test]
    fn measure_counts_every_file_in_a_tree() {
        let d = tempfile::tempdir().unwrap();
        std::fs::write(d.path().join("a"), vec![0u8; 100]).unwrap();
        let sub = d.path().join("sub");
        std::fs::create_dir(&sub).unwrap();
        std::fs::write(sub.join("b"), vec![0u8; 250]).unwrap();
        std::fs::write(sub.join("c"), vec![0u8; 150]).unwrap();

        let (bytes, files) = measure(&[d.path().to_path_buf()], &AtomicBool::new(false));
        assert_eq!(files, 3, "missed files in a nested directory");
        assert_eq!(bytes, 500, "byte total wrong: {bytes}");
    }

    /// A symlink pointing at its own ancestor must not send the sizing
    /// pass into an infinite loop — the classic file-manager hang.
    #[test]
    #[cfg(unix)]
    fn measure_does_not_follow_symlinks_into_a_cycle() {
        let d = tempfile::tempdir().unwrap();
        let sub = d.path().join("sub");
        std::fs::create_dir(&sub).unwrap();
        std::fs::write(sub.join("a"), vec![0u8; 10]).unwrap();
        // sub/loop -> the directory that contains it.
        std::os::unix::fs::symlink(d.path(), sub.join("loop")).unwrap();

        let (_, files) = measure(&[d.path().to_path_buf()], &AtomicBool::new(false));
        // The link counts as one entry; the tree behind it is not walked.
        assert_eq!(files, 2, "followed a symlink cycle: {files} files");
    }

    #[test]
    fn measure_stops_when_cancelled() {
        let d = tempfile::tempdir().unwrap();
        for i in 0..50 {
            std::fs::write(d.path().join(format!("f{i}")), vec![0u8; 10]).unwrap();
        }
        let cancel = AtomicBool::new(true);
        let (_, files) = measure(&[d.path().to_path_buf()], &cancel);
        assert!(
            files < 50,
            "sizing ignored the cancel flag and walked everything"
        );
    }

    #[test]
    fn human_bytes_keeps_a_short_constant_ish_width() {
        assert_eq!(human_bytes(0), "0B");
        assert_eq!(human_bytes(999), "999B");
        assert_eq!(human_bytes(1024), "1.0K");
        assert_eq!(human_bytes(1536), "1.5K");
        assert_eq!(human_bytes(20 * 1024), "20K");
        assert_eq!(human_bytes(5 * 1024 * 1024), "5.0M");
        assert_eq!(human_bytes(3 * 1024 * 1024 * 1024), "3.0G");
        for n in [0u64, 1, 1023, 1024, 999_999, u64::MAX] {
            assert!(
                human_bytes(n).len() <= 5,
                "{n} rendered as {} — too wide for a constant-width chip",
                human_bytes(n)
            );
        }
    }

    /// "0s" on a live transfer reads as finished.
    #[test]
    fn human_eta_never_says_zero() {
        assert_eq!(human_eta(Duration::from_millis(10)), "1s");
        assert_eq!(human_eta(Duration::from_secs(12)), "12s");
        assert_eq!(human_eta(Duration::from_secs(64)), "1m 04s");
        assert_eq!(human_eta(Duration::from_secs(7500)), "2h 05m");
    }

    // ── the worker paths ───────────────────────────────────────────────
    //
    // Both data-loss bugs the review found lived here, in code with no
    // test at all. Everything below runs against tempdirs only.

    fn drain(rx: &std::sync::mpsc::Receiver<TransferMsg>) -> Vec<TransferMsg> {
        let mut v = Vec::new();
        while let Ok(m) = rx.try_recv() {
            v.push(m);
        }
        v
    }

    fn tree(root: &Path) {
        std::fs::create_dir_all(root.join("sub")).unwrap();
        std::fs::write(root.join("top.txt"), vec![0u8; 100]).unwrap();
        std::fs::write(root.join("sub/a.txt"), vec![0u8; 50]).unwrap();
        std::fs::write(root.join("sub/b.txt"), vec![0u8; 25]).unwrap();
    }

    #[test]
    fn a_copy_lands_every_file_and_reports_done() {
        let d = tempfile::tempdir().unwrap();
        let src = d.path().join("src");
        let dst = d.path().join("dst");
        tree(&src);
        std::fs::create_dir(&dst).unwrap();

        let (tx, rx) = std::sync::mpsc::channel();
        run(
            1,
            TransferKind::Copy,
            vec![(src.clone(), dst.join("src"))],
            Arc::new(AtomicBool::new(false)),
            tx,
        );

        assert!(dst.join("src/top.txt").is_file(), "top-level file missing");
        assert!(dst.join("src/sub/a.txt").is_file(), "nested file missing");
        assert!(src.join("top.txt").is_file(), "a COPY removed the source");

        let msgs = drain(&rx);
        assert!(
            matches!(msgs.last(), Some(TransferMsg::Done { skipped: 0, .. })),
            "did not finish cleanly: {msgs:?}"
        );
    }

    /// CRITICAL #1. `copy_one` used to return `Ok(())` when it stopped on
    /// cancel, which is indistinguishable from success — and the Move
    /// branch reads success as licence to delete the source. Cancelling a
    /// large cross-filesystem move therefore destroyed the original while
    /// the destination held only part of the tree.
    #[test]
    fn a_cancelled_copy_reports_stopped_not_completed() {
        let d = tempfile::tempdir().unwrap();
        let src = d.path().join("src");
        tree(&src);
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut rep = Reporter {
            id: 1,
            tx,
            bytes_done: 0,
            files_done: 0,
            last_sent: Instant::now(),
        };
        let mut created = Vec::new();
        let mut skipped = 0;

        let out = copy_one(
            &src,
            &d.path().join("dst"),
            &mut rep,
            &AtomicBool::new(true),
            &mut created,
            &mut skipped,
        )
        .unwrap();
        assert_eq!(
            out,
            CopyOutcome::Stopped,
            "a cancelled copy reported success — a Move would now delete the source"
        );

        // And the uncancelled call must still say Completed, or Move
        // could never remove a source at all.
        let mut created = Vec::new();
        let out = copy_one(
            &src,
            &d.path().join("dst2"),
            &mut rep,
            &AtomicBool::new(false),
            &mut created,
            &mut skipped,
        )
        .unwrap();
        assert_eq!(out, CopyOutcome::Completed);
    }

    /// CRITICAL #2. `created` used to record each intended target before
    /// writing it, without checking whether it was already there. So a
    /// cancel after a paste over an existing name deleted the user's
    /// original file, and a cancel during a merge into an existing folder
    /// called `remove_dir_all` on a tree this transfer had only added to.
    #[test]
    fn cleanup_never_removes_something_the_transfer_did_not_create() {
        let d = tempfile::tempdir().unwrap();
        let src = d.path().join("src");
        std::fs::create_dir(&src).unwrap();
        std::fs::write(src.join("new.txt"), b"new").unwrap();

        // The destination ALREADY exists and already holds a file.
        let dst = d.path().join("dst");
        std::fs::create_dir(&dst).unwrap();
        std::fs::write(dst.join("keep.txt"), b"precious").unwrap();

        let (tx, _rx) = std::sync::mpsc::channel();
        let mut rep = Reporter {
            id: 1,
            tx,
            bytes_done: 0,
            files_done: 0,
            last_sent: Instant::now(),
        };
        let mut created = Vec::new();
        let mut skipped = 0;
        copy_one(
            &src,
            &dst,
            &mut rep,
            &AtomicBool::new(false),
            &mut created,
            &mut skipped,
        )
        .unwrap();

        assert!(
            !created.contains(&dst),
            "recorded a directory that already existed: {created:?}"
        );
        assert!(
            created.contains(&dst.join("new.txt")),
            "did not record the file it actually created: {created:?}"
        );

        // Now the cancel path. It must take back only what it added.
        assert!(cleanup(&created), "cleanup reported debris");
        assert!(
            dst.join("keep.txt").is_file(),
            "cleanup deleted a file that predated the transfer"
        );
        assert!(dst.is_dir(), "cleanup deleted a pre-existing directory");
        assert!(
            !dst.join("new.txt").exists(),
            "cleanup left behind what the transfer created"
        );
    }

    /// The same rule for a plain file: pasting over an existing name and
    /// then cancelling must not leave the user with neither copy.
    #[test]
    fn cleanup_does_not_delete_a_file_it_overwrote() {
        let d = tempfile::tempdir().unwrap();
        let src = d.path().join("src");
        std::fs::create_dir(&src).unwrap();
        std::fs::write(src.join("f.txt"), b"incoming").unwrap();
        let dst = d.path().join("dst");
        std::fs::create_dir(&dst).unwrap();
        std::fs::write(dst.join("f.txt"), b"original").unwrap();

        let (tx, _rx) = std::sync::mpsc::channel();
        let mut rep = Reporter {
            id: 1,
            tx,
            bytes_done: 0,
            files_done: 0,
            last_sent: Instant::now(),
        };
        let mut created = Vec::new();
        let mut skipped = 0;
        copy_one(
            &src,
            &dst,
            &mut rep,
            &AtomicBool::new(false),
            &mut created,
            &mut skipped,
        )
        .unwrap();
        cleanup(&created);
        assert!(
            dst.join("f.txt").is_file(),
            "cancelling an overwrite destroyed both copies"
        );
    }

    /// Warning #3 — the rename fast path assigned the WHOLE transfer's
    /// total after moving just one source, so a two-source move showed
    /// 100% before the second had started, then counted past the total.
    #[test]
    fn a_multi_source_move_never_reports_more_than_the_total() {
        let d = tempfile::tempdir().unwrap();
        let a = d.path().join("a");
        let b = d.path().join("b");
        std::fs::create_dir(&a).unwrap();
        std::fs::create_dir(&b).unwrap();
        std::fs::write(a.join("f"), vec![0u8; 100]).unwrap();
        std::fs::write(b.join("f"), vec![0u8; 300]).unwrap();
        let dst = d.path().join("dst");
        std::fs::create_dir(&dst).unwrap();

        let (tx, rx) = std::sync::mpsc::channel();
        run(
            1,
            TransferKind::Move,
            vec![(a.clone(), dst.join("a")), (b.clone(), dst.join("b"))],
            Arc::new(AtomicBool::new(false)),
            tx,
        );

        let msgs = drain(&rx);
        let total = msgs
            .iter()
            .find_map(|m| match m {
                TransferMsg::Total { bytes_total, .. } => Some(*bytes_total),
                _ => None,
            })
            .expect("no Total");
        assert_eq!(total, 400);
        let progress: Vec<u64> = msgs
            .iter()
            .filter_map(|m| match m {
                TransferMsg::Progress { bytes_done, .. } => Some(*bytes_done),
                _ => None,
            })
            .collect();
        assert!(!progress.is_empty(), "no progress at all: {msgs:?}");
        assert_eq!(
            progress[0], 100,
            "after moving only the 100-byte source, progress read {} of \
             {total} — the fast path credited the whole transfer",
            progress[0]
        );
        for b in &progress {
            assert!(*b <= total, "progress ran past the total: {b} > {total}");
        }
        assert_eq!(
            progress.last().copied(),
            Some(total),
            "the finished move never reached its total: {progress:?}"
        );
        assert!(dst.join("a/f").is_file() && dst.join("b/f").is_file());
        assert!(!a.exists() && !b.exists(), "a move left its sources behind");
    }

    /// Warning #4 — only cancel used to clean up. A hard failure left a
    /// partial destination with no signal, which is the exact thing the
    /// module refuses to do on cancel.
    #[test]
    fn a_failure_cleans_up_and_says_so() {
        let d = tempfile::tempdir().unwrap();
        let src = d.path().join("src");
        tree(&src);

        let (tx, rx) = std::sync::mpsc::channel();
        // Copying a directory into itself is the self-copy guard's case,
        // and one keystroke away in a file browser.
        run(
            1,
            TransferKind::Copy,
            vec![(src.clone(), src.join("src"))],
            Arc::new(AtomicBool::new(false)),
            tx,
        );
        let msgs = drain(&rx);
        assert!(
            msgs.iter().any(|m| matches!(
                m,
                TransferMsg::Failed {
                    cleaned_up: true,
                    ..
                }
            )),
            "no Failed carrying a cleanup verdict: {msgs:?}"
        );
    }

    #[test]
    fn cancelling_is_visible_before_the_worker_acknowledges_it() {
        let x = t(TransferKind::Move);
        assert!(!x.is_cancelling());
        x.cancel();
        assert!(
            x.is_cancelling(),
            "a requested cancel was invisible until the worker replied"
        );
    }

    /// Once terminal, `is_cancelling` must go quiet — otherwise a
    /// cancelled transfer renders as "cancelling…" forever.
    #[test]
    fn a_cancelled_transfer_stops_reporting_as_cancelling() {
        let mut x = t(TransferKind::Move);
        x.cancel();
        x.apply(&TransferMsg::Cancelled {
            id: 1,
            cleaned_up: true,
        });
        assert!(!x.is_cancelling());
        assert!(x.state.is_terminal());
    }
}