From a3f979c32dd2a17c0b4763e721992c83c7455ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Sat, 7 Sep 2019 13:37:51 +0100 Subject: [PATCH] Store files under their versions --- src/index.rs | 9 ++++++++- src/repository.rs | 10 ++-------- src/repository_item.rs | 2 +- tests/system_tests.rs | 1 + 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/index.rs b/src/index.rs index 1abef3b..871875f 100644 --- a/src/index.rs +++ b/src/index.rs @@ -4,8 +4,9 @@ use serde::{Deserialize, Serialize}; use crate::error::BakareError; use crate::repository_item::RepositoryItem; +use std::fmt; +use std::fmt::Formatter; use std::fs::File; -use std::ops::Deref; #[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)] pub struct ItemVersion(Box<[u8]>); @@ -36,6 +37,12 @@ impl From<&[u8]> for ItemVersion { } } +impl fmt::Display for ItemVersion { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + write!(f, "{}", hex::encode(self)) + } +} + impl Index { pub fn new(repository_path: &Path) -> Self { Index { diff --git a/src/repository.rs b/src/repository.rs index 31e4fc7..9498f92 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -57,18 +57,12 @@ impl<'a> Repository<'a> { if !source_path.is_absolute() { return Err(BakareError::PathToStoreNotAbsolute); } - let destination_path: &str = &(self.path.to_string_lossy() + source_path.to_string_lossy()); + let version = Repository::calculate_version(source_path)?; + let destination_path = self.path.join(version.to_string()); let destination_path = Path::new(&destination_path); - if source_path == destination_path { - return Err(BakareError::SourceSameAsRepository); - } - if source_path.is_dir() { - fs::create_dir(destination_path)?; - } if source_path.is_file() { fs::create_dir_all(destination_path.parent().unwrap())?; - let version = Repository::calculate_version(source_path)?; fs::copy(source_path, destination_path)?; self.index.remember(RepositoryItem::from( diff --git a/src/repository_item.rs b/src/repository_item.rs index 83cd3cc..c3da056 100644 --- a/src/repository_item.rs +++ b/src/repository_item.rs @@ -27,7 +27,7 @@ impl RepositoryItem { return Err(BakareError::PathToStoreNotAbsolute); } - let target_path = save_to.join(&self.relative_path); + let target_path = save_to.join(&self.original_source_path.strip_prefix("/")?); if !target_path.is_absolute() { return Err(BakareError::PathToStoreNotAbsolute); } diff --git a/tests/system_tests.rs b/tests/system_tests.rs index 7a8f99f..5353913 100644 --- a/tests/system_tests.rs +++ b/tests/system_tests.rs @@ -163,3 +163,4 @@ fn get_sorted_files_recursively(path: &Path) -> Result>, BakareErr // TODO: deduplicate data // TODO: test that index is stored separately from data // TODO: index corruption +// TODO: forbid source within repository