2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-06-09 17:34:25 +00:00
|
|
|
//! The Ref object is a helper over the link functionality, so one is able to create references to
|
|
|
|
//! files outside of the imag store.
|
|
|
|
|
2018-03-18 16:52:41 +00:00
|
|
|
use std::path::Path;
|
2016-06-09 17:34:25 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2018-01-08 22:34:13 +00:00
|
|
|
use libimagentryutil::isa::Is;
|
|
|
|
use libimagentryutil::isa::IsKindHeaderPathProvider;
|
2018-02-13 20:11:45 +00:00
|
|
|
use libimagstore::store::Entry;
|
2018-10-30 17:40:52 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2016-06-09 17:34:25 +00:00
|
|
|
|
2018-03-18 16:52:41 +00:00
|
|
|
use toml::Value;
|
2018-02-13 20:11:45 +00:00
|
|
|
use toml_query::read::TomlValueReadExt;
|
2018-02-18 17:00:41 +00:00
|
|
|
use toml_query::delete::TomlValueDeleteExt;
|
2018-03-18 16:52:41 +00:00
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
2018-10-30 17:40:52 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::Error;
|
2016-06-14 09:05:38 +00:00
|
|
|
|
2018-02-18 17:00:41 +00:00
|
|
|
use refstore::UniqueRefPathGenerator;
|
2016-06-09 17:34:25 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
pub trait Ref {
|
2016-06-09 17:34:25 +00:00
|
|
|
|
2018-01-08 22:34:13 +00:00
|
|
|
/// Check whether the underlying object is actually a ref
|
|
|
|
fn is_ref(&self) -> Result<bool>;
|
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// Get the stored hash.
|
2017-08-27 18:38:26 +00:00
|
|
|
///
|
2018-02-13 20:11:45 +00:00
|
|
|
/// Does not need a `UniqueRefPathGenerator` as it reads the hash stored in the header
|
2018-02-13 21:37:22 +00:00
|
|
|
fn get_hash(&self) -> Result<&str>;
|
2017-08-27 18:38:26 +00:00
|
|
|
|
2018-03-18 16:52:41 +00:00
|
|
|
/// Make this object a ref
|
|
|
|
fn make_ref<P: AsRef<Path>>(&mut self, hash: String, path: P) -> Result<()>;
|
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// Get the referenced path.
|
|
|
|
///
|
|
|
|
/// Does not need a `UniqueRefPathGenerator` as it reads the path stored in the header.
|
|
|
|
fn get_path(&self) -> Result<PathBuf>;
|
2016-06-24 15:31:28 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// Check whether the referenced file still matches its hash
|
2018-10-30 17:40:52 +00:00
|
|
|
fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> Result<bool>;
|
2016-06-14 09:08:58 +00:00
|
|
|
|
2018-02-18 17:00:41 +00:00
|
|
|
fn remove_ref(&mut self) -> Result<()>;
|
|
|
|
|
2016-06-14 09:08:58 +00:00
|
|
|
/// Alias for `r.fs_link_exists() && r.deref().is_file()`
|
2017-08-27 18:38:26 +00:00
|
|
|
fn is_ref_to_file(&self) -> Result<bool> {
|
2018-02-13 20:11:45 +00:00
|
|
|
self.get_path().map(|p| p.is_file())
|
2016-06-14 09:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Alias for `r.fs_link_exists() && r.deref().is_dir()`
|
2017-08-27 18:38:26 +00:00
|
|
|
fn is_ref_to_dir(&self) -> Result<bool> {
|
2018-02-13 20:11:45 +00:00
|
|
|
self.get_path().map(|p| p.is_dir())
|
2016-06-09 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 09:07:18 +00:00
|
|
|
/// Alias for `!Ref::fs_link_exists()`
|
2017-08-27 18:38:26 +00:00
|
|
|
fn is_dangling(&self) -> Result<bool> {
|
2018-02-13 20:11:45 +00:00
|
|
|
self.get_path().map(|p| !p.exists())
|
2016-06-09 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
}
|
2016-06-24 15:39:58 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
provide_kindflag_path!(pub IsRef, "ref.is_ref");
|
2016-06-24 15:39:58 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
impl Ref for Entry {
|
2016-06-24 15:39:58 +00:00
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
/// Check whether the underlying object is actually a ref
|
|
|
|
fn is_ref(&self) -> Result<bool> {
|
2018-10-30 17:40:52 +00:00
|
|
|
self.is::<IsRef>().map_err(Error::from)
|
2016-06-09 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:37:22 +00:00
|
|
|
fn get_hash(&self) -> Result<&str> {
|
|
|
|
self.get_header()
|
|
|
|
.read("ref.hash")
|
2018-10-30 17:40:52 +00:00
|
|
|
.map_err(Error::from)?
|
|
|
|
.ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.hash")))
|
|
|
|
.and_then(|v| {
|
|
|
|
v.as_str().ok_or_else(|| Error::from(EM::EntryHeaderTypeError2("ref.hash", "string")))
|
|
|
|
})
|
2016-06-09 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 16:52:41 +00:00
|
|
|
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)
|
2018-10-30 17:40:52 +00:00
|
|
|
.ok_or_else(|| EM::UTF8Error)?;
|
2018-03-18 16:52:41 +00:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2018-02-13 20:11:45 +00:00
|
|
|
fn get_path(&self) -> Result<PathBuf> {
|
2018-02-13 21:37:22 +00:00
|
|
|
self.get_header()
|
|
|
|
.read("ref.path")
|
2018-10-30 17:40:52 +00:00
|
|
|
.map_err(Error::from)?
|
|
|
|
.ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.path")))
|
|
|
|
.and_then(|v| {
|
|
|
|
v.as_str()
|
|
|
|
.ok_or_else(|| EM::EntryHeaderTypeError2("ref.path", "string"))
|
|
|
|
.map_err(Error::from)
|
|
|
|
})
|
2018-02-13 21:37:22 +00:00
|
|
|
.map(PathBuf::from)
|
2016-06-09 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 17:40:52 +00:00
|
|
|
fn hash_valid<RPG: UniqueRefPathGenerator>(&self) -> Result<bool> {
|
2018-02-13 21:37:22 +00:00
|
|
|
self.get_path()
|
|
|
|
.map(PathBuf::from)
|
2018-10-30 17:40:52 +00:00
|
|
|
.map_err(Error::from)
|
2018-02-13 21:37:22 +00:00
|
|
|
.and_then(|pb| RPG::unique_hash(pb))
|
|
|
|
.and_then(|h| Ok(h == self.get_hash()?))
|
2016-07-14 18:37:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:00:41 +00:00
|
|
|
fn remove_ref(&mut self) -> Result<()> {
|
|
|
|
let hdr = self.get_header_mut();
|
|
|
|
let _ = hdr.delete("ref.hash")?;
|
|
|
|
let _ = hdr.delete("ref.path")?;
|
|
|
|
let _ = hdr.delete("ref")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:37:32 +00:00
|
|
|
}
|
|
|
|
|