From 24342c37ec2c6826e0d8b18b38f6ea4383178419 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 31 Dec 2019 13:16:23 +0100 Subject: Add MutRefWithHasher::rename_file(), which requires Ref impl in its own impl Signed-off-by: Matthias Beyer --- lib/entry/libimagentryref/src/reference.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index 595962c4..e9bf5272 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -262,7 +262,7 @@ impl<'a, H: Hasher> Ref for RefWithHasher<'a, H> { } -pub trait MutRef { +pub trait MutRef : Ref { fn remove_ref(&mut self) -> Result<()>; /// Make a ref out of a normal (non-ref) entry. @@ -273,6 +273,9 @@ pub trait MutRef { where P: AsRef, Coll: AsRef; + fn rename_file

(&mut self, config: &Config, new_path: P) -> Result<()> + where P: AsRef; + } impl<'a, H: Hasher> Ref for MutRefWithHasher<'a, H> { @@ -469,6 +472,28 @@ impl<'a, H> MutRef for MutRefWithHasher<'a, H> .map(|_| ()) } + /// Rename the file referenced by this ref + /// + /// Uses `MutRef::make_ref()` to ensure all ref settings are correct. + fn rename_file

(&mut self, config: &Config, new_path: P) -> Result<()> + where P: AsRef + { + let basepath_name = self.0 + .get_header() + .read_string("ref.basepath")? + .ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.basepath")))?; + + let path = self.get_path_with_basepath_setting(config, basepath_name.clone())?; + + trace!("Renaming {} to {}", path.display(), new_path.as_ref().display()); + ::std::fs::rename(path, new_path.as_ref())?; + + // worked... + // ensuring ref settings are correct + trace!("Ensuring ref settings are correct with Self::make_ref()"); + self.make_ref(new_path, basepath_name, config, true) + } + } /// Create a new header section for a "ref". -- cgit v1.2.3