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
use std::fs::{self, File};
use std::io::Read;
use std::path::Path;

use walkdir::WalkDir;

use crate::error::{IoResultExt, Result};
use crate::fs::{
    detect_sparse_regions, read_data_regions, read_xattrs, FileMetadata, FileType, HardlinkTracker,
};
use crate::hash::{compute_symlink_hash, Hash, SYMLINK_MODE};
use crate::namespace::outside_to_inside;
use crate::object::{write_blob, write_commit, write_tree};
use crate::refs::write_ref;
use crate::repo::Repo;
use crate::types::{Commit, EntryKind, Tree, TreeEntry};

/// commit a directory tree to a ref
pub fn commit(
    repo: &Repo,
    source: &Path,
    ref_name: &str,
    message: Option<&str>,
    author: Option<&str>,
) -> Result<Hash> {
    commit_with_metadata(repo, source, ref_name, message, author, &[])
}

/// commit a directory tree to a ref with custom metadata
pub fn commit_with_metadata(
    repo: &Repo,
    source: &Path,
    ref_name: &str,
    message: Option<&str>,
    author: Option<&str>,
    metadata: &[(&str, &str)],
) -> Result<Hash> {
    let mut hardlink_tracker = HardlinkTracker::new();

    // commit the root tree
    let tree_hash = commit_tree(repo, source, "", &mut hardlink_tracker)?;

    // get parent commit if ref exists
    let parents = match crate::refs::read_ref(repo, ref_name) {
        Ok(parent) => vec![parent],
        Err(crate::Error::RefNotFound(_)) => vec![],
        Err(e) => return Err(e),
    };

    // create commit with metadata
    let mut commit = Commit::new(
        tree_hash,
        parents,
        author.unwrap_or("zub"),
        message.unwrap_or(""),
    );
    for (key, value) in metadata {
        commit = commit.with_metadata(*key, *value);
    }

    let commit_hash = write_commit(repo, &commit)?;

    // update ref
    write_ref(repo, ref_name, &commit_hash)?;

    Ok(commit_hash)
}

/// commit a directory tree (recursive helper)
fn commit_tree(
    repo: &Repo,
    dir: &Path,
    prefix: &str,
    hardlink_tracker: &mut HardlinkTracker,
) -> Result<Hash> {
    let ns = &repo.config().namespace;
    let mut entries = Vec::new();

    // read directory entries
    let mut dir_entries: Vec<_> = fs::read_dir(dir)
        .with_path(dir)?
        .collect::<std::io::Result<Vec<_>>>()
        .with_path(dir)?;
    dir_entries.sort_by(|a, b| a.file_name().cmp(&b.file_name()));

    for entry in dir_entries {
        let path = entry.path();
        let name = entry.file_name().to_string_lossy().to_string();
        let logical_path = if prefix.is_empty() {
            name.clone()
        } else {
            format!("{}/{}", prefix, name)
        };

        let meta = FileMetadata::from_path(&path)?;

        // convert outside uid/gid to inside values
        let inside_uid =
            outside_to_inside(meta.uid, &ns.uid_map).ok_or(crate::Error::UnmappedUid(meta.uid))?;
        let inside_gid =
            outside_to_inside(meta.gid, &ns.gid_map).ok_or(crate::Error::UnmappedGid(meta.gid))?;

        let kind = match meta.file_type {
            FileType::Regular => {
                // check for hardlink
                if meta.could_be_hardlink() {
                    if let Some(target) = hardlink_tracker.check(meta.dev, meta.ino, &logical_path)
                    {
                        entries.push(TreeEntry::new(name, EntryKind::hardlink(target)));
                        continue;
                    }
                }

                // read file content and xattrs
                let xattrs = read_xattrs(&path)?;
                let mut file = File::open(&path).with_path(&path)?;

                // check for sparse file
                let sparse_regions = detect_sparse_regions(&file)?;

                let (content, sparse_map) = match sparse_regions {
                    Some(ref regions) if !regions.is_empty() => {
                        // sparse file: read only data regions
                        let data = read_data_regions(&mut file, regions)?;
                        (data, Some(regions.clone()))
                    }
                    Some(_) => {
                        // all holes (empty sparse file)
                        (vec![], Some(vec![]))
                    }
                    None => {
                        // non-sparse: read entire file
                        // seek back to start (sparse detection may have moved position)
                        use std::io::Seek;
                        file.seek(std::io::SeekFrom::Start(0)).with_path(&path)?;
                        let mut content = Vec::new();
                        file.read_to_end(&mut content).with_path(&path)?;
                        (content, None)
                    }
                };

                // write blob
                let hash = write_blob(repo, &content, inside_uid, inside_gid, meta.mode, &xattrs)?;

                match sparse_map {
                    Some(map) => EntryKind::sparse(hash, meta.size, map),
                    None => EntryKind::regular(hash, meta.size),
                }
            }

            FileType::Symlink => {
                let target = crate::fs::read_symlink_target(&path)?;
                let xattrs = read_xattrs(&path)?;

                // symlink hash: target is content, mode is always SYMLINK_MODE
                let hash = compute_symlink_hash(inside_uid, inside_gid, &xattrs, &target);

                // write symlink as blob (target bytes with symlink metadata)
                write_blob(
                    repo,
                    target.as_bytes(),
                    inside_uid,
                    inside_gid,
                    SYMLINK_MODE,
                    &xattrs,
                )?;

                EntryKind::symlink(hash)
            }

            FileType::Directory => {
                let xattrs = read_xattrs(&path)?;

                // recurse
                let subtree_hash = commit_tree(repo, &path, &logical_path, hardlink_tracker)?;

                EntryKind::directory_with_xattrs(
                    subtree_hash,
                    inside_uid,
                    inside_gid,
                    meta.mode,
                    xattrs,
                )
            }

            FileType::BlockDevice => {
                let (major, minor) = meta.rdev.unwrap_or((0, 0));
                let xattrs = read_xattrs(&path)?;

                EntryKind::BlockDevice {
                    major,
                    minor,
                    uid: inside_uid,
                    gid: inside_gid,
                    mode: meta.mode,
                    xattrs,
                }
            }

            FileType::CharDevice => {
                let (major, minor) = meta.rdev.unwrap_or((0, 0));
                let xattrs = read_xattrs(&path)?;

                EntryKind::CharDevice {
                    major,
                    minor,
                    uid: inside_uid,
                    gid: inside_gid,
                    mode: meta.mode,
                    xattrs,
                }
            }

            FileType::Fifo => {
                let xattrs = read_xattrs(&path)?;

                EntryKind::Fifo {
                    uid: inside_uid,
                    gid: inside_gid,
                    mode: meta.mode,
                    xattrs,
                }
            }

            FileType::Socket => {
                let xattrs = read_xattrs(&path)?;

                EntryKind::Socket {
                    uid: inside_uid,
                    gid: inside_gid,
                    mode: meta.mode,
                    xattrs,
                }
            }
        };

        entries.push(TreeEntry::new(name, kind));
    }

    // create and write tree
    let tree = Tree::new(entries)?;
    write_tree(repo, &tree)
}

/// count files in a directory (for progress reporting)
#[allow(dead_code)]
pub fn count_files(path: &Path) -> usize {
    WalkDir::new(path)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .count()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::unix::fs::symlink;
    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_commit_single_file() {
        let (dir, repo) = test_repo();

        // create source directory with a file
        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        fs::write(source.join("hello.txt"), "world").unwrap();

        // commit
        let hash = commit(&repo, &source, "test/ref", Some("test commit"), None).unwrap();

        // verify ref was created
        let resolved = crate::refs::resolve_ref(&repo, "test/ref").unwrap();
        assert_eq!(hash, resolved);

        // read commit and tree
        let commit_obj = crate::object::read_commit(&repo, &hash).unwrap();
        let tree = crate::object::read_tree(&repo, &commit_obj.tree).unwrap();

        assert_eq!(tree.len(), 1);
        assert!(tree.get("hello.txt").is_some());
    }

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

        let source = dir.path().join("source");
        fs::create_dir_all(source.join("a/b/c")).unwrap();
        fs::write(source.join("a/b/c/file.txt"), "deep").unwrap();
        fs::write(source.join("top.txt"), "top").unwrap();

        let hash = commit(&repo, &source, "nested", None, None).unwrap();

        let commit_obj = crate::object::read_commit(&repo, &hash).unwrap();
        let tree = crate::object::read_tree(&repo, &commit_obj.tree).unwrap();

        assert_eq!(tree.len(), 2);
        assert!(tree.get("a").is_some());
        assert!(tree.get("top.txt").is_some());

        // check nested
        if let Some(entry) = tree.get("a") {
            if let EntryKind::Directory { hash, .. } = &entry.kind {
                let subtree = crate::object::read_tree(&repo, hash).unwrap();
                assert!(subtree.get("b").is_some());
            } else {
                panic!("expected directory");
            }
        }
    }

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

        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        symlink("/target/path", source.join("link")).unwrap();

        let hash = commit(&repo, &source, "symlink-test", None, None).unwrap();

        let commit_obj = crate::object::read_commit(&repo, &hash).unwrap();
        let tree = crate::object::read_tree(&repo, &commit_obj.tree).unwrap();

        let entry = tree.get("link").unwrap();
        assert!(entry.kind.is_symlink());
    }

    #[test]
    fn test_commit_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();

        let hash = commit(&repo, &source, "hardlink-test", None, None).unwrap();

        let commit_obj = crate::object::read_commit(&repo, &hash).unwrap();
        let tree = crate::object::read_tree(&repo, &commit_obj.tree).unwrap();

        // one should be regular, one should be hardlink
        let mut found_regular = false;
        let mut found_hardlink = false;

        for entry in tree.entries() {
            match &entry.kind {
                EntryKind::Regular { .. } => found_regular = true,
                EntryKind::Hardlink { .. } => found_hardlink = true,
                _ => {}
            }
        }

        assert!(found_regular);
        assert!(found_hardlink);
    }

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

        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();
        fs::write(source.join("file.txt"), "v1").unwrap();

        // first commit
        let hash1 = commit(&repo, &source, "versioned", Some("v1"), None).unwrap();

        // modify and commit again
        fs::write(source.join("file.txt"), "v2").unwrap();
        let hash2 = commit(&repo, &source, "versioned", Some("v2"), None).unwrap();

        // second commit should have first as parent
        let commit2 = crate::object::read_commit(&repo, &hash2).unwrap();
        assert_eq!(commit2.parents.len(), 1);
        assert_eq!(commit2.parents[0], hash1);
    }

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

        let source = dir.path().join("source");
        fs::create_dir(&source).unwrap();

        let hash = commit(&repo, &source, "empty", None, None).unwrap();

        let commit_obj = crate::object::read_commit(&repo, &hash).unwrap();
        let tree = crate::object::read_tree(&repo, &commit_obj.tree).unwrap();

        assert!(tree.is_empty());
    }
}