diff --git a/src-tauri/src/store/mod.rs b/src-tauri/src/store/mod.rs index d0109b0..099effa 100644 --- a/src-tauri/src/store/mod.rs +++ b/src-tauri/src/store/mod.rs @@ -92,12 +92,13 @@ where /// Save the relevant Store as a file pub fn save(&self) -> Result<(), anyhow::Error> { - fs::create_dir_all(self.path.parent().unwrap())?; + let target_path = fs::canonicalize(&self.path).unwrap_or_else(|_| self.path.clone()); + fs::create_dir_all(target_path.parent().unwrap())?; let contents = serde_json::to_string_pretty(&T::into_value(&self.value)?)?; - let temp_path = self.path.with_extension("json.temp"); - let backup_path = self.path.with_extension("json.bak"); + let temp_path = target_path.with_extension("json.temp"); + let backup_path = target_path.with_extension("json.bak"); // Write to temporary file let mut temp_file = fs::OpenOptions::new().write(true).truncate(true).create(true).open(&temp_path)?; @@ -108,12 +109,12 @@ where drop(temp_file); // If main file exists, back it up - if self.path.exists() { - fs::rename(&self.path, &backup_path)?; + if target_path.exists() { + fs::rename(&target_path, &backup_path)?; } // Rename temp file to main file - fs::rename(&temp_path, &self.path)?; + fs::rename(&temp_path, &target_path)?; // Remove backup file if everything succeeded if backup_path.exists() { diff --git a/src-tauri/src/store/profiles.rs b/src-tauri/src/store/profiles.rs index 691f8e3..1d85703 100644 --- a/src-tauri/src/store/profiles.rs +++ b/src-tauri/src/store/profiles.rs @@ -243,7 +243,7 @@ pub fn get_device_profiles(device: &str) -> Result, anyhow::Error> { let entries = fs::read_dir(device_path)?; for entry in entries.flatten() { - if entry.metadata()?.is_file() { + if entry.path().is_file() { let mut id = entry.file_name().to_string_lossy().into_owned(); if id.ends_with(".json") { id.truncate(id.len() - 5); @@ -255,10 +255,10 @@ pub fn get_device_profiles(device: &str) -> Result, anyhow::Error> { continue; } profiles.push(id); - } else if entry.metadata()?.is_dir() { + } else if entry.path().is_dir() { let entries = fs::read_dir(entry.path())?; for subentry in entries.flatten() { - if subentry.metadata()?.is_file() { + if subentry.path().is_file() { let mut id = format!("{}/{}", entry.file_name().to_string_lossy(), subentry.file_name().to_string_lossy()); if id.ends_with(".json") { id.truncate(id.len() - 5);