From 4a7be9bd49ed8a20989c10c77eb49d5e3912da11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Thu, 4 Oct 2018 16:11:47 +0100 Subject: [PATCH] Start testing index --- Cargo.toml | 1 + src/backup.rs | 9 +++++++-- src/lib.rs | 3 +++ src/restore.rs | 5 +++-- src/storage.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/storage.rs 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 + } +}