Skip to content

Commit

Permalink
Add static reference to the lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
a-kenji committed Aug 23, 2024
1 parent 5bf73be commit c381806
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
23 changes: 12 additions & 11 deletions client/src/lock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

pub const LOCK_FILE_NAME: &str = "centerpiece.lock";
pub const XDG_RUNTIME_DIR_ENV: &str = "XDG_RUNTIME_DIR";
Expand All @@ -14,6 +15,11 @@ fn get_xdg_runtime_dir() -> Option<String> {
pub struct LockFile(PathBuf);

impl LockFile {
pub fn get_or_init() -> &'static Option<Self> {
static LOCK_FILE: OnceLock<Option<LockFile>> = OnceLock::new();
LOCK_FILE.get_or_init(Self::init)
}

fn init() -> Option<Self> {
Some(Self(Self::get_lock_file_path()?))
}
Expand All @@ -31,24 +37,25 @@ impl LockFile {
if self.path().is_file() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"file not found",
"File found",
));
} else {
let _ = File::create(self.path())?;
};
Ok(())
}

pub fn unlock(&self) -> std::io::Result<()> {
std::fs::remove_file(&self.path())?;
pub fn unlock() -> std::io::Result<()> {
if let Some(lock_file) = Self::get_or_init() {
std::fs::remove_file(&lock_file.path())?;
}
Ok(())
}

/// Attempts to hold an exclusive lock in the runtime dir.
/// If we can't find the XDG_RUNTIME_DIR, we don't hold a lock.
/// If the lock is not successful, then exit `centerpiece`.
pub fn run_exclusive() {
if let Some(lock_file) = Self::init() {
if let Some(lock_file) = Self::get_or_init() {
if lock_file.try_lock().is_err() {
eprintln!(
"Could not hold an exclusive lock in {lock_file:?} stopping centerpiece."
Expand All @@ -58,9 +65,3 @@ impl LockFile {
}
}
}

impl Drop for LockFile {
fn drop(&mut self) {
let _ = self.unlock();
}
}
8 changes: 6 additions & 2 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Application for Centerpiece {
type Flags = crate::cli::CliArgs;

fn new(flags: crate::cli::CliArgs) -> (Self, iced::Command<Message>) {
let _lock = lock::LockFile::run_exclusive();
lock::LockFile::run_exclusive();
let settings = crate::settings::Settings::try_from(flags).unwrap_or_else(|_| {
eprintln!("There is an issue with the settings, please check the configuration file.");
std::process::exit(0);
Expand Down Expand Up @@ -107,6 +107,7 @@ impl Application for Centerpiece {
}
iced::keyboard::Event::KeyReleased { key, .. } => {
if key == iced::keyboard::Key::Named(iced::keyboard::key::Named::Escape) {
let _ = lock::LockFile::unlock();
return iced::window::close(iced::window::Id::MAIN);
}
iced::Command::none()
Expand All @@ -128,7 +129,10 @@ impl Application for Centerpiece {

Message::UpdateEntries(plugin_id, entries) => self.update_entries(plugin_id, entries),

Message::Exit => iced::window::close(iced::window::Id::MAIN),
Message::Exit => {
let _ = lock::LockFile::unlock();
iced::window::close(iced::window::Id::MAIN)
}
}
}

Expand Down

0 comments on commit c381806

Please sign in to comment.