Simplify create_if_not_exist

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

View file

@ -4,14 +4,12 @@ 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);
options
.clone()
.create_new(true)
fs::File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)
.or_else(|_| options.open(path))
.and_then(FileLock::new_exclusive)
.map(|file_lock| file_lock.set_file_path(path))
}