diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-12-31 13:16:23 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-01-01 20:07:43 +0100 |
commit | 24342c37ec2c6826e0d8b18b38f6ea4383178419 (patch) | |
tree | 4fb4a1726e395c4efb0ff368dea8b7701a79e3c1 | |
parent | 2315a68543b1cb3b7064742d3f931bd82785cf2d (diff) |
Add MutRefWithHasher::rename_file(), which requires Ref impl in its own impl
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/entry/libimagentryref/src/reference.rs | 27 |
1 files changed, 26 insertions, 1 deletions
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<Path>, Coll: AsRef<str>; + fn rename_file<P>(&mut self, config: &Config, new_path: P) -> Result<()> + where P: AsRef<Path>; + } 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<P>(&mut self, config: &Config, new_path: P) -> Result<()> + where P: AsRef<Path> + { + 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". |