canic-backup 0.88.1

Manifest and orchestration primitives for Canic deployment backup and restore
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
//! Module: persistence::artifact_commit
//!
//! Responsibility: durably publish one verified snapshot artifact directory.
//! Does not own: snapshot download, journal transitions, or manifest creation.
//! Boundary: accepts journal-bound sibling paths and expected checksum bytes.

use crate::persistence::PersistenceError;

use std::path::Path;

/// Result of publishing a temporary artifact or recovering its published tree.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ArtifactCommitOutcome {
    Published,
    Recovered,
}

/// Durably publish or recover one checksum-verified snapshot directory.
pub fn commit_artifact_directory(
    temporary: &Path,
    canonical: &Path,
    expected_checksum: &str,
) -> Result<ArtifactCommitOutcome, PersistenceError> {
    #[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
    {
        supported::commit_with_hook(temporary, canonical, expected_checksum, |_, _| Ok(()))
    }

    #[cfg(not(any(target_os = "linux", target_os = "android", target_vendor = "apple")))]
    {
        let _ = (temporary, canonical, expected_checksum);
        Err(PersistenceError::ArtifactCommitUnsupportedPlatform {
            platform: std::env::consts::OS,
        })
    }
}

#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
mod supported {
    use super::ArtifactCommitOutcome;
    use crate::{artifacts::ArtifactChecksum, persistence::PersistenceError};

    use std::{
        ffi::OsStr,
        fs::{self, File},
        io::{self, Seek, SeekFrom},
        os::unix::ffi::OsStrExt,
        path::{Path, PathBuf},
    };

    use rustix::{
        fd::{AsFd, OwnedFd},
        fs::{self as unix_fs, AtFlags, Dir, FileType, Mode, OFlags, RenameFlags},
    };

    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub(super) enum ArtifactCommitStep {
        RegularFileSync,
        NestedDirectorySync,
        RootDirectorySync,
        Publication,
        ParentDirectorySync,
    }

    pub(super) fn commit_with_hook(
        temporary: &Path,
        canonical: &Path,
        expected_checksum: &str,
        mut before: impl FnMut(ArtifactCommitStep, &Path) -> io::Result<()>,
    ) -> Result<ArtifactCommitOutcome, PersistenceError> {
        let (parent, temporary_name, canonical_name) = sibling_paths(temporary, canonical)?;
        let temporary_exists = path_exists_without_following(temporary)?;
        let canonical_exists = path_exists_without_following(canonical)?;
        let parent_fd = open_parent_directory(parent)?;

        match (temporary_exists, canonical_exists) {
            (true, false) => {
                sync_tree(&parent_fd, temporary_name, temporary, false, &mut before)?;
                before(ArtifactCommitStep::Publication, canonical)?;
                unix_fs::renameat_with(
                    &parent_fd,
                    temporary_name,
                    &parent_fd,
                    canonical_name,
                    RenameFlags::NOREPLACE,
                )
                .map_err(errno_to_io)?;
                before(ArtifactCommitStep::ParentDirectorySync, parent)?;
                unix_fs::fsync(&parent_fd).map_err(errno_to_io)?;
                Ok(ArtifactCommitOutcome::Published)
            }
            (false, true) => {
                let checksum = sync_tree(&parent_fd, canonical_name, canonical, true, &mut before)?
                    .ok_or_else(|| {
                        io::Error::other("artifact recovery checksum was not collected")
                    })?;
                checksum.verify(expected_checksum)?;
                before(ArtifactCommitStep::ParentDirectorySync, parent)?;
                unix_fs::fsync(&parent_fd).map_err(errno_to_io)?;
                Ok(ArtifactCommitOutcome::Recovered)
            }
            (true, true) => Err(PersistenceError::ArtifactCommitPathConflict {
                temporary: temporary.display().to_string(),
                canonical: canonical.display().to_string(),
            }),
            (false, false) => Err(PersistenceError::ArtifactCommitPathMissing {
                temporary: temporary.display().to_string(),
                canonical: canonical.display().to_string(),
            }),
        }
    }

    fn sibling_paths<'a>(
        temporary: &'a Path,
        canonical: &'a Path,
    ) -> Result<(&'a Path, &'a OsStr, &'a OsStr), PersistenceError> {
        let invalid_paths = || PersistenceError::ArtifactCommitPathMismatch {
            temporary: temporary.display().to_string(),
            canonical: canonical.display().to_string(),
        };
        let temporary_parent = temporary.parent().ok_or_else(&invalid_paths)?;
        let canonical_parent = canonical.parent().ok_or_else(&invalid_paths)?;
        let temporary_name = temporary.file_name().ok_or_else(&invalid_paths)?;
        let canonical_name = canonical.file_name().ok_or_else(&invalid_paths)?;
        if temporary == canonical || temporary_parent != canonical_parent {
            return Err(invalid_paths());
        }

        Ok((temporary_parent, temporary_name, canonical_name))
    }

    fn path_exists_without_following(path: &Path) -> io::Result<bool> {
        match fs::symlink_metadata(path) {
            Ok(_) => Ok(true),
            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
            Err(error) => Err(error),
        }
    }

    fn open_parent_directory(path: &Path) -> io::Result<OwnedFd> {
        unix_fs::open(
            path,
            OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .map_err(errno_to_io)
    }

    fn sync_tree(
        parent_fd: &impl AsFd,
        directory_name: &OsStr,
        display_root: &Path,
        calculate_checksum: bool,
        before: &mut impl FnMut(ArtifactCommitStep, &Path) -> io::Result<()>,
    ) -> Result<Option<ArtifactChecksum>, PersistenceError> {
        let directory_fd = unix_fs::openat(
            parent_fd,
            directory_name,
            OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .map_err(errno_to_io)?;
        let mut checksums = calculate_checksum.then(Vec::new);
        sync_directory(
            &directory_fd,
            Path::new(""),
            display_root,
            &mut checksums,
            before,
        )?;
        Ok(checksums.map(ArtifactChecksum::from_relative_file_checksums))
    }

    fn sync_directory(
        directory_fd: &OwnedFd,
        relative_directory: &Path,
        display_root: &Path,
        checksums: &mut Option<Vec<(PathBuf, ArtifactChecksum)>>,
        before: &mut impl FnMut(ArtifactCommitStep, &Path) -> io::Result<()>,
    ) -> Result<(), PersistenceError> {
        let mut directory = Dir::read_from(directory_fd).map_err(errno_to_io)?;
        while let Some(entry) = directory.read() {
            let entry = entry.map_err(errno_to_io)?;
            let name_bytes = entry.file_name().to_bytes();
            if matches!(name_bytes, b"." | b"..") {
                continue;
            }

            let name = OsStr::from_bytes(name_bytes);
            let relative_path = relative_directory.join(name);
            let display_path = display_root.join(&relative_path);
            let observed =
                unix_fs::statat(directory_fd, entry.file_name(), AtFlags::SYMLINK_NOFOLLOW)
                    .map_err(errno_to_io)?;
            let observed_type = FileType::from_raw_mode(observed.st_mode);
            match observed_type {
                FileType::RegularFile => {
                    let child_fd = open_child(directory_fd, entry.file_name())?;
                    ensure_opened_type(&child_fd, FileType::RegularFile, &display_path)?;
                    let mut file = File::from(child_fd);
                    before(ArtifactCommitStep::RegularFileSync, &display_path)?;
                    file.sync_all()?;
                    if let Some(checksums) = checksums {
                        file.seek(SeekFrom::Start(0))?;
                        checksums.push((relative_path, ArtifactChecksum::from_reader(&mut file)?));
                    }
                }
                FileType::Directory => {
                    let child_fd = open_child(directory_fd, entry.file_name())?;
                    ensure_opened_type(&child_fd, FileType::Directory, &display_path)?;
                    sync_directory(&child_fd, &relative_path, display_root, checksums, before)?;
                }
                kind => return Err(unsupported_entry(&display_path, kind)),
            }
        }

        let (step, display_path) = if relative_directory.as_os_str().is_empty() {
            (
                ArtifactCommitStep::RootDirectorySync,
                display_root.to_path_buf(),
            )
        } else {
            (
                ArtifactCommitStep::NestedDirectorySync,
                display_root.join(relative_directory),
            )
        };
        before(step, &display_path)?;
        unix_fs::fsync(directory_fd).map_err(errno_to_io)?;
        Ok(())
    }

    fn open_child(parent: &impl AsFd, name: &std::ffi::CStr) -> io::Result<OwnedFd> {
        unix_fs::openat(
            parent,
            name,
            OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .map_err(errno_to_io)
    }

    fn ensure_opened_type(
        fd: &impl AsFd,
        expected: FileType,
        path: &Path,
    ) -> Result<(), PersistenceError> {
        let opened = unix_fs::fstat(fd).map_err(errno_to_io)?;
        let actual = FileType::from_raw_mode(opened.st_mode);
        if actual == expected {
            Ok(())
        } else {
            Err(unsupported_entry(path, actual))
        }
    }

    fn unsupported_entry(path: &Path, kind: FileType) -> PersistenceError {
        PersistenceError::UnsupportedArtifactEntry {
            path: path.display().to_string(),
            kind: format!("{kind:?}"),
        }
    }

    fn errno_to_io(error: rustix::io::Errno) -> io::Error {
        io::Error::from_raw_os_error(error.raw_os_error())
    }
}

#[cfg(all(
    test,
    any(target_os = "linux", target_os = "android", target_vendor = "apple")
))]
mod tests {
    use super::{ArtifactCommitOutcome, supported::*};
    use crate::{
        artifacts::ArtifactChecksum, persistence::PersistenceError, test_support::temp_dir,
    };

    use std::{fs, io};

    #[test]
    fn publishes_and_recovers_the_exact_verified_tree() {
        let root = temp_dir("canic-backup-artifact-commit");
        let temporary = root.join("snapshot.tmp");
        let canonical = root.join("snapshot");
        write_tree(&temporary);
        let expected = checksum(&temporary);

        let published = commit_with_hook(&temporary, &canonical, &expected, |_, _| Ok(()))
            .expect("publish artifact");
        assert_eq!(published, ArtifactCommitOutcome::Published);
        assert!(!temporary.exists());
        assert_eq!(checksum(&canonical), expected);

        let recovered = commit_with_hook(&temporary, &canonical, &expected, |_, _| Ok(()))
            .expect("recover artifact");
        assert_eq!(recovered, ArtifactCommitOutcome::Recovered);
        assert_eq!(checksum(&canonical), expected);

        fs::remove_dir_all(root).expect("remove temp root");
    }

    #[test]
    fn rejects_existing_destinations_and_unsupported_entries() {
        let conflict_root = temp_dir("canic-backup-artifact-conflict");
        let conflict_temporary = conflict_root.join("snapshot.tmp");
        let conflict_canonical = conflict_root.join("snapshot");
        write_tree(&conflict_temporary);
        write_tree(&conflict_canonical);
        let expected = checksum(&conflict_temporary);

        let conflict = commit_with_hook(
            &conflict_temporary,
            &conflict_canonical,
            &expected,
            |_, _| Ok(()),
        )
        .expect_err("existing destination must reject");
        std::assert_matches!(
            conflict,
            PersistenceError::ArtifactCommitPathConflict { .. }
        );

        let symlink_root = temp_dir("canic-backup-artifact-symlink");
        let symlink_temporary = symlink_root.join("snapshot.tmp");
        let symlink_canonical = symlink_root.join("snapshot");
        write_tree(&symlink_temporary);
        std::os::unix::fs::symlink(
            symlink_temporary.join("root.txt"),
            symlink_temporary.join("linked.txt"),
        )
        .expect("create artifact symlink");

        let symlink =
            commit_with_hook(&symlink_temporary, &symlink_canonical, &expected, |_, _| {
                Ok(())
            })
            .expect_err("symlink must reject");
        std::assert_matches!(symlink, PersistenceError::UnsupportedArtifactEntry { .. });

        fs::remove_dir_all(conflict_root).expect("remove conflict root");
        fs::remove_dir_all(symlink_root).expect("remove symlink root");
    }

    #[test]
    fn publication_race_cannot_replace_a_new_destination() {
        let root = temp_dir("canic-backup-artifact-publication-race");
        let temporary = root.join("snapshot.tmp");
        let canonical = root.join("snapshot");
        write_tree(&temporary);
        let expected = checksum(&temporary);

        let error = commit_with_hook(&temporary, &canonical, &expected, |step, _| {
            if step == ArtifactCommitStep::Publication {
                fs::create_dir(&canonical)?;
                fs::write(canonical.join("other.txt"), b"other")?;
            }
            Ok(())
        })
        .expect_err("atomic no-replace must reject a publication race");

        std::assert_matches!(
            error,
            PersistenceError::Io(ref source)
                if source.kind() == io::ErrorKind::AlreadyExists
        );
        assert!(temporary.exists());
        assert_eq!(
            fs::read(canonical.join("other.txt")).expect("read raced destination"),
            b"other"
        );

        fs::remove_dir_all(root).expect("remove temp root");
    }

    #[test]
    fn injected_commit_failures_never_expose_a_partial_tree() {
        let steps = [
            ArtifactCommitStep::RegularFileSync,
            ArtifactCommitStep::NestedDirectorySync,
            ArtifactCommitStep::RootDirectorySync,
            ArtifactCommitStep::Publication,
            ArtifactCommitStep::ParentDirectorySync,
        ];

        for step in steps {
            let root = temp_dir(&format!("canic-backup-artifact-failure-{step:?}"));
            let temporary = root.join("snapshot.tmp");
            let canonical = root.join("snapshot");
            write_tree(&temporary);
            let expected = checksum(&temporary);
            let mut failed = false;

            let error = commit_with_hook(&temporary, &canonical, &expected, |current, _| {
                if current == step && !failed {
                    failed = true;
                    return Err(io::Error::other("injected commit failure"));
                }
                Ok(())
            })
            .expect_err("injected step must fail");
            std::assert_matches!(error, PersistenceError::Io(_));

            if step == ArtifactCommitStep::ParentDirectorySync {
                assert!(!temporary.exists());
                assert_eq!(checksum(&canonical), expected);
                let recovered = commit_with_hook(&temporary, &canonical, &expected, |_, _| Ok(()))
                    .expect("recover post-publication artifact");
                assert_eq!(recovered, ArtifactCommitOutcome::Recovered);
            } else {
                assert!(temporary.exists());
                assert!(!canonical.exists());
            }

            fs::remove_dir_all(root).expect("remove failure root");
        }
    }

    fn write_tree(root: &std::path::Path) {
        fs::create_dir_all(root.join("nested")).expect("create artifact tree");
        fs::write(root.join("root.txt"), b"root snapshot").expect("write root artifact");
        fs::write(root.join("nested/state.bin"), b"nested snapshot")
            .expect("write nested artifact");
    }

    fn checksum(path: &std::path::Path) -> String {
        ArtifactChecksum::from_directory(path)
            .expect("checksum artifact")
            .hash
    }
}