2016-06-27 16:00:12 +00:00
|
|
|
use error::{MapErrInto, StoreError as SE, StoreErrorKind as SEK};
|
2016-01-18 17:43:36 +00:00
|
|
|
use std::io::{Seek, SeekFrom};
|
2016-01-17 17:27:19 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-03-10 18:05:38 +00:00
|
|
|
use std::fs::{File, OpenOptions, create_dir_all};
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
/// `LazyFile` type
|
|
|
|
///
|
|
|
|
/// A lazy file is either absent, but a path to it is available, or it is present.
|
2016-01-28 20:00:13 +00:00
|
|
|
#[derive(Debug)]
|
2016-01-17 17:27:19 +00:00
|
|
|
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> {
|
2016-03-10 18:05:38 +00:00
|
|
|
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
|
|
|
OpenOptions::new().write(true).read(true).create(true).open(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LazyFile {
|
2016-01-21 20:30:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mutable file behind a LazyFile object
|
|
|
|
*/
|
2016-05-26 20:17:08 +00:00
|
|
|
pub fn get_file_mut(&mut self) -> Result<&mut File, SE> {
|
2016-01-28 20:00:33 +00:00
|
|
|
debug!("Getting lazy file: {:?}", self);
|
2016-01-17 17:27:19 +00:00
|
|
|
let file = match *self {
|
2016-01-18 17:43:36 +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
|
2016-03-01 20:23:09 +00:00
|
|
|
f.seek(SeekFrom::Start(0))
|
2016-06-27 16:00:12 +00:00
|
|
|
.map_err_into(SEK::FileNotCreated)
|
2016-03-01 20:23:09 +00:00
|
|
|
.map(|_| f)
|
2016-01-18 17:43:36 +00:00
|
|
|
},
|
2016-06-27 16:00:12 +00:00
|
|
|
LazyFile::Absent(ref p) => try!(open_file(p).map_err_into(SEK::FileNotFound)),
|
2016-01-17 17:27:19 +00:00
|
|
|
};
|
|
|
|
*self = LazyFile::File(file);
|
|
|
|
if let LazyFile::File(ref mut f) = *self {
|
|
|
|
return Ok(f);
|
|
|
|
}
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:30:41 +00:00
|
|
|
/**
|
|
|
|
* Create a file out of this LazyFile object
|
|
|
|
*/
|
2016-05-26 20:17:08 +00:00
|
|
|
pub fn create_file(&mut self) -> Result<&mut File, SE> {
|
2016-01-28 20:00:33 +00:00
|
|
|
debug!("Creating lazy file: {:?}", self);
|
2016-01-17 17:27:19 +00:00
|
|
|
let file = match *self {
|
|
|
|
LazyFile::File(ref mut f) => return Ok(f),
|
2016-06-27 16:00:12 +00:00
|
|
|
LazyFile::Absent(ref p) => try!(create_file(p).map_err_into(SEK::FileNotFound)),
|
2016-01-17 17:27:19 +00:00
|
|
|
};
|
|
|
|
*self = LazyFile::File(file);
|
|
|
|
if let LazyFile::File(ref mut f) = *self {
|
|
|
|
return Ok(f);
|
|
|
|
}
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::LazyFile;
|
2016-03-21 18:39:07 +00:00
|
|
|
use std::io::{Read, Write};
|
2016-01-17 17:27:19 +00:00
|
|
|
use std::path::PathBuf;
|
2016-01-18 17:20:50 +00:00
|
|
|
use tempdir::TempDir;
|
|
|
|
|
|
|
|
fn get_dir() -> TempDir {
|
|
|
|
TempDir::new("test-image").unwrap()
|
|
|
|
}
|
2016-01-17 17:27:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lazy_file() {
|
2016-01-18 17:20:50 +00:00
|
|
|
let dir = get_dir();
|
|
|
|
let mut path = PathBuf::from(dir.path());
|
|
|
|
path.set_file_name("test1");
|
2016-03-21 18:42:16 +00:00
|
|
|
let mut lf = LazyFile::Absent(path);
|
2016-01-17 17:27:19 +00:00
|
|
|
|
|
|
|
write!(lf.create_file().unwrap(), "Hello World").unwrap();
|
2016-01-18 17:20:50 +00:00
|
|
|
dir.close().unwrap();
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lazy_file_with_file() {
|
2016-01-18 17:20:50 +00:00
|
|
|
let dir = get_dir();
|
|
|
|
let mut path = PathBuf::from(dir.path());
|
|
|
|
path.set_file_name("test2");
|
2016-03-21 18:42:16 +00:00
|
|
|
let mut lf = LazyFile::Absent(path.clone());
|
2016-01-17 17:27:19 +00:00
|
|
|
|
2016-01-18 17:30:55 +00:00
|
|
|
{
|
|
|
|
let mut file = lf.create_file().unwrap();
|
|
|
|
|
|
|
|
file.write(b"Hello World").unwrap();
|
|
|
|
file.sync_all().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-21 18:42:16 +00:00
|
|
|
let mut file = lf.get_file_mut().unwrap();
|
2016-01-18 17:30:55 +00:00
|
|
|
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
|
|
|
|
|
|
|
dir.close().unwrap();
|
2016-01-17 17:27:19 +00:00
|
|
|
}
|
|
|
|
}
|