second-brain-core 0.13.4

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
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
//! Files sealed at rest: chunked XChaCha20-Poly1305 under a key the caller
//! holds. A sealed file starts with a header naming the key it was sealed
//! under and a random id for this written version of the file; each 64 KiB
//! chunk is sealed with a nonce made of that id and the chunk index, and
//! authenticated together with the file's logical name, the chunk index and
//! whether it is the last chunk, so a file that was cut short, reordered or
//! renamed fails to open.

mod directory;

pub use directory::SealedDirectory;

use std::fmt;
use std::fs::{self, File, OpenOptions};
use std::io::{self, BufReader, ErrorKind, Read, Write};
use std::os::unix::fs::FileExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{Context, Result, anyhow};
use chacha20poly1305::aead::{Aead, KeyInit, Payload};
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use hkdf::Hkdf;
use rand::TryRngCore;
use rand::rngs::OsRng;
use sha2::Sha256;

use crate::keys::KeyGuard;

pub const CHUNK_LEN: usize = 64 * 1024;
const MAGIC: &[u8; 8] = b"SBSEAL\x00\x01";
const KEY_ID_LEN: usize = 8;
const FILE_ID_LEN: usize = 16;
const HEADER_LEN: usize = MAGIC.len() + KEY_ID_LEN + FILE_ID_LEN;
const TAG_LEN: usize = 16;
const SEALED_CHUNK_LEN: usize = CHUNK_LEN + TAG_LEN;
const AAD_DOMAIN: &[u8] = b"second-brain sealed file v1";
const KEY_ID_INFO: &[u8] = b"second-brain sealed file key id v1";
const CIPHER_KEY_INFO: &[u8] = b"second-brain sealed file cipher v1";
const TMP_MARKER: &str = ".seal-tmp.";

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SealKeyId(pub [u8; KEY_ID_LEN]);

impl fmt::Display for SealKeyId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&crate::keys::hex(&self.0))
    }
}

impl fmt::Debug for SealKeyId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SealKeyId({self})")
    }
}

/// One 32-byte key files are sealed under. Its id is derived from it, so a
/// file's header says which key opens it without revealing anything of it.
pub struct SealKey {
    id: SealKeyId,
    secret: KeyGuard<[u8; 32]>,
    cipher: XChaCha20Poly1305,
}

impl SealKey {
    pub fn generate() -> Result<Self> {
        Self::from_bytes(KeyGuard::random()?)
    }

    pub fn from_bytes(secret: KeyGuard<[u8; 32]>) -> Result<Self> {
        let hkdf = Hkdf::<Sha256>::new(None, secret.expose());
        let mut id = [0u8; KEY_ID_LEN];
        hkdf.expand(KEY_ID_INFO, &mut id)
            .map_err(|e| anyhow!("deriving a sealing key id: {e}"))?;
        let mut cipher_key = KeyGuard::new([0u8; 32]);
        hkdf.expand(CIPHER_KEY_INFO, cipher_key.expose_mut())
            .map_err(|e| anyhow!("deriving a sealing cipher key: {e}"))?;
        let cipher = XChaCha20Poly1305::new(cipher_key.expose().into());
        Ok(Self {
            id: SealKeyId(id),
            secret,
            cipher,
        })
    }

    pub fn id(&self) -> SealKeyId {
        self.id
    }

    pub(crate) fn expose(&self) -> &[u8; 32] {
        self.secret.expose()
    }
}

impl fmt::Debug for SealKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SealKey({})", self.id)
    }
}

/// The key new files are sealed under, plus retired keys that still open
/// files written before a rotation until they are resealed.
#[derive(Clone, Debug)]
pub struct SealKeys {
    current: Arc<SealKey>,
    retired: Arc<[Arc<SealKey>]>,
}

impl SealKeys {
    pub fn new(current: SealKey, retired: Vec<SealKey>) -> Self {
        Self {
            current: Arc::new(current),
            retired: retired.into_iter().map(Arc::new).collect(),
        }
    }

    pub fn single(current: SealKey) -> Self {
        Self::new(current, Vec::new())
    }

    pub fn current_id(&self) -> SealKeyId {
        self.current.id
    }

    pub fn retired_ids(&self) -> Vec<SealKeyId> {
        self.retired.iter().map(|k| k.id).collect()
    }

    fn find(&self, id: SealKeyId) -> Option<&SealKey> {
        std::iter::once(&self.current)
            .chain(self.retired.iter())
            .find(|k| k.id == id)
            .map(|k| k.as_ref())
    }
}

/// Why a sealed file did not open, as the root cause of the returned
/// error, so a caller can tell a file it may rebuild from damage.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SealError {
    /// The file is plaintext, or holds something else entirely.
    NotSealed,
    /// The file is sealed, and no key given has its id.
    UnknownKey(SealKeyId),
    /// The file is sealed but does not open under its key: cut short,
    /// altered, or opened under another name.
    Damaged,
    /// No key was given, and the file is sealed.
    KeyRequired,
}

impl fmt::Display for SealError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotSealed => f.write_str("the file is not sealed"),
            Self::UnknownKey(id) => {
                write!(f, "the file is sealed under key {id}, which is not held")
            }
            Self::Damaged => f.write_str("the sealed file is damaged or truncated"),
            Self::KeyRequired => f.write_str("the file is sealed and no key was given to open it"),
        }
    }
}

impl std::error::Error for SealError {}

/// The `SealError` at the root of `error`, if any.
pub fn seal_error(error: &anyhow::Error) -> Option<&SealError> {
    error.chain().find_map(|e| e.downcast_ref::<SealError>())
}

#[derive(Clone, Copy)]
struct Header {
    key_id: SealKeyId,
    file_id: [u8; FILE_ID_LEN],
}

impl Header {
    fn fresh(key: &SealKey) -> Result<Self> {
        let mut file_id = [0u8; FILE_ID_LEN];
        OsRng
            .try_fill_bytes(&mut file_id)
            .map_err(|e| anyhow!("reading OS randomness: {e}"))?;
        Ok(Self {
            key_id: key.id,
            file_id,
        })
    }

    fn encode(&self) -> [u8; HEADER_LEN] {
        let mut out = [0u8; HEADER_LEN];
        out[..MAGIC.len()].copy_from_slice(MAGIC);
        out[MAGIC.len()..MAGIC.len() + KEY_ID_LEN].copy_from_slice(&self.key_id.0);
        out[MAGIC.len() + KEY_ID_LEN..].copy_from_slice(&self.file_id);
        out
    }

    fn decode(bytes: &[u8]) -> Option<Self> {
        if bytes.len() < HEADER_LEN || &bytes[..MAGIC.len()] != MAGIC {
            return None;
        }
        let mut key_id = [0u8; KEY_ID_LEN];
        key_id.copy_from_slice(&bytes[MAGIC.len()..MAGIC.len() + KEY_ID_LEN]);
        let mut file_id = [0u8; FILE_ID_LEN];
        file_id.copy_from_slice(&bytes[MAGIC.len() + KEY_ID_LEN..HEADER_LEN]);
        Some(Self {
            key_id: SealKeyId(key_id),
            file_id,
        })
    }

    fn nonce(&self, chunk: u64) -> XNonce {
        let mut nonce = [0u8; FILE_ID_LEN + 8];
        nonce[..FILE_ID_LEN].copy_from_slice(&self.file_id);
        nonce[FILE_ID_LEN..].copy_from_slice(&chunk.to_be_bytes());
        nonce.into()
    }

    fn aad(&self, name: &str, chunk: u64, last: bool) -> Vec<u8> {
        let mut aad = Vec::with_capacity(AAD_DOMAIN.len() + HEADER_LEN + name.len() + 13);
        aad.extend_from_slice(AAD_DOMAIN);
        aad.extend_from_slice(&self.encode());
        aad.extend_from_slice(&(name.len() as u32).to_be_bytes());
        aad.extend_from_slice(name.as_bytes());
        aad.extend_from_slice(&chunk.to_be_bytes());
        aad.push(u8::from(last));
        aad
    }
}

/// Where each chunk lies in a sealed file of `file_len` bytes, and how much
/// plaintext it holds; None when no sealed file has that length.
#[derive(Clone, Copy)]
struct Layout {
    chunks: u64,
    last_len: usize,
}

impl Layout {
    fn of(file_len: u64) -> Option<Self> {
        let body = file_len.checked_sub(HEADER_LEN as u64)?;
        if body < TAG_LEN as u64 {
            return None;
        }
        let chunks = body.div_ceil(SEALED_CHUNK_LEN as u64).max(1);
        let last_sealed = body - (chunks - 1) * SEALED_CHUNK_LEN as u64;
        let last_len = usize::try_from(last_sealed).ok()?.checked_sub(TAG_LEN)?;
        if chunks > 1 && last_len == 0 {
            return None;
        }
        Some(Self { chunks, last_len })
    }

    fn plaintext_len(&self) -> u64 {
        (self.chunks - 1) * CHUNK_LEN as u64 + self.last_len as u64
    }

    fn chunk_len(&self, chunk: u64) -> usize {
        if chunk + 1 == self.chunks {
            self.last_len
        } else {
            CHUNK_LEN
        }
    }

    fn offset(chunk: u64) -> u64 {
        HEADER_LEN as u64 + chunk * SEALED_CHUNK_LEN as u64
    }
}

fn seal_chunk(
    key: &SealKey,
    header: &Header,
    name: &str,
    chunk: u64,
    last: bool,
    plaintext: &[u8],
) -> Result<Vec<u8>> {
    let aad = header.aad(name, chunk, last);
    key.cipher
        .encrypt(
            &header.nonce(chunk),
            Payload {
                msg: plaintext,
                aad: &aad,
            },
        )
        .map_err(|_| anyhow!("sealing chunk {chunk} of {name}"))
}

fn open_chunk(
    key: &SealKey,
    header: &Header,
    name: &str,
    chunk: u64,
    last: bool,
    sealed: &[u8],
) -> Result<Vec<u8>> {
    let aad = header.aad(name, chunk, last);
    key.cipher
        .decrypt(
            &header.nonce(chunk),
            Payload {
                msg: sealed,
                aad: &aad,
            },
        )
        .map_err(|_| {
            anyhow::Error::new(SealError::Damaged)
                .context(format!("opening chunk {chunk} of {name}"))
        })
}

fn key_for<'a>(keys: &'a SealKeys, header: &Header) -> Result<&'a SealKey> {
    keys.find(header.key_id)
        .ok_or_else(|| SealError::UnknownKey(header.key_id).into())
}

/// A read that ended early says something about the file; any other I/O
/// error says nothing about it and is kept as it is.
fn short_read_as(error: io::Error, cause: SealError) -> anyhow::Error {
    if error.kind() == ErrorKind::UnexpectedEof {
        cause.into()
    } else {
        error.into()
    }
}

/// Seals `plaintext` whole, as `write_file` would write it.
pub fn seal(keys: &SealKeys, name: &str, plaintext: &[u8]) -> Result<Vec<u8>> {
    let mut writer = SealWriter::new(Vec::new(), keys, name)?;
    writer.write_all(plaintext)?;
    writer.finish()
}

/// Opens bytes `seal` produced under any of `keys`.
pub fn open(keys: &SealKeys, name: &str, sealed: &[u8]) -> Result<Vec<u8>> {
    let header = Header::decode(sealed).ok_or(SealError::NotSealed)?;
    let key = key_for(keys, &header)?;
    let layout = Layout::of(sealed.len() as u64).ok_or(SealError::Damaged)?;
    let mut out = Vec::with_capacity(usize::try_from(layout.plaintext_len())?);
    for chunk in 0..layout.chunks {
        let start = usize::try_from(Layout::offset(chunk))?;
        let end = start + layout.chunk_len(chunk) + TAG_LEN;
        let last = chunk + 1 == layout.chunks;
        out.extend_from_slice(&open_chunk(
            key,
            &header,
            name,
            chunk,
            last,
            &sealed[start..end],
        )?);
    }
    Ok(out)
}

/// True when `bytes` begin like a sealed file.
pub fn looks_sealed(bytes: &[u8]) -> bool {
    Header::decode(bytes).is_some()
}

/// The id of the key `bytes` were sealed under; None when they are not
/// sealed.
pub fn key_id_of(bytes: &[u8]) -> Option<SealKeyId> {
    Header::decode(bytes).map(|h| h.key_id)
}

/// The id of the key the file at `path` is sealed under; None for a file
/// that is not sealed. A missing file is an error.
pub fn sealed_key_id(path: &Path) -> Result<Option<SealKeyId>> {
    let mut file = File::open(path).with_context(|| format!("opening {}", path.display()))?;
    let mut head = [0u8; HEADER_LEN];
    match file.read_exact(&mut head) {
        Ok(()) => Ok(Header::decode(&head).map(|h| h.key_id)),
        Err(e) if e.kind() == ErrorKind::UnexpectedEof => Ok(None),
        Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
    }
}

/// Reads the whole file at `path`: opened under `keys` when sealed, as-is
/// when it is plaintext and `keys` is None. A sealed file without keys is a
/// `KeyRequired` error; a plaintext file with keys is read as-is, since that
/// is how files written before sealing are adopted.
pub fn read_file(path: &Path, keys: Option<&SealKeys>, name: &str) -> Result<Vec<u8>> {
    let mut file = File::open(path).with_context(|| format!("opening {}", path.display()))?;
    let mut head = [0u8; HEADER_LEN];
    let sealed = match file.read_exact_at(&mut head, 0) {
        Ok(()) => Header::decode(&head).is_some(),
        Err(e) if e.kind() == ErrorKind::UnexpectedEof => false,
        Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())),
    };
    match keys {
        Some(keys) if sealed => SealedReader::from_file(file, path, keys, name)?.read_all(),
        None if sealed => Err(anyhow::Error::new(SealError::KeyRequired)
            .context(format!("opening {}", path.display()))),
        _ => {
            let mut plaintext = Vec::new();
            file.read_to_end(&mut plaintext)
                .with_context(|| format!("reading {}", path.display()))?;
            Ok(plaintext)
        }
    }
}

/// Writes `plaintext` to `path` sealed under the current key of `keys`, or
/// as-is when `keys` is None, through a synced temporary file renamed into
/// place, so a reader sees the old file or the new one whole.
pub fn write_file(
    path: &Path,
    keys: Option<&SealKeys>,
    name: &str,
    plaintext: &[u8],
) -> Result<()> {
    let (tmp_path, file) = create_tmp(path)?;
    let result = (|| {
        let mut file = file;
        match keys {
            Some(keys) => {
                let mut writer = SealWriter::new(io::BufWriter::new(&mut file), keys, name)?;
                writer.write_all(plaintext)?;
                writer.finish()?.flush()?;
            }
            None => file.write_all(plaintext)?,
        }
        file.sync_all()?;
        Ok::<_, anyhow::Error>(())
    })();
    finish_tmp(result, &tmp_path, path)
}

/// Rewrites the file at `path` under the current key of `keys` unless it is
/// sealed under it already: a plaintext file is sealed, and one sealed
/// under a retired key is opened and sealed again, a chunk at a time.
/// True when the file was rewritten.
pub fn reseal_file(path: &Path, keys: &SealKeys, name: &str) -> Result<bool> {
    let sealed_under = sealed_key_id(path)?;
    if sealed_under == Some(keys.current_id()) {
        return Ok(false);
    }
    let (tmp_path, file) = create_tmp(path)?;
    let result = (|| {
        let mut file = file;
        {
            let mut writer = SealWriter::new(io::BufWriter::new(&mut file), keys, name)?;
            match sealed_under {
                Some(_) => {
                    let reader = SealedReader::open(path, keys, name)?;
                    for chunk in 0..reader.layout.chunks {
                        writer.write_all(&reader.read_chunk(chunk)?)?;
                    }
                }
                None => {
                    let mut source = BufReader::new(
                        File::open(path).with_context(|| format!("opening {}", path.display()))?,
                    );
                    io::copy(&mut source, &mut writer)?;
                }
            }
            writer.finish()?.flush()?;
        }
        file.sync_all()?;
        Ok::<_, anyhow::Error>(())
    })();
    finish_tmp(result, &tmp_path, path)?;
    Ok(true)
}

/// True for the temporary files `write_file` and `reseal_file` write,
/// which a crash can leave behind.
pub fn is_tmp_name(name: &str) -> bool {
    name.contains(TMP_MARKER)
}

fn create_tmp(path: &Path) -> Result<(PathBuf, File)> {
    let mut suffix = [0u8; 8];
    OsRng
        .try_fill_bytes(&mut suffix)
        .map_err(|e| anyhow!("reading OS randomness: {e}"))?;
    let file_name = path
        .file_name()
        .and_then(|n| n.to_str())
        .ok_or_else(|| anyhow!("{} has no utf-8 file name", path.display()))?;
    let tmp_path = path.with_file_name(format!(
        "{file_name}{TMP_MARKER}{}",
        crate::keys::hex(&suffix)
    ));
    let file = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&tmp_path)
        .with_context(|| format!("creating {}", tmp_path.display()))?;
    Ok((tmp_path, file))
}

fn finish_tmp(result: Result<()>, tmp_path: &Path, path: &Path) -> Result<()> {
    let renamed = result.and_then(|()| {
        fs::rename(tmp_path, path)
            .with_context(|| format!("renaming {} to {}", tmp_path.display(), path.display()))
    });
    if renamed.is_err()
        && let Err(e) = fs::remove_file(tmp_path)
        && e.kind() != ErrorKind::NotFound
    {
        tracing::warn!("cannot remove {}: {e}", tmp_path.display());
    }
    renamed?;
    if let Some(parent) = path.parent() {
        File::open(parent)
            .and_then(|dir| dir.sync_all())
            .with_context(|| format!("syncing {}", parent.display()))?;
    }
    Ok(())
}

/// Seals what is written to it in chunks as they fill. A chunk is sealed
/// only once a byte beyond it arrives, so the last chunk, the one marked as
/// last, is never empty unless nothing was written at all.
pub struct SealWriter<W: Write> {
    inner: Option<W>,
    key: Arc<SealKey>,
    header: Header,
    name: String,
    chunk: u64,
    buf: Vec<u8>,
}

impl<W: Write> SealWriter<W> {
    pub fn new(mut inner: W, keys: &SealKeys, name: &str) -> Result<Self> {
        let key = keys.current.clone();
        let header = Header::fresh(&key)?;
        inner.write_all(&header.encode())?;
        Ok(Self {
            inner: Some(inner),
            key,
            header,
            name: name.to_string(),
            chunk: 0,
            buf: Vec::with_capacity(CHUNK_LEN + 1),
        })
    }

    fn inner(&mut self) -> io::Result<&mut W> {
        self.inner
            .as_mut()
            .ok_or_else(|| io::Error::other("the sealed file was already finished"))
    }

    fn emit(&mut self, last: bool) -> io::Result<()> {
        let take = self.buf.len().min(CHUNK_LEN);
        let sealed = seal_chunk(
            &self.key,
            &self.header,
            &self.name,
            self.chunk,
            last,
            &self.buf[..take],
        )
        .map_err(io::Error::other)?;
        self.inner()?.write_all(&sealed)?;
        self.buf.drain(..take);
        self.chunk += 1;
        Ok(())
    }

    /// Seals the last chunk and hands back the writer underneath.
    pub fn finish(mut self) -> Result<W> {
        self.finish_ref()?;
        self.inner
            .take()
            .ok_or_else(|| anyhow!("the sealed file was already finished"))
    }

    fn finish_ref(&mut self) -> io::Result<()> {
        while self.buf.len() > CHUNK_LEN {
            self.emit(false)?;
        }
        self.emit(true)?;
        self.inner()?.flush()
    }
}

impl<W: Write> Write for SealWriter<W> {
    fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        self.inner()?;
        let mut rest = data;
        while !rest.is_empty() {
            if self.buf.len() > CHUNK_LEN {
                self.emit(false)?;
            }
            let room = CHUNK_LEN + 1 - self.buf.len();
            let take = rest.len().min(room);
            self.buf.extend_from_slice(&rest[..take]);
            rest = &rest[take..];
        }
        Ok(data.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner()?.flush()
    }
}

/// Reads a sealed file a chunk at a time, at any offset.
pub struct SealedReader {
    file: File,
    key: Arc<SealKey>,
    header: Header,
    layout: Layout,
    name: String,
}

impl fmt::Debug for SealedReader {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SealedReader({})", self.name)
    }
}

impl SealedReader {
    pub fn open(path: &Path, keys: &SealKeys, name: &str) -> Result<Self> {
        let file = File::open(path).with_context(|| format!("opening {}", path.display()))?;
        Self::from_file(file, path, keys, name)
    }

    fn from_file(file: File, path: &Path, keys: &SealKeys, name: &str) -> Result<Self> {
        let mut head = [0u8; HEADER_LEN];
        file.read_exact_at(&mut head, 0)
            .map_err(|e| short_read_as(e, SealError::NotSealed))
            .with_context(|| format!("reading {}", path.display()))?;
        let header = Header::decode(&head)
            .ok_or(SealError::NotSealed)
            .with_context(|| format!("reading {}", path.display()))?;
        let key = std::iter::once(&keys.current)
            .chain(keys.retired.iter())
            .find(|k| k.id == header.key_id)
            .cloned()
            .ok_or(SealError::UnknownKey(header.key_id))
            .with_context(|| format!("opening {}", path.display()))?;
        let file_len = file.metadata()?.len();
        let layout = Layout::of(file_len)
            .ok_or(SealError::Damaged)
            .with_context(|| format!("opening {}", path.display()))?;
        Ok(Self {
            file,
            key,
            header,
            layout,
            name: name.to_string(),
        })
    }

    pub fn len(&self) -> u64 {
        self.layout.plaintext_len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn key_id(&self) -> SealKeyId {
        self.header.key_id
    }

    /// Unique to this written version of the file.
    pub fn file_id(&self) -> u128 {
        u128::from_be_bytes(self.header.file_id)
    }

    pub fn chunks(&self) -> u64 {
        self.layout.chunks
    }

    pub fn read_chunk(&self, chunk: u64) -> Result<Vec<u8>> {
        anyhow::ensure!(
            chunk < self.layout.chunks,
            "{} has no chunk {chunk}",
            self.name
        );
        let mut sealed = vec![0u8; self.layout.chunk_len(chunk) + TAG_LEN];
        self.file
            .read_exact_at(&mut sealed, Layout::offset(chunk))
            .map_err(|e| short_read_as(e, SealError::Damaged))
            .with_context(|| format!("reading chunk {chunk} of {}", self.name))?;
        let last = chunk + 1 == self.layout.chunks;
        open_chunk(&self.key, &self.header, &self.name, chunk, last, &sealed)
    }

    pub fn read_all(&self) -> Result<Vec<u8>> {
        let mut out = Vec::with_capacity(usize::try_from(self.len())?);
        for chunk in 0..self.layout.chunks {
            out.extend_from_slice(&self.read_chunk(chunk)?);
        }
        Ok(out)
    }
}

#[cfg(test)]
mod tests;