From 8d55c9962e24922563ed4fbf8806b103c257515b Mon Sep 17 00:00:00 2001 From: Austin Morton Date: Thu, 14 Dec 2023 15:12:23 +0000 Subject: [PATCH] Lock ozy config directory when updating --- rozy/Cargo.lock | 2 +- rozy/Cargo.toml | 2 +- rozy/src/files.rs | 8 ++++++++ rozy/src/main.rs | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/rozy/Cargo.lock b/rozy/Cargo.lock index 85db8a5..7cea9fb 100644 --- a/rozy/Cargo.lock +++ b/rozy/Cargo.lock @@ -911,7 +911,7 @@ checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" [[package]] name = "ozy" -version = "0.1.8" +version = "0.1.9" dependencies = [ "anyhow", "bzip2", diff --git a/rozy/Cargo.toml b/rozy/Cargo.toml index 21e6e98..9204c9c 100644 --- a/rozy/Cargo.toml +++ b/rozy/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ozy" -version = "0.1.8" +version = "0.1.9" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/rozy/src/files.rs b/rozy/src/files.rs index 100c699..f40b4f8 100644 --- a/rozy/src/files.rs +++ b/rozy/src/files.rs @@ -1,6 +1,7 @@ use std::env; use anyhow::{Context, Error, Result}; +use file_lock::{FileLock, FileOptions}; fn get_home_dir() -> Result { let home_dir = std::env::var("HOME").context("While checking $HOME")?; @@ -78,3 +79,10 @@ pub fn softlink(from_command: &str, to_command: &str) -> Result { })?; Ok(was_there) } + +pub fn lock_ozy_dir() -> Result { + let lock_for_writing = FileOptions::new().create(true).write(true).read(true); + let lock_path = get_ozy_dir()?.join("ozy.lock"); + let lock = FileLock::lock(lock_path, true, lock_for_writing).context("Locking ozy config")?; + Ok(lock) +} diff --git a/rozy/src/main.rs b/rozy/src/main.rs index 858e3e6..4db449e 100644 --- a/rozy/src/main.rs +++ b/rozy/src/main.rs @@ -233,6 +233,7 @@ fn get_apps(config: &serde_yaml::Mapping) -> Result> { fn update(path_to_ozy: &std::path::PathBuf, url: &Option) -> Result<()> { files::ensure_ozy_dirs()?; + let _lock = files::lock_ozy_dir()?; let old_base_ozy_conf = files::get_ozy_dir()?.join("ozy.yaml"); let new_base_ozy_conf = files::get_ozy_dir()?.join("ozy.yaml.tmp");