incpush 0.1.3

Incrementally back up a big local folder to object storage (GCS/S3/Azure), uploading only changed files and preserving previous versions
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
//! Front-Coded Manifest (FCM): a compact, sorted, on-disk manifest format.
//!
//! Records are sorted by path and packed into fixed-size blocks. Within a block,
//! each path is **front-coded** against the previous one (store shared-prefix
//! length + suffix), then the whole block is zstd-compressed. A footer index
//! holds the first path + offset of each block for `O(log n)` point seeks.
//!
//! This exploits the massive prefix redundancy of a backup tree (every file in a
//! directory repeats that directory's path). It pairs naturally with a
//! merge-join diff ([`merge_diff`]): both sides stream in sorted order, so the
//! whole compare is one linear pass that also yields deletions for free.
//!
//! Layout:
//! ```text
//! [32B header] magic "FCMANIF1" | block_size:u32 | record_count:u64
//!              | block_count:u32 | index_offset:u64
//! [blocks]     repeated: comp_len:u32 | zstd(block_body)
//! [index]      repeated: block_offset:u64 | comp_len:u32 | first_path_len:u32 | first_path
//! ```
//! Each block resets front-coding to empty, so blocks decode independently.

use anyhow::{Context, Result, bail};
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

const MAGIC: &[u8; 8] = b"FCMANIF1";
const HEADER_LEN: u64 = 8 + 4 + 8 + 4 + 8;
pub const DEFAULT_BLOCK_SIZE: usize = 1024;

/// One manifest entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Record {
    pub path: String,
    pub size: i64,
    pub mtime: i64,
    pub crc32c: Option<u32>,
}

impl Record {
    /// A deletion marker for `path` (used in LSM segments to hide an older entry).
    /// Encoded as a negative size, which real files never have.
    pub fn tombstone(path: String) -> Record {
        Record {
            path,
            size: -1,
            mtime: -1,
            crc32c: None,
        }
    }

    pub fn is_tombstone(&self) -> bool {
        self.size < 0
    }
}

/// Canonical manifest path order: byte-lexicographic, except the `/` separator
/// sorts *before* every other byte. This is the order a depth-first walk that
/// sorts each directory's entries by raw name naturally emits — so the walk needs
/// no separate global sort. Filenames can't contain `/` or NUL, so mapping the
/// separator to 0 is unambiguous.
pub fn path_cmp(a: &str, b: &str) -> std::cmp::Ordering {
    #[inline]
    fn key(byte: u8) -> u8 {
        if byte == b'/' { 0 } else { byte }
    }
    let (a, b) = (a.as_bytes(), b.as_bytes());
    let n = a.len().min(b.len());
    for i in 0..n {
        let (ka, kb) = (key(a[i]), key(b[i]));
        if ka != kb {
            return ka.cmp(&kb);
        }
    }
    a.len().cmp(&b.len())
}

// --- varint (LEB128, unsigned) -------------------------------------------------

fn put_uvarint(out: &mut Vec<u8>, mut v: u64) {
    loop {
        let b = (v & 0x7f) as u8;
        v >>= 7;
        if v != 0 {
            out.push(b | 0x80);
        } else {
            out.push(b);
            break;
        }
    }
}

fn get_uvarint(buf: &[u8], pos: &mut usize) -> Result<u64> {
    let mut v = 0u64;
    let mut shift = 0;
    loop {
        let b = *buf.get(*pos).context("truncated varint")?;
        *pos += 1;
        v |= ((b & 0x7f) as u64) << shift;
        if b & 0x80 == 0 {
            break;
        }
        shift += 7;
        if shift >= 64 {
            bail!("varint overflow");
        }
    }
    Ok(v)
}

fn common_prefix(a: &[u8], b: &[u8]) -> usize {
    let n = a.len().min(b.len());
    let mut i = 0;
    while i < n && a[i] == b[i] {
        i += 1;
    }
    i
}

fn encode_block(records: &[Record]) -> Vec<u8> {
    let mut out = Vec::new();
    let mut prev: &[u8] = b"";
    for r in records {
        let p = r.path.as_bytes();
        let lcp = common_prefix(prev, p);
        let suf = &p[lcp..];
        put_uvarint(&mut out, lcp as u64);
        put_uvarint(&mut out, suf.len() as u64);
        out.extend_from_slice(suf);
        put_uvarint(&mut out, r.size as u64);
        put_uvarint(&mut out, r.mtime as u64);
        put_uvarint(&mut out, r.crc32c.map_or(0, |c| c as u64 + 1));
        prev = p;
    }
    out
}

fn decode_block(buf: &[u8], out: &mut Vec<Record>) -> Result<()> {
    let mut pos = 0;
    let mut prev: Vec<u8> = Vec::new();
    while pos < buf.len() {
        let lcp = get_uvarint(buf, &mut pos)? as usize;
        let sl = get_uvarint(buf, &mut pos)? as usize;
        if lcp > prev.len() || pos + sl > buf.len() {
            bail!("corrupt block");
        }
        let mut path = Vec::with_capacity(lcp + sl);
        path.extend_from_slice(&prev[..lcp]);
        path.extend_from_slice(&buf[pos..pos + sl]);
        pos += sl;
        let size = get_uvarint(buf, &mut pos)? as i64;
        let mtime = get_uvarint(buf, &mut pos)? as i64;
        let crcv = get_uvarint(buf, &mut pos)?;
        let crc32c = if crcv == 0 {
            None
        } else {
            Some((crcv - 1) as u32)
        };
        prev = path.clone();
        out.push(Record {
            path: String::from_utf8(path).context("non-utf8 path in block")?,
            size,
            mtime,
            crc32c,
        });
    }
    Ok(())
}

/// Streaming writer. Push records **in sorted path order**; blocks are encoded
/// and flushed as they fill, so peak memory is `O(block_size)` regardless of how
/// many records are written. Call [`FcmWriter::finish`] to write the footer +
/// header and return the file size.
pub struct FcmWriter {
    path: PathBuf,
    w: BufWriter<File>,
    block_size: usize,
    level: i32,
    buf: Vec<Record>,
    index: Vec<(u64, u32, String)>,
    offset: u64,
    record_count: u64,
    block_count: u32,
    last_path: Option<String>,
}

impl FcmWriter {
    pub fn create(path: &Path, block_size: usize, level: i32) -> Result<Self> {
        let f = File::create(path).with_context(|| format!("creating {}", path.display()))?;
        let mut w = BufWriter::new(f);
        // Placeholder header; rewritten by finish() once offsets are known.
        w.write_all(&[0u8; HEADER_LEN as usize])?;
        Ok(Self {
            path: path.to_path_buf(),
            w,
            block_size: block_size.max(1),
            level,
            buf: Vec::with_capacity(block_size.max(1)),
            index: Vec::new(),
            offset: HEADER_LEN,
            record_count: 0,
            block_count: 0,
            last_path: None,
        })
    }

    /// Append one record. Records must arrive in non-decreasing path order.
    pub fn push(&mut self, rec: Record) -> Result<()> {
        debug_assert!(
            self.last_path
                .as_deref()
                .is_none_or(|p| path_cmp(p, rec.path.as_str()) != std::cmp::Ordering::Greater),
            "FcmWriter requires sorted input"
        );
        self.last_path = Some(rec.path.clone());
        self.buf.push(rec);
        if self.buf.len() >= self.block_size {
            self.flush_block()?;
        }
        Ok(())
    }

    fn flush_block(&mut self) -> Result<()> {
        if self.buf.is_empty() {
            return Ok(());
        }
        let body = encode_block(&self.buf);
        let comp = zstd::encode_all(&body[..], self.level).context("zstd block")?;
        let comp_len = comp.len() as u32;
        self.w.write_all(&comp_len.to_le_bytes())?;
        self.w.write_all(&comp)?;
        self.index
            .push((self.offset, comp_len, self.buf[0].path.clone()));
        self.offset += 4 + comp.len() as u64;
        self.record_count += self.buf.len() as u64;
        self.block_count += 1;
        self.buf.clear();
        Ok(())
    }

    /// Flush the final block, write the footer index + header, return file size.
    pub fn finish(mut self) -> Result<u64> {
        self.flush_block()?;
        let index_offset = self.offset;
        for (boff, clen, first) in &self.index {
            self.w.write_all(&boff.to_le_bytes())?;
            self.w.write_all(&clen.to_le_bytes())?;
            let fb = first.as_bytes();
            self.w.write_all(&(fb.len() as u32).to_le_bytes())?;
            self.w.write_all(fb)?;
        }
        self.w.flush()?;
        let mut file = self.w.into_inner()?;
        file.seek(SeekFrom::Start(0))?;
        let mut header = Vec::with_capacity(HEADER_LEN as usize);
        header.extend_from_slice(MAGIC);
        header.extend_from_slice(&(self.block_size as u32).to_le_bytes());
        header.extend_from_slice(&self.record_count.to_le_bytes());
        header.extend_from_slice(&self.block_count.to_le_bytes());
        header.extend_from_slice(&index_offset.to_le_bytes());
        file.write_all(&header)?;
        file.flush()?;
        Ok(std::fs::metadata(&self.path)?.len())
    }
}

/// Convenience: sort `records` by path and write them via [`FcmWriter`].
/// (For unbounded input, use `FcmWriter` directly with a sorted stream.)
pub fn write(path: &Path, records: &mut [Record], block_size: usize, level: i32) -> Result<u64> {
    records.sort_unstable_by(|a, b| path_cmp(&a.path, &b.path));
    let mut w = FcmWriter::create(path, block_size, level)?;
    for r in records.iter() {
        w.push(r.clone())?;
    }
    w.finish()
}

/// A handle to an FCM file. Holds only the header and the sparse block index in
/// memory (≈ `first_path` per block, ~one entry per `block_size` records) — never
/// the whole file. Iteration streams blocks from disk; point lookups seek.
pub struct Fcm {
    path: PathBuf,
    block_size: usize,
    record_count: u64,
    block_count: u32,
    /// (block_offset, comp_len, first_path) per block.
    index: Vec<(u64, u32, String)>,
}

impl Fcm {
    pub fn open(path: &Path) -> Result<Self> {
        let mut f = File::open(path).with_context(|| format!("opening {}", path.display()))?;
        let mut header = [0u8; HEADER_LEN as usize];
        f.read_exact(&mut header)
            .with_context(|| format!("reading FCM header of {}", path.display()))?;
        if &header[..8] != MAGIC {
            bail!("not an FCM file: {}", path.display());
        }
        let block_size = u32::from_le_bytes(header[8..12].try_into().unwrap()) as usize;
        let record_count = u64::from_le_bytes(header[12..20].try_into().unwrap());
        let block_count = u32::from_le_bytes(header[20..24].try_into().unwrap());
        let index_offset = u64::from_le_bytes(header[24..32].try_into().unwrap());

        // Read only the footer index region.
        f.seek(SeekFrom::Start(index_offset))?;
        let mut r = BufReader::new(f);
        let mut index = Vec::with_capacity(block_count as usize);
        for _ in 0..block_count {
            let boff = read_u64(&mut r)?;
            let clen = read_u32(&mut r)?;
            let flen = read_u32(&mut r)? as usize;
            let mut fb = vec![0u8; flen];
            r.read_exact(&mut fb)?;
            let first = String::from_utf8(fb).context("non-utf8 first_path in index")?;
            index.push((boff, clen, first));
        }
        Ok(Self {
            path: path.to_path_buf(),
            block_size,
            record_count,
            block_count,
            index,
        })
    }

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

    pub fn block_size(&self) -> usize {
        self.block_size
    }

    pub fn block_count(&self) -> u32 {
        self.block_count
    }

    /// Stream all records in sorted (path) order, decoding one block at a time
    /// from disk. Peak memory is `O(block_size)`.
    pub fn iter(&self) -> Result<FcmReader> {
        let mut f = File::open(&self.path)?;
        f.seek(SeekFrom::Start(HEADER_LEN))?;
        Ok(FcmReader {
            r: BufReader::new(f),
            remaining_blocks: self.block_count,
            block_size: self.block_size,
            buf: Vec::new(),
            idx: 0,
        })
    }

    /// Point lookup via the sparse index: binary-search for the candidate block,
    /// seek to it, decode, and scan. Reads a single block off disk.
    pub fn lookup(&self, path: &str) -> Result<Option<Record>> {
        if self.index.is_empty() {
            return Ok(None);
        }
        // Largest block whose first_path <= path (in canonical order).
        let mut lo = 0usize;
        let mut hi = self.index.len();
        while lo < hi {
            let mid = (lo + hi) / 2;
            if path_cmp(self.index[mid].2.as_str(), path) != std::cmp::Ordering::Greater {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        if lo == 0 {
            return Ok(None); // path precedes the first key
        }
        let (boff, clen, _) = &self.index[lo - 1];
        let mut f = File::open(&self.path)?;
        f.seek(SeekFrom::Start(boff + 4))?; // skip the u32 length prefix
        let mut comp = vec![0u8; *clen as usize];
        f.read_exact(&mut comp)?;
        let body = zstd::decode_all(&comp[..])?;
        let mut recs = Vec::with_capacity(self.block_size);
        decode_block(&body, &mut recs)?;
        Ok(recs.into_iter().find(|r| r.path == path))
    }
}

fn read_u32(r: &mut impl Read) -> Result<u32> {
    let mut b = [0u8; 4];
    r.read_exact(&mut b)?;
    Ok(u32::from_le_bytes(b))
}
fn read_u64(r: &mut impl Read) -> Result<u64> {
    let mut b = [0u8; 8];
    r.read_exact(&mut b)?;
    Ok(u64::from_le_bytes(b))
}

/// Streaming record iterator: reads and decodes one block at a time from disk.
pub struct FcmReader {
    r: BufReader<File>,
    remaining_blocks: u32,
    block_size: usize,
    buf: Vec<Record>,
    idx: usize,
}

impl FcmReader {
    fn load_next_block(&mut self) -> Result<bool> {
        if self.remaining_blocks == 0 {
            return Ok(false);
        }
        let comp_len = read_u32(&mut self.r)? as usize;
        let mut comp = vec![0u8; comp_len];
        self.r.read_exact(&mut comp)?;
        let body = zstd::decode_all(&comp[..])?;
        self.buf.clear();
        self.buf.reserve(self.block_size);
        decode_block(&body, &mut self.buf)?;
        self.idx = 0;
        self.remaining_blocks -= 1;
        Ok(true)
    }
}

impl Iterator for FcmReader {
    type Item = Result<Record>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx >= self.buf.len() {
            match self.load_next_block() {
                Ok(false) => return None,
                Ok(true) => {}
                Err(e) => {
                    self.remaining_blocks = 0;
                    return Some(Err(e));
                }
            }
        }
        let r = self.buf[self.idx].clone();
        self.idx += 1;
        Some(Ok(r))
    }
}

/// Result of a merge-join diff.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DiffCounts {
    pub new: u64,
    pub changed: u64,
    pub unchanged: u64,
    pub deleted: u64,
}

/// Merge-join two **sorted-by-path** streams of `(path, size, mtime)`:
/// `manifest` (previous state) and `walk` (current disk state). One linear pass
/// classifies every path as new / changed / unchanged / deleted.
pub fn merge_diff<M, W>(mut manifest: M, mut walk: W) -> Result<DiffCounts>
where
    M: Iterator<Item = Result<(String, i64, i64)>>,
    W: Iterator<Item = Result<(String, i64, i64)>>,
{
    let mut c = DiffCounts::default();
    let mut m = manifest.next().transpose()?;
    let mut w = walk.next().transpose()?;
    loop {
        match (&m, &w) {
            (None, None) => break,
            (Some(_), None) => {
                c.deleted += 1;
                m = manifest.next().transpose()?;
            }
            (None, Some(_)) => {
                c.new += 1;
                w = walk.next().transpose()?;
            }
            (Some((mp, msz, mmt)), Some((wp, wsz, wmt))) => match mp.as_str().cmp(wp.as_str()) {
                std::cmp::Ordering::Less => {
                    c.deleted += 1;
                    m = manifest.next().transpose()?;
                }
                std::cmp::Ordering::Greater => {
                    c.new += 1;
                    w = walk.next().transpose()?;
                }
                std::cmp::Ordering::Equal => {
                    if msz != wsz || mmt != wmt {
                        c.changed += 1;
                    } else {
                        c.unchanged += 1;
                    }
                    m = manifest.next().transpose()?;
                    w = walk.next().transpose()?;
                }
            },
        }
    }
    Ok(c)
}

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

    fn rec(p: &str, size: i64, mtime: i64, crc: Option<u32>) -> Record {
        Record {
            path: p.into(),
            size,
            mtime,
            crc32c: crc,
        }
    }

    #[test]
    fn write_read_roundtrip_sorted() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("m.fcm");
        let mut recs = vec![
            rec("photos/2024/b.jpg", 20, 200, Some(2)),
            rec("photos/2024/a.jpg", 10, 100, None),
            rec("docs/readme.txt", 5, 50, Some(9)),
            rec("photos/2023/z.png", 30, 300, Some(7)),
        ];
        // small block size to exercise multi-block + index
        write(&file, &mut recs, 2, 3).unwrap();

        let fcm = Fcm::open(&file).unwrap();
        assert_eq!(fcm.record_count(), 4);
        let got: Vec<Record> = fcm.iter().unwrap().map(|r| r.unwrap()).collect();
        // Must come back in sorted path order.
        let mut expect = recs.clone();
        expect.sort_by(|a, b| path_cmp(&a.path, &b.path));
        assert_eq!(got, expect);
    }

    #[test]
    fn empty_fcm_roundtrips() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("empty.fcm");
        write(&file, &mut [], 16, 3).unwrap();
        let fcm = Fcm::open(&file).unwrap();
        assert_eq!(fcm.record_count(), 0);
        assert_eq!(fcm.iter().unwrap().count(), 0);
        assert_eq!(fcm.lookup("anything").unwrap(), None);
    }

    #[test]
    fn point_lookup_via_index() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("m.fcm");
        let mut recs = vec![
            rec("a/1", 1, 1, None),
            rec("a/2", 2, 2, Some(2)),
            rec("b/1", 3, 3, None),
            rec("c/9", 9, 9, Some(4)),
            rec("c/z", 26, 26, None),
        ];
        write(&file, &mut recs, 2, 3).unwrap();
        let fcm = Fcm::open(&file).unwrap();

        assert_eq!(fcm.lookup("b/1").unwrap(), Some(rec("b/1", 3, 3, None)));
        assert_eq!(fcm.lookup("c/z").unwrap(), Some(rec("c/z", 26, 26, None)));
        assert_eq!(fcm.lookup("a/1").unwrap(), Some(rec("a/1", 1, 1, None)));
        assert_eq!(fcm.lookup("missing").unwrap(), None);
        assert_eq!(fcm.lookup("a/0").unwrap(), None); // before everything in-range
    }

    #[test]
    fn front_coding_handles_long_shared_prefixes() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("m.fcm");
        let base = "very/deep/nested/directory/structure/that/repeats/file_";
        let mut recs: Vec<Record> = (0..100)
            .map(|i| rec(&format!("{base}{i:03}.dat"), i, i * 10, Some(i as u32)))
            .collect();
        write(&file, &mut recs, 16, 3).unwrap();
        let fcm = Fcm::open(&file).unwrap();
        let got: Vec<Record> = fcm.iter().unwrap().map(|r| r.unwrap()).collect();
        recs.sort_by(|a, b| path_cmp(&a.path, &b.path));
        assert_eq!(got, recs);
    }

    #[test]
    fn path_cmp_orders_separator_first() {
        use std::cmp::Ordering;
        // "a/x" sorts before "a.txt" because '/' is treated as less than '.'.
        assert_eq!(path_cmp("a/x", "a.txt"), Ordering::Less);
        assert_eq!(path_cmp("a", "a/x"), Ordering::Less);
        assert_eq!(path_cmp("a/b", "a/b"), Ordering::Equal);
        assert_eq!(path_cmp("ab", "a/x"), Ordering::Greater); // 'b' > '/'->0
    }

    #[test]
    fn merge_diff_classifies_all_cases() {
        // manifest: a, b, c, d   walk: b(changed), c(same), e(new)   -> a,d deleted
        let manifest = vec![
            Ok(("a".to_string(), 1, 1)),
            Ok(("b".to_string(), 2, 2)),
            Ok(("c".to_string(), 3, 3)),
            Ok(("d".to_string(), 4, 4)),
        ];
        let walk = vec![
            Ok(("b".to_string(), 2, 999)), // mtime changed
            Ok(("c".to_string(), 3, 3)),   // unchanged
            Ok(("e".to_string(), 5, 5)),   // new
        ];
        let counts = merge_diff(manifest.into_iter(), walk.into_iter()).unwrap();
        assert_eq!(
            counts,
            DiffCounts {
                new: 1,
                changed: 1,
                unchanged: 1,
                deleted: 2
            }
        );
    }

    #[test]
    fn merge_diff_via_fcm_reader() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("m.fcm");
        let mut recs = vec![
            rec("x/a", 1, 1, None),
            rec("x/b", 2, 2, None),
            rec("x/c", 3, 3, None),
        ];
        write(&file, &mut recs, 2, 3).unwrap();
        let fcm = Fcm::open(&file).unwrap();

        let manifest = fcm
            .iter()
            .unwrap()
            .map(|r| r.map(|x| (x.path, x.size, x.mtime)));
        let walk = vec![
            Ok(("x/a".to_string(), 1, 1)),  // unchanged
            Ok(("x/b".to_string(), 99, 2)), // size changed
            Ok(("x/d".to_string(), 4, 4)),  // new; x/c deleted
        ];
        let counts = merge_diff(manifest, walk.into_iter()).unwrap();
        assert_eq!(
            counts,
            DiffCounts {
                new: 1,
                changed: 1,
                unchanged: 1,
                deleted: 1
            }
        );
    }
}