Skip to content

Commit 33290a9

Browse files
committed
Proposed changes from review
Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
1 parent dac77f0 commit 33290a9

File tree

7 files changed

+43
-8
lines changed

7 files changed

+43
-8
lines changed

crates/core/src/backend.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,12 @@ pub trait WriteBackend: ReadBackend {
358358
/// If the file could not be read.
359359
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
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(anyhow::anyhow!("No locking configured."))
366+
}
362367
}
363368
}
364369

crates/core/src/backend/cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ impl WriteBackend for CachedBackend {
217217
}
218218

219219
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
220+
if !self.can_lock() {
221+
return Err(anyhow::anyhow!("No locking configured."));
222+
}
223+
220224
self.be.lock(tpe, id, until)
221225
}
222226
}

crates/core/src/backend/decrypt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ impl<C: CryptoKey> WriteBackend for DecryptBackend<C> {
585585
}
586586

587587
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
588+
if !self.can_lock() {
589+
return Err(anyhow::anyhow!("No locking configured."));
590+
}
591+
588592
self.be.lock(tpe, id, until)
589593
}
590594
}

crates/core/src/backend/dry_run.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ impl<BE: DecryptFullBackend> WriteBackend for DryRunBackend<BE> {
163163
}
164164

165165
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
166+
if !self.can_lock() {
167+
return Err(anyhow::anyhow!("No locking configured."));
168+
}
169+
166170
self.be.lock(tpe, id, until)
167171
}
168172
}

crates/core/src/backend/hotcold.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ impl WriteBackend for HotColdBackend {
105105
}
106106

107107
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
108+
if !self.can_lock() {
109+
return Err(anyhow::anyhow!("No locking configured."));
110+
}
111+
108112
self.be.lock(tpe, id, until)
109113
}
110114
}

crates/core/src/backend/lock.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{process::Command, sync::Arc};
33
use anyhow::Result;
44
use bytes::Bytes;
55
use chrono::{DateTime, Local};
6-
use log::{debug, warn};
6+
use log::{debug, error};
77

88
use crate::{
99
backend::{FileType, ReadBackend, WriteBackend},
@@ -68,7 +68,7 @@ impl ReadBackend for LockBackend {
6868
}
6969
}
7070

71-
fn path(tpe: FileType, id: &Id) -> String {
71+
fn path_to_id_from_file_type(tpe: FileType, id: &Id) -> String {
7272
let hex_id = id.to_hex();
7373
match tpe {
7474
FileType::Config => "config".into(),
@@ -95,19 +95,33 @@ impl WriteBackend for LockBackend {
9595
}
9696

9797
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> {
98+
if !self.can_lock() {
99+
return Err(anyhow::anyhow!("No locking configured."));
100+
}
101+
98102
let until = until.map_or_else(String::new, |u| u.to_rfc3339());
99-
let path = path(tpe, id);
103+
104+
let path = path_to_id_from_file_type(tpe, id);
105+
100106
let args = self.command.args().iter().map(|c| {
101107
c.replace("%id", &id.to_hex())
102108
.replace("%type", tpe.dirname())
103109
.replace("%path", &path)
104110
.replace("%until", &until)
105111
});
112+
106113
debug!("calling {:?}...", self.command);
114+
107115
let status = Command::new(self.command.command()).args(args).status()?;
116+
108117
if !status.success() {
109-
warn!("lock command was not successful for {tpe:?}, id: {id}. {status}");
118+
error!("lock command was not successful for {tpe:?}, id: {id}. {status}");
119+
120+
return Err(anyhow::anyhow!(
121+
"lock command was not successful for {tpe:?}, id: {id}. {status}"
122+
));
110123
}
124+
111125
Ok(())
112126
}
113127
}

crates/core/src/commands/lock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ fn lock_files<P: ProgressBars, S, ID: RepoId + std::fmt::Debug>(
6262
pool.in_place_scope(|scope| {
6363
for id in ids {
6464
scope.spawn(move |_| {
65-
if let Err(e) = backend.lock(ID::TYPE, id, until) {
66-
// FIXME: Use error handling
67-
error!("lock failed for {:?} {id:?}. {e}", ID::TYPE);
65+
if let Err(err) = backend.lock(ID::TYPE, id, until) {
66+
// FIXME: Use error handling, e.g. use a channel to collect the errors
67+
error!("lock failed for {:?} {id:?}. {err}", ID::TYPE);
6868
};
6969
p.inc(1);
7070
});

0 commit comments

Comments
 (0)