From 9a531f656e158c9a5e0fab4cebdac6f46b19d919 Mon Sep 17 00:00:00 2001 From: edef Date: Tue, 3 May 2022 00:29:10 +0000 Subject: ripple/fossil: expose add_directory Change-Id: I8e976279bd7aaaaf325129dc5c68a6ca5c750dc6 --- ripple/fossil/src/lib.rs | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'ripple/fossil/src') diff --git a/ripple/fossil/src/lib.rs b/ripple/fossil/src/lib.rs index 0c8a61e..53e4d7b 100644 --- a/ripple/fossil/src/lib.rs +++ b/ripple/fossil/src/lib.rs @@ -64,10 +64,7 @@ impl Store { }) } - pub fn add_path(&self, path: impl AsRef) -> Node { - let path = path.as_ref(); - let node = self.add_path_inner(path); - + fn flush(&self) { // NOTE: sled *can* flush without us explicitly asking for it, so it's // possible for the store to end up containing pointers to chunks that // aren't fsynced yet. The easiest fix is to always `chunks_file.sync_data()` @@ -75,27 +72,42 @@ impl Store { // TODO(edef): keep pending and known-durable blobs/chunks separate in the database self.chunks_file.borrow_mut().sync_data().unwrap(); self.meta.flush().unwrap(); + } + pub fn add_directory(&self, path: impl AsRef) -> Directory { + let (d, _) = self.add_directory_inner(path.as_ref()); + self.flush(); + d + } + + pub fn add_path(&self, path: impl AsRef) -> Node { + let node = self.add_path_inner(path.as_ref()); + self.flush(); node } + fn add_directory_inner(&self, path: &Path) -> (Directory, u32) { + let mut d = Directory::new(); + let mut size: u32 = 0; + + for entry in path.read_dir().unwrap() { + let entry = entry.unwrap(); + let name = entry.file_name().into_string().unwrap(); + + let child = self.add_path_inner(&entry.path()); + size = size.checked_add(child.size()).expect("overflow"); + d.children.insert(name, child); + } + + (d, size) + } + fn add_path_inner(&self, path: &Path) -> Node { let meta = fs::symlink_metadata(path).unwrap(); match meta.file_type() { ty if ty.is_dir() => { - let mut d = Directory::new(); - let mut size: u32 = 0; - - for entry in path.read_dir().unwrap() { - let entry = entry.unwrap(); - let name = entry.file_name().into_string().unwrap(); - - let child = self.add_path_inner(&entry.path()); - size = size.checked_add(child.size()).expect("overflow"); - d.children.insert(name, child); - } - + let (d, size) = self.add_directory_inner(path); let blob = d.into_pb().encode_to_vec(); Node::Directory(DirectoryRef { -- cgit 1.4.1