2016-07-21 14:00:59 +00:00
|
|
|
pub use self::fs::LazyFile;
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-07-21 14:00:59 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod fs {
|
|
|
|
use error::StoreError as SE;
|
|
|
|
use std::io::Cursor;
|
|
|
|
use std::path::PathBuf;
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-07-22 11:39:29 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref MAP: Mutex<HashMap<PathBuf, Cursor<Vec<u8>>>> = {
|
|
|
|
Mutex::new(HashMap::new())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `LazyFile` type, this is the Test version!
|
2016-07-21 14:00:59 +00:00
|
|
|
///
|
|
|
|
/// A lazy file is either absent, but a path to it is available, or it is present.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum LazyFile {
|
|
|
|
Absent(PathBuf),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LazyFile {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mutable file behind a LazyFile object
|
|
|
|
*/
|
2016-07-22 11:39:29 +00:00
|
|
|
pub fn get_file_content(&mut self) -> Result<Cursor<Vec<u8>>, SE> {
|
2016-07-21 14:00:59 +00:00
|
|
|
debug!("Getting lazy file: {:?}", self);
|
2016-07-22 11:39:29 +00:00
|
|
|
match *self {
|
|
|
|
LazyFile::Absent(ref f) => {
|
|
|
|
let map = MAP.lock().unwrap();
|
|
|
|
return Ok(map.get(f).unwrap().clone());
|
2016-07-21 14:00:59 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-07-22 11:39:29 +00:00
|
|
|
pub fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE> {
|
|
|
|
match *self {
|
|
|
|
LazyFile::Absent(ref f) => {
|
|
|
|
let mut map = MAP.lock().unwrap();
|
|
|
|
if let Some(ref mut cur) = map.get_mut(f) {
|
|
|
|
let mut vec = cur.get_mut();
|
|
|
|
vec.clear();
|
|
|
|
vec.extend_from_slice(buf);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let vec = Vec::from(buf);
|
|
|
|
map.insert(f.clone(), Cursor::new(vec));
|
|
|
|
return Ok(());
|
|
|
|
},
|
2016-07-21 14:00:59 +00:00
|
|
|
};
|
2016-03-10 18:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 14:00:59 +00:00
|
|
|
#[cfg(not(test))]
|
|
|
|
mod fs {
|
|
|
|
use error::{MapErrInto, StoreError as SE, StoreErrorKind as SEK};
|
2016-07-22 11:39:29 +00:00
|
|
|
use std::io::{Seek, SeekFrom, Read};
|
2016-07-21 14:00:59 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::fs::{File, OpenOptions, create_dir_all};
|
2016-01-21 20:30:41 +00:00
|
|
|
|
2016-07-21 14:00:59 +00:00
|
|
|
/// `LazyFile` type
|
|
|
|
///
|
|
|
|
/// A lazy file is either absent, but a path to it is available, or it is present.
|
|
|
|
#[derive(Debug)]
|
|
|
|
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> {
|
|
|
|
if let Some(parent) = p.as_ref().parent() {
|
|
|
|
debug!("Implicitely creating directory: {:?}", parent);
|
|
|
|
if let Err(e) = create_dir_all(parent) {
|
|
|
|
return Err(e);
|
|
|
|
}
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|
2016-07-21 14:00:59 +00:00
|
|
|
OpenOptions::new().write(true).read(true).create(true).open(p)
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 14:00:59 +00:00
|
|
|
impl LazyFile {
|
|
|
|
|
|
|
|
/**
|
2016-07-22 11:39:29 +00:00
|
|
|
* Get the content behind this file
|
2016-07-21 14:00:59 +00:00
|
|
|
*/
|
2016-07-22 11:39:29 +00:00
|
|
|
pub fn get_file_content(&mut self) -> Result<&mut Read, SE> {
|
2016-07-21 14:00:59 +00:00
|
|
|
debug!("Getting lazy file: {:?}", self);
|
|
|
|
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
|
2016-07-22 11:39:29 +00:00
|
|
|
try!(f.seek(SeekFrom::Start(0))
|
|
|
|
.map_err_into(SEK::FileNotSeeked));
|
|
|
|
Ok(f)
|
2016-07-21 14:00:59 +00:00
|
|
|
},
|
|
|
|
LazyFile::Absent(ref p) => try!(open_file(p).map_err_into(SEK::FileNotFound)),
|
|
|
|
};
|
|
|
|
*self = LazyFile::File(file);
|
|
|
|
if let LazyFile::File(ref mut f) = *self {
|
|
|
|
return Ok(f);
|
|
|
|
}
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-22 11:39:29 +00:00
|
|
|
* Write the content of this file
|
2016-07-21 14:00:59 +00:00
|
|
|
*/
|
2016-07-22 11:39:29 +00:00
|
|
|
pub fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE> {
|
|
|
|
use std::io::Write;
|
2016-07-21 14:00:59 +00:00
|
|
|
let file = match *self {
|
2016-07-22 11:39:29 +00:00
|
|
|
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
|
|
|
|
try!(f.seek(SeekFrom::Start(0))
|
|
|
|
.map_err_into(SEK::FileNotCreated));
|
|
|
|
f.write_all(buf).map_err_into(SEK::FileNotWritten)
|
2016-07-21 14:00:59 +00:00
|
|
|
},
|
2016-07-22 11:39:29 +00:00
|
|
|
LazyFile::Absent(ref p) => try!(create_file(p).map_err_into(SEK::FileNotCreated)),
|
2016-07-21 14:00:59 +00:00
|
|
|
};
|
|
|
|
*self = LazyFile::File(file);
|
|
|
|
if let LazyFile::File(ref mut f) = *self {
|
2016-07-22 11:39:29 +00:00
|
|
|
return f.write_all(buf).map_err_into(SEK::FileNotWritten);
|
2016-07-21 14:00:59 +00:00
|
|
|
}
|
2016-07-22 11:39:29 +00:00
|
|
|
unreachable!();
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2016-07-21 13:57:56 +00:00
|
|
|
// use super::LazyFile;
|
|
|
|
// use std::io::{Read, Write};
|
|
|
|
// use std::path::PathBuf;
|
|
|
|
// use tempdir::TempDir;
|
2016-01-18 17:20:50 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// fn get_dir() -> TempDir {
|
|
|
|
// TempDir::new("test-image").unwrap()
|
|
|
|
// }
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// #[test]
|
|
|
|
// fn lazy_file() {
|
|
|
|
// let dir = get_dir();
|
|
|
|
// let mut path = PathBuf::from(dir.path());
|
|
|
|
// path.set_file_name("test1");
|
|
|
|
// let mut lf = LazyFile::Absent(path);
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// write!(lf.create_file().unwrap(), "Hello World").unwrap();
|
|
|
|
// dir.close().unwrap();
|
|
|
|
// }
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// #[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::Absent(path.clone());
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// {
|
|
|
|
// let mut file = lf.create_file().unwrap();
|
2016-01-18 17:30:55 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// file.write(b"Hello World").unwrap();
|
|
|
|
// file.sync_all().unwrap();
|
|
|
|
// }
|
2016-01-18 17:30:55 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// {
|
|
|
|
// let mut file = lf.get_file_mut().unwrap();
|
|
|
|
// let mut s = Vec::new();
|
|
|
|
// file.read_to_end(&mut s).unwrap();
|
|
|
|
// assert_eq!(s, "Hello World".to_string().into_bytes());
|
|
|
|
// }
|
2016-01-18 17:20:50 +00:00
|
|
|
|
2016-07-21 13:57:56 +00:00
|
|
|
// dir.close().unwrap();
|
|
|
|
// }
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|