zub-store 0.0.3

Git-like content-addressed filesystem store with metadata preservation
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
use std::fs;
use std::path::Path;

use crate::error::{Error, IoResultExt, Result};
use crate::fs::{
    apply_metadata, create_block_device, create_char_device, create_fifo, create_hardlink,
    create_socket_placeholder, create_symlink, write_sparse_file, CheckoutHardlinkTracker,
};
use crate::hash::Hash;
use crate::object::{blob_path, read_blob, read_commit, read_tree};
use crate::refs::resolve_ref;
use crate::repo::Repo;
use crate::types::{EntryKind, Tree};

/// checkout options
#[derive(Clone)]
pub struct CheckoutOptions {
    /// overwrite existing files
    pub force: bool,
    /// use hardlinks when possible (default: true)
    pub hardlink: bool,
    /// preserve sparse file holes
    pub preserve_sparse: bool,
}

impl Default for CheckoutOptions {
    fn default() -> Self {
        Self {
            force: false,
            hardlink: true,
            preserve_sparse: false,
        }
    }
}

/// checkout a ref to a target directory
pub fn checkout(repo: &Repo, ref_name: &str, target: &Path, opts: CheckoutOptions) -> Result<()> {
    // resolve ref to commit
    let commit_hash = resolve_ref(repo, ref_name)?;
    let commit = read_commit(repo, &commit_hash)?;

    // load root tree
    let tree = read_tree(repo, &commit.tree)?;

    // check target
    if target.exists() {
        if !opts.force {
            // check if empty
            let is_empty = target.read_dir().with_path(target)?.next().is_none();
            if !is_empty {
                return Err(Error::TargetNotEmpty(target.to_path_buf()));
            }
        }
    } else {
        fs::create_dir_all(target).with_path(target)?;
    }

    // checkout tree
    let mut hardlink_tracker = CheckoutHardlinkTracker::new();
    checkout_tree(repo, &tree, target, "", &mut hardlink_tracker, &opts)
}

/// checkout a tree to a directory (recursive helper)
fn checkout_tree(
    repo: &Repo,
    tree: &Tree,
    target: &Path,
    prefix: &str,
    hardlink_tracker: &mut CheckoutHardlinkTracker,
    opts: &CheckoutOptions,
) -> Result<()> {
    fs::create_dir_all(target).with_path(target)?;

    // first pass: checkout all non-hardlink entries
    // this ensures hardlink targets exist before we create hardlinks
    for entry in tree.entries() {
        let entry_path = target.join(&entry.name);
        let logical_path = if prefix.is_empty() {
            entry.name.clone()
        } else {
            format!("{}/{}", prefix, entry.name)
        };

        match &entry.kind {
            EntryKind::Hardlink { .. } => {
                // skip hardlinks in first pass
                continue;
            }

            EntryKind::Regular {
                hash, sparse_map, ..
            } => {
                checkout_regular_file(repo, &entry_path, hash, sparse_map.as_deref(), opts)?;
                hardlink_tracker.record(&logical_path, entry_path);
            }

            EntryKind::Symlink { hash } => {
                checkout_symlink(repo, &entry_path, hash)?;
                hardlink_tracker.record(&logical_path, entry_path);
            }

            EntryKind::Directory {
                hash,
                uid,
                gid,
                mode,
                xattrs,
            } => {
                // recurse
                let subtree = read_tree(repo, hash)?;
                checkout_tree(
                    repo,
                    &subtree,
                    &entry_path,
                    &logical_path,
                    hardlink_tracker,
                    opts,
                )?;

                // apply directory metadata after contents are created
                apply_metadata(&entry_path, *uid, *gid, *mode, xattrs)?;
            }

            EntryKind::BlockDevice {
                major,
                minor,
                uid,
                gid,
                mode,
                xattrs,
            } => {
                match create_block_device(&entry_path, *major, *minor, *uid, *gid, *mode, xattrs) {
                    Ok(()) => {}
                    Err(Error::DeviceNodePermission(_)) => {
                        eprintln!(
                            "warning: cannot create block device {:?} without privileges, skipping",
                            entry_path
                        );
                    }
                    Err(e) => return Err(e),
                }
            }

            EntryKind::CharDevice {
                major,
                minor,
                uid,
                gid,
                mode,
                xattrs,
            } => match create_char_device(&entry_path, *major, *minor, *uid, *gid, *mode, xattrs) {
                Ok(()) => {}
                Err(Error::DeviceNodePermission(_)) => {
                    eprintln!(
                        "warning: cannot create char device {:?} without privileges, skipping",
                        entry_path
                    );
                }
                Err(e) => return Err(e),
            },

            EntryKind::Fifo {
                uid,
                gid,
                mode,
                xattrs,
            } => {
                create_fifo(&entry_path, *uid, *gid, *mode, xattrs)?;
            }

            EntryKind::Socket {
                uid,
                gid,
                mode,
                xattrs,
            } => {
                create_socket_placeholder(&entry_path, *uid, *gid, *mode, xattrs)?;
            }
        }
    }

    // second pass: create hardlinks
    for entry in tree.entries() {
        if let EntryKind::Hardlink { target_path } = &entry.kind {
            let entry_path = target.join(&entry.name);

            // look up the target's filesystem path
            let target_fs_path = hardlink_tracker
                .get(target_path)
                .ok_or_else(|| Error::HardlinkTargetNotFound(target_path.clone()))?;

            create_hardlink(&entry_path, target_fs_path)?;
        }
    }

    Ok(())
}

/// checkout a regular file (hardlink from blob store, or copy for sparse/--copy)
fn checkout_regular_file(
    repo: &Repo,
    dest: &Path,
    hash: &Hash,
    sparse_map: Option<&[crate::types::SparseRegion]>,
    opts: &CheckoutOptions,
) -> Result<()> {
    // remove existing
    if dest.exists() {
        fs::remove_file(dest).with_path(dest)?;
    }

    match sparse_map {
        Some(regions) if !regions.is_empty() && opts.preserve_sparse => {
            // sparse file: must copy and recreate holes
            let data = read_blob(repo, hash)?;
            let total_size: u64 = regions.iter().map(|r| r.end()).max().unwrap_or(0);
            write_sparse_file(dest, &data, regions, total_size)?;

            // copy metadata from blob
            let blob = blob_path(repo, hash);
            let meta = fs::metadata(&blob).with_path(&blob)?;
            fs::set_permissions(dest, meta.permissions()).with_path(dest)?;
        }

        Some(regions) if regions.is_empty() => {
            // all holes (empty sparse file)
            // just create empty file
            fs::write(dest, b"").with_path(dest)?;
        }

        _ if opts.hardlink => {
            // non-sparse with hardlink: hardlink from blob store
            let blob = blob_path(repo, hash);
            fs::hard_link(&blob, dest).with_path(dest)?;
            // metadata comes along with the hardlink (shared inode)
        }

        _ => {
            // copy mode (--copy flag or sparse without preserve_sparse)
            let blob = blob_path(repo, hash);
            fs::copy(&blob, dest).with_path(dest)?;
            // metadata was copied with the file
        }
    }

    Ok(())
}

/// checkout a symlink
fn checkout_symlink(repo: &Repo, dest: &Path, hash: &Hash) -> Result<()> {
    // symlink blob contains the target path as content
    let target_bytes = read_blob(repo, hash)?;
    let target = String::from_utf8_lossy(&target_bytes);

    // read metadata from blob file
    let blob = blob_path(repo, hash);
    let meta = fs::symlink_metadata(&blob).with_path(&blob)?;

    use std::os::unix::fs::MetadataExt;
    create_symlink(dest, &target, meta.uid(), meta.gid(), &[])?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ops::commit::commit;
    use std::os::unix::fs::MetadataExt;
    use tempfile::tempdir;

    fn test_repo() -> (tempfile::TempDir, Repo) {
        let dir = tempdir().unwrap();
        let repo_path = dir.path().join("repo");
        let repo = Repo::init(&repo_path).unwrap();
        (dir, repo)
    }

    #[test]
    fn test_checkout_single_file() {
        let (dir, repo) = test_repo();

        // create and commit source
        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        fs::write(source.join("hello.txt"), "world").unwrap();
        commit(&repo, &source, "test/ref", None, None).unwrap();

        // checkout
        let target = dir.path().join("target");
        checkout(&repo, "test/ref", &target, Default::default()).unwrap();

        // verify
        let content = fs::read_to_string(target.join("hello.txt")).unwrap();
        assert_eq!(content, "world");
    }

    #[test]
    fn test_checkout_uses_hardlinks() {
        let (dir, repo) = test_repo();

        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        fs::write(source.join("file.txt"), "content").unwrap();
        let commit_hash = commit(&repo, &source, "test", None, None).unwrap();

        let target = dir.path().join("target");
        checkout(&repo, "test", &target, Default::default()).unwrap();

        // get blob hash from commit
        let commit_obj = read_commit(&repo, &commit_hash).unwrap();
        let tree = read_tree(&repo, &commit_obj.tree).unwrap();
        let entry = tree.get("file.txt").unwrap();
        if let EntryKind::Regular { hash, .. } = &entry.kind {
            let blob = blob_path(&repo, hash);
            let checked_out = target.join("file.txt");

            // verify same inode (hardlink)
            let blob_ino = fs::metadata(&blob).unwrap().ino();
            let target_ino = fs::metadata(&checked_out).unwrap().ino();
            assert_eq!(blob_ino, target_ino);
        } else {
            panic!("expected regular file");
        }
    }

    #[test]
    fn test_checkout_nested_directories() {
        let (dir, repo) = test_repo();

        let source = dir.path().join("source");
        fs::create_dir_all(source.join("a/b")).unwrap();
        fs::write(source.join("a/b/deep.txt"), "deep content").unwrap();
        commit(&repo, &source, "nested", None, None).unwrap();

        let target = dir.path().join("target");
        checkout(&repo, "nested", &target, Default::default()).unwrap();

        assert!(target.join("a/b/deep.txt").exists());
        let content = fs::read_to_string(target.join("a/b/deep.txt")).unwrap();
        assert_eq!(content, "deep content");
    }

    #[test]
    fn test_checkout_symlink() {
        let (dir, repo) = test_repo();

        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        std::os::unix::fs::symlink("/target/path", source.join("link")).unwrap();
        commit(&repo, &source, "symlink", None, None).unwrap();

        let target = dir.path().join("target");
        checkout(&repo, "symlink", &target, Default::default()).unwrap();

        let link_target = fs::read_link(target.join("link")).unwrap();
        assert_eq!(link_target.to_string_lossy(), "/target/path");
    }

    #[test]
    fn test_checkout_hardlinks() {
        let (dir, repo) = test_repo();

        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        fs::write(source.join("original"), "content").unwrap();
        fs::hard_link(source.join("original"), source.join("link")).unwrap();
        commit(&repo, &source, "hardlink", None, None).unwrap();

        let target = dir.path().join("target");
        checkout(&repo, "hardlink", &target, Default::default()).unwrap();

        // both files should exist and be hardlinks
        let orig_ino = fs::metadata(target.join("original")).unwrap().ino();
        let link_ino = fs::metadata(target.join("link")).unwrap().ino();
        assert_eq!(orig_ino, link_ino);
    }

    #[test]
    fn test_checkout_force() {
        let (dir, repo) = test_repo();

        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        fs::write(source.join("file.txt"), "content").unwrap();
        commit(&repo, &source, "test", None, None).unwrap();

        // create non-empty target
        let target = dir.path().join("target");
        fs::create_dir(&target).unwrap();
        fs::write(target.join("existing.txt"), "existing").unwrap();

        // checkout without force should fail
        let result = checkout(&repo, "test", &target, Default::default());
        assert!(result.is_err());

        // checkout with force should succeed
        checkout(
            &repo,
            "test",
            &target,
            CheckoutOptions {
                force: true,
                ..Default::default()
            },
        )
        .unwrap();

        assert!(target.join("file.txt").exists());
    }

    #[test]
    fn test_roundtrip() {
        let (dir, repo) = test_repo();

        // create complex source
        let source = dir.path().join("source");
        fs::create_dir_all(source.join("dir1/dir2")).unwrap();
        fs::write(source.join("file1.txt"), "content1").unwrap();
        fs::write(source.join("dir1/file2.txt"), "content2").unwrap();
        fs::write(source.join("dir1/dir2/file3.txt"), "content3").unwrap();
        std::os::unix::fs::symlink("../file1.txt", source.join("dir1/link")).unwrap();

        commit(&repo, &source, "roundtrip", None, None).unwrap();

        // checkout to new location
        let target = dir.path().join("target");
        checkout(&repo, "roundtrip", &target, Default::default()).unwrap();

        // verify structure
        assert_eq!(
            fs::read_to_string(target.join("file1.txt")).unwrap(),
            "content1"
        );
        assert_eq!(
            fs::read_to_string(target.join("dir1/file2.txt")).unwrap(),
            "content2"
        );
        assert_eq!(
            fs::read_to_string(target.join("dir1/dir2/file3.txt")).unwrap(),
            "content3"
        );
        assert_eq!(
            fs::read_link(target.join("dir1/link"))
                .unwrap()
                .to_string_lossy(),
            "../file1.txt"
        );
    }
}