[pbs-devel] [PATCH proxmox 1/3] proxmox/tools/fs: add shared lock helper
Dominik Csapak
d.csapak at proxmox.com
Fri Sep 25 16:13:14 CEST 2020
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
proxmox/src/tools/fs.rs | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs
index b1a95b5..2cd1a1b 100644
--- a/proxmox/src/tools/fs.rs
+++ b/proxmox/src/tools/fs.rs
@@ -505,15 +505,26 @@ pub fn lock_file<F: AsRawFd>(
Ok(())
}
+/// Open or create a lock file (append mode). Then try to
+/// acquire a shared lock using `lock_file()`.
+pub fn open_file_locked_shared<P: AsRef<Path>>(path: P, timeout: Duration) -> Result<File, Error> {
+ open_file_locked_impl(path, timeout, false)
+}
+
/// Open or create a lock file (append mode). Then try to
/// acquire a lock using `lock_file()`.
pub fn open_file_locked<P: AsRef<Path>>(path: P, timeout: Duration) -> Result<File, Error> {
+ open_file_locked_impl(path, timeout, true)
+}
+
+fn open_file_locked_impl<P: AsRef<Path>>(path: P, timeout: Duration, exclusive: bool) -> Result<File, Error> {
+
let path = path.as_ref();
let mut file = match OpenOptions::new().create(true).append(true).open(path) {
Ok(file) => file,
Err(err) => bail!("Unable to open lock {:?} - {}", path, err),
};
- match lock_file(&mut file, true, Some(timeout)) {
+ match lock_file(&mut file, exclusive, Some(timeout)) {
Ok(_) => Ok(file),
Err(err) => bail!("Unable to acquire lock {:?} - {}", path, err),
}
--
2.20.1
More information about the pbs-devel
mailing list