Reset and restart implementation

This commit is contained in:
Matthias Beyer 2015-11-23 19:25:27 +01:00
parent e623b74139
commit b8fa7c5696
2 changed files with 31 additions and 58 deletions

View file

@ -14,7 +14,7 @@ use glob::Paths;
use storage::file::File;
use storage::file_id::*;
type BackendOperationResult = Result<(), StorageBackendError>;
pub type BackendOperationResult = Result<(), StorageBackendError>;
pub struct StorageBackend {
basepath: String,
@ -47,36 +47,29 @@ impl StorageBackend {
}
}
fn createEmpty(&self) -> Option<FileID> {
self.new_file_handle().and_then(|(id, _)| Some(id))
/*
* Write a file to disk.
*
* The file is moved to this function as the file won't be edited afterwards
*/
pub fn put_file(f: File) -> BackendOperationResult {
}
fn createFile(&self) -> Option<File> {
self.new_file_handle().and_then(|(id, h)| Some(File::from_handle(id, h)))
/*
* 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
*/
pub fn update_file(f: File) -> BackendOperationResult {
}
fn writeFile(f: File) -> BackendOperationResult {
}
fn createFileWithContent(content: String) -> BackendOperationResult {
}
fn readFile(id: FileID) -> String {
}
// TODO: Meta files are not covered yet
fn new_file_handle(&self) -> Option<(FileID, FSFile)> {
use uuid::Uuid;
let uuid = Uuid::new_v4().to_hyphenated_string();
let pathstr = self.basepath + uuid.as_str();
let path = Path::new(&pathstr);
if let Ok(f) = FSFile::create(path) {
Some((uuid, f))
} else {
None
}
/*
* Find a file by its ID and return it if found. Return nothing if not
* found, of course.
*
* TODO: Needs refactoring, as there might be an error when reading from
* disk OR the id just does not exist.
*/
pub fn get_file_by_id(id: FileID) -> Option<File> {
}
}

View file

@ -165,49 +165,29 @@ pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData)
None
}
/*
* Internal abstract view on a file. Does not exist on the FS and is just kept
* internally until it is written to disk.
*/
pub struct File {
header : FileHeaderData,
data : String,
id : FileID,
handle : Option<FSFile>,
}
impl<'a> File {
fn new<HP>(prs: &Parser<HP>, path: &String) -> Result<File, ParserError>
where HP: FileHeaderParser<'a>
{
File::read_file(path).and_then(|p| prs.read(p))
.and_then(|(h, d)|
Ok(File {
header: h,
data: d,
id: from_path_string(path),
handle: None,
}))
}
pub fn from_handle(id: FileID, f: FSFile) -> File {
use std::io::Read;
let mut contents = String::new();
f.read_to_string(&mut contents);
impl File {
pub fn new() -> File {
File {
header: FileHeaderData::Null,
data: contents,
id: id,
handle: Some(f)
data: String::from(""),
id: File::get_new_file_id(),
}
}
fn getID(&self) -> FileID {
self.id.clone()
fn get_new_file_id() -> FileID {
use uuid::Uuid;
Uuid::new_v4().to_hyphenated_string()
}
fn read_file(p: &String) -> Result<String, ParserError> {
Ok(String::from(""))
}
}