From ba2e52788bd8239d4100f005e24b35ac35a66f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sat, 23 Jan 2016 16:15:34 +0100 Subject: [PATCH] Add entry_creation --- libimagstore/src/entry.rs | 8 +++++++ libimagstore/src/error.rs | 2 ++ libimagstore/src/header.rs | 18 ++++++++++++++++ libimagstore/src/store.rs | 43 +++++++++++++++++++++++++++----------- 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/libimagstore/src/entry.rs b/libimagstore/src/entry.rs index b463a81e..5bcd760d 100644 --- a/libimagstore/src/entry.rs +++ b/libimagstore/src/entry.rs @@ -16,6 +16,14 @@ pub struct Entry { impl Entry { + fn new(loc: StoreId) -> Entry { + Entry { + location: loc, + header: EntryHeader::new_current(), + content: EntryContent::new() + } + } + pub fn get_location(&self) -> &StoreId { &self.location } diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index eabb26b7..7774a9f8 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -18,6 +18,7 @@ pub enum StoreErrorKind { StorePathExists, StorePathCreate, LockPoisoned, + EntryAlreadyBorrowed // maybe more } @@ -33,6 +34,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { &StoreErrorKind::StorePathCreate => "Store path create", &StoreErrorKind::LockPoisoned => "The internal Store Lock has been poisoned", + &StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed", } } diff --git a/libimagstore/src/header.rs b/libimagstore/src/header.rs index dc432da2..657182d5 100644 --- a/libimagstore/src/header.rs +++ b/libimagstore/src/header.rs @@ -1,6 +1,7 @@ use std::collections::BTreeMap; use std::error::Error; use std::result::Result as RResult; +use std::collections::BTreeMap; use toml::{Array, Table, Value}; use version; @@ -102,6 +103,23 @@ impl EntryHeader { } } + pub fn new_current() -> EntryHeader { + EntryHeader { + toml: { + let mut header = BTreeMap::new(); + let sub = { + let mut sub = BTreeMap::new(); + sub.insert("version".into(), Value::String(version!())); + + Value::Table(sub) + }; + + header.insert("imag".into(), sub); + header + } + } + } + /** * Get the table which lives in the background */ diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 9c92980a..593ab19c 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -11,21 +11,29 @@ use fs2::FileExt; use entry::Entry; use error::{StoreError, StoreErrorKind}; use storeid::StoreId; +use lazyfile::LazyFile; /// The Result Type returned by any interaction with the store that could fail pub type Result = RResult; -#[derive(PartialEq)] -enum StoreEntryPresence { - Present, - Borrowed -} /// A store entry, depending on the option type it is either borrowed currently /// or not. -struct StoreEntry { - file: File, - entry: StoreEntryPresence +enum StoreEntry { + Present(StoreId, LazyFile), + Borrowed +} + +impl PartialEq for StoreEntry { + fn eq(&self, other: &StoreEntry) -> bool { + use store::StoreEntry::*; + match (*self, *other) { + (Borrowed, Borrowed) => true, + (Borrowed, Present(_,_)) => false, + (Present(_,_), Borrowed) => false, + (Present(ref a,_), Present(ref b, _)) => a == b + } + } } @@ -33,7 +41,20 @@ impl StoreEntry { /// The entry is currently borrowed, meaning that some thread is currently /// mutating it fn is_borrowed(&self) -> bool { - self.entry == StoreEntryPresence::Borrowed + *self == StoreEntry::Borrowed + } + + fn get_entry(&self) -> Result { + if let &StoreEntry::Present(ref id, ref file) = self { + let file = file.get_file(); + if let Err(StoreError{err_type: StoreErrorKind::FileNotFound, ..}) = file { + Ok(Entry::new(id.clone())) + } else { + unimplemented!() + } + } else { + return Err(StoreError::new(StoreErrorKind::EntryAlreadyBorrowed, None)) + } } } @@ -134,11 +155,9 @@ impl Drop for Store { /** * Unlock all files on drop * - * TODO: Error message when file cannot be unlocked? + * TODO: Unlock them */ fn drop(&mut self) { - self.entries.write().unwrap() - .iter().map(|f| f.1.file.unlock()); } }