-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Add Repository::lock_repo #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aawsome
wants to merge
2
commits into
main
Choose a base branch
from
lock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use std::{process::Command, sync::Arc}; | ||
|
||
use bytes::Bytes; | ||
use chrono::{DateTime, Local}; | ||
use log::debug; | ||
|
||
use crate::{ | ||
CommandInput, ErrorKind, RusticError, RusticResult, | ||
backend::{FileType, ReadBackend, WriteBackend}, | ||
id::Id, | ||
}; | ||
|
||
/// A backend which warms up files by simply accessing them. | ||
#[derive(Clone, Debug)] | ||
pub struct LockBackend { | ||
/// The backend to use. | ||
be: Arc<dyn WriteBackend>, | ||
/// The command to be called to lock files in the backend | ||
command: CommandInput, | ||
} | ||
|
||
impl LockBackend { | ||
/// Creates a new `WarmUpAccessBackend`. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `be` - The backend to use. | ||
pub fn new_lock(be: Arc<dyn WriteBackend>, command: CommandInput) -> Arc<dyn WriteBackend> { | ||
Arc::new(Self { be, command }) | ||
} | ||
} | ||
|
||
impl ReadBackend for LockBackend { | ||
fn location(&self) -> String { | ||
self.be.location() | ||
} | ||
|
||
fn list_with_size(&self, tpe: FileType) -> RusticResult<Vec<(Id, u32)>> { | ||
self.be.list_with_size(tpe) | ||
} | ||
|
||
fn read_full(&self, tpe: FileType, id: &Id) -> RusticResult<Bytes> { | ||
self.be.read_full(tpe, id) | ||
} | ||
|
||
fn read_partial( | ||
&self, | ||
tpe: FileType, | ||
id: &Id, | ||
cacheable: bool, | ||
offset: u32, | ||
length: u32, | ||
) -> RusticResult<Bytes> { | ||
self.be.read_partial(tpe, id, cacheable, offset, length) | ||
} | ||
|
||
fn list(&self, tpe: FileType) -> RusticResult<Vec<Id>> { | ||
self.be.list(tpe) | ||
} | ||
|
||
fn needs_warm_up(&self) -> bool { | ||
self.be.needs_warm_up() | ||
} | ||
|
||
fn warm_up(&self, tpe: FileType, id: &Id) -> RusticResult<()> { | ||
self.be.warm_up(tpe, id) | ||
} | ||
} | ||
|
||
fn path_to_id_from_file_type(tpe: FileType, id: &Id) -> String { | ||
let hex_id = id.to_hex(); | ||
match tpe { | ||
FileType::Config => "config".into(), | ||
FileType::Pack => format!("data/{}/{}", &hex_id[0..2], &*hex_id), | ||
_ => format!("{}/{}", tpe.dirname(), &*hex_id), | ||
} | ||
} | ||
|
||
impl WriteBackend for LockBackend { | ||
fn create(&self) -> RusticResult<()> { | ||
self.be.create() | ||
} | ||
|
||
fn write_bytes(&self, tpe: FileType, id: &Id, cacheable: bool, buf: Bytes) -> RusticResult<()> { | ||
self.be.write_bytes(tpe, id, cacheable, buf) | ||
} | ||
|
||
fn remove(&self, tpe: FileType, id: &Id, cacheable: bool) -> RusticResult<()> { | ||
self.be.remove(tpe, id, cacheable) | ||
} | ||
|
||
fn can_lock(&self) -> bool { | ||
true | ||
} | ||
|
||
fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> { | ||
if !self.can_lock() { | ||
return Err(RusticError::new( | ||
ErrorKind::Backend, | ||
"No locking configured on backend.", | ||
)); | ||
} | ||
|
||
let until = until.map_or_else(String::new, |u| u.to_rfc3339()); | ||
|
||
let path = path_to_id_from_file_type(tpe, id); | ||
|
||
let args = self.command.args().iter().map(|c| { | ||
c.replace("%id", &id.to_hex()) | ||
.replace("%type", tpe.dirname()) | ||
.replace("%path", &path) | ||
.replace("%until", &until) | ||
}); | ||
|
||
debug!("calling {:?}...", self.command); | ||
|
||
let status = Command::new(self.command.command()) | ||
.args(args) | ||
.status() | ||
.map_err(|err| { | ||
RusticError::with_source( | ||
ErrorKind::Internal, | ||
"error calling lock command for {tpe}, id: {id}.", | ||
err, | ||
) | ||
.attach_context("tpe", tpe.to_string()) | ||
.attach_context("id", id.to_string()) | ||
})?; | ||
|
||
if !status.success() { | ||
return Err(RusticError::new( | ||
ErrorKind::Backend, | ||
"lock command was not successful for {tpe}, id: {id}. {status}", | ||
) | ||
.attach_context("tpe", tpe.to_string()) | ||
.attach_context("id", id.to_string()) | ||
.attach_context("status", status.to_string())); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.