imag/src/storage/backend.rs

125 lines
2.8 KiB
Rust
Raw Normal View History

2015-11-10 18:27:54 +00:00
use std::error::Error;
use std::fmt::Display;
2015-11-10 18:51:43 +00:00
use std::fmt::Formatter;
use std::fmt::Result as FMTResult;
use std::path::Path;
2015-11-23 17:22:02 +00:00
use std::path::PathBuf;
use std::vec::Vec;
use std::fs::File as FSFile;
use std::io::Read;
use glob::glob;
2015-11-23 17:22:02 +00:00
use glob::Paths;
2015-11-10 18:27:54 +00:00
2015-11-23 17:42:55 +00:00
use storage::file::File;
use storage::file_id::*;
2015-11-10 18:27:54 +00:00
type BackendOperationResult = Result<(), StorageBackendError>;
2015-11-23 17:45:31 +00:00
pub struct StorageBackend {
2015-11-10 18:27:54 +00:00
basepath: String,
}
2015-11-23 17:45:31 +00:00
impl StorageBackend {
2015-11-10 18:27:54 +00:00
2015-11-23 17:45:31 +00:00
fn new(basepath: String) -> StorageBackend {
2015-11-10 19:53:35 +00:00
StorageBackend {
basepath: basepath,
}
2015-11-10 18:27:54 +00:00
}
2015-11-23 17:22:02 +00:00
fn getFileList(&self) -> Option<Vec<FileID>> {
let list = glob(&self.basepath[..]);
if let Ok(globlist) = list {
let mut v = vec![];
for entry in globlist {
if let Ok(path) = entry {
2015-11-23 17:42:55 +00:00
v.push(from_pathbuf(&path));
2015-11-23 17:22:02 +00:00
} else {
// Entry is not a path
}
}
Some(v)
} else {
None
}
2015-11-10 18:27:54 +00:00
}
fn createEmpty(&self) -> Option<FileID> {
self.new_file_handle().and_then(|(id, _)| Some(id))
2015-11-10 18:27:54 +00:00
}
fn createFile() -> File {
}
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
}
}
2015-11-10 18:27:54 +00:00
}
#[derive(Debug)]
2015-11-10 18:51:43 +00:00
pub struct StorageBackendError {
pub action: String, // The file system action in words
pub desc: String, // A short description
pub explanation: String, // A long, user friendly description
pub dataDump: Option<String> // Data dump, if any
}
2015-11-10 18:27:54 +00:00
impl StorageBackendError {
2015-11-10 18:51:43 +00:00
fn new(action: &'static str,
desc : &'static str,
explan: &'static str,
data : Option<String>) -> StorageBackendError
{
StorageBackendError {
action: String::from(action),
desc: String::from(desc),
explanation: String::from(explan),
dataDump: data,
}
}
2015-11-10 18:27:54 +00:00
}
impl Error for StorageBackendError {
2015-11-10 18:51:43 +00:00
fn description(&self) -> &str {
&self.desc[..]
}
fn cause(&self) -> Option<&Error> {
None
}
2015-11-10 18:27:54 +00:00
}
impl Display for StorageBackendError {
2015-11-10 18:51:43 +00:00
fn fmt(&self, f: &mut Formatter) -> FMTResult {
write!(f, "StorageBackendError[{}]: {}\n\n{}",
self.action, self.desc, self.explanation)
}
2015-11-10 18:27:54 +00:00
}