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
//! Change detection: walk the source tree and decide, per file, whether it
//! needs uploading — comparing against the last-known manifest state. No
//! destination reads are ever performed here.

use anyhow::{Context, Result};
use ignore::WalkBuilder;
use ignore::overrides::OverrideBuilder;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH;

/// A file's last-known state, as recorded in the manifest.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileRecord {
    pub size: i64,
    /// Modification time in nanoseconds since the Unix epoch.
    pub mtime: i64,
    /// crc32c of the file contents. `None` in `mtime` diff mode.
    pub crc32c: Option<u32>,
}

/// Reserved subdirectory (both local and in the bucket) for incpush's own
/// state. Always excluded from the walk.
pub const RESERVED_DIR: &str = ".incpush";

/// How aggressively to detect changes. See the crate README for the cost trade-offs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiffMode {
    /// Compare size + mtime only. No file contents are read. Default.
    Mtime,
    /// Cheap size/mtime filter first; hash only the candidates to suppress
    /// re-uploads of touched-but-identical files.
    Hash,
    /// Hash every file. Integrity-audit use only; expensive.
    FullHash,
}

/// A file discovered on disk during the walk.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
    /// Relative, `/`-normalized path from the source root.
    pub rel_path: String,
    /// Absolute path on disk.
    pub abs_path: PathBuf,
    pub size: i64,
    /// Modification time in nanoseconds since the Unix epoch.
    pub mtime: i64,
}

/// The outcome of diffing one file against its manifest record.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Decision {
    /// Unchanged; no upload and no manifest write.
    Skip,
    /// Content identical but metadata (mtime) moved. Refresh the manifest row
    /// (so future cheap-filter runs match) but do not upload.
    Touch { crc32c: Option<u32> },
    /// New or changed; upload it. `crc32c` is stored in the manifest afterwards.
    Upload { crc32c: Option<u32> },
}

/// Decide what to do with one file. `hash` is invoked lazily and only when the
/// chosen mode actually needs the file's contents, so callers pay for I/O only
/// where a hash is required.
pub fn decide<F>(
    mode: DiffMode,
    prior: Option<&FileRecord>,
    size: i64,
    mtime: i64,
    hash: F,
) -> Result<Decision>
where
    F: FnOnce() -> Result<u32>,
{
    match mode {
        DiffMode::Mtime => Ok(match prior {
            Some(p) if p.size == size && p.mtime == mtime => Decision::Skip,
            _ => Decision::Upload { crc32c: None },
        }),

        DiffMode::Hash => {
            // Cheap filter: unchanged metadata means unchanged content — no hash.
            if let Some(p) = prior
                && p.size == size
                && p.mtime == mtime
            {
                return Ok(Decision::Skip);
            }
            // Candidate: hash to see whether the content actually differs.
            let c = hash()?;
            if let Some(p) = prior
                && p.size == size
                && p.crc32c == Some(c)
            {
                // Touched but byte-identical: refresh mtime, skip upload.
                return Ok(Decision::Touch { crc32c: Some(c) });
            }
            Ok(Decision::Upload { crc32c: Some(c) })
        }

        DiffMode::FullHash => {
            let c = hash()?;
            match prior {
                Some(p) if p.size == size && p.crc32c == Some(c) => {
                    if p.mtime == mtime {
                        Ok(Decision::Skip)
                    } else {
                        Ok(Decision::Touch { crc32c: Some(c) })
                    }
                }
                _ => Ok(Decision::Upload { crc32c: Some(c) }),
            }
        }
    }
}

/// Stream a file through crc32c without loading it fully into memory.
pub fn hash_file(path: &Path) -> Result<u32> {
    let mut f =
        std::fs::File::open(path).with_context(|| format!("opening {} to hash", path.display()))?;
    let mut buf = vec![0u8; 1 << 20]; // 1 MiB
    let mut crc = 0u32;
    loop {
        let n = f.read(&mut buf)?;
        if n == 0 {
            break;
        }
        crc = crc32c::crc32c_append(crc, &buf[..n]);
    }
    Ok(crc)
}

/// Normalize a relative path to forward slashes for stable, cross-platform keys.
fn normalize(rel: &Path) -> String {
    let s = rel.to_string_lossy();
    if std::path::MAIN_SEPARATOR == '/' {
        s.into_owned()
    } else {
        s.replace(std::path::MAIN_SEPARATOR, "/")
    }
}

/// Walk `root`, yielding a file [`Entry`] for every regular file, honoring
/// `.backupignore` files and the extra `excludes` globs, and always skipping the
/// reserved [`RESERVED_DIR`]. Errors reading individual entries are surfaced as
/// `Err` items rather than aborting the whole walk.
///
/// This is a **sorted** depth-first walk: `ignore`'s sequential walker sorts each
/// directory's entries by raw file-name bytes, which — because [`path_cmp`] treats
/// the `/` separator as sorting before every other byte — emits paths in exactly
/// the manifest's canonical order. So the diff needs no separate global sort of
/// the walk. Memory is bounded by directory size + tree depth.
pub fn walk_source(
    root: &Path,
    excludes: &[String],
    backupignore: bool,
) -> Result<impl Iterator<Item = Result<Entry>>> {
    let mut ov = OverrideBuilder::new(root);
    // Overrides use gitignore syntax; a leading `!` would *include*, so plain
    // globs here are treated as ignore patterns via the `!`-inverted whitelist.
    for pat in excludes {
        // `!pat` means "exclude pat" in override terms.
        ov.add(&format!("!{pat}"))
            .with_context(|| format!("invalid --exclude glob: {pat}"))?;
    }
    let overrides = ov.build().context("building exclude overrides")?;

    let mut builder = WalkBuilder::new(root);
    builder
        .hidden(false) // don't skip dotfiles by default
        .git_ignore(false)
        .git_global(false)
        .git_exclude(false)
        .ignore(false)
        .parents(false)
        .overrides(overrides)
        .filter_entry(|e| {
            // Never descend into or record the reserved state dir.
            e.file_name() != std::ffi::OsStr::new(RESERVED_DIR)
        })
        // Sort siblings by raw name bytes -> sorted-DFS yields canonical order.
        .sort_by_file_name(|a, b| a.as_encoded_bytes().cmp(b.as_encoded_bytes()));
    if backupignore {
        // Honoring per-directory ignore files makes the walker probe every folder
        // for `.backupignore`; only opt in when requested (see `--no-backupignore`).
        builder.add_custom_ignore_filename(".backupignore");
    }
    let walker = builder.build();

    let root = root.to_path_buf();
    Ok(walker.filter_map(move |res| {
        let dent = match res {
            Ok(d) => d,
            Err(e) => return Some(Err(anyhow::anyhow!("walk error: {e}"))),
        };
        if !dent.file_type().is_some_and(|t| t.is_file()) {
            return None;
        }
        Some(entry_from(&root, &dent))
    }))
}

fn entry_from(root: &Path, dent: &ignore::DirEntry) -> Result<Entry> {
    let abs = dent.path();
    let meta = dent
        .metadata()
        .with_context(|| format!("stat {}", abs.display()))?;
    let mtime = meta
        .modified()
        .ok()
        .and_then(|m| m.duration_since(UNIX_EPOCH).ok())
        .map(|d| d.as_nanos() as i64)
        .unwrap_or(0);
    let rel = abs.strip_prefix(root).unwrap_or(abs);
    Ok(Entry {
        rel_path: normalize(rel),
        abs_path: abs.to_path_buf(),
        size: meta.len() as i64,
        mtime,
    })
}

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

    fn rec(size: i64, mtime: i64, crc: Option<u32>) -> FileRecord {
        FileRecord {
            size,
            mtime,
            crc32c: crc,
        }
    }

    // A hasher that records whether it was invoked, so we can assert that cheap
    // modes never read file contents.
    fn tracked(value: u32) -> (impl Fn() -> Result<u32>, std::rc::Rc<Cell<bool>>) {
        let called = std::rc::Rc::new(Cell::new(false));
        let c = called.clone();
        (
            move || {
                c.set(true);
                Ok(value)
            },
            called,
        )
    }

    #[test]
    fn mtime_new_file_uploads_without_hashing() {
        let (h, called) = tracked(1);
        let d = decide(DiffMode::Mtime, None, 10, 100, || h()).unwrap();
        assert_eq!(d, Decision::Upload { crc32c: None });
        assert!(!called.get(), "mtime mode must never hash");
    }

    #[test]
    fn mtime_unchanged_skips() {
        let prior = rec(10, 100, None);
        let d = decide(DiffMode::Mtime, Some(&prior), 10, 100, || unreachable!()).unwrap();
        assert_eq!(d, Decision::Skip);
    }

    #[test]
    fn mtime_size_or_mtime_change_uploads() {
        let prior = rec(10, 100, None);
        assert_eq!(
            decide(DiffMode::Mtime, Some(&prior), 11, 100, || unreachable!()).unwrap(),
            Decision::Upload { crc32c: None }
        );
        assert_eq!(
            decide(DiffMode::Mtime, Some(&prior), 10, 101, || unreachable!()).unwrap(),
            Decision::Upload { crc32c: None }
        );
    }

    #[test]
    fn hash_skips_on_cheap_filter_without_hashing() {
        let prior = rec(10, 100, Some(42));
        let (h, called) = tracked(42);
        let d = decide(DiffMode::Hash, Some(&prior), 10, 100, || h()).unwrap();
        assert_eq!(d, Decision::Skip);
        assert!(
            !called.get(),
            "matching size+mtime must short-circuit the hash"
        );
    }

    #[test]
    fn hash_touched_identical_content_is_touch_not_upload() {
        // mtime moved but content (and size) identical -> Touch.
        let prior = rec(10, 100, Some(42));
        let (h, called) = tracked(42);
        let d = decide(DiffMode::Hash, Some(&prior), 10, 200, || h()).unwrap();
        assert_eq!(d, Decision::Touch { crc32c: Some(42) });
        assert!(called.get(), "candidate should have been hashed");
    }

    #[test]
    fn hash_real_content_change_uploads() {
        let prior = rec(10, 100, Some(42));
        let d = decide(DiffMode::Hash, Some(&prior), 10, 200, || Ok(99)).unwrap();
        assert_eq!(d, Decision::Upload { crc32c: Some(99) });
    }

    #[test]
    fn fullhash_hashes_every_file() {
        let (h, called) = tracked(7);
        let d = decide(DiffMode::FullHash, None, 10, 100, || h()).unwrap();
        assert_eq!(d, Decision::Upload { crc32c: Some(7) });
        assert!(called.get());
    }

    #[test]
    fn fullhash_identical_content_skips_or_touches() {
        let prior = rec(10, 100, Some(7));
        assert_eq!(
            decide(DiffMode::FullHash, Some(&prior), 10, 100, || Ok(7)).unwrap(),
            Decision::Skip
        );
        assert_eq!(
            decide(DiffMode::FullHash, Some(&prior), 10, 555, || Ok(7)).unwrap(),
            Decision::Touch { crc32c: Some(7) }
        );
    }

    #[test]
    fn fullhash_uploads_when_prior_has_no_crc() {
        // Prior recorded in mtime mode (crc None): cannot compare, so upload.
        let prior = rec(10, 100, None);
        let d = decide(DiffMode::FullHash, Some(&prior), 10, 100, || Ok(7)).unwrap();
        assert_eq!(d, Decision::Upload { crc32c: Some(7) });
    }

    #[test]
    fn hash_file_matches_known_crc32c() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("data.bin");
        std::fs::File::create(&p)
            .unwrap()
            .write_all(b"123456789")
            .unwrap();
        // Standard crc32c check value for "123456789".
        assert_eq!(hash_file(&p).unwrap(), 0xe306_9283);
    }

    #[test]
    fn walk_finds_files_excludes_reserved_and_globs() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        std::fs::create_dir_all(root.join("a/b")).unwrap();
        std::fs::create_dir_all(root.join(RESERVED_DIR)).unwrap();
        std::fs::write(root.join("a/b/keep.txt"), b"hi").unwrap();
        std::fs::write(root.join("a/skip.tmp"), b"x").unwrap();
        std::fs::write(root.join(RESERVED_DIR).join("manifest.db"), b"db").unwrap();

        let mut found: Vec<String> = walk_source(root, &["*.tmp".into()], true)
            .unwrap()
            .map(|r| r.unwrap().rel_path)
            .collect();
        found.sort();
        assert_eq!(found, vec!["a/b/keep.txt".to_string()]);
    }

    #[test]
    fn backupignore_honored_only_when_enabled() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        std::fs::write(root.join(".backupignore"), b"secret.txt\n").unwrap();
        std::fs::write(root.join("secret.txt"), b"x").unwrap();
        std::fs::write(root.join("keep.txt"), b"y").unwrap();

        // Enabled: the pattern in .backupignore excludes secret.txt. The
        // .backupignore file itself is still walked (like git with .gitignore,
        // an ignore file doesn't self-exclude unless it lists itself).
        let mut on: Vec<String> = walk_source(root, &[], true)
            .unwrap()
            .map(|r| r.unwrap().rel_path)
            .collect();
        on.sort();
        assert_eq!(on, vec![".backupignore".to_string(), "keep.txt".to_string()]);

        // Disabled: the file is never consulted, so secret.txt (and the
        // .backupignore file itself) are walked like any other file.
        let mut off: Vec<String> = walk_source(root, &[], false)
            .unwrap()
            .map(|r| r.unwrap().rel_path)
            .collect();
        off.sort();
        assert_eq!(
            off,
            vec![
                ".backupignore".to_string(),
                "keep.txt".to_string(),
                "secret.txt".to_string(),
            ]
        );
    }

    #[test]
    fn walk_emits_canonical_sorted_order() {
        use crate::fcm::path_cmp;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        // Names chosen so raw-string order != separator-first order:
        //   "a.txt" vs "a/x": '.' < '/' as raw bytes, but canonical puts "a/x" first.
        std::fs::create_dir_all(root.join("a")).unwrap();
        std::fs::create_dir_all(root.join("ab")).unwrap();
        std::fs::write(root.join("a.txt"), b"1").unwrap();
        std::fs::write(root.join("a/x"), b"2").unwrap();
        std::fs::write(root.join("a/y"), b"3").unwrap();
        std::fs::write(root.join("ab/z"), b"4").unwrap();
        std::fs::write(root.join("a-b"), b"5").unwrap();

        let got: Vec<String> = walk_source(root, &[], true)
            .unwrap()
            .map(|r| r.unwrap().rel_path)
            .collect();

        // The walk must already be in canonical order (no re-sorting needed).
        let mut expect = got.clone();
        expect.sort_by(|a, b| path_cmp(a, b));
        assert_eq!(got, expect, "walk output must be in canonical path order");
        // Sanity: separator-first really is in effect.
        let ax = got.iter().position(|p| p == "a/x").unwrap();
        let atxt = got.iter().position(|p| p == "a.txt").unwrap();
        assert!(ax < atxt, "'a/x' must precede 'a.txt' in canonical order");
    }
}