Fix index loading problems

This commit is contained in:
Cyryl Płotnicki 2019-09-07 09:51:38 +01:00
parent 14490bbe78
commit f397c99659
4 changed files with 22 additions and 10 deletions

View file

@ -18,7 +18,7 @@ pub enum BakareError {
#[fail(display = "corrupted repository - cannot find file")]
CorruptedRepoNoFile,
#[fail(display = "index loading error")]
IndexLoadingError,
IndexLoadingError(Option<serde_cbor::Error>),
}
impl From<io::Error> for BakareError {
@ -40,7 +40,7 @@ impl From<StripPrefixError> for BakareError {
}
impl From<serde_cbor::Error> for BakareError {
fn from(_: serde_cbor::Error) -> Self {
BakareError::IndexLoadingError
fn from(e: serde_cbor::Error) -> Self {
BakareError::IndexLoadingError(Some(e))
}
}

View file

@ -2,7 +2,9 @@ use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::error::BakareError;
use crate::repository_item::RepositoryItem;
use std::fs::File;
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)]
pub struct IndexItem {
@ -27,6 +29,19 @@ impl Index {
}
}
pub fn load(path: &Path) -> Result<Self, BakareError> {
let index_file_path = path.join("index");
let index_file = File::open(index_file_path)?;
let index: Index = serde_cbor::from_reader(index_file)?;
Ok(index)
}
pub fn save(&self) -> Result<(), BakareError> {
let index_file = File::create(self.index_file_path())?;
serde_cbor::to_writer(index_file, &self)?;
Ok(())
}
pub fn index_file_path(&self) -> &Path {
Path::new(&self.index_path)
}

View file

@ -34,8 +34,7 @@ impl<'a> Iterator for RepositoryIterator<'a> {
impl<'a> Repository<'a> {
pub fn init(path: &Path) -> Result<(), BakareError> {
let index = Index::new(path);
let index_file = File::create(index.index_file_path())?;
serde_cbor::to_writer(index_file, &index)?;
index.save()?;
Ok(())
}
@ -44,10 +43,7 @@ impl<'a> Repository<'a> {
return Err(BakareError::RepositoryPathNotAbsolute);
}
let index_file_path = path.join("index");
let index_file = File::open(index_file_path)?;
let index: Index = serde_cbor::from_reader(index_file)?;
let index = Index::load(path)?;
Ok(Repository { path, index })
}
@ -82,6 +78,7 @@ impl<'a> Repository<'a> {
destination_path.strip_prefix(self.path)?,
version,
));
self.index.save()?;
}
Ok(())
}

View file

@ -25,7 +25,7 @@ impl<'a> Engine<'a> {
}
fn restore(&self, item: &RepositoryItem) -> Result<(), BakareError> {
println!("restoring {:#?}", item);
println!("restoring {}", item);
item.save(self.target_path)?;
Ok(())
}