Impl Ref::create()

This commit is contained in:
Matthias Beyer 2016-06-14 11:33:29 +02:00
parent ca86f3c6ad
commit 4ccbb174fa

View file

@ -4,9 +4,11 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::ops::Deref; use std::ops::Deref;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::collections::BTreeMap;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
@ -15,6 +17,7 @@ use toml::Value;
use error::RefErrorKind as REK; use error::RefErrorKind as REK;
use flags::RefFlags; use flags::RefFlags;
use result::Result; use result::Result;
use module_path::ModuleEntryPath;
pub struct Ref<'a>(FileLockEntry<'a>); pub struct Ref<'a>(FileLockEntry<'a>);
@ -39,8 +42,138 @@ impl<'a> Ref<'a> {
} }
/// Create a Ref object which refers to `pb` /// Create a Ref object which refers to `pb`
pub fn create(store: &Store, pb: PathBuf, flags: RefFlags) -> Result<Ref<'a>> { pub fn create(store: &'a Store, pb: PathBuf, flags: RefFlags) -> Result<Ref<'a>> {
unimplemented!() use std::fs::File;
use std::io::Read;
use crypto::sha1::Sha1;
use crypto::digest::Digest;
if !pb.exists() {
return Err(REK::RefTargetDoesNotExist.into_error());
}
if flags.get_content_hashing() && pb.is_dir() {
return Err(REK::RefTargetCannotBeHashed.into_error());
}
let (mut fle, content_hash, permissions, canonical_path) = { // scope to be able to fold
try!(File::open(pb.clone())
.map_err(Box::new)
.map_err(|e| REK::RefTargetFileCannotBeOpened.into_error_with_cause(e))
// 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() {
let mut hasher = Sha1::new();
let mut s = String::new();
file.read_to_string(&mut s);
hasher.input_str(&s[..]);
Some(hasher.result_str())
} 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())
.map_err(Box::new)
.map_err(|e| REK::RefTargetCannotReadPermissions.into_error_with_cause(e))
))
} else {
None
};
Ok((file, opt_contenthash, opt_permissions))
})
// and then we try to canonicalize the PathBuf, because we want to store a
// canonicalized path
// and return (file, content hash, permissions, canonicalized path)
.and_then(|(file, opt_contenthash, opt_permissions)| {
pb.canonicalize()
.map(|can| (file, opt_contenthash, opt_permissions, can))
// if PathBuf::canonicalize() failed, build an error from the return value
.map_err(|e| REK::PathCanonicalizationError.into_error_with_cause(Box::new(e)))
})
// and then we hash the canonicalized path
// and return (file, content hash, permissions, canonicalized path, path hash)
.and_then(|(file, opt_contenthash, opt_permissions, can)| {
let path_hash = try!(Ref::hash_path(&can)
.map_err(Box::new)
.map_err(|e| REK::PathHashingError.into_error_with_cause(e))
);
Ok((file, opt_contenthash, opt_permissions, can, path_hash))
})
// 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)
.and_then(|(file, opt_conhash, opt_perm, can, path_hash)| {
match can.to_str().map(String::from) {
// UTF convert error in PathBuf::to_str(),
None => Err(REK::PathUTF8Error.into_error()),
Some(can) => Ok((file, opt_conhash, opt_perm, can, path_hash))
}
})
// and then we create the FileLockEntry in the Store
// and return (filelockentry, content hash, permissions, canonicalized path)
.and_then(|(file, opt_conhash, opt_perm, can, path_hash)| {
let fle = try!(store
.create(ModuleEntryPath::new(path_hash))
.map_err(Box::new)
.map_err(|e| REK::StoreWriteError.into_error_with_cause(e))
);
Ok((fle, opt_conhash, opt_perm, can))
})
)
};
for tpl in [
Some(("ref", Value::Table(BTreeMap::new()))),
Some(("ref.permissions", Value::Table(BTreeMap::new()))),
Some(("ref.path", Value::String(canonical_path))),
content_hash.map(|h| ("ref.content_hash", Value::String(h))),
permissions.map(|p| ("ref.permissions.ro", Value::Boolean(p.readonly()))),
].into_iter()
{
match tpl {
&Some((ref s, ref v)) => {
match fle.get_header_mut().insert(s, v.clone()) {
Ok(false) => {
let e = REK::HeaderFieldAlreadyExistsError.into_error();
let e = Box::new(e);
let e = REK::HeaderFieldWriteError.into_error_with_cause(e);
return Err(e);
},
Err(e) => {
let e = Box::new(e);
let e = REK::HeaderFieldWriteError.into_error_with_cause(e);
return Err(e);
},
_ => (),
}
}
&None => {
debug!("Not going to insert.");
}
}
}
Ok(Ref(fle))
} }
/// Creates a Hash from a PathBuf by making the PathBuf absolute and then running a hash /// Creates a Hash from a PathBuf by making the PathBuf absolute and then running a hash