|
| 1 | +use std::{process::Command, sync::Arc}; |
| 2 | + |
| 3 | +use bytes::Bytes; |
| 4 | +use chrono::{DateTime, Local}; |
| 5 | +use log::{debug, warn}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + CommandInput, ErrorKind, RusticError, RusticResult, |
| 9 | + backend::{FileType, ReadBackend, WriteBackend}, |
| 10 | + id::Id, |
| 11 | +}; |
| 12 | + |
| 13 | +/// A backend which warms up files by simply accessing them. |
| 14 | +#[derive(Clone, Debug)] |
| 15 | +pub struct LockBackend { |
| 16 | + /// The backend to use. |
| 17 | + be: Arc<dyn WriteBackend>, |
| 18 | + /// The command to be called to lock files in the backend |
| 19 | + command: CommandInput, |
| 20 | +} |
| 21 | + |
| 22 | +impl LockBackend { |
| 23 | + /// Creates a new `WarmUpAccessBackend`. |
| 24 | + /// |
| 25 | + /// # Arguments |
| 26 | + /// |
| 27 | + /// * `be` - The backend to use. |
| 28 | + pub fn new_lock(be: Arc<dyn WriteBackend>, command: CommandInput) -> Arc<dyn WriteBackend> { |
| 29 | + Arc::new(Self { be, command }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl ReadBackend for LockBackend { |
| 34 | + fn location(&self) -> String { |
| 35 | + self.be.location() |
| 36 | + } |
| 37 | + |
| 38 | + fn list_with_size(&self, tpe: FileType) -> RusticResult<Vec<(Id, u32)>> { |
| 39 | + self.be.list_with_size(tpe) |
| 40 | + } |
| 41 | + |
| 42 | + fn read_full(&self, tpe: FileType, id: &Id) -> RusticResult<Bytes> { |
| 43 | + self.be.read_full(tpe, id) |
| 44 | + } |
| 45 | + |
| 46 | + fn read_partial( |
| 47 | + &self, |
| 48 | + tpe: FileType, |
| 49 | + id: &Id, |
| 50 | + cacheable: bool, |
| 51 | + offset: u32, |
| 52 | + length: u32, |
| 53 | + ) -> RusticResult<Bytes> { |
| 54 | + self.be.read_partial(tpe, id, cacheable, offset, length) |
| 55 | + } |
| 56 | + |
| 57 | + fn list(&self, tpe: FileType) -> RusticResult<Vec<Id>> { |
| 58 | + self.be.list(tpe) |
| 59 | + } |
| 60 | + |
| 61 | + fn needs_warm_up(&self) -> bool { |
| 62 | + self.be.needs_warm_up() |
| 63 | + } |
| 64 | + |
| 65 | + fn warm_up(&self, tpe: FileType, id: &Id) -> RusticResult<()> { |
| 66 | + self.be.warm_up(tpe, id) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn path(tpe: FileType, id: &Id) -> String { |
| 71 | + let hex_id = id.to_hex(); |
| 72 | + match tpe { |
| 73 | + FileType::Config => "config".into(), |
| 74 | + FileType::Pack => format!("data/{}/{}", &hex_id[0..2], &*hex_id), |
| 75 | + _ => format!("{}/{}", tpe.dirname(), &*hex_id), |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl WriteBackend for LockBackend { |
| 80 | + fn create(&self) -> RusticResult<()> { |
| 81 | + self.be.create() |
| 82 | + } |
| 83 | + |
| 84 | + fn write_bytes(&self, tpe: FileType, id: &Id, cacheable: bool, buf: Bytes) -> RusticResult<()> { |
| 85 | + self.be.write_bytes(tpe, id, cacheable, buf) |
| 86 | + } |
| 87 | + |
| 88 | + fn remove(&self, tpe: FileType, id: &Id, cacheable: bool) -> RusticResult<()> { |
| 89 | + self.be.remove(tpe, id, cacheable) |
| 90 | + } |
| 91 | + |
| 92 | + fn can_lock(&self) -> bool { |
| 93 | + true |
| 94 | + } |
| 95 | + |
| 96 | + fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> RusticResult<()> { |
| 97 | + let until = until.map_or_else(String::new, |u| u.to_rfc3339()); |
| 98 | + let path = path(tpe, id); |
| 99 | + let args = self.command.args().iter().map(|c| { |
| 100 | + c.replace("%id", &id.to_hex()) |
| 101 | + .replace("%type", tpe.dirname()) |
| 102 | + .replace("%path", &path) |
| 103 | + .replace("%until", &until) |
| 104 | + }); |
| 105 | + debug!("calling {:?}...", self.command); |
| 106 | + let status = Command::new(self.command.command()) |
| 107 | + .args(args) |
| 108 | + .status() |
| 109 | + .map_err(|err| { |
| 110 | + RusticError::with_source( |
| 111 | + ErrorKind::Internal, |
| 112 | + "error calling lock command for {tpe}, id: {id}.", |
| 113 | + err, |
| 114 | + ) |
| 115 | + .attach_context("tpe", tpe.to_string()) |
| 116 | + .attach_context("id", id.to_string()) |
| 117 | + })?; |
| 118 | + if !status.success() { |
| 119 | + warn!("lock command was not successful for {tpe:?}, id: {id}. {status}"); |
| 120 | + } |
| 121 | + Ok(()) |
| 122 | + } |
| 123 | +} |
0 commit comments