From c12bbf6241ee86d2b1420097998dac8210b3c2e7 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 12 Jun 2024 15:28:54 +0200 Subject: [PATCH] product-config: add open_secret_lockfile We need this for things like shadow.json.lock. Signed-off-by: Wolfgang Bumiller --- .../src/filesystem_helpers.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/proxmox-product-config/src/filesystem_helpers.rs b/proxmox-product-config/src/filesystem_helpers.rs index 4a7fabd4..1a12304a 100644 --- a/proxmox-product-config/src/filesystem_helpers.rs +++ b/proxmox-product-config/src/filesystem_helpers.rs @@ -142,3 +142,22 @@ pub fn open_api_lockfile>( let file = proxmox_sys::fs::open_file_locked(&path, timeout, exclusive, options)?; Ok(ApiLockGuard(Some(file))) } +/// +/// Open or create a lock file owned by root and lock it. +/// +/// File mode is `0600`. +/// Default timeout is 10 seconds. +/// +/// The lock is released as soon as you drop the returned lock guard. +/// +/// Note: This method needs to be called by user `root`. +pub fn open_secret_lockfile>( + path: P, + timeout: Option, + exclusive: bool, +) -> Result { + let options = secret_create_options(); + let timeout = timeout.unwrap_or(std::time::Duration::new(10, 0)); + let file = proxmox_sys::fs::open_file_locked(&path, timeout, exclusive, options)?; + Ok(ApiLockGuard(Some(file))) +}