Skip to content

Commit 5c229c5

Browse files
simonsanaawsome
authored andcommitted
Proposed changes from review
Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
1 parent 1a526bd commit 5c229c5

File tree

7 files changed

+64
-10
lines changed

7 files changed

+64
-10
lines changed

crates/core/src/backend.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,15 @@ pub trait WriteBackend: ReadBackend {
358358
/// If the file could not be read.
359359
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> {
360360
debug!("no locking implemented. {tpe:?}, {id}, {until:?}");
361-
Ok(())
361+
362+
if self.can_lock() {
363+
unimplemented!("Using default implementation. No locking implemented in backend.");
364+
} else {
365+
Err(RusticError::new(
366+
ErrorKind::Backend,
367+
"No locking configured on backend.",
368+
))
369+
}
362370
}
363371
}
364372

crates/core/src/backend/cache.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ impl WriteBackend for CachedBackend {
233233
}
234234

235235
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> {
236+
if !self.can_lock() {
237+
return Err(RusticError::new(
238+
ErrorKind::Backend,
239+
"No locking configured on backend.",
240+
));
241+
}
242+
236243
self.be.lock(tpe, id, until)
237244
}
238245
}

crates/core/src/backend/decrypt.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,13 @@ impl<C: CryptoKey> WriteBackend for DecryptBackend<C> {
643643
}
644644

645645
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> {
646+
if !self.can_lock() {
647+
return Err(RusticError::new(
648+
ErrorKind::Backend,
649+
"No locking configured on backend.",
650+
));
651+
}
652+
646653
self.be.lock(tpe, id, until)
647654
}
648655
}

crates/core/src/backend/dry_run.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ impl<BE: DecryptFullBackend> WriteBackend for DryRunBackend<BE> {
171171
}
172172

173173
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> {
174+
if !self.can_lock() {
175+
return Err(RusticError::new(
176+
ErrorKind::Backend,
177+
"No locking configured on backend.",
178+
));
179+
}
174180
self.be.lock(tpe, id, until)
175181
}
176182
}

crates/core/src/backend/hotcold.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bytes::Bytes;
44
use chrono::{DateTime, Local};
55

66
use crate::{
7+
ErrorKind, RusticError,
78
backend::{FileType, ReadBackend, WriteBackend},
89
error::RusticResult,
910
id::Id,
@@ -105,6 +106,12 @@ impl WriteBackend for HotColdBackend {
105106
}
106107

107108
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> {
109+
if !self.can_lock() {
110+
return Err(RusticError::new(
111+
ErrorKind::Backend,
112+
"No locking configured on backend.",
113+
));
114+
}
108115
self.be.lock(tpe, id, until)
109116
}
110117
}

crates/core/src/backend/lock.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{process::Command, sync::Arc};
22

33
use bytes::Bytes;
44
use chrono::{DateTime, Local};
5-
use log::{debug, warn};
5+
use log::debug;
66

77
use crate::{
88
CommandInput, ErrorKind, RusticError, RusticResult,
@@ -67,7 +67,7 @@ impl ReadBackend for LockBackend {
6767
}
6868
}
6969

70-
fn path(tpe: FileType, id: &Id) -> String {
70+
fn path_to_id_from_file_type(tpe: FileType, id: &Id) -> String {
7171
let hex_id = id.to_hex();
7272
match tpe {
7373
FileType::Config => "config".into(),
@@ -94,15 +94,26 @@ impl WriteBackend for LockBackend {
9494
}
9595

9696
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> {
97+
if !self.can_lock() {
98+
return Err(RusticError::new(
99+
ErrorKind::Backend,
100+
"No locking configured on backend.",
101+
));
102+
}
103+
97104
let until = until.map_or_else(String::new, |u| u.to_rfc3339());
98-
let path = path(tpe, id);
105+
106+
let path = path_to_id_from_file_type(tpe, id);
107+
99108
let args = self.command.args().iter().map(|c| {
100109
c.replace("%id", &id.to_hex())
101110
.replace("%type", tpe.dirname())
102111
.replace("%path", &path)
103112
.replace("%until", &until)
104113
});
114+
105115
debug!("calling {:?}...", self.command);
116+
106117
let status = Command::new(self.command.command())
107118
.args(args)
108119
.status()
@@ -115,9 +126,17 @@ impl WriteBackend for LockBackend {
115126
.attach_context("tpe", tpe.to_string())
116127
.attach_context("id", id.to_string())
117128
})?;
129+
118130
if !status.success() {
119-
warn!("lock command was not successful for {tpe:?}, id: {id}. {status}");
131+
return Err(RusticError::new(
132+
ErrorKind::Backend,
133+
"lock command was not successful for {tpe}, id: {id}. {status}",
134+
)
135+
.attach_context("tpe", tpe.to_string())
136+
.attach_context("id", id.to_string())
137+
.attach_context("status", status.to_string()));
120138
}
139+
121140
Ok(())
122141
}
123142
}

crates/core/src/commands/lock.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pub fn lock_all_files<P: ProgressBars, S, ID: RepoId + std::fmt::Debug>(
3535
) -> RusticResult<()> {
3636
if !repo.be.can_lock() {
3737
return Err(RusticError::new(
38-
ErrorKind::Internal,
39-
"Tried to call lock_all_files on a backend which isn't able to lock.",
38+
ErrorKind::Backend,
39+
"No locking configured on backend.",
4040
));
4141
}
4242

@@ -71,9 +71,9 @@ fn lock_files<P: ProgressBars, S, ID: RepoId + std::fmt::Debug>(
7171
pool.in_place_scope(|scope| {
7272
for id in ids {
7373
scope.spawn(move |_| {
74-
if let Err(e) = backend.lock(ID::TYPE, id, until) {
75-
// FIXME: Use error handling
76-
error!("lock failed for {:?} {id:?}. {e}", ID::TYPE);
74+
if let Err(err) = backend.lock(ID::TYPE, id, until) {
75+
// FIXME: Use error handling, e.g. use a channel to collect the errors
76+
error!("lock failed for {:?} {id:?}. {err}", ID::TYPE);
7777
}
7878
p.inc(1);
7979
});

0 commit comments

Comments
 (0)