Skip to content

Commit

Permalink
Allow longer read lock
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff committed Oct 17, 2024
1 parent ad345d9 commit 4059b52
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/bin/julialauncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn run_app() -> Result<i32> {
do_initial_setup(&paths.juliaupconfig)
.with_context(|| "The Julia launcher failed to run the initial setup steps.")?;

let config_file = load_config_db(&paths)
let config_file = load_config_db(&paths, None)
.with_context(|| "The Julia launcher failed to load a configuration file.")?;

let versiondb_data = load_versions_db(&paths)
Expand Down
2 changes: 1 addition & 1 deletion src/command_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn run_command_api(command: &str, paths: &GlobalPaths) -> Result<()> {
other_versions: Vec::new(),
};

let config_file = load_config_db(paths).with_context(|| {
let config_file = load_config_db(paths, None).with_context(|| {
"Failed to load configuration file while running the getconfig1 API command."
})?;

Expand Down
2 changes: 1 addition & 1 deletion src/command_config_versionsdbupdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn run_command_config_versionsdbupdate(
}
}
None => {
let config_file = load_config_db(paths)
let config_file = load_config_db(paths, None)
.with_context(|| "`config` command failed to load configuration data.")?;

if !quiet {
Expand Down
2 changes: 1 addition & 1 deletion src/command_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn run_command_info(paths: &GlobalPaths) -> Result<()> {
};

#[cfg(not(feature = "selfupdate"))]
let _config_file = load_config_db(paths).with_context(|| {
let _config_file = load_config_db(paths, None).with_context(|| {
"`run_command_update_version_db` command failed to load configuration db."
})?;

Expand Down
2 changes: 1 addition & 1 deletion src/command_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct OverrideRow {
}

pub fn run_command_override_status(paths: &GlobalPaths) -> Result<()> {
let config_file = load_config_db(paths)
let config_file = load_config_db(paths, None)
.with_context(|| "`override status` command failed to load configuration file.")?;

let rows_in_table: Vec<_> = config_file
Expand Down
2 changes: 1 addition & 1 deletion src/command_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct ChannelRow {
}

pub fn run_command_status(paths: &GlobalPaths) -> Result<()> {
let config_file = load_config_db(paths)
let config_file = load_config_db(paths, None)
.with_context(|| "`status` command failed to load configuration file.")?;

let versiondb_data =
Expand Down
39 changes: 27 additions & 12 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ fn is_default_versionsdb_update_interval(i: &i64) -> bool {
*i == default_versionsdb_update_interval()
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct JuliaupConfigVersion {
#[serde(rename = "Path")]
pub path: String,
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
#[serde(untagged)]
pub enum JuliaupConfigChannel {
DirectDownloadChannel {
Expand All @@ -53,7 +53,7 @@ pub enum JuliaupConfigChannel {
},
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct JuliaupConfigSettings {
#[serde(
rename = "CreateChannelSymlinks",
Expand All @@ -78,15 +78,15 @@ impl Default for JuliaupConfigSettings {
}
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct JuliaupOverride {
#[serde(rename = "Path")]
pub path: String,
#[serde(rename = "Channel")]
pub channel: String,
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct JuliaupConfig {
#[serde(rename = "Default")]
pub default: Option<String>,
Expand Down Expand Up @@ -142,7 +142,7 @@ pub struct JuliaupReadonlyConfigFile {
pub self_data: JuliaupSelfConfig,
}

pub fn load_config_db(paths: &GlobalPaths) -> Result<JuliaupReadonlyConfigFile> {
pub fn get_read_lock(paths: &GlobalPaths) -> Result<FlockLock<File>> {
std::fs::create_dir_all(&paths.juliauphome)
.with_context(|| "Could not create juliaup home folder.")?;

Expand All @@ -156,17 +156,30 @@ pub fn load_config_db(paths: &GlobalPaths) -> Result<JuliaupReadonlyConfigFile>
Err(e) => return Err(anyhow!("Could not create lockfile: {}.", e)),
};

let file_lock = match SharedFlock::try_lock(&lock_file) {
let file_lock = match SharedFlock::try_lock(lock_file) {
Ok(lock) => lock,
Err(_e) => {
Err(e) => {
eprintln!(
"Juliaup configuration is locked by another process, waiting for it to unlock."
);

SharedFlock::wait_lock(&lock_file).unwrap()
SharedFlock::wait_lock(e.into()).unwrap()
}
};

return Ok(file_lock);
}

pub fn load_config_db(
paths: &GlobalPaths,
existing_lock: Option<&FlockLock<File>>,
) -> Result<JuliaupReadonlyConfigFile> {
let mut file_lock: Option<FlockLock<File>> = None;

if existing_lock.is_none() {
file_lock = Some(get_read_lock(paths)?);
}

let v = match std::fs::OpenOptions::new()
.read(true)
.open(&paths.juliaupconfig)
Expand Down Expand Up @@ -229,9 +242,11 @@ pub fn load_config_db(paths: &GlobalPaths) -> Result<JuliaupReadonlyConfigFile>
};
}

file_lock
.unlock()
.with_context(|| "Failed to unlock configuration file.")?;
if let Some(file_lock) = file_lock {
file_lock
.unlock()
.with_context(|| "Failed to unlock configuration file.")?;
}

Ok(JuliaupReadonlyConfigFile {
data: v,
Expand Down
Loading

0 comments on commit 4059b52

Please sign in to comment.