Store files under their versions

This commit is contained in:
Cyryl Płotnicki 2019-09-07 13:37:51 +01:00
parent 8f67cccac9
commit a3f979c32d
4 changed files with 12 additions and 10 deletions

View file

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

View file

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

View file

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

View file

@ -163,3 +163,4 @@ fn get_sorted_files_recursively(path: &Path) -> Result<Vec<Box<Path>>, BakareErr
// TODO: deduplicate data
// TODO: test that index is stored separately from data
// TODO: index corruption
// TODO: forbid source within repository