2016-07-22 13:12:56 +00:00
|
|
|
pub use self::fs::FileAbstraction;
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// This whole thing can be written better with a trait based mechanism that is embedded into the
|
|
|
|
// store. However it would mean rewriting most things to be generic which can be a pain in the ass.
|
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())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-22 13:12:56 +00:00
|
|
|
/// `FileAbstraction` 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)]
|
2016-07-22 13:12:56 +00:00
|
|
|
pub enum FileAbstraction {
|
2016-07-21 14:00:59 +00:00
|
|
|
Absent(PathBuf),
|
|
|
|
}
|
|
|
|
|
2016-07-22 13:12:56 +00:00
|
|
|
impl FileAbstraction {
|
2016-07-21 14:00:59 +00:00
|
|
|
|
|
|
|
/**
|
2016-07-22 13:12:56 +00:00
|
|
|
* Get the mutable file behind a FileAbstraction object
|
2016-07-21 14:00:59 +00:00
|
|
|
*/
|
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 {
|
2016-07-22 13:12:56 +00:00
|
|
|
FileAbstraction::Absent(ref f) => {
|
2016-07-22 11:39:29 +00:00
|
|
|
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 {
|
2016-07-22 13:12:56 +00:00
|
|
|
FileAbstraction::Absent(ref f) => {
|
2016-07-22 11:39:29 +00:00
|
|
|
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-07-22 13:12:56 +00:00
|
|
|
|
|
|
|
pub fn remove_file(_: &PathBuf) -> Result<(), SE> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn copy(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
|
|
|
|
let mut map = MAP.lock().unwrap();
|
|
|
|
let a = map.get(from).unwrap().clone();
|
|
|
|
map.insert(to.clone(), a);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rename(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
|
|
|
|
let mut map = MAP.lock().unwrap();
|
|
|
|
let a = map.get(from).unwrap().clone();
|
|
|
|
map.insert(to.clone(), a);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_dir_all(_: &PathBuf) -> Result<(), SE> {
|
|
|
|
Ok(())
|
|
|
|
}
|
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};
|
2016-07-22 13:12:56 +00:00
|
|
|
use std::fs::{File, OpenOptions, create_dir_all, remove_file, copy, rename};
|
2016-01-21 20:30:41 +00:00
|
|
|
|
2016-07-22 13:12:56 +00:00
|
|
|
/// `FileAbstraction` type
|
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)]
|
2016-07-22 13:12:56 +00:00
|
|
|
pub enum FileAbstraction {
|
2016-07-21 14:00:59 +00:00
|
|
|
Absent(PathBuf),
|
2016-07-22 13:12:56 +00:00
|
|
|
File(File, PathBuf)
|
2016-07-21 14:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-22 13:12:56 +00:00
|
|
|
impl FileAbstraction {
|
2016-07-21 14:00:59 +00:00
|
|
|
|
|
|
|
/**
|
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);
|
2016-07-22 13:12:56 +00:00
|
|
|
let (file, path) = match *self {
|
|
|
|
FileAbstraction::File(ref mut f, _) => return {
|
2016-07-21 14:00:59 +00:00
|
|
|
// 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
|
|
|
},
|
2016-07-22 13:12:56 +00:00
|
|
|
FileAbstraction::Absent(ref p) => (try!(open_file(p).map_err_into(SEK::FileNotFound)),
|
|
|
|
p.clone()),
|
2016-07-21 14:00:59 +00:00
|
|
|
};
|
2016-07-22 13:12:56 +00:00
|
|
|
*self = FileAbstraction::File(file, path);
|
|
|
|
if let FileAbstraction::File(ref mut f, _) = *self {
|
2016-07-21 14:00:59 +00:00
|
|
|
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-22 13:12:56 +00:00
|
|
|
let (file, path) = match *self {
|
|
|
|
FileAbstraction::File(ref mut f, _) => return {
|
2016-07-22 11:39:29 +00:00
|
|
|
// 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 13:12:56 +00:00
|
|
|
FileAbstraction::Absent(ref p) => (try!(create_file(p).map_err_into(SEK::FileNotCreated)),
|
|
|
|
p.clone()),
|
2016-07-21 14:00:59 +00:00
|
|
|
};
|
2016-07-22 13:12:56 +00:00
|
|
|
*self = FileAbstraction::File(file, path);
|
|
|
|
if let FileAbstraction::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
|
|
|
}
|
2016-07-22 13:12:56 +00:00
|
|
|
|
|
|
|
pub fn remove_file(path: &PathBuf) -> Result<(), SE> {
|
|
|
|
remove_file(path).map_err_into(SEK::FileNotRemoved)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn copy(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
|
|
|
|
copy(from, to).map_err_into(SEK::FileNotCopied).map(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rename(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
|
|
|
|
rename(from, to).map_err_into(SEK::FileNotRenamed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_dir_all(path: &PathBuf) -> Result<(), SE> {
|
|
|
|
create_dir_all(path).map_err_into(SEK::DirNotCreated)
|
|
|
|
}
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2016-07-22 13:12:56 +00:00
|
|
|
use super::FileAbstraction;
|
2016-07-22 11:54:13 +00:00
|
|
|
use std::io::Read;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lazy_file() {
|
|
|
|
let mut path = PathBuf::from("/tests");
|
|
|
|
path.set_file_name("test1");
|
2016-07-22 13:12:56 +00:00
|
|
|
let mut lf = FileAbstraction::Absent(path);
|
2016-07-22 11:54:13 +00:00
|
|
|
lf.write_file_content(b"Hello World").unwrap();
|
|
|
|
let mut bah = Vec::new();
|
|
|
|
lf.get_file_content().unwrap().read_to_end(&mut bah).unwrap();
|
|
|
|
assert_eq!(bah, b"Hello World");
|
|
|
|
}
|
|
|
|
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|