diff options
-rw-r--r-- | ripple/fossil/src/lib.rs | 44 |
1 files changed, 28 insertions, 16 deletions
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<Path>) -> 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<Path>) -> Directory { + let (d, _) = self.add_directory_inner(path.as_ref()); + self.flush(); + d + } + + pub fn add_path(&self, path: impl AsRef<Path>) -> 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 { |