passalong-core 0.2.1

Core library for passalong: configuration, item model, storage traits, and telemetry
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
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
//! Re-encrypting every item: migrating a plaintext store, and rotating an
//! encrypted store's data key. One journalled engine does both:
//!
//! 1. `.rewrite/` appears, with `plan.json` saying what runs, in one rename
//!    of a whole journal; it is the lock (see the `journal` module).
//! 2. The new header comes with the journal, in `.rewrite/header/`; a
//!    rotation also keeps the current one in `.rewrite/old-header/`.
//! 3. The source moves into `.rewrite/source/` in one rename: `items/` for
//!    a migration, which then writes the stop file, or `v2/items/` for a
//!    rotation.
//! 4. Each source item, oldest first, is sealed under the new key into
//!    `v2/items/`, keeping its metadata and creation time. Its new id
//!    follows from its SHA-256, so an item already there is skipped and a
//!    resumed run repeats nothing.
//! 5. Every new item is read back and its SHA-256 and size compared.
//! 6. The current header moves into `.rewrite/previous/` and the new one
//!    takes its place; then `.rewrite/source/`, and last `.rewrite/`
//!    itself, are removed.
//!
//! [`finish`] resumes an interrupted run; [`undo`] puts the source back as
//! long as the new header is not yet in place. Each claims
//! `.rewrite/recovery/` while it runs, so two recoveries never run at once.

use std::sync::Arc;

use serde::{Deserialize, Serialize};
use tokio::io::AsyncReadExt;

use super::admin::{plain_tmp, refuse};
use super::header::{encryption_dir, read_header_in, write_header_in};
use super::header_change::{finish_change, install, revert};
use super::journal::{
    Journal, NEW_HEADER, OLD_HEADER, PLAN_FILE, PREVIOUS, claim_recovery, publish, read_journal,
    release_lock, release_unstarted, unclaim, under_rewrite, unsupported,
};
use super::{
    EncryptionError, HEADER_FILE, STOP_FILE, StoreHeader, StoreState, inspect, read_header,
    write_stop_file,
};
use crate::clock::Clock;
use crate::crypto::{DataKey, KdfParams, Sealer, Words, unwrap, wrap};
use crate::fs::{FsError, RemoteFs, RemotePath, SubFs};
use crate::model::ContentHasher;
use crate::random::StdRandom;
use crate::store::{FsStore, Store, StoreError};

const SOURCE: &str = "source";

/// What a re-encryption does.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RewriteKind {
    /// A plaintext store's items are encrypted.
    Migrate,
    /// An encrypted store's items move to a new data key.
    Rotate,
}

/// `.rewrite/plan.json`: what an interrupted re-encryption was doing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RewritePlan {
    /// Migration or rotation.
    pub kind: RewriteKind,
    /// When it started, RFC 3339.
    pub started_at: String,
    /// The old key's id, for a rotation.
    pub from_key: Option<String>,
    /// The new key's id.
    pub to_key: String,
    /// How many items the source held.
    pub items: usize,
}

/// Where the source's items sit inside `.rewrite/source/`, and where they
/// came from.
fn source_items(kind: RewriteKind) -> &'static str {
    match kind {
        RewriteKind::Migrate => STOP_FILE,
        RewriteKind::Rotate => "v2/items",
    }
}

/// Reads the plan of the re-encryption in progress, if any.
///
/// # Errors
///
/// The filesystem's error, and [`EncryptionError::Layout`] for a journal
/// that cannot be read or records another change; [`read_journal`] tells
/// every journal apart.
pub async fn read_plan<F: RemoteFs + ?Sized>(fs: &F) -> Result<Option<RewritePlan>, StoreError> {
    match read_journal(fs).await? {
        None => Ok(None),
        Some(Journal::Rewrite(plan)) => Ok(Some(plan)),
        Some(Journal::Unreadable { reason }) => {
            Err(EncryptionError::Layout(format!("{PLAN_FILE}: {reason}")).into())
        }
        Some(other) => Err(unsupported(&other)),
    }
}

/// The plan a re-encryption's `journal` records, for resuming it.
fn rewrite_plan(journal: Option<Journal>) -> Result<RewritePlan, StoreError> {
    match journal {
        Some(Journal::Rewrite(plan)) => Ok(plan),
        None | Some(Journal::Unreadable { .. }) => Err(EncryptionError::Layout(
            "the re-encryption stopped before its plan was saved; undo it instead".to_owned(),
        )
        .into()),
        Some(other) => Err(unsupported(&other)),
    }
}

fn started_at(clock: &dyn Clock) -> String {
    clock
        .now()
        .to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}

/// Encrypts every item of a plaintext store under a new data key wrapped
/// with `words`, and returns the key.
///
/// # Errors
///
/// The refusal for a store that is not plaintext, and any failure on the
/// way, after which [`finish`] or [`undo`] recovers.
pub async fn migrate<F: RemoteFs + ?Sized>(
    fs: &F,
    words: &Words,
    kdf: KdfParams,
    clock: Arc<dyn Clock>,
) -> Result<DataKey, StoreError> {
    let items = match inspect(fs).await? {
        StoreState::Plain { items } => items,
        other => return Err(refuse(other)),
    };
    let key = DataKey::generate()?;
    let header = StoreHeader::new(wrap(&key, words, kdf)?);
    let plan = RewritePlan {
        kind: RewriteKind::Migrate,
        started_at: started_at(clock.as_ref()),
        from_key: None,
        to_key: key.key_id().to_string(),
        items,
    };
    publish(fs, &plan, &[(NEW_HEADER, &header)]).await?;
    run(fs, RewriteKind::Migrate, &key, None, clock).await?;
    Ok(key)
}

/// Moves every item of an encrypted store from `old` to a new data key
/// wrapped with `words`, and returns the new key. Devices holding the old
/// key are refused afterwards until they join again.
///
/// # Errors
///
/// [`EncryptionError::KeyMismatch`] when `old` is not the store's key, the
/// refusal for a store that is not encrypted, and any failure on the way,
/// after which [`finish`] or [`undo`] recovers.
pub async fn rotate<F: RemoteFs + ?Sized>(
    fs: &F,
    old: &DataKey,
    words: &Words,
    kdf: KdfParams,
    clock: Arc<dyn Clock>,
) -> Result<DataKey, StoreError> {
    match inspect(fs).await? {
        StoreState::Encrypted { key_id, .. } if key_id == old.key_id() => {}
        StoreState::Encrypted { key_id, .. } => {
            return Err(EncryptionError::KeyMismatch {
                device: old.key_id().short(),
                store: key_id.short(),
            }
            .into());
        }
        other => return Err(refuse(other)),
    }
    let current = read_header(fs)
        .await?
        .ok_or(EncryptionError::HeaderMissing)?;
    let old_store = FsStore::sealed(
        fs,
        clock.clone(),
        Box::new(StdRandom::new()),
        Sealer::new(old.clone()),
    );
    let items = old_store.list_ids().await?.len();
    let key = DataKey::generate()?;
    let header = StoreHeader::new(wrap(&key, words, kdf)?);
    let plan = RewritePlan {
        kind: RewriteKind::Rotate,
        started_at: started_at(clock.as_ref()),
        from_key: Some(old.key_id().to_string()),
        to_key: key.key_id().to_string(),
        items,
    };
    publish(fs, &plan, &[(NEW_HEADER, &header), (OLD_HEADER, &current)]).await?;
    run(fs, RewriteKind::Rotate, &key, Some(old), clock).await?;
    Ok(key)
}

/// Resumes an interrupted re-encryption. `new_words` unlock the new key;
/// a rotation whose items are not all moved yet also needs the old key,
/// from `old_key` or unlocked with `old_words`. Returns the new key.
///
/// # Errors
///
/// [`EncryptionError::Layout`] when nothing is to be recovered,
/// [`EncryptionError::Recovering`] while another recovery runs, wrong
/// words, and any failure on the way.
pub async fn finish<F: RemoteFs + ?Sized>(
    fs: &F,
    new_words: &Words,
    old_key: Option<&DataKey>,
    old_words: Option<&Words>,
    clock: Arc<dyn Clock>,
) -> Result<DataKey, StoreError> {
    claim_recovery(fs).await?;
    let result = finish_claimed(fs, new_words, old_key, old_words, clock).await;
    unclaim(fs, result).await
}

async fn finish_claimed<F: RemoteFs + ?Sized>(
    fs: &F,
    new_words: &Words,
    old_key: Option<&DataKey>,
    old_words: Option<&Words>,
    clock: Arc<dyn Clock>,
) -> Result<DataKey, StoreError> {
    let plan = match read_journal(fs).await? {
        Some(Journal::HeaderChange(change)) => {
            return finish_change(fs, &change, new_words).await;
        }
        journal => rewrite_plan(journal)?,
    };
    let pending = read_header_in(fs, &under_rewrite(NEW_HEADER)?).await?;
    let current = read_header(fs).await?;
    let (header, swapped) = match (pending, current) {
        (Some(header), _) => (header, false),
        (None, Some(current)) if current.key_id().to_string() == plan.to_key => (current, true),
        _ => {
            return Err(EncryptionError::Layout(
                "the re-encryption stopped before its new key was saved; undo it instead"
                    .to_owned(),
            )
            .into());
        }
    };
    let key = unwrap(header.wrapped(), new_words)?;
    if swapped {
        cleanup(fs).await?;
        return Ok(key);
    }
    let old = match plan.kind {
        RewriteKind::Migrate => None,
        RewriteKind::Rotate => Some(old_key_for(fs, old_key, old_words).await?),
    };
    run(fs, plan.kind, &key, old.as_ref(), clock).await?;
    Ok(key)
}

/// The old key of a rotation: `old_key` when it is the one recorded, or
/// the one `old_words` unlock.
async fn old_key_for<F: RemoteFs + ?Sized>(
    fs: &F,
    old_key: Option<&DataKey>,
    old_words: Option<&Words>,
) -> Result<DataKey, StoreError> {
    let old_header = read_header_in(fs, &under_rewrite(OLD_HEADER)?)
        .await?
        .ok_or(EncryptionError::HeaderMissing)?;
    if let Some(key) = old_key.filter(|key| key.key_id() == old_header.key_id()) {
        return Ok(key.clone());
    }
    match old_words {
        Some(words) => Ok(unwrap(old_header.wrapped(), words)?),
        None => Err(EncryptionError::KeyMismatch {
            device: old_key.map(|key| key.key_id().short()).unwrap_or_default(),
            store: old_header.key_id().short(),
        }
        .into()),
    }
}

/// Puts an interrupted re-encryption's source back and releases the lock.
///
/// # Errors
///
/// [`EncryptionError::Layout`] when nothing is to be recovered, when the
/// new header is already in place and only [`finish`] can complete it, and
/// when a lock whose journal is missing or unreadable holds anything else;
/// [`EncryptionError::Recovering`] while another recovery runs.
pub async fn undo<F: RemoteFs + ?Sized>(fs: &F) -> Result<(), StoreError> {
    claim_recovery(fs).await?;
    let result = undo_claimed(fs).await;
    unclaim(fs, result).await
}

async fn undo_claimed<F: RemoteFs + ?Sized>(fs: &F) -> Result<(), StoreError> {
    let plan = match read_journal(fs).await? {
        Some(Journal::Rewrite(plan)) => plan,
        Some(Journal::HeaderChange(change)) => return revert(fs, &change).await,
        // Locked, but stopped before its plan was whole.
        None | Some(Journal::Unreadable { .. }) => return release_unstarted(fs).await,
        Some(other) => return Err(unsupported(&other)),
    };
    let pending = under_rewrite(&format!("{NEW_HEADER}/{HEADER_FILE}"))?;
    let swapped = fs.stat(&pending).await?.is_none()
        && read_header(fs)
            .await?
            .is_some_and(|header| header.key_id().to_string() == plan.to_key);
    if swapped {
        return Err(EncryptionError::Layout(
            "the new key is already in place; finish the re-encryption instead".to_owned(),
        )
        .into());
    }
    let moved = under_rewrite(&format!("{SOURCE}/{}", source_items(plan.kind)))?;
    match plan.kind {
        RewriteKind::Migrate => {
            fs.remove_dir_all(&RemotePath::new("v2")?).await?;
            let items = RemotePath::new(STOP_FILE)?;
            if fs.stat(&items).await?.is_some_and(|meta| !meta.is_dir) {
                fs.remove_file(&items).await?;
            }
            if fs.stat(&moved).await?.is_some() {
                fold(fs, &moved, &items).await?;
            }
        }
        RewriteKind::Rotate => {
            let items = RemotePath::new("v2/items")?;
            if fs.stat(&moved).await?.is_some() {
                fs.remove_dir_all(&items).await?;
                fs.rename(&moved, &items).await?;
            }
            // A swap cut short leaves no header: the old one comes back.
            let current = encryption_dir()?;
            if fs.stat(&current).await?.is_none() {
                let previous = under_rewrite(PREVIOUS)?;
                if fs.stat(&previous).await?.is_some() {
                    fs.rename(&previous, &current).await?;
                } else {
                    let old = read_header_in(fs, &under_rewrite(OLD_HEADER)?)
                        .await?
                        .ok_or(EncryptionError::HeaderMissing)?;
                    write_header_in(fs, &current, &old).await?;
                }
            }
        }
    }
    release_lock(fs).await
}

/// Moves the folder `from` to `to`: in one rename when `to` does not exist,
/// entry by entry otherwise, dropping entries `to` already has.
pub(super) async fn fold<F: RemoteFs + ?Sized>(
    fs: &F,
    from: &RemotePath,
    to: &RemotePath,
) -> Result<(), StoreError> {
    if fs.stat(to).await?.is_none() {
        if let Some(parent) = to.parent() {
            fs.create_dir_all(&parent).await?;
        }
        fs.rename(from, to).await?;
        return Ok(());
    }
    for entry in fs.read_dir(from).await? {
        let source = from.join(&entry.name)?;
        match fs.rename(&source, &to.join(&entry.name)?).await {
            Ok(()) => {}
            Err(FsError::AlreadyExists(_)) => fs.remove_dir_all(&source).await?,
            Err(err) => return Err(err.into()),
        }
    }
    fs.remove_dir_all(from).await?;
    Ok(())
}

/// Steps 3 to 6, from wherever an earlier run stopped.
async fn run<F: RemoteFs + ?Sized>(
    fs: &F,
    kind: RewriteKind,
    key: &DataKey,
    old: Option<&DataKey>,
    clock: Arc<dyn Clock>,
) -> Result<(), StoreError> {
    let moved = under_rewrite(&format!("{SOURCE}/{}", source_items(kind)))?;
    let original = RemotePath::new(source_items(kind))?;
    if fs.stat(&moved).await?.is_none() {
        fs.create_dir_all(&moved.parent().expect("under .rewrite"))
            .await?;
        if fs.stat(&original).await?.is_some_and(|meta| meta.is_dir) {
            fs.rename(&original, &moved).await?;
        } else {
            fs.create_dir_all(&moved).await?;
        }
    }
    if kind == RewriteKind::Migrate
        && let Err(err) = write_stop_file(fs).await
    {
        // A client before v0.2.0 recreated `items/`: its items join the
        // source, once.
        if !matches!(err, StoreError::Encryption(EncryptionError::Layout(_))) {
            return Err(err);
        }
        fold(fs, &original, &moved).await?;
        write_stop_file(fs).await?;
    }
    if kind == RewriteKind::Migrate {
        // With the stop file in place, nothing staged in the plaintext
        // `tmp/` can be published any more.
        fs.remove_dir_all(&plain_tmp()).await?;
    }

    let source_fs = SubFs::new(fs, under_rewrite(SOURCE)?);
    let rng = || Box::new(StdRandom::new());
    let source = match old {
        Some(old) => FsStore::sealed(source_fs, clock.clone(), rng(), Sealer::new(old.clone())),
        None => FsStore::new(source_fs, clock.clone(), rng()),
    };
    let target = FsStore::sealed(fs, clock, rng(), Sealer::new(key.clone()));
    let mut ids = source.list_ids().await?;
    ids.reverse();
    let mut metas = Vec::with_capacity(ids.len());
    for id in &ids {
        let meta = source.get_meta(id).await?;
        if !target.exists(&target.id_for(&meta)?).await? {
            let (meta, content) = source.get(id).await?;
            target.import(&meta, content).await?;
        }
        metas.push(meta);
    }
    for meta in &metas {
        verify(&target, meta).await?;
    }
    tracing::info!(items = metas.len(), key = %key.key_id().short(), "items re-encrypted and verified");
    install(fs).await?;
    cleanup(fs).await
}

/// Reads back the new copy of `meta`'s item and compares its SHA-256 and
/// size.
async fn verify<F: RemoteFs>(
    target: &FsStore<F>,
    meta: &crate::model::ItemMeta,
) -> Result<(), StoreError> {
    let id = target.id_for(meta)?;
    let (_, mut content) = target.get(&id).await?;
    let mut hasher = ContentHasher::new();
    let mut buf = vec![0_u8; 64 * 1024];
    loop {
        let n = content
            .read(&mut buf)
            .await
            .map_err(|err| StoreError::Content(err.to_string()))?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }
    let digest = hasher.finalize();
    if digest.sha256_hex() != meta.sha256 || digest.size() != meta.size {
        return Err(StoreError::Corrupt {
            id: id.to_string(),
            reason: format!("its re-encrypted copy does not match {}", meta.id),
        });
    }
    Ok(())
}

/// Removes the source, journals staged by locks never taken, and then the
/// lock.
async fn cleanup<F: RemoteFs + ?Sized>(fs: &F) -> Result<(), StoreError> {
    fs.remove_dir_all(&under_rewrite(SOURCE)?).await?;
    release_lock(fs).await
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::crypto::KDF_SALT_LEN;
    use crate::encryption::{open_with_key, set_up};
    use crate::fs::LocalFs;
    use crate::model::{ItemMeta, NewItem};
    use crate::testing::{FaultyFs, FsOp, ManualClock};
    use std::collections::BTreeMap;
    use std::path::Path;
    use tempfile::TempDir;

    pub(crate) const W1: &str = "abacus abdomen abdominal abide abiding ability";
    pub(crate) const W2: &str = "zoom zoom zoom zoom zoom zoom";
    pub(crate) const OPS: [FsOp; 9] = [
        FsOp::CreateDirAll,
        FsOp::ReadDir,
        FsOp::OpenRead,
        FsOp::OpenWrite,
        FsOp::Rename,
        FsOp::RemoveDirAll,
        FsOp::Stat,
        FsOp::CreateDir,
        FsOp::RemoveFile,
    ];

    pub(crate) fn words(text: &str) -> Words {
        Words::parse(text).unwrap()
    }

    pub(crate) fn quick() -> KdfParams {
        KdfParams {
            m_kib: 64,
            t: 1,
            p: 1,
            salt: [7; KDF_SALT_LEN],
        }
    }

    pub(crate) fn clock() -> Arc<ManualClock> {
        Arc::new(ManualClock::at("2026-09-15T08:00:00Z"))
    }

    fn text(bytes: &[u8]) -> crate::fs::BoxRead {
        Box::new(std::io::Cursor::new(bytes.to_vec()))
    }

    /// A plaintext store with a text and a file, a second apart.
    pub(crate) async fn plain_items(dir: &Path) -> Vec<ItemMeta> {
        let clock = clock();
        let store = FsStore::new(LocalFs::new(dir), clock.clone(), Box::new(StdRandom::new()));
        let first = store
            .put(NewItem::text("box"), text(b"first item"))
            .await
            .unwrap()
            .meta;
        clock.advance(1);
        let second = store
            .put(NewItem::file("b.txt", "lap"), text(b"second item"))
            .await
            .unwrap()
            .meta;
        vec![first, second]
    }

    async fn open(dir: &Path, key: &DataKey) -> Result<Box<dyn Store>, StoreError> {
        open_with_key(
            LocalFs::new(dir),
            Some(key.clone()),
            clock(),
            Box::new(StdRandom::new()),
        )
        .await
    }

    /// An encrypted store with two items, and its key.
    pub(crate) async fn sealed_items(dir: &Path) -> (DataKey, Vec<ItemMeta>) {
        let key = set_up(&LocalFs::new(dir), &words(W1), quick())
            .await
            .unwrap();
        let store = open(dir, &key).await.unwrap();
        let mut metas = Vec::new();
        for body in ["first sealed", "second sealed"] {
            metas.push(
                store
                    .put(NewItem::text("box"), text(body.as_bytes()))
                    .await
                    .unwrap()
                    .meta,
            );
        }
        (key, metas)
    }

    /// Asserts the store under `key` holds `originals`, with their times,
    /// metadata, and contents.
    pub(crate) async fn assert_holds(dir: &Path, key: &DataKey, originals: &[ItemMeta]) {
        let store = open(dir, key).await.unwrap();
        let listed = store.list().await.unwrap();
        assert_eq!(listed.len(), originals.len(), "{listed:?}");
        for original in originals {
            let copy = listed
                .iter()
                .find(|meta| meta.sha256 == original.sha256)
                .expect("every item is kept");
            assert_eq!(
                (
                    copy.created_at,
                    &copy.name,
                    &copy.device,
                    copy.kind,
                    copy.size
                ),
                (
                    original.created_at,
                    &original.name,
                    &original.device,
                    original.kind,
                    original.size
                )
            );
            let (_, mut content) = store.get(&copy.id).await.unwrap();
            let mut bytes = Vec::new();
            content.read_to_end(&mut bytes).await.unwrap();
            let mut hasher = ContentHasher::new();
            hasher.update(&bytes);
            assert_eq!(hasher.finalize().sha256_hex(), original.sha256);
        }
    }

    /// Every file and folder below `root`, except staging folders.
    pub(crate) fn snapshot(root: &Path) -> BTreeMap<String, Option<Vec<u8>>> {
        let mut found = BTreeMap::new();
        let mut pending = vec![root.to_path_buf()];
        while let Some(dir) = pending.pop() {
            for entry in std::fs::read_dir(&dir).unwrap() {
                let path = entry.unwrap().path();
                let rel = path
                    .strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .into_owned();
                if rel == "tmp" || rel.starts_with("tmp/") || rel.starts_with("v2/tmp") {
                    continue;
                }
                if path.is_dir() {
                    found.insert(rel, None);
                    pending.push(path);
                } else {
                    found.insert(rel, Some(std::fs::read(&path).unwrap()));
                }
            }
        }
        found
    }

    /// Bytes that mark plaintext a test left where encryption must remove it.
    pub(crate) const MARKER: &[u8] = b"staged-plaintext-4c7";

    /// Fills the plaintext staging of the store at `root` with an upload and
    /// a deletion cut short, both holding [`MARKER`].
    pub(crate) fn seed_staging(root: &Path) {
        for (dir, file) in [("tmp/0123", "content"), ("tmp/deleted-abc", "meta.json")] {
            std::fs::create_dir_all(root.join(dir)).unwrap();
            std::fs::write(root.join(dir).join(file), MARKER).unwrap();
        }
    }

    /// Whether any file below `root` holds [`MARKER`].
    pub(crate) fn holds_marker(root: &Path) -> bool {
        let mut pending = vec![root.to_path_buf()];
        while let Some(dir) = pending.pop() {
            for entry in std::fs::read_dir(&dir).unwrap() {
                let path = entry.unwrap().path();
                if path.is_dir() {
                    pending.push(path);
                } else if std::fs::read(&path)
                    .unwrap()
                    .windows(MARKER.len())
                    .any(|w| w == MARKER)
                {
                    return true;
                }
            }
        }
        false
    }

    /// Where a send is held while another client rotates the store's key:
    /// calls counted from when the hold is set, after the store is open.
    const HOLDS: [(FsOp, usize, &str); 3] = [
        (FsOp::OpenWrite, 1, "while its content is written"),
        (FsOp::Rename, 1, "just before it is published"),
        // The first stat is the check before the send; the second, the
        // check after publishing.
        (FsOp::Stat, 2, "just after it is published"),
    ];

    #[tokio::test]
    async fn a_send_held_across_a_rotation_never_succeeds_under_the_old_key() {
        use crate::testing::PausingFs;
        for (op, nth, at) in HOLDS {
            let dir = TempDir::new().unwrap();
            let (old, originals) = sealed_items(dir.path()).await;
            let paused = Arc::new(PausingFs::new(LocalFs::new(dir.path())));
            let store = open_with_key(
                Arc::clone(&paused),
                Some(old.clone()),
                clock(),
                Box::new(StdRandom::new()),
            )
            .await
            .unwrap();
            paused.pause_at(op, nth);
            let send = store.put(NewItem::text("box"), text(b"raced"));
            let rotation = async {
                tokio::time::timeout(std::time::Duration::from_secs(10), paused.reached())
                    .await
                    .unwrap_or_else(|_| panic!("{at}: the send never reached its hold"));
                if op == FsOp::Stat {
                    let published = std::fs::read_dir(dir.path().join("v2/items"))
                        .unwrap()
                        .count();
                    assert_eq!(published, originals.len() + 1, "{at}");
                }
                let new = rotate(
                    &LocalFs::new(dir.path()),
                    &old,
                    &words(W2),
                    quick(),
                    clock(),
                )
                .await
                .unwrap();
                paused.release();
                new
            };
            let (sent, new) = tokio::join!(send, rotation);
            assert!(
                matches!(
                    sent,
                    Err(StoreError::Encryption(
                        EncryptionError::KeyChanged | EncryptionError::Rewriting { .. }
                    ))
                ),
                "{at}: {sent:?}"
            );
            // Every item left opens under the new key.
            let reopened = open(dir.path(), &new).await.unwrap();
            for id in reopened.list_ids().await.unwrap() {
                reopened
                    .get_meta(&id)
                    .await
                    .unwrap_or_else(|err| panic!("{at}: {id}: {err}"));
            }
            // Sent again, it is stored under the new key, once.
            reopened
                .put(NewItem::text("box"), text(b"raced"))
                .await
                .unwrap();
            let raced = reopened
                .list()
                .await
                .unwrap()
                .into_iter()
                .filter(|meta| meta.size == 5)
                .count();
            assert_eq!(raced, 1, "{at}");
        }
    }

    #[tokio::test]
    async fn a_migration_leaves_no_plaintext_staging() {
        let dir = TempDir::new().unwrap();
        let originals = plain_items(dir.path()).await;
        seed_staging(dir.path());
        let key = migrate(&LocalFs::new(dir.path()), &words(W1), quick(), clock())
            .await
            .unwrap();
        assert!(!dir.path().join("tmp").exists());
        assert!(!holds_marker(dir.path()));
        assert_holds(dir.path(), &key, &originals).await;
    }

    pub(crate) fn copy_tree(from: &Path, to: &Path) {
        std::fs::create_dir_all(to).unwrap();
        for entry in std::fs::read_dir(from).unwrap() {
            let entry = entry.unwrap();
            let target = to.join(entry.file_name());
            if entry.path().is_dir() {
                copy_tree(&entry.path(), &target);
            } else {
                std::fs::copy(entry.path(), target).unwrap();
            }
        }
    }

    #[tokio::test]
    async fn a_migration_encrypts_every_item_keeping_its_time_and_metadata() {
        let dir = TempDir::new().unwrap();
        let originals = plain_items(dir.path()).await;
        let fs = LocalFs::new(dir.path());
        let key = migrate(&fs, &words(W1), quick(), clock()).await.unwrap();
        assert_eq!(
            inspect(&fs).await.unwrap(),
            StoreState::Encrypted {
                key_id: key.key_id(),
                plain_left: 0
            }
        );
        assert!(dir.path().join("items").is_file());
        assert!(!dir.path().join(".rewrite").exists());
        assert_eq!(read_plan(&fs).await.unwrap(), None);
        assert_holds(dir.path(), &key, &originals).await;
    }

    #[tokio::test]
    async fn a_rotation_moves_every_item_to_a_new_key_and_shuts_out_the_old() {
        let dir = TempDir::new().unwrap();
        let (old, originals) = sealed_items(dir.path()).await;
        let fs = LocalFs::new(dir.path());
        let stranger = DataKey::generate().unwrap();
        assert!(matches!(
            rotate(&fs, &stranger, &words(W2), quick(), clock()).await,
            Err(StoreError::Encryption(EncryptionError::KeyMismatch { .. }))
        ));
        let new = rotate(&fs, &old, &words(W2), quick(), clock())
            .await
            .unwrap();
        assert_ne!(new.key_id(), old.key_id());
        assert_holds(dir.path(), &new, &originals).await;
        let ids = open(dir.path(), &new)
            .await
            .unwrap()
            .list_ids()
            .await
            .unwrap();
        assert!(originals.iter().all(|meta| !ids.contains(&meta.id)));
        assert!(matches!(
            open(dir.path(), &old).await,
            Err(StoreError::Encryption(EncryptionError::KeyMismatch { .. }))
        ));
    }

    #[tokio::test]
    async fn a_second_rewrite_waits_for_the_first() {
        let dir = TempDir::new().unwrap();
        plain_items(dir.path()).await;
        std::fs::create_dir(dir.path().join(".rewrite")).unwrap();
        let fs = LocalFs::new(dir.path());
        assert!(matches!(
            migrate(&fs, &words(W1), quick(), clock()).await,
            Err(StoreError::Encryption(EncryptionError::Rewriting { .. }))
        ));
        assert!(!dir.path().join("v2").exists());
        // A lock without a plan is released by undo.
        undo(&fs).await.unwrap();
        assert!(!dir.path().join(".rewrite").exists());
        assert!(undo(&fs).await.is_err());
        assert!(finish(&fs, &words(W1), None, None, clock()).await.is_err());
    }

    /// How many calls of each kind a whole migration of `template` makes.
    async fn migration_calls(template: &Path) -> [usize; 9] {
        let scratch = TempDir::new().unwrap();
        copy_tree(template, scratch.path());
        let fs = FaultyFs::new(LocalFs::new(scratch.path()));
        migrate(&fs, &words(W1), quick(), clock()).await.unwrap();
        OPS.map(|op| fs.calls(op))
    }

    #[tokio::test]
    async fn resuming_uploads_nothing_already_published() {
        let template = TempDir::new().unwrap();
        let originals = plain_items(template.path()).await;
        let reads = migration_calls(template.path()).await[2];
        let dir = TempDir::new().unwrap();
        copy_tree(template.path(), dir.path());
        // The last read is the check of the last re-encrypted item.
        let fs = FaultyFs::new(LocalFs::new(dir.path()));
        fs.fail_nth(FsOp::OpenRead, reads);
        assert!(migrate(&fs, &words(W1), quick(), clock()).await.is_err());
        let fs = FaultyFs::new(LocalFs::new(dir.path()));
        let key = finish(&fs, &words(W1), None, None, clock()).await.unwrap();
        assert_eq!(
            fs.calls(FsOp::OpenWrite),
            1,
            "only the stop file is written again"
        );
        assert_holds(dir.path(), &key, &originals).await;
    }

    /// Recovers a store left locked by a cut run, by finishing it at `cut`
    /// and undoing it in a copy: each must either restore `before` or leave
    /// every item under the new key.
    async fn recover_both_ways(
        cut: &Path,
        before: &BTreeMap<String, Option<Vec<u8>>>,
        originals: &[ItemMeta],
        new_words: &Words,
        old_key: Option<&DataKey>,
        at: &str,
    ) {
        let copy = TempDir::new().unwrap();
        copy_tree(cut, copy.path());
        let local = LocalFs::new(cut);
        match finish(&local, new_words, old_key, None, clock()).await {
            Ok(key) => assert_holds(cut, &key, originals).await,
            Err(_) => {
                undo(&local)
                    .await
                    .unwrap_or_else(|err| panic!("{at}: {err}"));
                assert_eq!(&snapshot(cut), before, "{at}: undo after a refused finish");
            }
        }
        let local = LocalFs::new(copy.path());
        match undo(&local).await {
            Ok(()) => assert_eq!(&snapshot(copy.path()), before, "{at}: undo"),
            Err(_) => {
                let key = finish(&local, new_words, old_key, None, clock())
                    .await
                    .unwrap_or_else(|err| panic!("{at}: {err}"));
                assert_holds(copy.path(), &key, originals).await;
            }
        }
    }

    #[tokio::test]
    async fn a_migration_cut_at_any_call_is_finished_or_undone_without_loss() {
        let template = TempDir::new().unwrap();
        let originals = plain_items(template.path()).await;
        let before = snapshot(template.path());
        let calls = migration_calls(template.path()).await;
        let mut recovered = 0;
        for (op, count) in OPS.into_iter().zip(calls) {
            for nth in 1..=count {
                let at = format!("{op:?} call {nth}");
                let cut = TempDir::new().unwrap();
                copy_tree(template.path(), cut.path());
                let fs = FaultyFs::new(LocalFs::new(cut.path()));
                fs.fail_nth(op, nth);
                let result = migrate(&fs, &words(W1), quick(), clock()).await;
                match (result, inspect(&LocalFs::new(cut.path())).await.unwrap()) {
                    (Ok(key), _) => assert_holds(cut.path(), &key, &originals).await,
                    (Err(_), StoreState::Plain { .. }) => {
                        assert_eq!(snapshot(cut.path()), before, "{at}");
                    }
                    (Err(_), StoreState::Rewriting { .. }) => {
                        recovered += 1;
                        recover_both_ways(cut.path(), &before, &originals, &words(W1), None, &at)
                            .await;
                    }
                    (Err(err), state) => panic!("{at}: {err} left {state:?}"),
                }
            }
        }
        assert!(recovered > 10, "only {recovered} cuts needed recovery");
    }

    /// No staged journal is left in `dir`, and `.rewrite/` is absent or
    /// holds a whole journal.
    async fn lock_is_whole_or_absent(dir: &Path) {
        if dir.join(".rewrite").exists() {
            assert!(matches!(
                read_journal(&LocalFs::new(dir)).await.unwrap(),
                Some(Journal::Rewrite(_))
            ));
        }
        let staged: Vec<String> = std::fs::read_dir(dir)
            .unwrap()
            .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
            .filter(|name| name.starts_with(".rewrite-"))
            .collect();
        assert!(staged.is_empty(), "{staged:?}");
    }

    #[tokio::test]
    async fn the_lock_appears_only_with_a_whole_journal() {
        let template = TempDir::new().unwrap();
        plain_items(template.path()).await;
        let before = snapshot(template.path());
        for after in [0, 1, 40, usize::MAX] {
            let dir = TempDir::new().unwrap();
            copy_tree(template.path(), dir.path());
            let fs = FaultyFs::new(LocalFs::new(dir.path()));
            // The journal is the migration's first write.
            fs.cut_nth_write(1, after);
            assert!(
                migrate(&fs, &words(W1), quick(), clock()).await.is_err(),
                "cut after {after}"
            );
            lock_is_whole_or_absent(dir.path()).await;
            assert_eq!(snapshot(dir.path()), before, "cut after {after}");
        }
    }

    #[tokio::test]
    async fn a_lock_without_a_whole_journal_is_released_only_when_nothing_moved() {
        let dir = TempDir::new().unwrap();
        plain_items(dir.path()).await;
        let fs = LocalFs::new(dir.path());
        let lock = dir.path().join(".rewrite");
        // passalong 0.2.0 stopped after creating its plan file.
        std::fs::create_dir(&lock).unwrap();
        std::fs::write(lock.join("plan.json"), "").unwrap();
        assert!(matches!(
            read_journal(&fs).await.unwrap(),
            Some(Journal::Unreadable { .. })
        ));
        assert!(read_plan(&fs).await.is_err());
        assert!(finish(&fs, &words(W1), None, None, clock()).await.is_err());
        undo(&fs).await.unwrap();
        assert!(!lock.exists());

        // Half a plan, and the source already moved: nothing is touched.
        std::fs::create_dir(&lock).unwrap();
        std::fs::write(lock.join("plan.json"), r#"{"kind":"mig"#).unwrap();
        std::fs::create_dir(lock.join("source")).unwrap();
        std::fs::rename(dir.path().join("items"), lock.join("source/items")).unwrap();
        let err = undo(&fs).await.unwrap_err();
        assert!(err.to_string().contains("source"), "{err}");
        assert!(lock.join("source/items").is_dir());
        assert!(
            !lock.join("recovery").exists(),
            "a failed recovery gives up its claim"
        );
    }

    #[tokio::test]
    async fn two_recoveries_never_run_at_once() {
        use crate::encryption::{recovery_in_progress, release_recovery};
        let template = TempDir::new().unwrap();
        let originals = plain_items(template.path()).await;
        let renames = migration_calls(template.path()).await[4];
        let dir = TempDir::new().unwrap();
        copy_tree(template.path(), dir.path());
        let fs = FaultyFs::new(LocalFs::new(dir.path()));
        // The last rename puts the new header in place.
        fs.fail_nth(FsOp::Rename, renames);
        assert!(migrate(&fs, &words(W1), quick(), clock()).await.is_err());
        let fs = LocalFs::new(dir.path());
        assert_eq!(recovery_in_progress(&fs).await.unwrap(), None);

        // Wrong words: the failed finish gives up its claim.
        assert!(finish(&fs, &words(W2), None, None, clock()).await.is_err());
        assert_eq!(recovery_in_progress(&fs).await.unwrap(), None);

        // A recovery that is running shuts out a second.
        std::fs::create_dir(dir.path().join(".rewrite/recovery")).unwrap();
        let marker = recovery_in_progress(&fs).await.unwrap().unwrap();
        assert!(!marker.is_stale(chrono::Utc::now()));
        for err in [
            undo(&fs).await.err(),
            finish(&fs, &words(W1), None, None, clock()).await.err(),
        ] {
            assert!(
                matches!(
                    err,
                    Some(StoreError::Encryption(EncryptionError::Recovering {
                        started: Some(_)
                    }))
                ),
                "{err:?}"
            );
        }

        // Taken over, it finishes.
        release_recovery(&fs).await.unwrap();
        let key = finish(&fs, &words(W1), None, None, clock()).await.unwrap();
        assert_holds(dir.path(), &key, &originals).await;
        assert!(!dir.path().join(".rewrite").exists());
    }

    #[tokio::test]
    async fn a_rotation_cut_at_any_call_is_finished_or_undone_without_loss() {
        let template = TempDir::new().unwrap();
        let (old, originals) = sealed_items(template.path()).await;
        let before = snapshot(template.path());
        let calls = {
            let scratch = TempDir::new().unwrap();
            copy_tree(template.path(), scratch.path());
            let fs = FaultyFs::new(LocalFs::new(scratch.path()));
            rotate(&fs, &old, &words(W2), quick(), clock())
                .await
                .unwrap();
            OPS.map(|op| fs.calls(op))
        };
        let mut recovered = 0;
        for (op, count) in OPS.into_iter().zip(calls) {
            for nth in 1..=count {
                let at = format!("{op:?} call {nth}");
                let cut = TempDir::new().unwrap();
                copy_tree(template.path(), cut.path());
                let fs = FaultyFs::new(LocalFs::new(cut.path()));
                fs.fail_nth(op, nth);
                let result = rotate(&fs, &old, &words(W2), quick(), clock()).await;
                match (result, inspect(&LocalFs::new(cut.path())).await.unwrap()) {
                    (Ok(key), _) => assert_holds(cut.path(), &key, &originals).await,
                    (Err(_), StoreState::Encrypted { key_id, .. }) if key_id == old.key_id() => {
                        assert_eq!(snapshot(cut.path()), before, "{at}");
                    }
                    (Err(_), StoreState::Rewriting { .. }) => {
                        recovered += 1;
                        recover_both_ways(
                            cut.path(),
                            &before,
                            &originals,
                            &words(W2),
                            Some(&old),
                            &at,
                        )
                        .await;
                    }
                    (Err(err), state) => panic!("{at}: {err} left {state:?}"),
                }
            }
        }
        assert!(recovered > 10, "only {recovered} cuts needed recovery");
    }
}