Start testing index

This commit is contained in:
Cyryl Płotnicki 2018-10-04 16:11:47 +01:00
parent 5ec019d16c
commit 4a7be9bd49
5 changed files with 56 additions and 4 deletions

View file

@ -5,6 +5,7 @@ authors = ["Cyryl Płotnicki <cyplo@cyplo.net>"]
[dependencies] [dependencies]
walkdir = "2.2" walkdir = "2.2"
rust-crypto = "0.2"
[dev-dependencies] [dev-dependencies]
tempfile = "3.0" tempfile = "3.0"

View file

@ -1,6 +1,7 @@
use std::fs; use std::fs;
use std::io; use std::io;
use std::path::Path; use std::path::Path;
use storage::Version;
use walkdir::DirEntry; use walkdir::DirEntry;
use walkdir::WalkDir; use walkdir::WalkDir;
@ -28,11 +29,15 @@ impl<'a> Engine<'a> {
Ok(()) Ok(())
} }
pub fn file_version(&self, path: &Path) -> u64 { pub fn file_version(&self, path: &Path) -> Version {
0 Version(0)
} }
fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> { fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> {
// TODO: remember entry in index
// TODO: store file data
if entry.file_type().is_dir() { if entry.file_type().is_dir() {
fs::create_dir(self.repository_path.join(entry.file_name()))?; fs::create_dir(self.repository_path.join(entry.file_name()))?;
} }

View file

@ -1,4 +1,5 @@
extern crate core; extern crate core;
extern crate crypto;
#[cfg(test)] #[cfg(test)]
extern crate dir_diff; extern crate dir_diff;
#[cfg(test)] #[cfg(test)]
@ -7,3 +8,5 @@ extern crate walkdir;
pub mod backup; pub mod backup;
pub mod restore; pub mod restore;
mod storage;

View file

@ -1,6 +1,7 @@
use std::fs; use std::fs;
use std::io; use std::io;
use std::path::Path; use std::path::Path;
use storage::Version;
use walkdir::DirEntry; use walkdir::DirEntry;
use walkdir::WalkDir; use walkdir::WalkDir;
@ -27,10 +28,10 @@ impl<'a> Engine<'a> {
} }
fn restore(&self, what: WhatToRestore) -> Result<(), io::Error> { 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); let walker = WalkDir::new(self.repository_path);
for maybe_entry in walker { for maybe_entry in walker {
match maybe_entry { match maybe_entry {

42
src/storage.rs Normal file
View file

@ -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
}
}