[pbs-devel] [RFC PATCH proxmox-backup 1/2] pbs-config: use trait object for the backup lock guard

Dominik Csapak d.csapak at proxmox.com
Fri Sep 3 09:17:51 CEST 2021


instead of a fixed type. The old implementation is now
BackupLockGuardImpl and implements the trait.

At the same time, introduce a type alias with the same name as the
previous struct, so that the users of it do not have to change anything.

This makes it possible for us to have a different lock implementation
for e.g. tests (where we do not actually want to lock)

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 pbs-config/src/lib.rs | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/pbs-config/src/lib.rs b/pbs-config/src/lib.rs
index 9d8c730d..604fe9d7 100644
--- a/pbs-config/src/lib.rs
+++ b/pbs-config/src/lib.rs
@@ -16,7 +16,14 @@ pub fn backup_group() -> Result<nix::unistd::Group, Error> {
     pbs_tools::sys::query_group(BACKUP_GROUP_NAME)?
         .ok_or_else(|| format_err!("Unable to lookup '{}' group.", BACKUP_GROUP_NAME))
 }
-pub struct BackupLockGuard(std::fs::File);
+
+pub trait BackupLockGuardTrait: Send + Sync + Unpin + std::panic::UnwindSafe + std::panic::RefUnwindSafe { }
+
+struct BackupLockGuardImpl(std::fs::File);
+
+impl BackupLockGuardTrait for BackupLockGuardImpl {}
+
+pub type BackupLockGuard = Box<dyn BackupLockGuardTrait>;
 
 /// Open or create a lock file owned by user "backup" and lock it.
 ///
@@ -39,7 +46,7 @@ pub fn open_backup_lockfile<P: AsRef<std::path::Path>>(
     let timeout = timeout.unwrap_or(std::time::Duration::new(10, 0));
 
     let file = proxmox::tools::fs::open_file_locked(&path, timeout, exclusive, options)?;
-    Ok(BackupLockGuard(file))
+    Ok(Box::new(BackupLockGuardImpl(file)))
 }
 
 /// Atomically write data to file owned by "root:backup" with permission "0640"
-- 
2.30.2






More information about the pbs-devel mailing list