Skip to content

Commit

Permalink
Lock ozy config directory when updating
Browse files Browse the repository at this point in the history
  • Loading branch information
apmorton committed Dec 14, 2023
1 parent 3827eda commit 8d55c99
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rozy/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rozy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions rozy/src/files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;

use anyhow::{Context, Error, Result};
use file_lock::{FileLock, FileOptions};

fn get_home_dir() -> Result<std::path::PathBuf, Error> {
let home_dir = std::env::var("HOME").context("While checking $HOME")?;
Expand Down Expand Up @@ -78,3 +79,10 @@ pub fn softlink(from_command: &str, to_command: &str) -> Result<bool> {
})?;
Ok(was_there)
}

pub fn lock_ozy_dir() -> Result<FileLock> {
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)
}
1 change: 1 addition & 0 deletions rozy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ fn get_apps(config: &serde_yaml::Mapping) -> Result<Vec<app::App>> {

fn update(path_to_ozy: &std::path::PathBuf, url: &Option<String>) -> 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");

Expand Down

0 comments on commit 8d55c99

Please sign in to comment.