iQIcBAABCgAGBQJWnTB3AAoJEN1O030MrHbi9V4P/3tis8CVncUcVFC5NohTBbRk
tFrubrLNw+ketVYV8JsHPtygtvZ8jfMy3DdSTHqxOoC0jPEERSMwkZ98AVySS70w D2A9Wjxt6s2heg23MMpEtP/axBMoSshj30Hu/S70qI9M0HskloJ1he02urhs9LqA kCUnS9oiiQ2qxtOuoGk4NLwOa/fyPFdznYjBjFOnIjf6Qmc0xaMTvxSei1H18GcW +Ug9eU3XERV4mehehXSqzrAKa3zTsF3dur6Y+e6rgYbaQpv7vBNlNr733R1ao0tY A8UUb/90hIUKpfgLNQYbAXI37AvrKj5njk/BGm5ca/gRUwWkL1nKkkolvJvsjQgO hyifT3JmHmtOa4A4mgDF1DLOAZVkOXCKPbA9HIaI0wpZMHEBjGD7UTf52QtLD4Cv 0rwgGqAR1qzUw4ijzwYm+s/YNbm9Ecn22cmRIvk5ZGGbWu+q2Y+9+b/sCbgr2EQP lWkxT8qF/sNw9MVhwrnu2FpimL0IaZ9iKSPGSzbsoaBm4M86STGPYXrIaycq6i3u 17Dqvv3U26R08AEw/5thZFwazOMj305M0CjqkyvNGCosKHG+FrHIgK4hgNmB5vGe T11RkfSRq15s+TRgcvjIYrPmtrcXB11+WBzI2K/nSQOnjCENYx2oBKRSBILwhuaC TQedJRXhwAp32VO3Hojb =TpUb -----END PGP SIGNATURE----- Seek to beginning of file before reading
This commit is contained in:
parent
927205dfcc
commit
a75ba8ea72
|
@ -1,5 +1,6 @@
|
|||
|
||||
use error::{StoreError, StoreErrorKind};
|
||||
use std::io::{Seek, SeekFrom};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fs::{File, OpenOptions};
|
||||
|
||||
|
@ -25,26 +26,22 @@ impl LazyFile {
|
|||
}
|
||||
|
||||
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);
|
||||
match self.get_file_mut() {
|
||||
Ok(file) => Ok(&*file),
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
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::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,
|
||||
|
@ -80,7 +77,7 @@ impl LazyFile {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::LazyFile;
|
||||
use std::io::{Read, Write};
|
||||
use std::io::{Read, Write, Seek, SeekFrom};
|
||||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
use tempdir::TempDir;
|
||||
|
@ -105,9 +102,9 @@ mod test {
|
|||
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 lf = LazyFile::new(path.clone());
|
||||
let mut file = lf.create_file().unwrap();
|
||||
|
||||
file.write(b"Hello World").unwrap();
|
||||
|
@ -115,9 +112,7 @@ mod test {
|
|||
}
|
||||
|
||||
{
|
||||
let mut lf = LazyFile::new(path);
|
||||
let mut file = lf.create_file().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());
|
||||
|
|
Loading…
Reference in New Issue