Reimplement StorageBackend::update_file()
This commit is contained in:
parent
8bd782c1a7
commit
25b26b03a6
1 changed files with 30 additions and 34 deletions
|
@ -129,44 +129,40 @@ impl StorageBackend {
|
||||||
* Update a file. We have the UUID and can find the file on FS with it and
|
* Update a file. We have the UUID and can find the file on FS with it and
|
||||||
* then replace its contents with the contents of the passed file object
|
* then replace its contents with the contents of the passed file object
|
||||||
*/
|
*/
|
||||||
pub fn update_file<'a, HP>(&self, f: File, p: &Parser<HP>)
|
pub fn update_file<HP>(&self, f: File, p: &Parser<HP>) -> BackendOperationResult
|
||||||
-> Result<BackendOperationResult, ParserError>
|
where HP: FileHeaderParser
|
||||||
where HP: FileHeaderParser<'a>
|
|
||||||
{
|
{
|
||||||
let contents = p.write(f.contents());
|
let contents = write_with_parser(&f, p);
|
||||||
|
if contents.is_err() { return Err(contents.err().unwrap()); }
|
||||||
if contents.is_err() {
|
let string = contents.unwrap();
|
||||||
debug!("Error parsing contents: {:?}", f.contents());
|
|
||||||
return Err(contents.err().unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
let content = contents.unwrap();
|
|
||||||
debug!("Success parsing content : {}", content);
|
|
||||||
|
|
||||||
let path = self.build_filepath(&f);
|
let path = self.build_filepath(&f);
|
||||||
debug!("Trying to write to file at {}", path);
|
debug!("Writing file: {}", path);
|
||||||
if let Err(_) = FSFile::open(&path) {
|
debug!(" string: {}", string);
|
||||||
debug!("Error opening {}", path);
|
|
||||||
return Ok(Err(StorageBackendError::new(
|
|
||||||
String::from("File::open()"),
|
|
||||||
format!("Tried to open '{}'", path),
|
|
||||||
String::from("Tried to update contents of this file, though file doesn't exist"),
|
|
||||||
None)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(mut file) = FSFile::create(&path) {
|
FSFile::open(&path).map(|mut file| {
|
||||||
if let Err(writeerr) = file.write_all(&content.clone().into_bytes()) {
|
debug!("Open file at '{}'", path);
|
||||||
debug!("Error writing to {}", path);
|
file.write_all(&string.clone().into_bytes())
|
||||||
return Ok(Err(StorageBackendError::new(
|
.map_err(|ioerr| {
|
||||||
String::from("File::write()"),
|
debug!("Could not write file");
|
||||||
format!("Tried to write '{}'", path),
|
let mut err = StorageBackendError::build(
|
||||||
String::from("Tried to write contents of this file, though operation did not succeed"),
|
"File::write()",
|
||||||
Some(content))))
|
"Tried to write contents of this file, though operation did not succeed",
|
||||||
}
|
Some(string)
|
||||||
}
|
);
|
||||||
|
err.caused_by = Some(Box::new(ioerr));
|
||||||
debug!("Successfully written to file.");
|
err
|
||||||
Ok(Ok(()))
|
})
|
||||||
|
}).map_err(|writeerr| {
|
||||||
|
debug!("Could not write file at '{}'", path);
|
||||||
|
let mut err = StorageBackendError::build(
|
||||||
|
"File::open()",
|
||||||
|
"Tried to update contents of this file, though file doesn't exist",
|
||||||
|
None
|
||||||
|
);
|
||||||
|
err.caused_by = Some(Box::new(writeerr));
|
||||||
|
err
|
||||||
|
}).and(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue