file-engine 1.1.1

Async, cross-platform file operations engine for desktop apps and developer tools: copy, move, sync, watch, and compress files with progress reporting and cancellation.
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
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken;

use crate::profiler::Entry;
use crate::progress::{Progress, ProgressReporter};

use super::action::EntryAction;
use super::config::ErrorStrategy;
use super::outcome::{OperationOutcome, StopReason};
use super::plan::ExecutionPlan;

enum Unit {
    Batch(Vec<Entry>),
    Stream(Entry),
}

/// Executes an `ExecutionPlan` against a concurrency-limited worker pool,
/// applying `error_strategy` to per-entry failures and `Error::is_fatal()`
/// to decide when to stop regardless of strategy. Cancellation is checked
/// between batches/streams only, not inside an in-progress batch's entry
/// loop. See dev-docs/design/batching-engine.md, "dispatcher.rs" and
/// "Cooperative cancellation contract".
///
/// Rollback for `ErrorStrategy::Undo` calls `action.undo()` directly on
/// every entry in `succeeded`, in reverse order, rather than going
/// through `undo.rs`'s `UndoLog`/`UndoOp` — `EntryAction` already exposes
/// exactly the compensating action needed (see `action.rs`), so there's
/// nothing left for a separate op-log to add here. `undo.rs` remains for
/// `move_path.rs`'s deferred deletion sweep, which isn't an `EntryAction`
/// operation and needs a different compensating action (restore-then-
/// delete) that doesn't fit this trait.
pub(crate) async fn dispatch<A: EntryAction + 'static>(
    plan: ExecutionPlan,
    action: A,
    dest_root: &Path,
    error_strategy: ErrorStrategy,
    concurrency: usize,
    cancel: CancellationToken,
    reporter: ProgressReporter,
) -> OperationOutcome {
    let bytes_total: u64 = plan.batches.iter().map(|b| b.total_bytes).sum::<u64>()
        + plan.streams.iter().map(|s| s.entry.size).sum::<u64>();
    let entries_total: usize =
        plan.batches.iter().map(|b| b.entries.len()).sum::<usize>() + plan.streams.len();

    let mut units: Vec<Unit> = Vec::with_capacity(plan.batches.len() + plan.streams.len());
    units.extend(plan.batches.into_iter().map(|b| Unit::Batch(b.entries)));
    units.extend(plan.streams.into_iter().map(|s| Unit::Stream(s.entry)));

    reporter.send(Progress::Started { bytes_total: Some(bytes_total), entries_total });

    let action = Arc::new(action);
    let dest_root = Arc::new(dest_root.to_path_buf());
    let semaphore = Arc::new(Semaphore::new(concurrency.max(1)));
    let outcome = Arc::new(Mutex::new(OperationOutcome::default()));
    let stop = Arc::new(AtomicBool::new(false));

    let mut join_set: JoinSet<()> = JoinSet::new();
    let mut stopped_by_cancel = false;

    for unit in units {
        if stop.load(Ordering::SeqCst) {
            break;
        }

        let permit = tokio::select! {
            biased;
            _ = cancel.cancelled() => {
                stopped_by_cancel = true;
                break;
            }
            permit = Arc::clone(&semaphore).acquire_owned() => {
                permit.expect("semaphore closed")
            }
        };

        // Acquiring a permit can block until an in-flight unit finishes
        // and releases one — by which point that unit may have set
        // `stop`. Re-check rather than spawning on stale information.
        if stop.load(Ordering::SeqCst) {
            break;
        }

        let action = Arc::clone(&action);
        let dest_root = Arc::clone(&dest_root);
        let outcome = Arc::clone(&outcome);
        let stop = Arc::clone(&stop);
        let reporter = reporter.clone();

        join_set.spawn(async move {
            let _permit = permit;
            let entries = match unit {
                Unit::Batch(entries) => entries,
                Unit::Stream(entry) => vec![entry],
            };

            for entry in entries {
                reporter.send(Progress::EntryStarted { entry: entry.clone() });

                match action.execute(&entry, &dest_root).await {
                    Ok(()) => {
                        reporter.send(Progress::EntryCompleted { entry: entry.clone() });
                        outcome.lock().unwrap().succeeded.push(entry);
                    }
                    Err(err) => {
                        reporter.send(Progress::EntryFailed { entry: entry.clone() });
                        let fatal = err.is_fatal();
                        let reason = if fatal {
                            Some(StopReason::Fatal)
                        } else {
                            match error_strategy {
                                ErrorStrategy::ContinueAndCollect => None,
                                ErrorStrategy::AbortOnError => Some(StopReason::AbortOnError),
                                ErrorStrategy::Undo => Some(StopReason::Undo),
                            }
                        };

                        {
                            let mut out = outcome.lock().unwrap();
                            out.failed.push((entry, err));
                            if let Some(reason) = reason {
                                if out.stopped_early.is_none() {
                                    out.stopped_early = Some(reason);
                                }
                            }
                        }

                        // Stop this unit's remaining entries too, not
                        // just future units — a triggering failure means
                        // "stop", not "stop everything except what I was
                        // already doing."
                        if reason.is_some() {
                            stop.store(true, Ordering::SeqCst);
                            break;
                        }
                    }
                }
            }
        });
    }

    while let Some(result) = join_set.join_next().await {
        let _ = result;
    }

    let mut outcome = Arc::try_unwrap(outcome)
        .unwrap_or_else(|_| panic!("dispatch: outcome has outstanding references after join"))
        .into_inner()
        .unwrap();

    if stopped_by_cancel && outcome.stopped_early.is_none() {
        outcome.stopped_early = Some(StopReason::Cancelled);
    }

    if matches!(error_strategy, ErrorStrategy::Undo) && outcome.stopped_early.is_some() {
        for entry in outcome.succeeded.iter().rev() {
            let _ = action.undo(entry, &dest_root).await;
        }
        outcome.succeeded.clear();
    }

    outcome
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::future::Future;
    use std::path::PathBuf;
    use std::pin::Pin;
    use std::sync::atomic::AtomicUsize;
    use std::time::Duration;

    use tempfile::tempdir;

    use crate::error::{Error, Result};

    use super::super::batch::Batch;
    use super::*;

    fn entry(name: &str) -> Entry {
        Entry {
            path: PathBuf::from(name),
            relative_path: PathBuf::from(name),
            size: 1,
            modified: None,
        }
    }

    /// One single-entry batch per input entry, so dispatch order and
    /// per-unit granularity are deterministic and easy to reason about
    /// in tests, independent of `batch.rs`'s own packing logic.
    fn single_entry_plan(entries: &[Entry]) -> ExecutionPlan {
        ExecutionPlan {
            batches: entries
                .iter()
                .cloned()
                .map(|e| Batch { entries: vec![e], total_bytes: 1 })
                .collect(),
            streams: Vec::new(),
        }
    }

    struct FakeAction {
        log: Arc<Mutex<Vec<String>>>,
        active: Arc<AtomicUsize>,
        max_active: Arc<AtomicUsize>,
        delay_for: Arc<HashMap<PathBuf, Duration>>,
        /// `Some(true)` = fails fatally, `Some(false)` = fails per-entry,
        /// absent = succeeds.
        fail: Arc<HashMap<PathBuf, bool>>,
    }

    impl EntryAction for FakeAction {
        fn execute<'a>(
            &'a self,
            entry: &'a Entry,
            _dest_root: &'a Path,
        ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
            Box::pin(async move {
                let current = self.active.fetch_add(1, Ordering::SeqCst) + 1;
                self.max_active.fetch_max(current, Ordering::SeqCst);
                self.log.lock().unwrap().push(format!("start:{}", entry.path.display()));

                if let Some(d) = self.delay_for.get(&entry.path) {
                    tokio::time::sleep(*d).await;
                }

                self.active.fetch_sub(1, Ordering::SeqCst);
                self.log.lock().unwrap().push(format!("end:{}", entry.path.display()));

                match self.fail.get(&entry.path) {
                    Some(true) => Err(Error::NoSpace { needed: 0, available: 0 }),
                    Some(false) => Err(Error::SourceNotFound { path: entry.path.clone() }),
                    None => Ok(()),
                }
            })
        }

        fn undo<'a>(
            &'a self,
            entry: &'a Entry,
            _dest_root: &'a Path,
        ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
            Box::pin(async move {
                self.log.lock().unwrap().push(format!("undo:{}", entry.path.display()));
                Ok(())
            })
        }
    }

    fn fake_action(
        delay_for: HashMap<PathBuf, Duration>,
        fail: HashMap<PathBuf, bool>,
    ) -> (FakeAction, Arc<Mutex<Vec<String>>>, Arc<AtomicUsize>) {
        let log = Arc::new(Mutex::new(Vec::new()));
        let active = Arc::new(AtomicUsize::new(0));
        let max_active = Arc::new(AtomicUsize::new(0));
        let action = FakeAction {
            log: Arc::clone(&log),
            active: Arc::clone(&active),
            max_active: Arc::clone(&max_active),
            delay_for: Arc::new(delay_for),
            fail: Arc::new(fail),
        };
        (action, log, max_active)
    }

    #[tokio::test]
    async fn no_more_than_concurrency_limit_active_at_once() {
        let entries: Vec<Entry> = (0..6).map(|i| entry(&format!("e{i}"))).collect();
        let plan = single_entry_plan(&entries);

        let delay_for = entries.iter().map(|e| (e.path.clone(), Duration::from_millis(30))).collect();
        let (action, _log, max_active) = fake_action(delay_for, HashMap::new());

        let dest_root = tempdir().unwrap();
        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::ContinueAndCollect,
            2,
            CancellationToken::new(),
        ProgressReporter::noop(),
        )
        .await;

        assert_eq!(outcome.succeeded.len(), 6);
        let observed_max = max_active.load(Ordering::SeqCst);
        assert!(observed_max <= 2, "observed {observed_max} concurrent workers, expected <= 2");
        assert!(observed_max >= 2, "expected overlap to actually happen under the delay, observed {observed_max}");
    }

    #[tokio::test]
    async fn every_entry_processed_exactly_once_when_no_failures() {
        let entries: Vec<Entry> = (0..5).map(|i| entry(&format!("e{i}"))).collect();
        let plan = single_entry_plan(&entries);
        let (action, _log, _max_active) = fake_action(HashMap::new(), HashMap::new());

        let dest_root = tempdir().unwrap();
        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::ContinueAndCollect,
            3,
            CancellationToken::new(),
        ProgressReporter::noop(),
        )
        .await;

        let mut succeeded_paths: Vec<_> = outcome.succeeded.iter().map(|e| e.path.clone()).collect();
        succeeded_paths.sort();
        let mut expected: Vec<_> = entries.iter().map(|e| e.path.clone()).collect();
        expected.sort();

        assert_eq!(succeeded_paths, expected);
        assert!(outcome.failed.is_empty());
        assert!(outcome.cleanup_failed.is_empty());
        assert_eq!(outcome.stopped_early, None);
    }

    #[tokio::test]
    async fn continue_and_collect_does_not_stop_other_units() {
        let entries: Vec<Entry> = (0..5).map(|i| entry(&format!("e{i}"))).collect();
        let plan = single_entry_plan(&entries);

        let mut fail = HashMap::new();
        fail.insert(entries[2].path.clone(), false);
        let (action, _log, _max_active) = fake_action(HashMap::new(), fail);

        let dest_root = tempdir().unwrap();
        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::ContinueAndCollect,
            3,
            CancellationToken::new(),
        ProgressReporter::noop(),
        )
        .await;

        assert_eq!(outcome.succeeded.len(), 4);
        assert_eq!(outcome.failed.len(), 1);
        assert_eq!(outcome.failed[0].0.path, entries[2].path);
        assert_eq!(outcome.stopped_early, None);
    }

    #[tokio::test]
    async fn abort_on_error_stops_queued_units_but_lets_in_flight_finish() {
        let a = entry("a");
        let b = entry("b");
        let c = entry("c");
        let plan = single_entry_plan(&[a.clone(), b.clone(), c.clone()]);

        let mut delay_for = HashMap::new();
        delay_for.insert(b.path.clone(), Duration::from_millis(60));
        let mut fail = HashMap::new();
        fail.insert(a.path.clone(), false);
        let (action, log, _max_active) = fake_action(delay_for, fail);

        let dest_root = tempdir().unwrap();
        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::AbortOnError,
            2,
            CancellationToken::new(),
        ProgressReporter::noop(),
        )
        .await;

        assert_eq!(outcome.failed.len(), 1);
        assert_eq!(outcome.failed[0].0.path, a.path);
        assert_eq!(outcome.succeeded.len(), 1);
        assert_eq!(outcome.succeeded[0].path, b.path);
        assert_eq!(outcome.stopped_early, Some(StopReason::AbortOnError));

        let log = log.lock().unwrap();
        assert!(!log.iter().any(|l| l.contains('c')));
    }

    #[tokio::test]
    async fn undo_strategy_rolls_back_completed_entries_in_reverse_order() {
        let a = entry("a");
        let b = entry("b");
        let c = entry("c");
        let plan = single_entry_plan(&[a.clone(), b.clone(), c.clone()]);

        let mut fail = HashMap::new();
        fail.insert(c.path.clone(), false);
        let (action, log, _max_active) = fake_action(HashMap::new(), fail);

        let dest_root = tempdir().unwrap();
        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::Undo,
            1,
            CancellationToken::new(),
        ProgressReporter::noop(),
        )
        .await;

        assert!(outcome.succeeded.is_empty(), "succeeded should be cleared after rollback");
        assert_eq!(outcome.failed.len(), 1);
        assert_eq!(outcome.failed[0].0.path, c.path);
        assert_eq!(outcome.stopped_early, Some(StopReason::Undo));

        let log = log.lock().unwrap();
        let undo_b = log.iter().position(|l| l == "undo:b").expect("b should have been undone");
        let undo_a = log.iter().position(|l| l == "undo:a").expect("a should have been undone");
        assert!(undo_b < undo_a, "undo should replay in reverse completion order");
    }

    #[tokio::test]
    async fn fatal_error_stops_dispatch_even_under_continue_and_collect() {
        let a = entry("a");
        let b = entry("b");
        let plan = single_entry_plan(&[a.clone(), b.clone()]);

        let mut fail = HashMap::new();
        fail.insert(a.path.clone(), true);
        let (action, _log, _max_active) = fake_action(HashMap::new(), fail);

        let dest_root = tempdir().unwrap();
        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::ContinueAndCollect,
            1,
            CancellationToken::new(),
        ProgressReporter::noop(),
        )
        .await;

        assert_eq!(outcome.failed.len(), 1);
        assert_eq!(outcome.failed[0].0.path, a.path);
        assert!(outcome.succeeded.is_empty());
        assert_eq!(outcome.stopped_early, Some(StopReason::Fatal));
    }

    #[tokio::test]
    async fn cancellation_lets_in_flight_finish_but_skips_queued() {
        let a = entry("a");
        let b = entry("b");
        let c = entry("c");
        let plan = single_entry_plan(&[a.clone(), b.clone(), c.clone()]);

        let mut delay_for = HashMap::new();
        delay_for.insert(a.path.clone(), Duration::from_millis(80));
        let (action, log, _max_active) = fake_action(delay_for, HashMap::new());

        let dest_root = tempdir().unwrap();
        let cancel = CancellationToken::new();
        let trigger = cancel.clone();

        let (outcome, ()) = tokio::join!(
            dispatch(plan, action, dest_root.path(), ErrorStrategy::ContinueAndCollect, 1, cancel, ProgressReporter::noop()),
            async {
                tokio::time::sleep(Duration::from_millis(20)).await;
                trigger.cancel();
            }
        );

        assert_eq!(outcome.succeeded.len(), 1);
        assert_eq!(outcome.succeeded[0].path, a.path);
        assert!(outcome.failed.is_empty());
        assert_eq!(outcome.stopped_early, Some(StopReason::Cancelled));

        let log = log.lock().unwrap();
        assert!(!log.iter().any(|l| l.contains('b') || l.contains('c')));
    }

    #[tokio::test]
    async fn cancellation_combined_with_undo_rolls_back_what_completed_before_it() {
        let a = entry("a");
        let b = entry("b");
        let plan = single_entry_plan(&[a.clone(), b.clone()]);

        let mut delay_for = HashMap::new();
        delay_for.insert(a.path.clone(), Duration::from_millis(80));
        let (action, log, _max_active) = fake_action(delay_for, HashMap::new());

        let dest_root = tempdir().unwrap();
        let cancel = CancellationToken::new();
        let trigger = cancel.clone();

        let (outcome, ()) = tokio::join!(
            dispatch(plan, action, dest_root.path(), ErrorStrategy::Undo, 1, cancel, ProgressReporter::noop()),
            async {
                tokio::time::sleep(Duration::from_millis(20)).await;
                trigger.cancel();
            }
        );

        assert!(outcome.succeeded.is_empty());
        assert_eq!(outcome.stopped_early, Some(StopReason::Cancelled));

        let log = log.lock().unwrap();
        assert!(log.contains(&"undo:a".to_string()));
        assert!(!log.iter().any(|l| l.contains('b')));
    }

    #[tokio::test]
    async fn cancel_after_completion_does_not_change_the_outcome() {
        let entries: Vec<Entry> = (0..3).map(|i| entry(&format!("e{i}"))).collect();
        let plan = single_entry_plan(&entries);
        let (action, _log, _max_active) = fake_action(HashMap::new(), HashMap::new());

        let dest_root = tempdir().unwrap();
        let cancel = CancellationToken::new();

        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::ContinueAndCollect,
            2,
            cancel.clone(),
        ProgressReporter::noop(),
        )
        .await;

        // dispatch() has already returned; cancelling now cannot retroactively
        // change a value it already produced.
        cancel.cancel();

        assert_eq!(outcome.succeeded.len(), 3);
        assert_eq!(outcome.stopped_early, None);
    }

    #[tokio::test]
    async fn empty_plan_completes_immediately_with_empty_outcome() {
        let plan = ExecutionPlan::default();
        let (action, _log, _max_active) = fake_action(HashMap::new(), HashMap::new());

        let dest_root = tempdir().unwrap();
        let outcome = dispatch(
            plan,
            action,
            dest_root.path(),
            ErrorStrategy::ContinueAndCollect,
            2,
            CancellationToken::new(),
        ProgressReporter::noop(),
        )
        .await;

        assert!(outcome.succeeded.is_empty());
        assert!(outcome.failed.is_empty());
        assert_eq!(outcome.stopped_early, None);
    }
}