Skip to content

Commit

Permalink
impl locking methode for derived backends and check capabilities in l…
Browse files Browse the repository at this point in the history
…ock commands
  • Loading branch information
aawsome committed Sep 28, 2024
1 parent 812f152 commit 34fe729
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
5 changes: 3 additions & 2 deletions crates/core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use anyhow::Result;
use bytes::Bytes;
use chrono::{DateTime, Local};
use enum_map::Enum;
use log::trace;
use log::{debug, trace};

#[cfg(test)]
use mockall::mock;
Expand Down Expand Up @@ -356,7 +356,8 @@ pub trait WriteBackend: ReadBackend {
/// # Errors
///
/// If the file could not be read.
fn lock(&self, _tpe: FileType, _id: &Id, _until: Option<DateTime<Local>>) -> Result<()> {
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
debug!("no locking implemented. {tpe:?}, {id}, {until:?}");
Ok(())
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/core/src/backend/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{

use anyhow::Result;
use bytes::Bytes;
use chrono::{DateTime, Local};
use dirs::cache_dir;
use log::{trace, warn};
use walkdir::WalkDir;
Expand Down Expand Up @@ -210,6 +211,14 @@ impl WriteBackend for CachedBackend {
}
self.be.remove(tpe, id, cacheable)
}

fn can_lock(&self) -> bool {
self.be.can_lock()
}

fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
self.be.lock(tpe, id, until)
}
}

/// Backend that caches data in a directory.
Expand Down
8 changes: 8 additions & 0 deletions crates/core/src/backend/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,14 @@ impl<C: CryptoKey> WriteBackend for DecryptBackend<C> {
fn remove(&self, tpe: FileType, id: &Id, cacheable: bool) -> Result<()> {
self.be.remove(tpe, id, cacheable)
}

fn can_lock(&self) -> bool {
self.be.can_lock()
}

fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
self.be.lock(tpe, id, until)
}
}

#[cfg(test)]
Expand Down
9 changes: 9 additions & 0 deletions crates/core/src/backend/dry_run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use bytes::Bytes;
use chrono::{DateTime, Local};
use zstd::decode_all;

use crate::{
Expand Down Expand Up @@ -156,4 +157,12 @@ impl<BE: DecryptFullBackend> WriteBackend for DryRunBackend<BE> {
self.be.remove(tpe, id, cacheable)
}
}

fn can_lock(&self) -> bool {
self.be.can_lock()
}

fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
self.be.lock(tpe, id, until)
}
}
9 changes: 9 additions & 0 deletions crates/core/src/backend/hotcold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use anyhow::Result;
use bytes::Bytes;
use chrono::{DateTime, Local};

use crate::{
backend::{FileType, ReadBackend, WriteBackend},
Expand Down Expand Up @@ -98,4 +99,12 @@ impl WriteBackend for HotColdBackend {
}
Ok(())
}

fn can_lock(&self) -> bool {
self.be.can_lock()
}

fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
self.be.lock(tpe, id, until)
}
}
17 changes: 16 additions & 1 deletion crates/core/src/commands/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
configfile::ConfigId, IndexFile, IndexId, KeyId, PackId, RepoId, SnapshotFile, SnapshotId,
},
repository::{Open, Repository},
BlobId, TreeId,
BlobId, TreeId, WriteBackend,
};

pub(super) mod constants {
Expand Down Expand Up @@ -52,6 +52,10 @@ impl LockOptions {
snapshots: &[SnapshotFile],
now: DateTime<Local>,
) -> RusticResult<()> {
if !repo.be.can_lock() {
return Err(CommandErrorKind::NoLockingConfigured.into());
}

let pb = &repo.pb;
let be = repo.dbe();

Expand Down Expand Up @@ -82,6 +86,10 @@ impl LockOptions {
snapshots: &[SnapshotFile],
now: DateTime<Local>,
) -> RusticResult<()> {
if !repo.be.can_lock() {
return Err(CommandErrorKind::NoLockingConfigured.into());
}

let mut new_snaps = Vec::new();
let mut remove_snaps = Vec::new();
let mut lock_snaps = Vec::new();
Expand Down Expand Up @@ -119,6 +127,9 @@ impl LockOptions {
index_files: Vec<(IndexId, IndexFile)>,
packs: &BTreeSet<PackId>,
) -> RusticResult<()> {
if !repo.be.can_lock() {
return Err(CommandErrorKind::NoLockingConfigured.into());
}
let mut lock_packs = Vec::new();
let mut remove_index = Vec::new();

Expand Down Expand Up @@ -182,6 +193,10 @@ pub fn lock_all_files<P: ProgressBars, S, ID: RepoId>(
repo: &Repository<P, S>,
until: Option<DateTime<Local>>,
) -> RusticResult<()> {
if !repo.be.can_lock() {
return Err(CommandErrorKind::NoLockingConfigured.into());
}

let p = &repo
.pb
.progress_spinner(format!("listing {:?} files..", ID::TYPE));
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ pub enum CommandErrorKind {
PackIdNotFoundinIndex(PackId),
/// Blob Id {0:?} not found in index
BlobIdNotFoundinIndex(BlobId),
/// No locking capability configured for the backend
NoLockingConfigured,
}

/// [`CryptoErrorKind`] describes the errors that can happen while dealing with Cryptographic functions
Expand Down

0 comments on commit 34fe729

Please sign in to comment.