Update create_if_not_exist to return FileLock on successs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
This commit is contained in:
Jiahao XU 2025-02-22 22:44:17 +11:00 committed by GitHub
parent 644c70819f
commit 90e824d57c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<fs::File> {
use fs_lock::FileLock;
/// Return exclusively locked file that is readable and writable.
pub(crate) fn create_if_not_exist(path: &Path) -> io::Result<FileLock> {
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<fs::File> {
.create_new(true)
.open(path)
.or_else(|_| options.open(path))
.and_then(FileLock::new_exclusive)
.map(|file_lock| file_lock.set_file_path(path))
}