From 87cb39050d938e01b7b26633dab3d141737330ed Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sat, 15 Sep 2018 20:34:14 +0100 Subject: A lot of refactoring in lockchain-files --- lockchain-files/examples/create.rs | 6 ++-- lockchain-files/src/config.rs | 4 ++- lockchain-files/src/create.rs | 44 +++++++++++++++++++++++ lockchain-files/src/fs.rs | 14 ++++---- lockchain-files/src/lib.rs | 74 +++++++++++--------------------------- lockchain-files/src/load.rs | 34 ++++++++++++++++++ lockchain-files/src/userstore.rs | 9 +++-- lockchain-http/src/lib.rs | 4 +-- lockchain-server/src/main.rs | 4 +-- 9 files changed, 123 insertions(+), 70 deletions(-) create mode 100644 lockchain-files/src/create.rs create mode 100644 lockchain-files/src/load.rs diff --git a/lockchain-files/examples/create.rs b/lockchain-files/examples/create.rs index 68fd980..67279a9 100644 --- a/lockchain-files/examples/create.rs +++ b/lockchain-files/examples/create.rs @@ -1,7 +1,7 @@ extern crate lockchain_core as lcc; extern crate lockchain_files as files; -use files::DataVault; +use files::FileVault; use lcc::traits::Vault; use lcc::users::User; use lcc::{EncryptedBody, Generator, Payload, Record, VaultType}; @@ -12,7 +12,7 @@ fn main() { let path = env::args().nth(1).unwrap(); let name = env::args().nth(2).unwrap(); - let mut vault: DataVault = Generator::new() + let mut vault: FileVault = Generator::new() .path(name, path) .user_type(VaultType::SoloUser { username: "spacekookie".into(), @@ -21,7 +21,7 @@ fn main() { .unwrap(); vault.sync(); - // let vault: DataVault = DataVault::new(&name, &path); + // let vault: FileVault = FileVault::new(&name, &path); // let mut store = match ( // vault.meta_pull_domain("userstore"), diff --git a/lockchain-files/src/config.rs b/lockchain-files/src/config.rs index ffec582..c862b31 100644 --- a/lockchain-files/src/config.rs +++ b/lockchain-files/src/config.rs @@ -11,6 +11,8 @@ use semver::Version; use toml; use utils::FileToString; +use lcc::Generator; + /// A set of errors around `lockchain-files` configs #[derive(Debug)] pub enum ConfigError { @@ -46,7 +48,7 @@ pub struct VaultConfig { } impl VaultConfig { - pub fn new() -> Self { + pub fn new(_: &Generator) -> Self { Self { version: "0.1".into(), created_at: SystemTime::now(), diff --git a/lockchain-files/src/create.rs b/lockchain-files/src/create.rs new file mode 100644 index 0000000..6c5255a --- /dev/null +++ b/lockchain-files/src/create.rs @@ -0,0 +1,44 @@ +//! A small submodule which handles all aspects of vault creation +#![allow(unused_imports)] + +use lcc::errors::VaultError; +use lcc::{traits::Body, Generator}; +use std::collections::HashMap; + +use ::FileVault; +use ::config::{VaultConfig, ConfigError}; +use ::fs::{Filesystem, FileType}; + + +impl FileVault { + /// A small utility to create a new file vault + pub(crate) fn create(gen: Generator) -> Result { + let (name, location) = Self::get_path(&gen)?; + + let fs = Filesystem::new(location, name); + fs.scaffold().map_err(|_| VaultError::FailedCreation)?; + + let cfg = VaultConfig::new(&gen); + + // Ok(Box::new( + // Self { + // meta_info: ( + // gen.name.clone().unwrap().into(), + // gen.location.clone().unwrap().into(), + // ), + // records: HashMap::new(), + // config: VaultConfig::new(), + // metadata: HashMap::new(), + // fs: Filesystem::new(&gen.location.unwrap(), &gen.name.unwrap()), + // users: UserStoreMapper::new(), + // }.initialize(), + // )) + unimplemented!() + } + + fn get_path(gen: &Generator) -> Result<(&str, &str), VaultError> { + + + Err(VaultError::IncompleteGenerator) + } +} diff --git a/lockchain-files/src/fs.rs b/lockchain-files/src/fs.rs index 1aa57bc..af47e50 100644 --- a/lockchain-files/src/fs.rs +++ b/lockchain-files/src/fs.rs @@ -13,7 +13,7 @@ use lcc::traits::AutoEncoder; use std::collections::HashMap; use std::error::Error; -use std::io::Write; +use std::io::{self, Write}; use std::{ fs::{self, File, OpenOptions as OO}, path::PathBuf, @@ -70,12 +70,12 @@ impl Filesystem { } /// Create required directories - pub fn scaffold(&self) -> Option<()> { - fs::create_dir_all(&self.root).ok()?; - fs::create_dir(&self.root.join("records")).ok()?; - fs::create_dir(&self.root.join("metadata")).ok()?; - fs::create_dir(&self.root.join("checksums")).ok()?; - Some(()) + pub fn scaffold(&self) -> Result<(), io::Error> { + fs::create_dir_all(&self.root)?; + fs::create_dir(&self.root.join("records"))?; + fs::create_dir(&self.root.join("metadata"))?; + fs::create_dir(&self.root.join("checksums"))?; + Ok(()) } /// Load all files of a certain type into a Vec diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs index a7941e9..cfb097a 100644 --- a/lockchain-files/src/lib.rs +++ b/lockchain-files/src/lib.rs @@ -58,12 +58,14 @@ use lcc::traits::{Body, LoadRecord, Vault}; use lcc::{ errors::VaultError, users::{Access, Token, UserStore}, - Generator, MetaDomain, Payload, Record, VaultMetadata, + Generator, Header, MetaDomain, Payload, Record, VaultMetadata, }; use std::collections::HashMap; mod config; +mod create; mod fs; +mod load; mod userstore; mod utils; @@ -87,50 +89,30 @@ use userstore::UserStoreMapper; /// /// The vault folder is safe to copy around – /// all vault metadata is kept inside it. -pub struct DataVault { - meta_info: (String, String), +pub struct FileVault { + /// A representation of the cached vault config config: VaultConfig, - records: HashMap>, - metadata: HashMap, + /// Filesystem wrapper utility fs: Filesystem, + /// A userstore utility derived from Metadata users: UserStoreMapper, + /// A mapping of loaded records + records: HashMap>, + /// An index of all existing headers + headers: HashMap, + /// A map of all metadata files + metadata: HashMap, } -impl DataVault { - /// Small utility function to setup file structure - fn initialize(self) -> Self { - self.fs.scaffold(); - self.config.save(&self.fs.root).unwrap(); - self - } - - fn load(mut self) -> Result, VaultError> { - self.config = match VaultConfig::load(&self.fs.root) { - Ok(cfg) => cfg, - _ => return Err(VaultError::FailedLoading), - }; +impl LoadRecord for FileVault {} - Ok(Box::new(self)) +impl Vault for FileVault { + fn new(gen: Generator) -> Result>, VaultError> { + Self::create(gen).map(|s| Box::new(s)) } -} - -impl LoadRecord for DataVault {} -impl Vault for DataVault { - fn new(gen: Generator) -> Result>, VaultError> { - Ok(Box::new( - Self { - meta_info: ( - gen.name.clone().unwrap().into(), - gen.location.clone().unwrap().into(), - ), - records: HashMap::new(), - config: VaultConfig::new(), - metadata: HashMap::new(), - fs: Filesystem::new(&gen.location.unwrap(), &gen.name.unwrap()), - users: UserStoreMapper::new(UserStore::new()), - }.initialize(), - )) + fn load(name: &str, location: &str) -> Result, VaultError> { + Self::load(name, location).map(|s| Box::new(s)) } fn create_user( @@ -143,22 +125,8 @@ impl Vault for DataVault { unimplemented!() } - fn delete_user(&mut self, token: Token, username: &str) {} - - // Checking if a vault exists is basically checking it's config - // against the compatible version of this library. - // - // If it's compatible we can open the vault into memory - // (loading all required paths into the struct), then return it - fn load(name: &str, location: &str) -> Result, VaultError> { - Self { - meta_info: (name.into(), location.into()), - records: HashMap::new(), - config: VaultConfig::new(), - metadata: HashMap::new(), - fs: Filesystem::new(location, name), - users: UserStoreMapper::new(UserStore::new()), - }.load() + fn delete_user(&mut self, token: Token, username: &str) { + unimplemented!() } fn authenticate(&mut self, username: &str, secret: &str) -> Token { diff --git a/lockchain-files/src/load.rs b/lockchain-files/src/load.rs new file mode 100644 index 0000000..fd4e66e --- /dev/null +++ b/lockchain-files/src/load.rs @@ -0,0 +1,34 @@ +//! A small submodule which handles all aspects of vault creation +#![allow(unused_imports)] + +use lcc::errors::VaultError; +use lcc::{traits::Body, Generator}; +use std::collections::HashMap; + +use ::FileVault; +use ::config::{VaultConfig, ConfigError}; +use ::fs::{Filesystem, FileType}; + + +impl FileVault { + /// A small utility to load an existing file vault + pub(crate) fn load(name: &str, location: &str) -> Result { + unimplemented!() + + // self.config = match VaultConfig::load(&self.fs.root) { + // Ok(cfg) => cfg, + // _ => return Err(VaultError::FailedLoading), + // }; + // Ok(Box::new(self)) + + // Self { + // meta_info: (name.into(), location.into()), + // records: HashMap::new(), + // config: VaultConfig::new(Gene), + // metadata: HashMap::new(), + // fs: Filesystem::new(location, name), + // users: UserStoreMapper::new(), + // }.load() + } + +} diff --git a/lockchain-files/src/userstore.rs b/lockchain-files/src/userstore.rs index 1d01db6..07c57fd 100644 --- a/lockchain-files/src/userstore.rs +++ b/lockchain-files/src/userstore.rs @@ -3,13 +3,18 @@ use lcc::users::UserStore; -#[derive(Debug)] pub struct UserStoreMapper { inner: UserStore, } impl UserStoreMapper { - pub fn new(store: UserStore) -> Store { + pub fn new() -> Self { + Self { + inner: UserStore::new(), + } + } + + pub fn load(store: UserStore) -> Self { Self { inner: store } } } diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs index 961ad21..005bdb6 100644 --- a/lockchain-http/src/lib.rs +++ b/lockchain-http/src/lib.rs @@ -50,12 +50,12 @@ pub type HttpApi = server::HttpServer>>>; /// ```norun /// use lockchain_core::{traits::*, EncryptedBody}; /// use lockchain_http::create_server; -/// use lockchain_files::DataVault; +/// use lockchain_files::FileVault; /// /// let server = create_server( /// "localhost", /// "8080", -/// DataVault::::new("name", "some-location"), +/// FileVault::::new("name", "some-location"), /// ).run(); /// ``` pub fn create_server( diff --git a/lockchain-server/src/main.rs b/lockchain-server/src/main.rs index f23cf71..d96c213 100644 --- a/lockchain-server/src/main.rs +++ b/lockchain-server/src/main.rs @@ -7,11 +7,11 @@ extern crate lockchain_files as files; extern crate lockchain_http as http; use core::EncryptedBody; -use files::DataVault; +use files::FileVault; use http::{create_server, state::ApiState}; fn main() { - let state = ApiState::> { + let state = ApiState::> { bound_scope: true, working_dir: ".".into(), -- cgit v1.2.3