corium-transactor 0.1.38

Corium transactor
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
//! Online, storage-native database backup and offline restore.
//!
//! A backup is one versioned binary archive. The transactor fixes the upper
//! transaction basis and hands the backup client an independent storage
//! connection; [`backup`] replays only through that basis. Re-running it
//! against the same file appends one binary checkpoint containing only the
//! records after the archive's previous checkpoint.

use std::fs::{self, File, OpenOptions};
use std::future::Future;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;

use corium_log::{
    FileLog, LogError, TransactionLog, TxRecord, append_framed_record, decode_framed_records,
};
use corium_protocol::pb;
use corium_store::{
    BlobId, BlobStore, DbRoot, FORMAT_VERSION, FsStore, RootStore, StoreError, db_root_name,
};
use thiserror::Error;

use crate::{LogBackend, NodeStore, StorageConnectionError, StoreSpec};

/// Binary backup container version written by this release.
pub const BACKUP_FORMAT_VERSION: u32 = 1;

const BACKUP_MAGIC: &[u8; 16] = b"CORIUM_BACKUP\0\0\0";
const BLOB_TAG: [u8; 4] = *b"BLOB";
const CHECKPOINT_TAG: [u8; 4] = *b"CKPT";
const CHECKPOINT_END: &[u8; 4] = b"DONE";
const WRITER_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Counters and snapshot identity returned by a backup.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BackupReport {
    /// Binary backup container version.
    pub backup_format_version: u32,
    /// Corium version that wrote the latest checkpoint.
    pub writer_version: String,
    /// Transaction basis preserved by the snapshot.
    pub basis_t: u64,
    /// Index basis named by the copied root.
    pub index_basis_t: u64,
    /// Immutable blobs newly copied during this run.
    pub copied_blobs: usize,
    /// Immutable blobs already present in an incremental destination.
    pub reused_blobs: usize,
    /// Transaction records appended during this run.
    pub replayed_transactions: usize,
}

/// Counters and identity returned by a restore.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RestoreReport {
    /// Binary backup container version.
    pub backup_format_version: u32,
    /// Corium version that wrote the latest checkpoint.
    pub writer_version: String,
    /// Source database name recorded in the backup.
    pub source_db: String,
    /// Restored database name (which may differ for clone restores).
    pub target_db: String,
    /// Restored transaction basis.
    pub basis_t: u64,
    /// Immutable blobs newly copied into the target store.
    pub copied_blobs: usize,
    /// Immutable blobs already shared by the target store.
    pub reused_blobs: usize,
}

/// Backup or restore failure.
#[derive(Debug, Error)]
pub enum BackupError {
    /// Filesystem operation failed.
    #[error("backup I/O failed: {0}")]
    Io(#[from] io::Error),
    /// Blob/root store operation failed.
    #[error(transparent)]
    Store(#[from] StoreError),
    /// Transaction log could not be read.
    #[error(transparent)]
    Log(#[from] LogError),
    /// The requested database does not exist.
    #[error("database {0:?} does not exist")]
    MissingDatabase(String),
    /// A required archive section, root, blob, or log record is malformed.
    #[error("invalid backup: {0}")]
    Invalid(String),
    /// Restore refuses to replace an existing database.
    #[error("database {0:?} already exists in the target")]
    TargetExists(String),
    /// The stored format cannot be read by this binary.
    #[error("storage format {found} is newer than supported format {supported}")]
    UnsupportedFormat {
        /// Version found in the backup.
        found: u32,
        /// Newest version understood by this binary.
        supported: u32,
    },
    /// The binary backup container is newer than this binary understands.
    #[error(
        "backup file format {found} (created by Corium {writer}) is newer than supported format {supported}"
    )]
    UnsupportedBackupFormat {
        /// Version found in the archive header.
        found: u32,
        /// Newest version understood by this binary.
        supported: u32,
        /// Corium version recorded by the archive creator.
        writer: String,
    },
    /// The advertised backend cannot be opened independently.
    #[error("storage backend cannot be backed up independently: {0}")]
    UnsupportedSource(String),
}

/// An independently accessible storage service plus the transaction basis
/// fixed by the running transactor.
#[derive(Clone, Debug)]
pub struct BackupSource {
    store: StoreSpec,
    data_dir: PathBuf,
    basis_t: u64,
}

impl BackupSource {
    /// Decodes the transactor's one-shot backup discovery response.
    ///
    /// # Errors
    /// Returns an error for missing connection details, an in-memory backend,
    /// or a backend omitted from this build.
    pub fn from_info(info: pb::GetStorageInfoResponse) -> Result<Self, BackupError> {
        let storage = info
            .storage
            .ok_or_else(|| BackupError::Invalid("transactor returned no storage backend".into()))?;
        let (store, data_dir) =
            StoreSpec::from_connection(storage).map_err(|error| match error {
                StorageConnectionError::Missing => {
                    BackupError::Invalid("transactor returned no storage backend".into())
                }
                StorageConnectionError::Unsupported(detail) => {
                    BackupError::UnsupportedSource(detail)
                }
            })?;
        Ok(Self {
            store,
            data_dir,
            basis_t: info.basis_t,
        })
    }

    /// Fixed upper transaction basis for this run.
    #[must_use]
    pub const fn basis_t(&self) -> u64 {
        self.basis_t
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct ArchiveHeader {
    creator_version: String,
    source_db: String,
    index_basis_t: u64,
    storage_format_version: u32,
    root: Vec<u8>,
}

#[derive(Debug)]
struct Archive {
    header: ArchiveHeader,
    writer_version: String,
    basis_t: u64,
    metadata: Vec<u8>,
    records: Vec<TxRecord>,
    blob_offsets: Vec<(u64, u64)>,
    durable_len: u64,
}

fn valid_db_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= 128
        && name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_')
}

fn meta_root_name(db: &str) -> String {
    format!("meta:{db}")
}

fn log_path(data_dir: &Path, db: &str) -> PathBuf {
    data_dir.join("logs").join(format!("{db}.log"))
}

fn invalid(detail: impl Into<String>) -> BackupError {
    BackupError::Invalid(detail.into())
}

fn write_u32(writer: &mut impl Write, value: u32) -> Result<(), BackupError> {
    writer.write_all(&value.to_be_bytes())?;
    Ok(())
}

fn write_u64(writer: &mut impl Write, value: u64) -> Result<(), BackupError> {
    writer.write_all(&value.to_be_bytes())?;
    Ok(())
}

fn read_u32(reader: &mut impl Read) -> Result<u32, BackupError> {
    let mut bytes = [0; 4];
    reader.read_exact(&mut bytes)?;
    Ok(u32::from_be_bytes(bytes))
}

fn read_u64(reader: &mut impl Read) -> Result<u64, BackupError> {
    let mut bytes = [0; 8];
    reader.read_exact(&mut bytes)?;
    Ok(u64::from_be_bytes(bytes))
}

fn write_bytes(writer: &mut impl Write, bytes: &[u8]) -> Result<(), BackupError> {
    write_u64(
        writer,
        u64::try_from(bytes.len()).map_err(|_| invalid("backup field is too large"))?,
    )?;
    writer.write_all(bytes)?;
    Ok(())
}

fn read_bytes(reader: &mut impl Read, field: &str) -> Result<Vec<u8>, BackupError> {
    let len = usize::try_from(read_u64(reader)?)
        .map_err(|_| invalid(format!("{field} is too large for this platform")))?;
    let mut bytes = vec![0; len];
    reader.read_exact(&mut bytes)?;
    Ok(bytes)
}

fn read_string(reader: &mut impl Read, field: &str) -> Result<String, BackupError> {
    String::from_utf8(read_bytes(reader, field)?)
        .map_err(|_| invalid(format!("{field} is not UTF-8")))
}

fn write_frame(writer: &mut impl Write, tag: [u8; 4], payload: &[u8]) -> Result<(), BackupError> {
    writer.write_all(&tag)?;
    write_u64(
        writer,
        u64::try_from(payload.len()).map_err(|_| invalid("backup frame is too large"))?,
    )?;
    writer.write_all(payload)?;
    Ok(())
}

fn write_header(writer: &mut impl Write, header: &ArchiveHeader) -> Result<(), BackupError> {
    writer.write_all(BACKUP_MAGIC)?;
    write_u32(writer, BACKUP_FORMAT_VERSION)?;
    write_bytes(writer, header.creator_version.as_bytes())?;
    write_bytes(writer, header.source_db.as_bytes())?;
    write_u32(writer, header.storage_format_version)?;
    write_u64(writer, header.index_basis_t)?;
    write_bytes(writer, &header.root)
}

fn encode_checkpoint(
    basis_t: u64,
    previous_basis_t: u64,
    metadata: &[u8],
    records: &[TxRecord],
) -> Result<Vec<u8>, BackupError> {
    let mut framed = Vec::new();
    for record in records {
        append_framed_record(&mut framed, record)?;
    }
    let mut payload = Vec::new();
    write_bytes(&mut payload, WRITER_VERSION.as_bytes())?;
    write_u64(&mut payload, basis_t)?;
    write_u64(
        &mut payload,
        if records.is_empty() {
            0
        } else {
            previous_basis_t.saturating_add(1)
        },
    )?;
    write_u64(
        &mut payload,
        u64::try_from(records.len()).map_err(|_| invalid("too many transaction records"))?,
    )?;
    write_bytes(&mut payload, metadata)?;
    write_bytes(&mut payload, &framed)?;
    payload.extend_from_slice(CHECKPOINT_END);
    Ok(payload)
}

fn read_frame_bytes(file: &mut File, frame_end: u64, field: &str) -> Result<Vec<u8>, BackupError> {
    let len = read_u64(file)?;
    let start = file.stream_position()?;
    let end = start
        .checked_add(len)
        .ok_or_else(|| invalid(format!("{field} length overflowed")))?;
    if end > frame_end {
        return Err(invalid(format!("{field} extends past its checkpoint")));
    }
    let len = usize::try_from(len)
        .map_err(|_| invalid(format!("{field} is too large for this platform")))?;
    let mut bytes = vec![0; len];
    file.read_exact(&mut bytes)?;
    Ok(bytes)
}

fn decode_checkpoint(
    file: &mut File,
    frame_end: u64,
    previous_basis_t: u64,
    include_records: bool,
) -> Result<(String, u64, Vec<u8>, Vec<TxRecord>), BackupError> {
    let writer_version = String::from_utf8(read_frame_bytes(
        file,
        frame_end,
        "checkpoint writer version",
    )?)
    .map_err(|_| invalid("checkpoint writer version is not UTF-8"))?;
    let basis_t = read_u64(file)?;
    let start_t = read_u64(file)?;
    let record_count = read_u64(file)?;
    let metadata = read_frame_bytes(file, frame_end, "checkpoint metadata")?;
    let framed_len = read_u64(file)?;
    let framed_start = file.stream_position()?;
    let records_end = framed_start
        .checked_add(framed_len)
        .ok_or_else(|| invalid("checkpoint transaction-record length overflowed"))?;
    if records_end
        .checked_add(4)
        .is_none_or(|checkpoint_end| checkpoint_end != frame_end)
    {
        return Err(invalid(
            "checkpoint transaction records do not fill their frame",
        ));
    }
    let framed = if include_records {
        let len = usize::try_from(framed_len)
            .map_err(|_| invalid("transaction records are too large for this platform"))?;
        let mut bytes = vec![0; len];
        file.read_exact(&mut bytes)?;
        bytes
    } else {
        file.seek(SeekFrom::Start(records_end))?;
        Vec::new()
    };
    let mut end = [0; 4];
    file.read_exact(&mut end)?;
    if &end != CHECKPOINT_END || file.stream_position()? != frame_end {
        return Err(invalid("checkpoint has invalid trailing bytes"));
    }
    let expected_count = basis_t
        .checked_sub(previous_basis_t)
        .ok_or_else(|| invalid("checkpoint basis regressed"))?;
    let expected_start = if expected_count == 0 {
        0
    } else {
        previous_basis_t
            .checked_add(1)
            .ok_or_else(|| invalid("checkpoint basis overflowed"))?
    };
    if record_count != expected_count || start_t != expected_start {
        return Err(invalid("checkpoint transaction range is not contiguous"));
    }
    let records = if include_records {
        let records = decode_framed_records(&framed)?;
        if records.len() as u64 != record_count {
            return Err(invalid(
                "checkpoint record count does not match its payload",
            ));
        }
        if record_count > 0 {
            validate_records(&records, start_t, basis_t)?;
        }
        records
    } else {
        Vec::new()
    };
    Ok((writer_version, basis_t, metadata, records))
}

fn read_header(file: &mut File) -> Result<ArchiveHeader, BackupError> {
    let mut magic = [0; 16];
    file.read_exact(&mut magic)?;
    if &magic != BACKUP_MAGIC {
        return Err(invalid("unknown backup file magic"));
    }
    let format_version = read_u32(file)?;
    let creator_version = read_string(file, "archive creator version")?;
    if format_version > BACKUP_FORMAT_VERSION {
        return Err(BackupError::UnsupportedBackupFormat {
            found: format_version,
            supported: BACKUP_FORMAT_VERSION,
            writer: creator_version,
        });
    }
    if format_version != BACKUP_FORMAT_VERSION {
        return Err(invalid(format!(
            "unsupported backup file format {format_version}"
        )));
    }
    let source_db = read_string(file, "source database name")?;
    let storage_format_version = read_u32(file)?;
    if storage_format_version > FORMAT_VERSION {
        return Err(BackupError::UnsupportedFormat {
            found: storage_format_version,
            supported: FORMAT_VERSION,
        });
    }
    Ok(ArchiveHeader {
        creator_version,
        source_db,
        storage_format_version,
        index_basis_t: read_u64(file)?,
        root: read_bytes(file, "database root")?,
    })
}

fn read_archive(path: &Path, include_records: bool) -> Result<Archive, BackupError> {
    if !path.is_file() {
        return Err(invalid(format!(
            "{} is not a binary backup file",
            path.display()
        )));
    }
    let mut file = File::open(path)?;
    let file_len = file.metadata()?.len();
    let header = read_header(&mut file)?;
    let mut writer_version = header.creator_version.clone();
    let mut basis_t = 0;
    let mut metadata = Vec::new();
    let mut records = Vec::new();
    let mut blob_offsets = Vec::new();
    let mut durable_len = file.stream_position()?;
    let mut saw_checkpoint = false;
    loop {
        let frame_start = file.stream_position()?;
        let mut tag = [0; 4];
        match file.read_exact(&mut tag) {
            Ok(()) => {}
            Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => break,
            Err(error) => return Err(error.into()),
        }
        let frame_len = match read_u64(&mut file) {
            Ok(len) => len,
            Err(BackupError::Io(error)) if error.kind() == io::ErrorKind::UnexpectedEof => break,
            Err(error) => return Err(error),
        };
        let payload_offset = file.stream_position()?;
        let frame_end = payload_offset
            .checked_add(frame_len)
            .ok_or_else(|| invalid("backup frame length overflowed"))?;
        if frame_end > file_len {
            break;
        }
        if tag == BLOB_TAG {
            if saw_checkpoint {
                return Err(invalid("blob frame appears after a checkpoint"));
            }
            blob_offsets.push((payload_offset, frame_len));
            file.seek(SeekFrom::Start(frame_end))?;
        } else if tag == CHECKPOINT_TAG {
            saw_checkpoint = true;
            let (checkpoint_writer, checkpoint_basis, checkpoint_meta, checkpoint_records) =
                decode_checkpoint(&mut file, frame_end, basis_t, include_records)?;
            writer_version = checkpoint_writer;
            basis_t = checkpoint_basis;
            metadata = checkpoint_meta;
            records.extend(checkpoint_records);
        } else {
            return Err(invalid(format!(
                "unknown backup frame tag at offset {frame_start}"
            )));
        }
        durable_len = frame_end;
    }
    if !saw_checkpoint {
        return Err(invalid("backup contains no complete checkpoint"));
    }
    Ok(Archive {
        header,
        writer_version,
        basis_t,
        metadata,
        records,
        blob_offsets,
        durable_len,
    })
}

/// Writes one blob and everything it references into the binary archive,
/// children first.
fn write_blob_tree<'a>(
    source: &'a dyn BlobStore,
    id: &'a BlobId,
    seen: &'a mut std::collections::HashSet<BlobId>,
    writer: &'a mut File,
    copied: &'a mut usize,
) -> Pin<Box<dyn Future<Output = Result<(), BackupError>> + Send + 'a>> {
    Box::pin(async move {
        if !seen.insert(id.clone()) {
            return Ok(());
        }
        let bytes = source
            .get(id)
            .await?
            .ok_or_else(|| BackupError::Invalid(format!("root references missing blob {id}")))?;
        for child in corium_store::index_blob_children(&bytes)? {
            write_blob_tree(source, &child, seen, writer, copied).await?;
        }
        write_frame(writer, BLOB_TAG, &bytes)?;
        *copied += 1;
        Ok(())
    })
}

fn validate_blob_tree<'a>(
    store: &'a dyn BlobStore,
    id: &'a BlobId,
    seen: &'a mut std::collections::HashSet<BlobId>,
) -> Pin<Box<dyn Future<Output = Result<(), BackupError>> + Send + 'a>> {
    Box::pin(async move {
        if !seen.insert(id.clone()) {
            return Ok(());
        }
        let bytes = store
            .get(id)
            .await?
            .ok_or_else(|| invalid(format!("archive is missing reachable blob {id}")))?;
        for child in corium_store::index_blob_children(&bytes)? {
            validate_blob_tree(store, &child, seen).await?;
        }
        Ok(())
    })
}

fn write_log_atomically(records: &[TxRecord], target: &Path) -> Result<(), BackupError> {
    if let Some(parent) = target.parent() {
        fs::create_dir_all(parent)?;
    }
    let temporary = target.with_extension("tmp");
    match fs::remove_file(&temporary) {
        Ok(()) => {}
        Err(error) if error.kind() == io::ErrorKind::NotFound => {}
        Err(error) => return Err(error.into()),
    }
    let log = FileLog::open(&temporary)?;
    for record in records {
        log.append(record)?;
    }
    fs::rename(temporary, target)?;
    Ok(())
}

fn bare_root() -> DbRoot {
    DbRoot {
        format_version: FORMAT_VERSION,
        lease_version: 0,
        owner: String::new(),
        lease_expires_unix_ms: 0,
        owner_endpoint: String::new(),
        index_basis_t: 0,
        roots: None,
        next_entity_id: 0,
        last_tx_instant: i64::MIN,
    }
}

fn sanitize_root(mut root: DbRoot) -> DbRoot {
    root.lease_version = 0;
    root.owner.clear();
    root.lease_expires_unix_ms = 0;
    root.owner_endpoint.clear();
    root
}

fn validate_records(
    records: &[corium_log::TxRecord],
    start: u64,
    end_inclusive: u64,
) -> Result<(), BackupError> {
    if start > end_inclusive {
        return Ok(());
    }
    let expected = end_inclusive - start + 1;
    if records.len() as u64 != expected
        || records.first().map(|record| record.t) != Some(start)
        || records.last().map(|record| record.t) != Some(end_inclusive)
        || records.windows(2).any(|pair| pair[1].t != pair[0].t + 1)
    {
        return Err(BackupError::Invalid(format!(
            "source log does not contain a contiguous range {start}..={end_inclusive}"
        )));
    }
    Ok(())
}

fn append_archive_checkpoint(
    destination: &Path,
    archive: &Archive,
    checkpoint: &[u8],
    changed: bool,
) -> Result<String, BackupError> {
    if !changed {
        return Ok(archive.writer_version.clone());
    }
    let mut file = OpenOptions::new().write(true).open(destination)?;
    file.set_len(archive.durable_len)?;
    file.seek(SeekFrom::Start(archive.durable_len))?;
    write_frame(&mut file, CHECKPOINT_TAG, checkpoint)?;
    file.sync_all()?;
    Ok(WRITER_VERSION.to_owned())
}

async fn create_archive(
    destination: &Path,
    db: &str,
    root: &DbRoot,
    source_store: &dyn BlobStore,
    checkpoint: &[u8],
) -> Result<usize, BackupError> {
    if let Some(parent) = destination.parent()
        && !parent.as_os_str().is_empty()
    {
        fs::create_dir_all(parent)?;
    }
    let mut temporary = destination.as_os_str().to_os_string();
    temporary.push(".tmp");
    let temporary = PathBuf::from(temporary);
    match fs::remove_file(&temporary) {
        Ok(()) => {}
        Err(error) if error.kind() == io::ErrorKind::NotFound => {}
        Err(error) => return Err(error.into()),
    }
    let mut file = File::create(&temporary)?;
    write_header(
        &mut file,
        &ArchiveHeader {
            creator_version: WRITER_VERSION.to_owned(),
            source_db: db.to_owned(),
            index_basis_t: root.index_basis_t,
            storage_format_version: root.format_version,
            root: root.encode(),
        },
    )?;
    let mut seen = std::collections::HashSet::new();
    let mut copied = 0;
    for id in root.roots.iter().flatten() {
        write_blob_tree(source_store, id, &mut seen, &mut file, &mut copied).await?;
    }
    write_frame(&mut file, CHECKPOINT_TAG, checkpoint)?;
    file.sync_all()?;
    fs::rename(temporary, destination)?;
    Ok(copied)
}

/// Copies one live database into `destination` through a fixed transaction
/// checkpoint.
///
/// `source` comes from [`BackupSource::from_info`], after the transactor fixes
/// its current `t`. The source log may continue growing; this function reads
/// only `(old_backup_t, source_t]`. Reusing a destination retains its index
/// snapshot and appends only missing transaction records.
///
/// # Errors
/// Returns an error if the database is missing, the checkpoint cannot be read
/// contiguously, the destination belongs to another database, or storage
/// cannot be opened.
pub async fn backup(
    source: &BackupSource,
    db: &str,
    destination: impl AsRef<Path>,
) -> Result<BackupReport, BackupError> {
    if !valid_db_name(db) {
        return Err(BackupError::MissingDatabase(db.to_owned()));
    }
    let destination = destination.as_ref();
    let source_store = Arc::new(NodeStore::open_existing(&source.store, &source.data_dir).await?);
    let source_logs =
        LogBackend::for_spec(&source.store, &source.data_dir, Arc::clone(&source_store));
    let db_name = db_root_name(db);
    let meta_name = meta_root_name(db);
    let source_db_bytes = source_store
        .get_root(&db_name)
        .await?
        .ok_or_else(|| BackupError::MissingDatabase(db.to_owned()))?;
    let meta = source_store
        .get_root(&meta_name)
        .await?
        .ok_or_else(|| BackupError::MissingDatabase(db.to_owned()))?;
    let source_root = DbRoot::decode(&source_db_bytes)
        .ok_or_else(|| BackupError::Invalid("database root cannot be decoded".into()))?;
    if source_root.format_version > FORMAT_VERSION {
        return Err(BackupError::UnsupportedFormat {
            found: source_root.format_version,
            supported: FORMAT_VERSION,
        });
    }
    let previous = destination
        .exists()
        .then(|| read_archive(destination, false))
        .transpose()?;
    if let Some(archive) = &previous {
        if archive.header.source_db != db {
            return Err(invalid(format!(
                "backup belongs to database {:?}, not {db:?}",
                archive.header.source_db
            )));
        }
        if archive.basis_t > source.basis_t {
            return Err(invalid(format!(
                "backup basis {} is ahead of source checkpoint {}",
                archive.basis_t, source.basis_t
            )));
        }
    }
    let existing_basis = previous.as_ref().map_or(0, |archive| archive.basis_t);
    let start = existing_basis.saturating_add(1);
    let records = if start <= source.basis_t {
        let end = source
            .basis_t
            .checked_add(1)
            .ok_or_else(|| BackupError::Invalid("source basis is too large".into()))?;
        let log = source_logs.open_read_only(db).await?;
        log.tx_range_async(start, Some(end)).await?
    } else {
        Vec::new()
    };
    validate_records(&records, start, source.basis_t)?;

    // An existing archive's snapshot is its stable replay base. On a first
    // backup, opportunistically retain the source snapshot only when it is
    // not ahead of the fixed log checkpoint; otherwise a bare root forces a
    // correct full-log replay on restore.
    let root = if let Some(archive) = &previous {
        let root = DbRoot::decode(&archive.header.root)
            .ok_or_else(|| invalid("archive database root cannot be decoded"))?;
        if root.index_basis_t != archive.header.index_basis_t
            || root.format_version != archive.header.storage_format_version
        {
            return Err(invalid("archive header does not match its database root"));
        }
        sanitize_root(root)
    } else if source_root.index_basis_t <= source.basis_t {
        sanitize_root(source_root)
    } else {
        bare_root()
    };
    let checkpoint = encode_checkpoint(source.basis_t, existing_basis, &meta, &records)?;
    let (copied_blobs, writer_version) = if let Some(archive) = &previous {
        (
            0,
            append_archive_checkpoint(destination, archive, &checkpoint, !records.is_empty())?,
        )
    } else {
        (
            create_archive(destination, db, &root, source_store.as_ref(), &checkpoint).await?,
            WRITER_VERSION.to_owned(),
        )
    };
    Ok(BackupReport {
        backup_format_version: BACKUP_FORMAT_VERSION,
        writer_version,
        basis_t: source.basis_t,
        index_basis_t: root.index_basis_t,
        copied_blobs,
        reused_blobs: 0,
        replayed_transactions: records.len(),
    })
}

/// Restores a backup under `target_db`, allowing restore-as-clone by choosing
/// a name different from the archive's source database.
///
/// Existing target databases are never overwritten. Immutable blobs already
/// present in the target store are shared rather than recopied.
///
/// # Errors
/// Returns an error for malformed/incomplete backups, unsupported formats,
/// existing targets, or I/O failures.
pub async fn restore(
    source: impl AsRef<Path>,
    target_data_dir: impl AsRef<Path>,
    target_db: &str,
) -> Result<RestoreReport, BackupError> {
    if !valid_db_name(target_db) {
        return Err(BackupError::Invalid(format!(
            "invalid target database name {target_db:?}"
        )));
    }
    let source = source.as_ref();
    let target_data_dir = target_data_dir.as_ref();
    let archive = read_archive(source, true)?;
    let root = DbRoot::decode(&archive.header.root)
        .ok_or_else(|| invalid("archive database root cannot be decoded"))?;
    if root.index_basis_t != archive.header.index_basis_t
        || root.format_version != archive.header.storage_format_version
    {
        return Err(invalid("archive header does not match its database root"));
    }

    let target_store = FsStore::open(target_data_dir.join("store"))?;
    let target_db_name = db_root_name(target_db);
    let target_meta_name = meta_root_name(target_db);
    if target_store.get_root(&target_db_name).await?.is_some()
        || target_store.get_root(&target_meta_name).await?.is_some()
        || log_path(target_data_dir, target_db).exists()
    {
        return Err(BackupError::TargetExists(target_db.to_owned()));
    }
    let mut backup_file = File::open(source)?;
    let mut copied_blobs = 0;
    let mut reused_blobs = 0;
    for (offset, len) in &archive.blob_offsets {
        backup_file.seek(SeekFrom::Start(*offset))?;
        let len =
            usize::try_from(*len).map_err(|_| invalid("blob is too large for this platform"))?;
        let mut bytes = vec![0; len];
        backup_file.read_exact(&mut bytes)?;
        let id = corium_store::digest(&bytes);
        if target_store.contains(&id).await? {
            reused_blobs += 1;
        } else {
            let stored = target_store.put(&bytes).await?;
            if stored != id {
                return Err(invalid(format!("blob digest changed while restoring {id}")));
            }
            copied_blobs += 1;
        }
    }
    let mut seen = std::collections::HashSet::new();
    for id in root.roots.iter().flatten() {
        validate_blob_tree(&target_store, id, &mut seen).await?;
    }
    let target_log = log_path(target_data_dir, target_db);
    write_log_atomically(&archive.records, &target_log)?;
    let db_bytes = archive.header.root;
    // Metadata is the catalog entry, so publish it last. A node can never
    // discover a partially restored database.
    let publish = match target_store
        .cas_root(&target_db_name, None, &db_bytes)
        .await
    {
        Ok(()) => {
            target_store
                .cas_root(&target_meta_name, None, &archive.metadata)
                .await
        }
        Err(error) => Err(error),
    };
    if let Err(error) = publish {
        let _ = target_store.delete_root(&target_db_name).await;
        let _ = target_store.delete_root(&target_meta_name).await;
        let _ = fs::remove_file(&target_log);
        return Err(error.into());
    }
    Ok(RestoreReport {
        backup_format_version: BACKUP_FORMAT_VERSION,
        writer_version: archive.writer_version,
        source_db: archive.header.source_db,
        target_db: target_db.to_owned(),
        basis_t: archive.basis_t,
        copied_blobs,
        reused_blobs,
    })
}