imag/lib/entry/libimagentryref/src/reference.rs

558 lines
21 KiB
Rust
Raw Normal View History

//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// 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.
use std::path::PathBuf;
use std::ops::Deref;
use std::ops::DerefMut;
2016-06-14 09:33:29 +00:00
use std::collections::BTreeMap;
use std::fs::File;
2016-07-04 17:41:21 +00:00
use std::fmt::{Display, Error as FmtError, Formatter};
use std::fs::Permissions;
2016-07-04 17:41:21 +00:00
use std::result::Result as RResult;
2016-06-09 17:34:25 +00:00
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
2016-06-14 09:33:29 +00:00
use libimagstore::storeid::IntoStoreId;
2016-06-09 17:34:25 +00:00
use libimagstore::store::Store;
2016-06-14 09:05:38 +00:00
use toml::Value;
2017-08-26 15:53:08 +00:00
use toml_query::read::TomlValueReadExt;
use toml_query::set::TomlValueSetExt;
use toml_query::insert::TomlValueInsertExt;
2016-06-14 09:05:38 +00:00
use error::RefErrorKind as REK;
use error::RefError as RE;
use error::ResultExt;
2016-06-09 17:34:25 +00:00
use flags::RefFlags;
use error::Result;
use hasher::*;
2016-06-14 09:33:29 +00:00
use module_path::ModuleEntryPath;
2016-06-09 17:34:25 +00:00
2016-06-26 09:38:07 +00:00
#[derive(Debug)]
2016-06-09 17:34:25 +00:00
pub struct Ref<'a>(FileLockEntry<'a>);
impl<'a> Ref<'a> {
2016-06-30 08:57:10 +00:00
/// Try to build a Ref object based on an existing FileLockEntry object
pub fn from_filelockentry(fle: FileLockEntry<'a>) -> Result<Ref<'a>> {
Ref::read_reference(&fle).map(|_| Ref(fle))
}
2016-06-14 09:05:38 +00:00
/// Try to get `si` as Ref object from the store
pub fn get(store: &'a Store, si: StoreId) -> Result<Ref<'a>> {
match store.get(si) {
Err(e) => return Err(e).chain_err(|| REK::StoreReadError),
Ok(None) => return Err(RE::from_kind(REK::RefNotInStore)),
2016-06-30 08:57:10 +00:00
Ok(Some(fle)) => Ref::from_filelockentry(fle),
2016-06-14 09:05:38 +00:00
}
}
2016-06-28 21:01:42 +00:00
/// Get a Ref object from the store by hash.
///
/// Returns None if the hash cannot be found.
pub fn get_by_hash(store: &'a Store, hash: String) -> Result<Option<Ref<'a>>> {
ModuleEntryPath::new(hash)
.into_storeid()
.and_then(|id| store.get(id))
2016-06-28 21:01:42 +00:00
.map(|opt_fle| opt_fle.map(|fle| Ref(fle)))
.chain_err(|| REK::StoreReadError)
2016-06-28 21:01:42 +00:00
}
/// Delete a ref by hash
2016-06-28 20:54:43 +00:00
///
/// If the returned Result contains an error, the ref might not be deleted.
pub fn delete_by_hash(store: &'a Store, hash: String) -> Result<()> {
ModuleEntryPath::new(hash)
.into_storeid()
.and_then(|id| store.delete(id))
.chain_err(|| REK::StoreWriteError)
2016-06-28 20:54:43 +00:00
}
2016-06-14 09:05:38 +00:00
fn read_reference(fle: &FileLockEntry<'a>) -> Result<PathBuf> {
2016-07-14 18:42:39 +00:00
match fle.get_header().read("ref.path") {
2017-08-26 15:53:08 +00:00
Ok(Some(&Value::String(ref s))) => Ok(PathBuf::from(s)),
Ok(Some(_)) => Err(RE::from_kind(REK::HeaderTypeError)),
Ok(None) => Err(RE::from_kind(REK::HeaderFieldMissingError)),
Err(e) => Err(e).chain_err(|| REK::StoreReadError),
2016-06-14 09:05:38 +00:00
}
2016-06-09 17:34:25 +00:00
}
pub fn create_with_hasher<H: Hasher>(store: &'a Store, pb: PathBuf, flags: RefFlags, mut h: H)
-> Result<Ref<'a>>
{
2016-06-14 09:33:29 +00:00
if !pb.exists() {
return Err(RE::from_kind(REK::RefTargetDoesNotExist));
2016-06-14 09:33:29 +00:00
}
if flags.get_content_hashing() && pb.is_dir() {
return Err(RE::from_kind(REK::RefTargetCannotBeHashed));
2016-06-14 09:33:29 +00:00
}
let (mut fle, content_hash, permissions, canonical_path) = { // scope to be able to fold
try!(File::open(pb.clone())
.chain_err(|| REK::RefTargetFileCannotBeOpened)
2016-06-14 09:33:29 +00:00
// If we were able to open this file,
// we hash the contents of the file and return (file, hash)
.and_then(|mut file| {
let opt_contenthash = if flags.get_content_hashing() {
Some(try!(h.create_hash(&pb, &mut file)))
2016-06-14 09:33:29 +00:00
} else {
None
};
Ok((file, opt_contenthash))
})
// and then we get the permissions if we have to
// and return (file, content hash, permissions)
.and_then(|(file, opt_contenthash)| {
let opt_permissions = if flags.get_permission_tracking() {
Some(try!(file
.metadata()
.map(|md| md.permissions())
.chain_err(|| REK::RefTargetCannotReadPermissions)
2016-06-14 09:33:29 +00:00
))
} else {
None
};
2017-02-05 14:01:46 +00:00
Ok((opt_contenthash, opt_permissions))
2016-06-14 09:33:29 +00:00
})
// and then we try to canonicalize the PathBuf, because we want to store a
// canonicalized path
// and return (file, content hash, permissions, canonicalized path)
2017-02-05 14:01:46 +00:00
.and_then(|(opt_contenthash, opt_permissions)| {
2016-06-14 09:33:29 +00:00
pb.canonicalize()
2017-02-05 14:01:46 +00:00
.map(|can| (opt_contenthash, opt_permissions, can))
2016-06-14 09:33:29 +00:00
// if PathBuf::canonicalize() failed, build an error from the return value
.chain_err(|| REK::PathCanonicalizationError)
2016-06-14 09:33:29 +00:00
})
// and then we hash the canonicalized path
// and return (file, content hash, permissions, canonicalized path, path hash)
2017-02-05 14:01:46 +00:00
.and_then(|(opt_contenthash, opt_permissions, can)| {
2016-06-14 09:33:29 +00:00
let path_hash = try!(Ref::hash_path(&can)
.chain_err(|| REK::PathHashingError)
2016-06-14 09:33:29 +00:00
);
2016-11-01 15:39:46 +00:00
Ok((opt_contenthash, opt_permissions, can, path_hash))
2016-06-14 09:33:29 +00:00
})
// and then we convert the PathBuf of the canonicalized path to a String to be able
// to save it in the Ref FileLockEntry obj
// and return
// (file, content hash, permissions, canonicalized path as String, path hash)
2016-11-01 15:39:46 +00:00
.and_then(|(opt_conhash, opt_perm, can, path_hash)| {
2016-06-14 09:33:29 +00:00
match can.to_str().map(String::from) {
// UTF convert error in PathBuf::to_str(),
None => Err(RE::from_kind(REK::PathUTF8Error)),
2016-10-07 22:53:52 +00:00
Some(can) => Ok((opt_conhash, opt_perm, can, path_hash))
2016-06-14 09:33:29 +00:00
}
})
// and then we create the FileLockEntry in the Store
// and return (filelockentry, content hash, permissions, canonicalized path)
2016-10-07 22:53:52 +00:00
.and_then(|(opt_conhash, opt_perm, can, path_hash)| {
2016-06-14 09:33:29 +00:00
let fle = try!(store
.create(ModuleEntryPath::new(path_hash))
.chain_err(|| REK::StoreWriteError)
2016-06-14 09:33:29 +00:00
);
Ok((fle, opt_conhash, opt_perm, can))
})
)
};
for tpl in [
Some((String::from("ref"), Value::Table(BTreeMap::new()))),
Some((String::from("ref.permissions"), Value::Table(BTreeMap::new()))),
Some((String::from("ref.path"), Value::String(canonical_path))),
Some((String::from("ref.content_hash"), Value::Table(BTreeMap::new()))),
content_hash.map(|hash| {
(format!("ref.content_hash.{}", h.hash_name()), Value::String(hash))
}),
permissions.map(|p| {
(String::from("ref.permissions.ro"), Value::Boolean(p.readonly()))
}),
2016-06-14 09:33:29 +00:00
].into_iter()
{
match tpl {
&Some((ref s, ref v)) => {
match fle.get_header_mut().insert(s, v.clone()) {
2017-08-26 15:53:08 +00:00
Ok(None) => {
debug!("Header insert worked");
}
Ok(Some(val)) => {
debug!("Overwrote: {}, which was: {:?}", s, val);
2016-06-14 09:33:29 +00:00
},
Err(e) => {
return Err(e).chain_err(|| REK::HeaderFieldWriteError);
2016-06-14 09:33:29 +00:00
},
}
}
&None => {
debug!("Not going to insert.");
}
}
}
Ok(Ref(fle))
2016-06-09 17:34:25 +00:00
}
/// Create a Ref object which refers to `pb`
pub fn create(store: &'a Store, pb: PathBuf, flags: RefFlags) -> Result<Ref<'a>> {
Ref::create_with_hasher(store, pb, flags, DefaultHasher::new())
}
2016-06-09 17:34:25 +00:00
/// Creates a Hash from a PathBuf by making the PathBuf absolute and then running a hash
/// algorithm on it
fn hash_path(pb: &PathBuf) -> Result<String> {
use crypto::sha1::Sha1;
use crypto::digest::Digest;
match pb.to_str() {
Some(s) => {
let mut hasher = Sha1::new();
hasher.input_str(s);
Ok(hasher.result_str())
},
None => return Err(RE::from_kind(REK::PathUTF8Error)),
}
2016-06-09 17:34:25 +00:00
}
2016-06-30 09:10:38 +00:00
/// Get the hash from the path of the ref
pub fn get_path_hash(&self) -> Result<String> {
self.0
.get_location()
.clone()
.into_pathbuf()
.chain_err(|| REK::StoreIdError)
.and_then(|pb| {
pb.file_name()
.and_then(|osstr| osstr.to_str())
.and_then(|s| s.split("~").next())
.map(String::from)
.ok_or(RE::from_kind(REK::StoreIdError))
})
2016-06-30 09:10:38 +00:00
}
/// Get the hash of the link target which is stored in the ref object
pub fn get_stored_hash(&self) -> Result<String> {
self.get_stored_hash_with_hasher(&DefaultHasher::new())
}
/// Get the hahs of the link target which is stored in the ref object, which is hashed with a
/// custom Hasher instance.
pub fn get_stored_hash_with_hasher<H: Hasher>(&self, h: &H) -> Result<String> {
match self.0.get_header().read(&format!("ref.content_hash.{}", h.hash_name())[..]) {
// content hash stored...
2017-08-26 15:53:08 +00:00
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
// content hash header field has wrong type
Ok(Some(_)) => Err(RE::from_kind(REK::HeaderTypeError)),
// content hash not stored
Ok(None) => Err(RE::from_kind(REK::HeaderFieldMissingError)),
// Error
Err(e) => Err(e).chain_err(|| REK::StoreReadError),
}
}
/// Get the hash of the link target by reading the link target and hashing the contents
pub fn get_current_hash(&self) -> Result<String> {
self.get_current_hash_with_hasher(DefaultHasher::new())
}
/// Get the hash of the link target by reading the link target and hashing the contents with the
/// custom hasher
pub fn get_current_hash_with_hasher<H: Hasher>(&self, mut h: H) -> Result<String> {
self.fs_file()
.and_then(|pb| {
File::open(pb.clone())
.map(|f| (pb, f))
.chain_err(|| REK::IOError)
})
.and_then(|(path, mut file)| h.create_hash(&path, &mut file))
}
/// Get the permissions of the file which are present
fn get_current_permissions(&self) -> Result<Permissions> {
self.fs_file()
.and_then(|pb| {
File::open(pb)
.chain_err(|| REK::HeaderFieldReadError)
})
.and_then(|file| {
file
.metadata()
.map(|md| md.permissions())
.chain_err(|| REK::RefTargetCannotReadPermissions)
})
}
2016-06-09 17:34:25 +00:00
/// check whether the pointer the Ref represents still points to a file which exists
2016-06-23 12:14:39 +00:00
pub fn fs_link_exists(&self) -> Result<bool> {
self.fs_file().map(|pathbuf| pathbuf.exists())
2016-06-14 09:08:58 +00:00
}
/// Alias for `r.fs_link_exists() && r.deref().is_file()`
2016-06-23 12:17:48 +00:00
pub fn is_ref_to_file(&self) -> Result<bool> {
self.fs_file().map(|pathbuf| pathbuf.is_file())
2016-06-14 09:09:19 +00:00
}
/// Alias for `r.fs_link_exists() && r.deref().is_dir()`
2016-06-23 12:18:16 +00:00
pub fn is_ref_to_dir(&self) -> Result<bool> {
self.fs_file().map(|pathbuf| pathbuf.is_dir())
2016-06-09 17:34:25 +00:00
}
2016-06-14 09:07:18 +00:00
/// Alias for `!Ref::fs_link_exists()`
2016-06-23 12:14:39 +00:00
pub fn is_dangling(&self) -> Result<bool> {
self.fs_link_exists().map(|b| !b)
2016-06-14 09:07:18 +00:00
}
2016-06-09 17:34:25 +00:00
/// check whether the pointer the Ref represents is valid
/// This includes:
/// - Hashsum of the file is still the same as stored in the Ref
/// - file permissions are still valid
2016-06-24 15:01:40 +00:00
pub fn fs_link_valid(&self) -> Result<bool> {
match (self.fs_link_valid_permissions(), self.fs_link_valid_hash()) {
(Ok(true) , Ok(true)) => Ok(true),
(Ok(_) , Ok(_)) => Ok(false),
(Err(e) , _) => Err(e),
(_ , Err(e)) => Err(e),
}
2016-06-09 17:34:25 +00:00
}
/// Check whether the file permissions of the referenced file are equal to the stored
/// permissions
2016-06-24 14:58:41 +00:00
pub fn fs_link_valid_permissions(&self) -> Result<bool> {
self.0
.get_header()
.read("ref.permissions.ro")
.chain_err(|| REK::HeaderFieldReadError)
2016-06-24 14:58:41 +00:00
.and_then(|ro| {
match ro {
2017-08-26 15:53:08 +00:00
Some(&Value::Boolean(b)) => Ok(b),
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
2016-06-24 14:58:41 +00:00
}
})
.and_then(|ro| self.get_current_permissions().map(|perm| ro == perm.readonly()))
.chain_err(|| REK::RefTargetCannotReadPermissions)
2016-06-09 17:34:25 +00:00
}
/// Check whether the Hashsum of the referenced file is equal to the stored hashsum
2016-06-23 13:26:28 +00:00
pub fn fs_link_valid_hash(&self) -> Result<bool> {
let stored_hash = try!(self.get_stored_hash());
let current_hash = try!(self.get_current_hash());
2016-06-23 13:26:28 +00:00
Ok(stored_hash == current_hash)
2016-06-09 17:34:25 +00:00
}
/// Update the Ref by re-checking the file from FS
/// This errors if the file is not present or cannot be read()
pub fn update_ref(&mut self) -> Result<()> {
self.update_ref_with_hasher(&DefaultHasher::new())
}
/// Update the Ref by re-checking the file from FS using the passed Hasher instance
/// This errors if the file is not present or cannot be read()
pub fn update_ref_with_hasher<H: Hasher>(&mut self, h: &H) -> Result<()> {
let current_hash = try!(self.get_current_hash()); // uses the default hasher
2016-06-24 15:39:58 +00:00
let current_perm = try!(self.get_current_permissions());
try!(self.0
.get_header_mut()
.set("ref.permissions.ro", Value::Boolean(current_perm.readonly()))
.chain_err(|| REK::StoreWriteError)
2016-06-24 15:39:58 +00:00
);
try!(self.0
.get_header_mut()
.set(&format!("ref.content_hash.{}", h.hash_name())[..], Value::String(current_hash))
.chain_err(|| REK::StoreWriteError)
2016-06-24 15:39:58 +00:00
);
Ok(())
2016-06-09 17:34:25 +00:00
}
/// Get the path of the file which is reffered to by this Ref
2016-06-23 12:16:05 +00:00
pub fn fs_file(&self) -> Result<PathBuf> {
match self.0.get_header().read("ref.path") {
2017-08-26 15:53:08 +00:00
Ok(Some(&Value::String(ref s))) => Ok(PathBuf::from(s)),
Ok(Some(_)) => Err(RE::from_kind(REK::HeaderTypeError)),
Ok(None) => Err(RE::from_kind(REK::HeaderFieldMissingError)),
Err(e) => Err(e).chain_err(|| REK::StoreReadError),
2016-06-23 12:16:05 +00:00
}
2016-06-09 17:34:25 +00:00
}
/// Check whether there is a reference to the file at `pb`
pub fn exists(store: &Store, pb: PathBuf) -> Result<bool> {
2016-06-23 21:00:34 +00:00
pb.canonicalize()
.chain_err(|| REK::PathCanonicalizationError)
2016-06-23 21:00:34 +00:00
.and_then(|can| {
Ref::hash_path(&can)
.chain_err(|| REK::PathHashingError)
2016-06-23 21:00:34 +00:00
})
.and_then(|hash| {
store.retrieve_for_module("ref").map(|iter| (hash, iter))
.chain_err(|| REK::StoreReadError)
2016-06-23 21:00:34 +00:00
})
.and_then(|(hash, possible_refs)| {
// This is kind of a manual Iterator::filter() call what we do here, but with the
// actual ::filter method we cannot return the error in a nice way, so we do it
// manually here. If you can come up with a better version of this, feel free to
// take this note as a todo.
for r in possible_refs {
let contains_hash = try!(r.to_str()
.chain_err(|| REK::TypeConversionError)
.map(|s| s.contains(&hash[..])));
2016-06-23 21:00:34 +00:00
if !contains_hash {
continue;
}
match store.get(r) {
Ok(Some(fle)) => {
if Ref::read_reference(&fle).map(|path| path == pb).unwrap_or(false) {
return Ok(true)
}
},
Ok(None) => { // Something weird just happened
return Err(RE::from_kind(REK::StoreReadError));
2016-06-23 21:00:34 +00:00
},
Err(e) => {
return Err(e).chain_err(|| REK::StoreReadError);
2016-06-23 21:00:34 +00:00
},
}
}
Ok(false)
})
2016-06-09 17:34:25 +00:00
}
/// Re-find a referenced file
///
/// This function tries to re-find a ref by searching all directories in `search_roots` recursively
2016-06-25 14:55:49 +00:00
/// for a file which matches the hash of the Ref.
2016-06-09 17:34:25 +00:00
///
/// If `search_roots` is `None`, it starts at the filesystem root `/`.
///
2016-06-25 14:55:49 +00:00
/// If the target cannot be found, this yields a RefTargetDoesNotExist error kind.
///
2016-06-09 17:34:25 +00:00
/// # Warning
///
/// This option causes heavy I/O as it recursively searches the Filesystem.
2016-06-25 14:55:49 +00:00
pub fn refind(&self, search_roots: Option<Vec<PathBuf>>) -> Result<PathBuf> {
self.refind_with_hasher(search_roots, DefaultHasher::new())
}
pub fn refind_with_hasher<H: Hasher>(&self, search_roots: Option<Vec<PathBuf>>, mut h: H)
-> Result<PathBuf>
{
2016-06-25 14:55:49 +00:00
use itertools::Itertools;
use walkdir::WalkDir;
self.get_stored_hash()
.and_then(|stored_hash| {
search_roots
.unwrap_or(vec![PathBuf::from("/")])
.into_iter()
.map(|root| {
WalkDir::new(root)
.follow_links(false)
.into_iter()
.map(|entry| {
entry
.chain_err(|| REK::IOError)
2016-06-25 14:55:49 +00:00
.and_then(|entry| {
let pb = PathBuf::from(entry.path());
File::open(entry.path())
.chain_err(|| REK::IOError)
2016-06-25 14:55:49 +00:00
.map(|f| (pb, f))
})
.and_then(|(p, mut f)| h.create_hash(&p, &mut f).map(|h| (p, h)))
2016-06-25 14:55:49 +00:00
.map(|(path, hash)| {
if hash == stored_hash {
Some(path)
} else {
None
}
})
.chain_err(|| REK::IOError)
2016-06-25 14:55:49 +00:00
})
.filter_map(|e| e.ok())
.filter_map(|e| e)
.next()
})
.flatten()
.next()
.ok_or(RE::from_kind(REK::RefTargetDoesNotExist))
2016-06-25 14:55:49 +00:00
})
2016-06-09 17:34:25 +00:00
}
}
impl<'a> Deref for Ref<'a> {
type Target = FileLockEntry<'a>;
fn deref(&self) -> &FileLockEntry<'a> {
&self.0
}
}
impl<'a> DerefMut for Ref<'a> {
fn deref_mut(&mut self) -> &mut FileLockEntry<'a> {
&mut self.0
}
}
2016-07-04 17:41:21 +00:00
impl<'a> Display for Ref<'a> {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
let path = self.fs_file()
.map(|pb| String::from(pb.to_str().unwrap_or("<UTF8-Error>")))
.unwrap_or(String::from("Could not read Path from reference object"));
let hash = self.get_stored_hash().unwrap_or(String::from("<could not read hash>"));
write!(fmt, "Ref({} -> {})", hash, path)
}
}
2016-07-14 18:37:32 +00:00
impl<'a> Into<FileLockEntry<'a>> for Ref<'a> {
fn into(self) -> FileLockEntry<'a> {
self.0
}
}