Auto merge of #115 - TheNeikos:add-lazy_file, r=matthiasbeyer
Add file creation @matthiasbeyer, @neithernut A possible implementation of a lazy enum to have lazy loading of files.
This commit is contained in:
commit
35e7ecfe6f
5 changed files with 158 additions and 1 deletions
28
libimagstore/Cargo.lock
generated
28
libimagstore/Cargo.lock
generated
|
@ -3,9 +3,19 @@ name = "libimagstore"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"fs2 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "advapi32-sys"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fs2"
|
||||
version = "0.2.2"
|
||||
|
@ -30,11 +40,29 @@ name = "libc"
|
|||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.3.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-serialize"
|
||||
version = "0.3.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "tempdir"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.1.25"
|
||||
|
|
|
@ -7,4 +7,4 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
|||
|
||||
fs2 = "0.2.2"
|
||||
toml = "0.1.25"
|
||||
|
||||
tempdir = "0.3.4"
|
||||
|
|
|
@ -10,6 +10,8 @@ pub enum StoreErrorKind {
|
|||
IdLocked,
|
||||
IdNotFound,
|
||||
OutOfMemory,
|
||||
FileNotFound,
|
||||
FileNotCreated,
|
||||
// maybe more
|
||||
}
|
||||
|
||||
|
@ -19,6 +21,8 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
|
|||
&StoreErrorKind::IdLocked => "ID locked",
|
||||
&StoreErrorKind::IdNotFound => "ID not found",
|
||||
&StoreErrorKind::OutOfMemory => "Out of Memory",
|
||||
&StoreErrorKind::FileNotFound => "File corresponding to ID not found",
|
||||
&StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
123
libimagstore/src/lazyfile.rs
Normal file
123
libimagstore/src/lazyfile.rs
Normal file
|
@ -0,0 +1,123 @@
|
|||
|
||||
use error::{StoreError, StoreErrorKind};
|
||||
use std::io::{Seek, SeekFrom};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fs::{File, OpenOptions};
|
||||
|
||||
pub enum LazyFile {
|
||||
Absent(PathBuf),
|
||||
File(File)
|
||||
}
|
||||
|
||||
fn open_file<A: AsRef<Path>>(p: A) -> ::std::io::Result<File> {
|
||||
OpenOptions::new().write(true).read(true).open(p)
|
||||
}
|
||||
|
||||
fn create_file<A: AsRef<Path>>(p: A) -> ::std::io::Result<File> {
|
||||
OpenOptions::new().write(true).read(true).create(true).open(p)
|
||||
}
|
||||
|
||||
impl LazyFile {
|
||||
pub fn new(p: PathBuf) -> LazyFile {
|
||||
LazyFile::Absent(p)
|
||||
}
|
||||
pub fn new_with_file(f: File) -> LazyFile {
|
||||
LazyFile::File(f)
|
||||
}
|
||||
|
||||
pub fn get_file(&mut self) -> Result<&File, StoreError> {
|
||||
match self.get_file_mut() {
|
||||
Ok(file) => Ok(&*file),
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_file_mut(&mut self) -> Result<&mut File, StoreError> {
|
||||
let file = match *self {
|
||||
LazyFile::File(ref mut f) => return {
|
||||
// We seek to the beginning of the file since we expect each
|
||||
// access to the file to be in a different context
|
||||
f.seek(SeekFrom::Start(0)).map_err(|e|
|
||||
StoreError::new(
|
||||
StoreErrorKind::FileNotCreated, Some(Box::new(e))));
|
||||
Ok(f)
|
||||
},
|
||||
LazyFile::Absent(ref p) => {
|
||||
try!(open_file(p).map_err(|e| {
|
||||
StoreError::new(StoreErrorKind::FileNotFound,
|
||||
Some(Box::new(e)))
|
||||
}))
|
||||
}
|
||||
};
|
||||
*self = LazyFile::File(file);
|
||||
if let LazyFile::File(ref mut f) = *self {
|
||||
return Ok(f);
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
pub fn create_file(&mut self) -> Result<&mut File, StoreError> {
|
||||
let file = match *self {
|
||||
LazyFile::File(ref mut f) => return Ok(f),
|
||||
LazyFile::Absent(ref p) => {
|
||||
try!(create_file(p).map_err(|e| {
|
||||
StoreError::new(StoreErrorKind::FileNotFound,
|
||||
Some(Box::new(e)))
|
||||
}))
|
||||
}
|
||||
};
|
||||
*self = LazyFile::File(file);
|
||||
if let LazyFile::File(ref mut f) = *self {
|
||||
return Ok(f);
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::LazyFile;
|
||||
use std::io::{Read, Write, Seek, SeekFrom};
|
||||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
use tempdir::TempDir;
|
||||
|
||||
fn get_dir() -> TempDir {
|
||||
TempDir::new("test-image").unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_file() {
|
||||
let dir = get_dir();
|
||||
let mut path = PathBuf::from(dir.path());
|
||||
path.set_file_name("test1");
|
||||
let mut lf = LazyFile::new(path);
|
||||
|
||||
write!(lf.create_file().unwrap(), "Hello World").unwrap();
|
||||
dir.close().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_file_with_file() {
|
||||
let dir = get_dir();
|
||||
let mut path = PathBuf::from(dir.path());
|
||||
path.set_file_name("test2");
|
||||
let mut lf = LazyFile::new(path.clone());
|
||||
|
||||
{
|
||||
let mut file = lf.create_file().unwrap();
|
||||
|
||||
file.write(b"Hello World").unwrap();
|
||||
file.sync_all().unwrap();
|
||||
}
|
||||
|
||||
{
|
||||
let mut file = lf.get_file().unwrap();
|
||||
let mut s = Vec::new();
|
||||
file.read_to_end(&mut s).unwrap();
|
||||
assert_eq!(s, "Hello World".to_string().into_bytes());
|
||||
}
|
||||
|
||||
dir.close().unwrap();
|
||||
}
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
extern crate fs2;
|
||||
extern crate toml;
|
||||
extern crate tempdir;
|
||||
|
||||
pub mod content;
|
||||
pub mod entry;
|
||||
pub mod error;
|
||||
pub mod header;
|
||||
pub mod store;
|
||||
mod lazyfile;
|
||||
|
||||
|
|
Loading…
Reference in a new issue