2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// 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;
|
2016-06-23 13:26:18 +00:00
|
|
|
use std::fs::File;
|
2016-06-24 15:36:55 +00:00
|
|
|
use std::fs::Permissions;
|
2016-06-09 17:34:25 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
use libimagstore::store::Entry;
|
2016-06-09 17:34:25 +00:00
|
|
|
|
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;
|
2016-06-14 09:05:38 +00:00
|
|
|
|
|
|
|
use error::RefErrorKind as REK;
|
2017-09-03 19:28:36 +00:00
|
|
|
use error::RefError as RE;
|
2017-09-03 13:28:13 +00:00
|
|
|
use error::ResultExt;
|
2017-09-03 19:39:32 +00:00
|
|
|
use error::Result;
|
2016-08-09 16:56:14 +00:00
|
|
|
use hasher::*;
|
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
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Get the hash from the path of the ref
|
|
|
|
fn get_path_hash(&self) -> Result<String>;
|
2016-06-09 17:34:25 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Get the hash of the link target which is stored in the ref object
|
|
|
|
fn get_stored_hash(&self) -> Result<String>;
|
2016-06-30 08:57:10 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Get the hahs of the link target which is stored in the ref object, which is hashed with a
|
|
|
|
/// custom Hasher instance.
|
|
|
|
fn get_stored_hash_with_hasher<H: Hasher>(&self, h: &H) -> Result<String>;
|
2016-06-14 09:05:38 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Get the hash of the link target by reading the link target and hashing the contents
|
|
|
|
fn get_current_hash(&self) -> Result<String>;
|
2016-06-28 21:01:42 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Get the hash of the link target by reading the link target and hashing the contents with the
|
|
|
|
/// custom hasher
|
|
|
|
fn get_current_hash_with_hasher<H: Hasher>(&self, h: H) -> Result<String>;
|
2016-06-28 20:54:43 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// check whether the pointer the Ref represents still points to a file which exists
|
|
|
|
fn fs_link_exists(&self) -> Result<bool>;
|
2016-06-09 17:34:25 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Alias for `r.fs_link_exists() && r.deref().is_file()`
|
|
|
|
fn is_ref_to_file(&self) -> Result<bool>;
|
2016-06-14 09:33:29 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Alias for `r.fs_link_exists() && r.deref().is_dir()`
|
|
|
|
fn is_ref_to_dir(&self) -> Result<bool>;
|
2016-06-14 09:33:29 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Alias for `!Ref::fs_link_exists()`
|
|
|
|
fn is_dangling(&self) -> Result<bool>;
|
2016-06-09 17:34:25 +00:00
|
|
|
|
2017-08-27 18:38:26 +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
|
|
|
|
fn fs_link_valid(&self) -> Result<bool>;
|
2016-08-09 16:56:14 +00:00
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Check whether the file permissions of the referenced file are equal to the stored
|
|
|
|
/// permissions
|
|
|
|
fn fs_link_valid_permissions(&self) -> Result<bool>;
|
|
|
|
|
|
|
|
/// Check whether the Hashsum of the referenced file is equal to the stored hashsum
|
|
|
|
fn fs_link_valid_hash(&self) -> Result<bool>;
|
|
|
|
|
|
|
|
/// Update the Ref by re-checking the file from FS
|
|
|
|
/// This errors if the file is not present or cannot be read()
|
|
|
|
fn update_ref(&mut self) -> Result<()>;
|
|
|
|
|
|
|
|
/// 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()
|
|
|
|
fn update_ref_with_hasher<H: Hasher>(&mut self, h: &H) -> Result<()>;
|
|
|
|
|
|
|
|
/// Get the path of the file which is reffered to by this Ref
|
|
|
|
fn fs_file(&self) -> Result<PathBuf>;
|
|
|
|
|
|
|
|
/// Re-find a referenced file
|
|
|
|
///
|
|
|
|
/// This function tries to re-find a ref by searching all directories in `search_roots` recursively
|
|
|
|
/// for a file which matches the hash of the Ref.
|
|
|
|
///
|
|
|
|
/// If `search_roots` is `None`, it starts at the filesystem root `/`.
|
|
|
|
///
|
|
|
|
/// If the target cannot be found, this yields a RefTargetDoesNotExist error kind.
|
|
|
|
///
|
|
|
|
/// # Warning
|
|
|
|
///
|
|
|
|
/// This option causes heavy I/O as it recursively searches the Filesystem.
|
|
|
|
fn refind(&self, search_roots: Option<Vec<PathBuf>>) -> Result<PathBuf>;
|
|
|
|
|
|
|
|
/// See documentation of `Ref::refind()`
|
|
|
|
fn refind_with_hasher<H: Hasher>(&self, search_roots: Option<Vec<PathBuf>>, h: H)
|
|
|
|
-> Result<PathBuf>;
|
|
|
|
|
|
|
|
/// Get the permissions of the file which are present
|
|
|
|
fn get_current_permissions(&self) -> Result<Permissions>;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Ref for Entry {
|
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
|
2017-08-27 18:38:26 +00:00
|
|
|
fn get_path_hash(&self) -> Result<String> {
|
|
|
|
self.get_location()
|
2016-09-06 10:23:44 +00:00
|
|
|
.clone()
|
|
|
|
.into_pathbuf()
|
2017-09-23 18:53:07 +00:00
|
|
|
.map_err(From::from)
|
2016-09-06 10:23:44 +00:00
|
|
|
.and_then(|pb| {
|
|
|
|
pb.file_name()
|
|
|
|
.and_then(|osstr| osstr.to_str())
|
|
|
|
.and_then(|s| s.split("~").next())
|
|
|
|
.map(String::from)
|
2017-09-23 18:53:07 +00:00
|
|
|
.ok_or(String::from("String splitting error"))
|
|
|
|
.map_err(From::from)
|
2016-09-06 10:23:44 +00:00
|
|
|
})
|
2016-06-30 09:10:38 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 15:31:28 +00:00
|
|
|
/// Get the hash of the link target which is stored in the ref object
|
2017-08-27 18:38:26 +00:00
|
|
|
fn get_stored_hash(&self) -> Result<String> {
|
2016-09-05 12:23:47 +00:00
|
|
|
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.
|
2017-08-27 18:38:26 +00:00
|
|
|
fn get_stored_hash_with_hasher<H: Hasher>(&self, h: &H) -> Result<String> {
|
2017-09-23 18:53:07 +00:00
|
|
|
match self.get_header().read(&format!("ref.content_hash.{}", h.hash_name())[..])? {
|
2016-06-24 15:31:28 +00:00
|
|
|
// content hash stored...
|
2017-09-23 18:53:07 +00:00
|
|
|
Some(&Value::String(ref s)) => Ok(s.clone()),
|
2016-06-24 15:31:28 +00:00
|
|
|
|
|
|
|
// content hash header field has wrong type
|
2017-09-23 18:53:07 +00:00
|
|
|
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
|
2016-06-24 15:31:28 +00:00
|
|
|
|
|
|
|
// content hash not stored
|
2017-09-23 18:53:07 +00:00
|
|
|
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
|
2016-06-24 15:31:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the hash of the link target by reading the link target and hashing the contents
|
2017-08-27 18:38:26 +00:00
|
|
|
fn get_current_hash(&self) -> Result<String> {
|
2016-08-09 16:56:14 +00:00
|
|
|
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
|
2017-08-27 18:38:26 +00:00
|
|
|
fn get_current_hash_with_hasher<H: Hasher>(&self, mut h: H) -> Result<String> {
|
2016-06-24 15:31:28 +00:00
|
|
|
self.fs_file()
|
2017-09-23 18:53:07 +00:00
|
|
|
.and_then(|pb| File::open(pb.clone()).map(|f| (pb, f)).map_err(From::from))
|
2016-08-09 16:56:14 +00:00
|
|
|
.and_then(|(path, mut file)| h.create_hash(&path, &mut file))
|
2016-06-24 15:31:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 17:34:25 +00:00
|
|
|
/// check whether the pointer the Ref represents still points to a file which exists
|
2017-08-27 18:38:26 +00:00
|
|
|
fn fs_link_exists(&self) -> Result<bool> {
|
2016-06-23 12:14:39 +00:00
|
|
|
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()`
|
2017-08-27 18:38:26 +00:00
|
|
|
fn is_ref_to_file(&self) -> Result<bool> {
|
2016-06-23 12:17:48 +00:00
|
|
|
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()`
|
2017-08-27 18:38:26 +00:00
|
|
|
fn is_ref_to_dir(&self) -> Result<bool> {
|
2016-06-23 12:18:16 +00:00
|
|
|
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()`
|
2017-08-27 18:38:26 +00:00
|
|
|
fn is_dangling(&self) -> Result<bool> {
|
2016-06-23 12:14:39 +00:00
|
|
|
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
|
2017-08-27 18:38:26 +00:00
|
|
|
fn fs_link_valid(&self) -> Result<bool> {
|
2016-06-24 15:01:40 +00:00
|
|
|
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
|
2017-08-27 18:38:26 +00:00
|
|
|
fn fs_link_valid_permissions(&self) -> Result<bool> {
|
|
|
|
self
|
2016-06-24 14:58:41 +00:00
|
|
|
.get_header()
|
|
|
|
.read("ref.permissions.ro")
|
2017-09-03 13:28:13 +00:00
|
|
|
.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),
|
2017-09-03 19:28:36 +00:00
|
|
|
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
|
|
|
|
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
|
2016-06-24 14:58:41 +00:00
|
|
|
}
|
|
|
|
})
|
2016-06-24 15:36:55 +00:00
|
|
|
.and_then(|ro| self.get_current_permissions().map(|perm| ro == perm.readonly()))
|
2017-09-03 13:28:13 +00:00
|
|
|
.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
|
2017-08-27 18:38:26 +00:00
|
|
|
fn fs_link_valid_hash(&self) -> Result<bool> {
|
2017-10-30 19:17:22 +00:00
|
|
|
let stored_hash = self.get_stored_hash()?;
|
|
|
|
let current_hash = 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()
|
2017-08-27 18:38:26 +00:00
|
|
|
fn update_ref(&mut self) -> Result<()> {
|
2016-09-05 12:21:56 +00:00
|
|
|
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()
|
2017-08-27 18:38:26 +00:00
|
|
|
fn update_ref_with_hasher<H: Hasher>(&mut self, h: &H) -> Result<()> {
|
2017-10-30 19:17:22 +00:00
|
|
|
let current_hash = self.get_current_hash()?; // uses the default hasher
|
|
|
|
let current_perm = self.get_current_permissions()?;
|
2016-06-24 15:39:58 +00:00
|
|
|
|
2017-10-30 19:17:22 +00:00
|
|
|
self
|
2016-06-24 15:39:58 +00:00
|
|
|
.get_header_mut()
|
|
|
|
.set("ref.permissions.ro", Value::Boolean(current_perm.readonly()))
|
2017-10-30 19:17:22 +00:00
|
|
|
?;
|
2016-06-24 15:39:58 +00:00
|
|
|
|
2017-10-30 19:17:22 +00:00
|
|
|
self
|
2016-06-24 15:39:58 +00:00
|
|
|
.get_header_mut()
|
2016-09-05 12:21:56 +00:00
|
|
|
.set(&format!("ref.content_hash.{}", h.hash_name())[..], Value::String(current_hash))
|
2017-10-30 19:17:22 +00:00
|
|
|
?;
|
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
|
2017-08-27 18:38:26 +00:00
|
|
|
fn fs_file(&self) -> Result<PathBuf> {
|
2017-09-23 18:53:07 +00:00
|
|
|
match self.get_header().read("ref.path")? {
|
|
|
|
Some(&Value::String(ref s)) => Ok(PathBuf::from(s)),
|
|
|
|
Some(_) => Err(RE::from_kind(REK::HeaderTypeError)),
|
|
|
|
None => Err(RE::from_kind(REK::HeaderFieldMissingError)),
|
2016-06-23 12:16:05 +00:00
|
|
|
}
|
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.
|
2017-08-27 18:38:26 +00:00
|
|
|
fn refind(&self, search_roots: Option<Vec<PathBuf>>) -> Result<PathBuf> {
|
2016-08-09 16:56:14 +00:00
|
|
|
self.refind_with_hasher(search_roots, DefaultHasher::new())
|
|
|
|
}
|
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// See documentation of `Ref::refind()`
|
|
|
|
fn refind_with_hasher<H: Hasher>(&self, search_roots: Option<Vec<PathBuf>>, mut h: H)
|
2016-08-09 16:56:14 +00:00
|
|
|
-> 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
|
2017-09-23 18:53:07 +00:00
|
|
|
.map_err(From::from)
|
2016-06-25 14:55:49 +00:00
|
|
|
.and_then(|entry| {
|
|
|
|
let pb = PathBuf::from(entry.path());
|
|
|
|
File::open(entry.path())
|
|
|
|
.map(|f| (pb, f))
|
2017-09-23 18:53:07 +00:00
|
|
|
.map_err(From::from)
|
|
|
|
})
|
|
|
|
.and_then(|(p, mut f)| {
|
|
|
|
h.create_hash(&p, &mut f)
|
|
|
|
.map(|h| (p, h))
|
|
|
|
.map_err(From::from)
|
2016-06-25 14:55:49 +00:00
|
|
|
})
|
|
|
|
.map(|(path, hash)| {
|
|
|
|
if hash == stored_hash {
|
|
|
|
Some(path)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-09-23 18:53:07 +00:00
|
|
|
.filter_map(Result::ok)
|
2016-06-25 14:55:49 +00:00
|
|
|
.filter_map(|e| e)
|
|
|
|
.next()
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.next()
|
2017-09-03 19:28:36 +00:00
|
|
|
.ok_or(RE::from_kind(REK::RefTargetDoesNotExist))
|
2016-06-25 14:55:49 +00:00
|
|
|
})
|
2016-06-09 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 18:38:26 +00:00
|
|
|
/// Get the permissions of the file which are present
|
|
|
|
fn get_current_permissions(&self) -> Result<Permissions> {
|
|
|
|
self.fs_file()
|
2017-09-04 21:02:45 +00:00
|
|
|
.and_then(|pb| {
|
|
|
|
File::open(pb)
|
|
|
|
.chain_err(|| REK::HeaderFieldReadError)
|
|
|
|
})
|
2017-08-27 18:38:26 +00:00
|
|
|
.and_then(|file| {
|
|
|
|
file
|
|
|
|
.metadata()
|
|
|
|
.map(|md| md.permissions())
|
2017-09-04 21:02:45 +00:00
|
|
|
.chain_err(|| REK::RefTargetCannotReadPermissions)
|
2017-08-27 18:38:26 +00:00
|
|
|
})
|
2016-07-14 18:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|