fix: write permissions for backup files

This commit is contained in:
Gabriele Musco 2023-07-11 20:40:56 +02:00
commit e3b74fd2ce
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE
3 changed files with 45 additions and 27 deletions

View file

@ -1,14 +1,14 @@
use crate::{ use crate::{
file_utils::{ file_utils::{
deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, get_xdg_data_dir, copy_file, deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir,
set_file_radonly, get_xdg_data_dir, set_file_readonly,
}, },
paths::SYSTEM_PREFIX, paths::SYSTEM_PREFIX,
profile::{Profile, XRServiceType}, profile::{Profile, XRServiceType},
}; };
use expect_dialog::ExpectDialog; use expect_dialog::ExpectDialog;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{fs::copy, path::Path}; use std::path::Path;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActiveRuntimeInnerRuntime { pub struct ActiveRuntimeInnerRuntime {
@ -57,11 +57,10 @@ fn backup_steam_active_runtime() {
let ar = get_current_active_runtime(); let ar = get_current_active_runtime();
if ar.is_some() { if ar.is_some() {
if is_steam(&ar.unwrap()) { if is_steam(&ar.unwrap()) {
copy( copy_file(
get_active_runtime_json_path(), &get_active_runtime_json_path(),
get_backup_steam_active_runtime_path(), &get_backup_steam_active_runtime_path(),
) );
.expect_dialog("Failed to back up active_runtime.json");
} }
} }
} }
@ -75,11 +74,9 @@ pub fn get_current_active_runtime() -> Option<ActiveRuntime> {
} }
fn dump_active_runtime_to_path(active_runtime: &ActiveRuntime, path_s: &String) { fn dump_active_runtime_to_path(active_runtime: &ActiveRuntime, path_s: &String) {
set_file_radonly(path_s, false);
let writer = get_writer(path_s); let writer = get_writer(path_s);
serde_json::to_writer_pretty(writer, active_runtime) serde_json::to_writer_pretty(writer, active_runtime)
.expect_dialog("Unable to save active runtime"); .expect_dialog("Unable to save active runtime");
set_file_radonly(path_s, true);
} }
pub fn dump_current_active_runtime(active_runtime: &ActiveRuntime) { 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() { 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()) 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) { 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(); backup_steam_active_runtime();
let pfx = profile.clone().prefix; let pfx = profile.clone().prefix;
let mut ar = build_profile_active_runtime(profile); let mut ar = build_profile_active_runtime(profile);
// hack: relativize libopenxr_monado.so path for system installs // hack: relativize libopenxr_monado.so path for system installs
if pfx == SYSTEM_PREFIX { if pfx == SYSTEM_PREFIX {
let path_s = get_active_runtime_json_path(); let path = Path::new(&dest);
let path = Path::new(&path_s);
let mut rel_chain = path let mut rel_chain = path
.components() .components()
.map(|_| String::from("..")) .map(|_| String::from(".."))
@ -146,6 +145,7 @@ pub fn set_current_active_runtime_to_profile(profile: &Profile) {
); );
} }
dump_current_active_runtime(&ar); dump_current_active_runtime(&ar);
set_file_readonly(&dest, true);
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,13 +1,12 @@
use crate::{ use crate::{
file_utils::{ file_utils::{
deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, get_xdg_data_dir, copy_file, deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir,
set_file_radonly, get_xdg_data_dir, set_file_readonly,
}, },
profile::Profile, profile::Profile,
}; };
use expect_dialog::ExpectDialog; use expect_dialog::ExpectDialog;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs::copy;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpenVrPaths { pub struct OpenVrPaths {
@ -56,11 +55,10 @@ fn backup_steam_openvrpaths() {
let openvrpaths = get_current_openvrpaths(); let openvrpaths = get_current_openvrpaths();
if openvrpaths.is_some() { if openvrpaths.is_some() {
if is_steam(&openvrpaths.unwrap()) { if is_steam(&openvrpaths.unwrap()) {
copy( copy_file(
get_openvrpaths_vrpath_path(), &get_openvrpaths_vrpath_path(),
get_backup_steam_openvrpaths_path(), &get_backup_steam_openvrpaths_path(),
) );
.expect_dialog("Failed to back up openvrpaths.vrpath");
} }
} }
} }
@ -74,10 +72,8 @@ pub fn get_current_openvrpaths() -> Option<OpenVrPaths> {
} }
fn dump_openvrpaths_to_path(ovr_paths: &OpenVrPaths, path_s: &String) { fn dump_openvrpaths_to_path(ovr_paths: &OpenVrPaths, path_s: &String) {
set_file_radonly(path_s, false);
let writer = get_writer(path_s); let writer = get_writer(path_s);
serde_json::to_writer_pretty(writer, ovr_paths).expect_dialog("Unable to save openvrpaths"); 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) { pub fn dump_current_openvrpaths(ovr_paths: &OpenVrPaths) {
@ -104,6 +100,7 @@ fn build_steam_openvrpaths() -> OpenVrPaths {
} }
pub fn set_current_openvrpaths_to_steam() { pub fn set_current_openvrpaths_to_steam() {
set_file_readonly(&get_openvrpaths_vrpath_path(), false);
dump_current_openvrpaths(&build_steam_openvrpaths()) 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) { pub fn set_current_openvrpaths_to_profile(profile: &Profile) {
let dest = get_openvrpaths_vrpath_path();
set_file_readonly(&dest, false);
backup_steam_openvrpaths(); backup_steam_openvrpaths();
dump_current_openvrpaths(&build_profile_openvrpaths(profile)) dump_current_openvrpaths(&build_profile_openvrpaths(profile));
set_file_readonly(&dest, true);
} }
#[cfg(test)] #[cfg(test)]

View file

@ -2,7 +2,7 @@ use crate::{constants::CMD_NAME, runner::Runner};
use expect_dialog::ExpectDialog; use expect_dialog::ExpectDialog;
use std::{ use std::{
env, 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}, io::{BufReader, BufWriter},
path::Path, path::Path,
}; };
@ -107,14 +107,17 @@ pub fn get_backup_dir() -> String {
let p = Path::new(&p_s); let p = Path::new(&p_s);
if !p.is_dir() { if !p.is_dir() {
if p.is_file() { 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() 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); let path = Path::new(&path_s);
if !path.is_file() { if !path.is_file() {
println!("WARN: trying to set readonly on a file that does not exist"); 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(_) => {} 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());
}