iQIcBAABCgAGBQJWnRh+AAoJEN1O030MrHbiUHQQAJ3bhzz4O7qytq9X4WUkW80W
A+Dk5Oyzm4QajdMDw1lW+FJT1LHAA4q14nTKTZlCZKT0doxZYbQENrBjzhAQaKin kIZSrCmKulziAVLIuXGq9wmaz6CJ4kHb+GWlC82U575yIb8XBpqpIjUAwboP9xZk xHaGT8l9+KPMUCXd9zU3KCJHg3ZO3ckOJm0gmG4JvmvaUX+r38cgO3zwRPpdVLVW cnQ/aPLkaeLfP2auSdRDeVOkhcl5uWgdQcvnqTkPUb+gQUXV01WpqzoxmVhEPVv1 kLWIGRzFEmxrNadAqJTC9AE5DBnoRM7/cge2QZ9vVtJsdcwRYwjw4qSunvPAusdb lXmA0+1aJPpkGMHVywNBHYq5fR89etrQBZ3Roz9LAp2eKNAsXyJYg0CT6PgzmEhp cPVOCHizXWwCLFRZI0zn9WSWS9EFm0H2FJDGfSDAoNPyO2RAmtu+8tXU+vgwvox+ B3j5jEQmF8c8A+SE0Qeh7IloQWMXCfnJrgeo7sbhYq8w0WPTLhiX7BEtu3K3Nvi6 bG00BHihvOD2wu1DiTIzPTJkP3rMK4VD6sZmXSUFjEVtzn1rN9l2hYjkyMQj/xlG RATEMP4TvB/eVktfZ+nFr5XtGGFAY1hsVBd5FnV874J6OHGD6zqt21lgxGCF0MUw cwqyKk6K+l6Ljf8UAdvx =Ni+I -----END PGP SIGNATURE----- Add file creation
This commit is contained in:
parent
a8bc18d39a
commit
5d3cb4a3af
3 changed files with 112 additions and 0 deletions
|
@ -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",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
107
libimagstore/src/lazyfile.rs
Normal file
107
libimagstore/src/lazyfile.rs
Normal file
|
@ -0,0 +1,107 @@
|
|||
|
||||
use error::{StoreError, StoreErrorKind};
|
||||
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> {
|
||||
let file = match *self {
|
||||
LazyFile::File(ref f) => return 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 f) = *self {
|
||||
return Ok(f);
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
pub fn get_file_mut(&mut self) -> Result<&mut File, StoreError> {
|
||||
let file = match *self {
|
||||
LazyFile::File(ref mut f) => return 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};
|
||||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
|
||||
#[test]
|
||||
fn lazy_file() {
|
||||
let path = PathBuf::from("/tmp/test");
|
||||
let mut lf = LazyFile::new(path);
|
||||
|
||||
write!(lf.create_file().unwrap(), "Hello World").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lazy_file_with_file() {
|
||||
let path = PathBuf::from("/tmp/test2");
|
||||
let mut lf = LazyFile::new_with_file(File::create(path).unwrap());
|
||||
let mut file = lf.get_file_mut().unwrap();
|
||||
|
||||
write!(file, "Hello World").unwrap();
|
||||
file.sync_all().unwrap();
|
||||
let mut s = String::new();
|
||||
file.read_to_string(&mut s).unwrap();
|
||||
assert_eq!(s, "Hello World");
|
||||
}
|
||||
}
|
|
@ -6,4 +6,5 @@ pub mod entry;
|
|||
pub mod error;
|
||||
pub mod header;
|
||||
pub mod store;
|
||||
mod lazyfile;
|
||||
|
||||
|
|
Loading…
Reference in a new issue