Add list_with_size to ReadBackend

This commit is contained in:
Alexander Weiss 2022-02-08 23:28:31 +01:00
parent 0b1e74ac37
commit addc0255de
3 changed files with 32 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use thiserror::Error;
use super::{FileType, Id, ReadBackend};
use super::{FileType, Id, IdWithSize, ReadBackend};
use crate::crypto::{CryptoError, Key};
/// RepoError describes the errors that can be returned by accessing this repository
@ -41,6 +41,12 @@ impl<R: ReadBackend> ReadBackend for DecryptBackend<R> {
self.backend.list(tpe).map_err(RepoError::RepoError)
}
fn list_with_size(&self, tpe: FileType) -> Result<Vec<IdWithSize>, Self::Error> {
self.backend
.list_with_size(tpe)
.map_err(RepoError::RepoError)
}
fn read_full(&self, tpe: FileType, id: Id) -> Result<Vec<u8>, Self::Error> {
self.key
.decrypt_data(&self.backend.read_full(tpe, id)?)

View File

@ -3,7 +3,7 @@ use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
use walkdir::WalkDir;
use super::{FileType, Id, ReadBackend};
use super::{FileType, Id, IdWithSize, ReadBackend};
#[derive(Clone)]
pub struct LocalBackend {
@ -50,6 +50,20 @@ impl ReadBackend for LocalBackend {
Ok(walker.collect())
}
fn list_with_size(&self, tpe: FileType) -> Result<Vec<IdWithSize>, Self::Error> {
let walker = WalkDir::new(self.path.join(tpe.name()))
.into_iter()
.filter_map(walkdir::Result::ok)
.filter(|e| e.file_type().is_file())
.map(|e| {
IdWithSize::new(
Id::from_hex(&e.file_name().to_string_lossy()).unwrap(),
e.metadata().unwrap().len().try_into().unwrap(),
)
});
Ok(walker.collect())
}
fn read_full(&self, tpe: FileType, id: Id) -> Result<Vec<u8>, Self::Error> {
fs::read(self.path(tpe, id))
}

View File

@ -1,11 +1,12 @@
use crate::id::*;
use derive_more::Constructor;
pub mod decrypt;
pub mod local;
pub use decrypt::DecryptBackend;
pub use local::LocalBackend;
use crate::id::*;
#[derive(Clone, Copy)]
pub enum FileType {
Config,
@ -27,11 +28,18 @@ impl FileType {
}
}
#[derive(Constructor)]
pub struct IdWithSize {
id: Id,
size: u32,
}
pub trait ReadBackend: Clone {
type Error: Send + Sync + std::error::Error + 'static;
fn location(&self) -> &str;
fn list(&self, tpe: FileType) -> Result<Vec<Id>, Self::Error>;
fn list_with_size(&self, tpe: FileType) -> Result<Vec<IdWithSize>, Self::Error>;
fn read_full(&self, tpe: FileType, id: Id) -> Result<Vec<u8>, Self::Error>;
fn read_partial(
&self,