From 90e824d57c5b5905d09ad43a9b0a7ae8c03e0523 Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Sat, 22 Feb 2025 22:44:17 +1100 Subject: [PATCH] Update create_if_not_exist to return FileLock on successs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> --- crates/binstalk-manifests/src/helpers.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/binstalk-manifests/src/helpers.rs b/crates/binstalk-manifests/src/helpers.rs index 029564c9..4b2f3f8e 100644 --- a/crates/binstalk-manifests/src/helpers.rs +++ b/crates/binstalk-manifests/src/helpers.rs @@ -1,7 +1,9 @@ use std::{fs, io, path::Path}; -/// Returned file is readable and writable. -pub(crate) fn create_if_not_exist(path: &Path) -> io::Result { +use fs_lock::FileLock; + +/// Return exclusively locked file that is readable and writable. +pub(crate) fn create_if_not_exist(path: &Path) -> io::Result { let mut options = fs::File::options(); options.read(true).write(true); @@ -10,4 +12,6 @@ pub(crate) fn create_if_not_exist(path: &Path) -> io::Result { .create_new(true) .open(path) .or_else(|_| options.open(path)) + .and_then(FileLock::new_exclusive) + .map(|file_lock| file_lock.set_file_path(path)) }