diff --git a/Cargo.toml b/Cargo.toml index 74529f0..a37f332 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Cyryl PÅ‚otnicki "] [dependencies] walkdir = "2.2" +rust-crypto = "0.2" [dev-dependencies] tempfile = "3.0" diff --git a/src/backup.rs b/src/backup.rs index e110a2f..ae4fedc 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -1,6 +1,7 @@ use std::fs; use std::io; use std::path::Path; +use storage::Version; use walkdir::DirEntry; use walkdir::WalkDir; @@ -28,11 +29,15 @@ impl<'a> Engine<'a> { Ok(()) } - pub fn file_version(&self, path: &Path) -> u64 { - 0 + pub fn file_version(&self, path: &Path) -> Version { + Version(0) } fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> { + // TODO: remember entry in index + + // TODO: store file data + if entry.file_type().is_dir() { fs::create_dir(self.repository_path.join(entry.file_name()))?; } diff --git a/src/lib.rs b/src/lib.rs index b212788..d26f206 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ extern crate core; +extern crate crypto; #[cfg(test)] extern crate dir_diff; #[cfg(test)] @@ -7,3 +8,5 @@ extern crate walkdir; pub mod backup; pub mod restore; + +mod storage; diff --git a/src/restore.rs b/src/restore.rs index 89c1517..77b3df1 100644 --- a/src/restore.rs +++ b/src/restore.rs @@ -1,6 +1,7 @@ use std::fs; use std::io; use std::path::Path; +use storage::Version; use walkdir::DirEntry; use walkdir::WalkDir; @@ -27,10 +28,10 @@ impl<'a> Engine<'a> { } fn restore(&self, what: WhatToRestore) -> Result<(), io::Error> { - self.restore_as_of_version(what, 0) + self.restore_as_of_version(what, Version(0)) } - pub fn restore_as_of_version(&self, what: WhatToRestore, version: u64) -> Result<(), io::Error> { + pub fn restore_as_of_version(&self, what: WhatToRestore, version: Version) -> Result<(), io::Error> { let walker = WalkDir::new(self.repository_path); for maybe_entry in walker { match maybe_entry { diff --git a/src/storage.rs b/src/storage.rs new file mode 100644 index 0000000..cb59102 --- /dev/null +++ b/src/storage.rs @@ -0,0 +1,42 @@ +use std::cmp::Ordering; +use std::path::Path; + +#[derive(Copy, Clone, PartialOrd, PartialEq)] +pub struct Version(pub u64); + +struct Index; + +impl Index { + fn new() -> Self { + Self {} + } + + fn store(&mut self, path: &Path, hash: &[u8]) -> Version { + Version(0) + } +} + +#[cfg(test)] +mod should { + + use super::*; + + #[test] + fn support_file_versions() { + // put path and hash into index -> v0 + // put same path different hash -> v1 + // query for v0, v1 + let mut index = Index::new(); + let v1 = index.store(Path::new("/some/path"), "some hash".as_bytes()); + let v2 = index.store(Path::new("/some/path"), "some other hash".as_bytes()); + + assert!(v2 > v1); + } + + #[test] + fn support_deduplication() { + // put path and hash into index + // put same hash, different path + // should get same storage paths + } +}