Provide Ref::make_ref() for making a ref out of an existing entry
This commit is contained in:
parent
1d3994d108
commit
5ceeacd6c8
2 changed files with 24 additions and 14 deletions
|
@ -20,6 +20,7 @@
|
||||||
//! The Ref object is a helper over the link functionality, so one is able to create references to
|
//! The Ref object is a helper over the link functionality, so one is able to create references to
|
||||||
//! files outside of the imag store.
|
//! files outside of the imag store.
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::result::Result as RResult;
|
use std::result::Result as RResult;
|
||||||
|
|
||||||
|
@ -27,8 +28,10 @@ use libimagentryutil::isa::Is;
|
||||||
use libimagentryutil::isa::IsKindHeaderPathProvider;
|
use libimagentryutil::isa::IsKindHeaderPathProvider;
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
|
|
||||||
|
use toml::Value;
|
||||||
use toml_query::read::TomlValueReadExt;
|
use toml_query::read::TomlValueReadExt;
|
||||||
use toml_query::delete::TomlValueDeleteExt;
|
use toml_query::delete::TomlValueDeleteExt;
|
||||||
|
use toml_query::insert::TomlValueInsertExt;
|
||||||
|
|
||||||
use refstore::UniqueRefPathGenerator;
|
use refstore::UniqueRefPathGenerator;
|
||||||
use error::Result;
|
use error::Result;
|
||||||
|
@ -45,6 +48,9 @@ pub trait Ref {
|
||||||
/// Does not need a `UniqueRefPathGenerator` as it reads the hash stored in the header
|
/// Does not need a `UniqueRefPathGenerator` as it reads the hash stored in the header
|
||||||
fn get_hash(&self) -> Result<&str>;
|
fn get_hash(&self) -> Result<&str>;
|
||||||
|
|
||||||
|
/// Make this object a ref
|
||||||
|
fn make_ref<P: AsRef<Path>>(&mut self, hash: String, path: P) -> Result<()>;
|
||||||
|
|
||||||
/// Get the referenced path.
|
/// Get the referenced path.
|
||||||
///
|
///
|
||||||
/// Does not need a `UniqueRefPathGenerator` as it reads the path stored in the header.
|
/// Does not need a `UniqueRefPathGenerator` as it reads the path stored in the header.
|
||||||
|
@ -89,6 +95,21 @@ impl Ref for Entry {
|
||||||
.and_then(|v| v.as_str().ok_or_else(|| REK::HeaderTypeError("ref.hash", "string").into()))
|
.and_then(|v| v.as_str().ok_or_else(|| REK::HeaderTypeError("ref.hash", "string").into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_ref<P: AsRef<Path>>(&mut self, hash: String, path: P) -> Result<()> {
|
||||||
|
let path_str : String = path
|
||||||
|
.as_ref()
|
||||||
|
.to_str()
|
||||||
|
.map(String::from)
|
||||||
|
.ok_or_else(|| RE::from(REK::PathUTF8Error))?;
|
||||||
|
|
||||||
|
let _ = self.set_isflag::<IsRef>()?;
|
||||||
|
let hdr = self.get_header_mut();
|
||||||
|
hdr.insert("ref.path", Value::String(String::from(path_str)))?;
|
||||||
|
hdr.insert("ref.hash", Value::String(hash))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn get_path(&self) -> Result<PathBuf> {
|
fn get_path(&self) -> Result<PathBuf> {
|
||||||
self.get_header()
|
self.get_header()
|
||||||
.read("ref.path")
|
.read("ref.path")
|
||||||
|
|
|
@ -23,14 +23,9 @@ use std::path::PathBuf;
|
||||||
use libimagstore::store::FileLockEntry;
|
use libimagstore::store::FileLockEntry;
|
||||||
use libimagstore::store::Store;
|
use libimagstore::store::Store;
|
||||||
use libimagstore::storeid::StoreId;
|
use libimagstore::storeid::StoreId;
|
||||||
use libimagentryutil::isa::Is;
|
|
||||||
|
|
||||||
use toml_query::insert::TomlValueInsertExt;
|
|
||||||
use toml::Value;
|
|
||||||
|
|
||||||
use error::RefError as RE;
|
use error::RefError as RE;
|
||||||
use error::RefErrorKind as REK;
|
use reference::Ref;
|
||||||
use reference::IsRef;
|
|
||||||
|
|
||||||
/// A UniqueRefPathGenerator generates unique Pathes
|
/// A UniqueRefPathGenerator generates unique Pathes
|
||||||
///
|
///
|
||||||
|
@ -108,21 +103,15 @@ impl<'a> RefStore<'a> for Store {
|
||||||
fn create_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A)
|
fn create_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A)
|
||||||
-> Result<FileLockEntry<'a>, RPG::Error>
|
-> Result<FileLockEntry<'a>, RPG::Error>
|
||||||
{
|
{
|
||||||
let path_str = path.as_ref().to_str().map(String::from).ok_or(REK::PathUTF8Error.into())?;
|
|
||||||
let hash = RPG::unique_hash(path)?;
|
let hash = RPG::unique_hash(path)?;
|
||||||
let pathbuf = PathBuf::from(format!("{}/{}", RPG::collection(), hash));
|
let pathbuf = PathBuf::from(format!("{}/{}", RPG::collection(), hash));
|
||||||
let sid = StoreId::new_baseless(pathbuf).map_err(RE::from)?;
|
let sid = StoreId::new_baseless(pathbuf.clone()).map_err(RE::from)?;
|
||||||
|
|
||||||
debug!("Creating: {:?}", sid);
|
debug!("Creating: {:?}", sid);
|
||||||
self.create(sid)
|
self.create(sid)
|
||||||
.map_err(RE::from)
|
.map_err(RE::from)
|
||||||
.and_then(|mut fle| {
|
.and_then(|mut fle| {
|
||||||
let _ = fle.set_isflag::<IsRef>()?;
|
fle.make_ref(hash, pathbuf)?;
|
||||||
{
|
|
||||||
let hdr = fle.get_header_mut();
|
|
||||||
hdr.insert("ref.path", Value::String(String::from(path_str)))?;
|
|
||||||
hdr.insert("ref.hash", Value::String(hash))?;
|
|
||||||
}
|
|
||||||
Ok(fle)
|
Ok(fle)
|
||||||
})
|
})
|
||||||
.map_err(RPG::Error::from)
|
.map_err(RPG::Error::from)
|
||||||
|
|
Loading…
Reference in a new issue