aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2017-10-28 13:19:29 -0700
committerBryan Newbold <bnewbold@robocracy.org>2017-10-28 13:19:29 -0700
commitdfbaa89a32d75acdbfbc06f686d952ee25af6bf7 (patch)
tree54d76b9240e2fc875e8bfad73dfe5af2c4922fcd
parent83d3ee0196af0b2c0b9bea1c945c794d4f84ae93 (diff)
downloadgeniza-dfbaa89a32d75acdbfbc06f686d952ee25af6bf7.tar.gz
geniza-dfbaa89a32d75acdbfbc06f686d952ee25af6bf7.zip
sketch out DatDrive API
-rw-r--r--src/drive.rs140
-rw-r--r--src/lib.rs2
2 files changed, 142 insertions, 0 deletions
diff --git a/src/drive.rs b/src/drive.rs
new file mode 100644
index 0000000..179bebb
--- /dev/null
+++ b/src/drive.rs
@@ -0,0 +1,140 @@
+
+use std::io::{Read, Write};
+use std::path::Path;
+use protobuf::Message;
+use protobuf::parse_from_bytes;
+
+use errors::*;
+use sleep_register::*;
+use metadata_msgs::{Index, Stat, Node};
+
+/// "Sort of" follows rust std::fs API for file system access.
+pub struct DatDrive {
+ metadata: SleepDirRegister,
+ content: SleepDirRegister,
+}
+
+impl DatDrive {
+
+ /// Instantiates a drive in the given directory. Path should be the complete path (eg, ending
+ /// in '/.dat/'), not an enclosing directory containing files.
+ pub fn create<P: AsRef<Path>>(path: P) -> Result<DatDrive> {
+ unimplemented!()
+ }
+
+ /// Path should be the complete path (eg, ending in '/.dat/'), not an enclosing directory
+ /// containing files.
+ pub fn open<P: AsRef<Path>>(path: P) -> Result<DatDrive> {
+ unimplemented!()
+ }
+}
+
+impl<'a> DatDrive {
+
+ pub fn history(start: u64) -> DriveHistory<'a> {
+ unimplemented!()
+ }
+
+ pub fn read_dir_recursive<P: AsRef<Path>>(path: P) -> ReadDriveDir<'a> {
+ unimplemented!()
+ }
+
+ pub fn read_dir<P: AsRef<Path>>(path: P) -> ReadDriveDir<'a> {
+ unimplemented!()
+ }
+
+ pub fn file_metadata<P: AsRef<Path>>(path: P) -> Result<Stat> {
+ unimplemented!()
+ }
+
+ pub fn create_file_bytes<P: AsRef<Path>>(path: P, stat: &Stat, data: &[u8]) -> Result<()> {
+ unimplemented!()
+ }
+
+ pub fn create_file<P: AsRef<Path>, R: Read>(path: P, stat: &Stat, source: R) -> Result<()> {
+ unimplemented!()
+ }
+
+ /// Copies Stat metadata and all content from a file in the "real" filesystem into the
+ /// DatDrive.
+ pub fn import_file<P: AsRef<Path>, Q: AsRef<Path>>(source: P, dest: Q) -> Result<()> {
+ unimplemented!()
+ }
+
+/* Possible future helper functions to be even more like std::fs
+ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()>
+ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()>
+ pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()>
+ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<()>
+*/
+
+}
+
+#[test]
+fn test_dd_open() {
+
+ let mut dd =
+ DatDrive::open(Path::new("test-data/dat/simple/.dat/")).unwrap();
+
+ // verified from dat log
+ assert_eq!(dd.history().collect().len(), 2);
+ assert_eq!(dd.read_dir().collect().len(), 1);
+ assert_eq!(dd.read_dir_recurisve().collect().len(), 1);
+}
+
+#[test]
+fn test_dd_create() {
+ use tempdir::TempDir;
+ let tmp_dir = TempDir::new("geniza-test").unwrap();
+ let mut dd = DatDrive::create(tmp_dir.path()).unwrap();
+
+ assert_eq!(dd.history().collect().len(), 0);
+ assert_eq!(dd.read_dir().collect().len(), 0);
+ assert_eq!(dd.read_dir_recurisve().collect().len(), 0);
+}
+
+pub struct DriveEntry {
+ node: Node,
+ index: u64,
+}
+
+pub struct DriveHistory<'a> {
+ drive: &'a mut DatDrive,
+}
+
+/// Iterator
+pub struct ReadDriveDir<'a> {
+ drive: &'a mut DatDrive,
+ recursive: bool,
+
+ // Entries to recurse over
+ entries: Vec<u64>,
+}
+
+impl<'a> ReadDriveDir<'a> {
+ fn init<P: AsRef<Path>>(drive: &mut DatDrive, path: P, recursive: bool) {
+ unimplemented!();
+ // TODO: starting from the last data entry, recurse up to nearest directory, then recurse
+ // down to base path
+ //ReadDriveDir {
+ // drive,
+ // recursive,
+ // entries: vec![],
+ //}
+ }
+}
+
+impl<'a> Iterator for DriveHistory<'a> {
+ type Item = DriveEntry;
+ fn next(&mut self) -> Option<DriveEntry> {
+ unimplemented!();
+ }
+}
+
+impl<'a> Iterator for ReadDriveDir<'a> {
+ type Item = DriveEntry;
+ fn next(&mut self) -> Option<DriveEntry> {
+ unimplemented!();
+ }
+}
+
diff --git a/src/lib.rs b/src/lib.rs
index ddd67d8..694265e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -46,6 +46,8 @@ mod sleep_file;
pub use sleep_file::*;
mod sleep_register;
pub use sleep_register::*;
+mod drive;
+pub use drive::*;
mod protocol;
pub use protocol::*;
pub mod network_msgs;