diff --git a/src/file_builders/active_runtime_json.rs b/src/file_builders/active_runtime_json.rs index fbae602..70efab2 100644 --- a/src/file_builders/active_runtime_json.rs +++ b/src/file_builders/active_runtime_json.rs @@ -1,14 +1,14 @@ use crate::{ file_utils::{ - deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, get_xdg_data_dir, - set_file_radonly, + copy_file, deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, + get_xdg_data_dir, set_file_readonly, }, paths::SYSTEM_PREFIX, profile::{Profile, XRServiceType}, }; use expect_dialog::ExpectDialog; use serde::{Deserialize, Serialize}; -use std::{fs::copy, path::Path}; +use std::path::Path; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ActiveRuntimeInnerRuntime { @@ -57,11 +57,10 @@ fn backup_steam_active_runtime() { let ar = get_current_active_runtime(); if ar.is_some() { if is_steam(&ar.unwrap()) { - copy( - get_active_runtime_json_path(), - get_backup_steam_active_runtime_path(), - ) - .expect_dialog("Failed to back up active_runtime.json"); + copy_file( + &get_active_runtime_json_path(), + &get_backup_steam_active_runtime_path(), + ); } } } @@ -75,11 +74,9 @@ pub fn get_current_active_runtime() -> Option { } fn dump_active_runtime_to_path(active_runtime: &ActiveRuntime, path_s: &String) { - set_file_radonly(path_s, false); let writer = get_writer(path_s); serde_json::to_writer_pretty(writer, active_runtime) .expect_dialog("Unable to save active runtime"); - set_file_radonly(path_s, true); } pub fn dump_current_active_runtime(active_runtime: &ActiveRuntime) { @@ -105,6 +102,7 @@ fn build_steam_active_runtime() -> ActiveRuntime { } pub fn set_current_active_runtime_to_steam() { + set_file_readonly(&get_active_runtime_json_path(), false); dump_current_active_runtime(&build_steam_active_runtime()) } @@ -127,13 +125,14 @@ pub fn build_profile_active_runtime(profile: &Profile) -> ActiveRuntime { } pub fn set_current_active_runtime_to_profile(profile: &Profile) { + let dest = get_active_runtime_json_path(); + set_file_readonly(&dest, false); backup_steam_active_runtime(); let pfx = profile.clone().prefix; let mut ar = build_profile_active_runtime(profile); // hack: relativize libopenxr_monado.so path for system installs if pfx == SYSTEM_PREFIX { - let path_s = get_active_runtime_json_path(); - let path = Path::new(&path_s); + let path = Path::new(&dest); let mut rel_chain = path .components() .map(|_| String::from("..")) @@ -146,6 +145,7 @@ pub fn set_current_active_runtime_to_profile(profile: &Profile) { ); } dump_current_active_runtime(&ar); + set_file_readonly(&dest, true); } #[cfg(test)] diff --git a/src/file_builders/openvrpaths_vrpath.rs b/src/file_builders/openvrpaths_vrpath.rs index 725d8be..bbdee19 100644 --- a/src/file_builders/openvrpaths_vrpath.rs +++ b/src/file_builders/openvrpaths_vrpath.rs @@ -1,13 +1,12 @@ use crate::{ file_utils::{ - deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, get_xdg_data_dir, - set_file_radonly, + copy_file, deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, + get_xdg_data_dir, set_file_readonly, }, profile::Profile, }; use expect_dialog::ExpectDialog; use serde::{Deserialize, Serialize}; -use std::fs::copy; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct OpenVrPaths { @@ -56,11 +55,10 @@ fn backup_steam_openvrpaths() { let openvrpaths = get_current_openvrpaths(); if openvrpaths.is_some() { if is_steam(&openvrpaths.unwrap()) { - copy( - get_openvrpaths_vrpath_path(), - get_backup_steam_openvrpaths_path(), - ) - .expect_dialog("Failed to back up openvrpaths.vrpath"); + copy_file( + &get_openvrpaths_vrpath_path(), + &get_backup_steam_openvrpaths_path(), + ); } } } @@ -74,10 +72,8 @@ pub fn get_current_openvrpaths() -> Option { } fn dump_openvrpaths_to_path(ovr_paths: &OpenVrPaths, path_s: &String) { - set_file_radonly(path_s, false); let writer = get_writer(path_s); serde_json::to_writer_pretty(writer, ovr_paths).expect_dialog("Unable to save openvrpaths"); - set_file_radonly(path_s, true); } pub fn dump_current_openvrpaths(ovr_paths: &OpenVrPaths) { @@ -104,6 +100,7 @@ fn build_steam_openvrpaths() -> OpenVrPaths { } pub fn set_current_openvrpaths_to_steam() { + set_file_readonly(&get_openvrpaths_vrpath_path(), false); dump_current_openvrpaths(&build_steam_openvrpaths()) } @@ -123,8 +120,11 @@ pub fn build_profile_openvrpaths(profile: &Profile) -> OpenVrPaths { } pub fn set_current_openvrpaths_to_profile(profile: &Profile) { + let dest = get_openvrpaths_vrpath_path(); + set_file_readonly(&dest, false); backup_steam_openvrpaths(); - dump_current_openvrpaths(&build_profile_openvrpaths(profile)) + dump_current_openvrpaths(&build_profile_openvrpaths(profile)); + set_file_readonly(&dest, true); } #[cfg(test)] diff --git a/src/file_utils.rs b/src/file_utils.rs index 2204279..3735d13 100644 --- a/src/file_utils.rs +++ b/src/file_utils.rs @@ -2,7 +2,7 @@ use crate::{constants::CMD_NAME, runner::Runner}; use expect_dialog::ExpectDialog; use std::{ env, - fs::{self, create_dir_all, remove_dir_all, File, OpenOptions}, + fs::{self, copy, create_dir_all, remove_dir_all, File, OpenOptions}, io::{BufReader, BufWriter}, path::Path, }; @@ -107,14 +107,17 @@ pub fn get_backup_dir() -> String { let p = Path::new(&p_s); if !p.is_dir() { if p.is_file() { - panic!("{} is a file but it should be a directory!", p.to_str().unwrap()); + panic!( + "{} is a file but it should be a directory!", + p.to_str().unwrap() + ); } - create_dir_all(p); + create_dir_all(p).expect_dialog("Failed to create backups dir"); } p.to_str().unwrap().to_string() } -pub fn set_file_radonly(path_s: &String, readonly: bool) { +pub fn set_file_readonly(path_s: &String, readonly: bool) { let path = Path::new(&path_s); if !path.is_file() { println!("WARN: trying to set readonly on a file that does not exist"); @@ -160,3 +163,18 @@ pub fn rm_rf(path_s: &String) { Ok(_) => {} } } + +pub fn copy_file(source_s: &String, dest_s: &String) { + let source = Path::new(source_s); + let dest = Path::new(dest_s); + let parent = dest.parent(); + if parent.is_some() { + if !parent.unwrap().is_dir() { + create_dir_all(parent.unwrap()).expect_dialog( + format!("Failed to create dir {}", parent.unwrap().to_str().unwrap()).as_str(), + ); + } + } + set_file_readonly(dest_s, false); + copy(source, dest).expect_dialog(format!("Failed to copy {} to {}", source_s, dest_s).as_str()); +}