From 712ec9d04146995e3db79fa3b03b00a8eb76c0d1 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Sun, 19 Nov 2017 21:27:03 -0800 Subject: drive api: return new versions when appropriate; add comments --- src/drive.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/drive.rs b/src/drive.rs index 0351587..10bcbda 100644 --- a/src/drive.rs +++ b/src/drive.rs @@ -316,12 +316,13 @@ impl<'a> DatDrive { } } - pub fn add_file_bytes>(&mut self, path: P, stat: &mut Stat, data: &[u8]) -> Result<()> { + /// On success, returns version number including the added data. + pub fn add_file_bytes>(&mut self, path: P, stat: &mut Stat, data: &[u8]) -> Result { self.add_file(path, stat, data) } - // TODO: return version - pub fn add_file, R: Read>(&mut self, path: P, stat: &mut Stat, mut source: R) -> Result<()> { + /// On success, returns version number including the added file. + pub fn add_file, R: Read>(&mut self, path: P, stat: &mut Stat, mut source: R) -> Result { // TODO: canonicalize path // TODO: check if file already exists let mut total_size: u64 = 0; @@ -355,7 +356,7 @@ impl<'a> DatDrive { /// If this metadata entry represents a change (overwriting a previous entry), then `remove` /// should be set to the old index. /// If this entry is a deletion/removal, `remove` should be set and `stat` should be None. - fn append_metadata_entry>(&mut self, path: P, stat: Option<&Stat>, remove: Option) -> Result <()> { + fn append_metadata_entry>(&mut self, path: P, stat: Option<&Stat>, remove: Option) -> Result { let index = self.entry_count()? + 1; let path = path.as_ref(); let mut children = self.new_child_index(&path, index)?; @@ -378,7 +379,7 @@ impl<'a> DatDrive { } node.set_paths(children); self.metadata.append(&node.write_to_bytes()?)?; - return Ok(()); + return Ok(index); } fn new_child_index>(&mut self, path: P, index: u64) -> Result>> { @@ -423,7 +424,8 @@ impl<'a> DatDrive { /// Copies Stat metadata and all content from a file in the "real" filesystem into the /// DatDrive. - pub fn import_file, Q: AsRef>(&mut self, source: P, dest: Q) -> Result<()> { + /// On success, returns version number including the added file. + pub fn import_file, Q: AsRef>(&mut self, source: P, dest: Q) -> Result { let in_file = File::open(source)?; let in_metadata = in_file.metadata()?; let mut stat = Stat::new(); @@ -486,7 +488,8 @@ impl<'a> DatDrive { Ok(()) } - pub fn remove_file>(&mut self, path: P) -> Result<()> { + /// Returns version number containing completed removal on success. + pub fn remove_file>(&mut self, path: P) -> Result { let path = path.as_ref(); let current = self.get_file_entry(path)?; if let Some(val) = current { @@ -496,20 +499,24 @@ impl<'a> DatDrive { } } - pub fn remove_dir_all>(&mut self, path: P) -> Result<()> { + /// Returns version number containing completed removal on success. + /// Partial success (returning an error) leaves the drive in an undefined state. + pub fn remove_dir_all>(&mut self, path: P) -> Result { // Crude implementation: // 1. get list of all file paths let path = path.as_ref(); let files: Vec = self.read_dir_recursive(path).map(|de| de.unwrap().path).collect(); // 2. remove each + let mut last_version = 0; for f in files { - self.remove_file(&f)?; + last_version= self.remove_file(&f)?; } - Ok(()) + Ok(last_version) } - pub fn copy, Q: AsRef>(&mut self, from: P, to: Q) -> Result<()> { + /// Returns version number of completed action on success. + pub fn copy, Q: AsRef>(&mut self, from: P, to: Q) -> Result { let from = from.as_ref(); let to = to.as_ref(); if from == to { @@ -529,7 +536,8 @@ impl<'a> DatDrive { return self.append_metadata_entry(&to, Some(&stat), None); } - pub fn rename, Q: AsRef>(&mut self, from: P, to: Q) -> Result<()> { + /// Returns version number containing rename action on success. + pub fn rename, Q: AsRef>(&mut self, from: P, to: Q) -> Result { // Crude implementation: // 1. copy file let from = from.as_ref(); -- cgit v1.2.3