From 6ef50f7dc209f2243105464ee13504810750df56 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 6 Feb 2026 00:07:14 -0600 Subject: [PATCH] fix offline cache_dir not updating to user-scoped path OfflineCacheState.cache_dir was set during new_empty() to the flat path (~/.cache/qbz/audio/) and never updated when init_at() opened the DB at the user-scoped path. This caused track files and artwork to be read/written at the wrong location. Wrapped cache_dir in Arc> and update it during init_at(). --- src-tauri/src/offline_cache/commands.rs | 19 ++++++++++--------- src-tauri/src/offline_cache/mod.rs | 21 ++++++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/offline_cache/commands.rs b/src-tauri/src/offline_cache/commands.rs index 1c366cdf9..58c0aa7d8 100644 --- a/src-tauri/src/offline_cache/commands.rs +++ b/src-tauri/src/offline_cache/commands.rs @@ -364,7 +364,7 @@ pub async fn remove_cached_track( // Clean up empty album folder (and artist folder if also empty) if let Some(album_dir) = path.parent() { - cleanup_empty_folder(album_dir, &cache_state.cache_dir); + cleanup_empty_folder(album_dir, &cache_state.cache_dir.read().unwrap()); } } } @@ -444,7 +444,8 @@ pub async fn purge_all_cached_files( } // Also clear the tracks directory (legacy unorganized files) - let tracks_dir = cache_state.cache_dir.join("tracks"); + let cache_dir = cache_state.cache_dir.read().unwrap().clone(); + let tracks_dir = cache_dir.join("tracks"); if tracks_dir.exists() { if let Ok(entries) = std::fs::read_dir(&tracks_dir) { for entry in entries.flatten() { @@ -455,7 +456,7 @@ pub async fn purge_all_cached_files( // Clear organized artist/album folders // Look for any subdirectories in cache_dir that are not "tracks" or system files - if let Ok(entries) = std::fs::read_dir(&cache_state.cache_dir) { + if let Ok(entries) = std::fs::read_dir(&cache_dir) { for entry in entries.flatten() { let path = entry.path(); if path.is_dir() { @@ -512,7 +513,7 @@ pub async fn open_offline_cache_folder( ) -> Result<(), String> { log::info!("Command: open_offline_cache_folder"); - let path = cache_state.cache_dir.clone(); + let path = cache_state.cache_dir.read().unwrap().clone(); // Ensure directory exists std::fs::create_dir_all(&path) @@ -662,7 +663,7 @@ async fn evict_if_needed( // Clean up empty album folder (and artist folder if also empty) if let Some(album_dir) = path.parent() { - cleanup_empty_folder(album_dir, &cache_state.cache_dir); + cleanup_empty_folder(album_dir, &cache_state.cache_dir.read().unwrap()); } } } @@ -678,7 +679,7 @@ pub async fn check_offline_root_mounted( ) -> Result { log::info!("Command: check_offline_root_mounted"); - let root_path = cache_state.cache_dir.to_string_lossy().to_string(); + let root_path = cache_state.cache_dir.read().unwrap().to_string_lossy().to_string(); super::path_validator::is_offline_root_available(&root_path) } @@ -695,7 +696,7 @@ pub async fn move_offline_cache_to_path( ) -> Result { log::info!("Command: move_offline_cache_to_path: {}", new_path); - let old_path = cache_state.cache_dir.to_string_lossy().to_string(); + let old_path = cache_state.cache_dir.read().unwrap().to_string_lossy().to_string(); super::path_validator::move_cached_files_to_new_path(&old_path, &new_path) } @@ -706,7 +707,7 @@ pub async fn detect_legacy_cached_files( ) -> Result { log::info!("Command: detect_legacy_cached_files"); - let tracks_dir = cache_state.cache_dir.join("tracks"); + let tracks_dir = cache_state.cache_dir.read().unwrap().join("tracks"); match super::detect_legacy_cached_files(&tracks_dir) { Ok(track_ids) => { @@ -730,7 +731,7 @@ pub async fn start_legacy_migration( ) -> Result<(), String> { log::info!("Command: start_legacy_migration"); - let tracks_dir = cache_state.cache_dir.join("tracks"); + let tracks_dir = cache_state.cache_dir.read().unwrap().join("tracks"); let track_ids = super::detect_legacy_cached_files(&tracks_dir)?; if track_ids.is_empty() { diff --git a/src-tauri/src/offline_cache/mod.rs b/src-tauri/src/offline_cache/mod.rs index 8624088e7..be93d692a 100644 --- a/src-tauri/src/offline_cache/mod.rs +++ b/src-tauri/src/offline_cache/mod.rs @@ -14,7 +14,7 @@ pub mod metadata; pub mod migration; use std::path::PathBuf; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use tokio::sync::{Mutex, Semaphore}; use serde::{Deserialize, Serialize}; @@ -131,7 +131,7 @@ pub struct TrackCacheInfo { pub struct OfflineCacheState { pub db: Arc>>, pub fetcher: Arc, - pub cache_dir: PathBuf, + pub cache_dir: Arc>, /// Cache limit in bytes (None = unlimited) pub limit_bytes: Arc>>, pub cache_semaphore: Arc, @@ -162,7 +162,7 @@ impl OfflineCacheState { let state = Self { db: Arc::new(Mutex::new(Some(db))), fetcher: Arc::new(StreamFetcher::new()), - cache_dir: cache_dir.clone(), + cache_dir: Arc::new(RwLock::new(cache_dir.clone())), limit_bytes: Arc::new(Mutex::new(default_limit)), cache_semaphore: Arc::new(Semaphore::new(3)), }; @@ -180,7 +180,7 @@ impl OfflineCacheState { Self { db: Arc::new(Mutex::new(None)), fetcher: Arc::new(StreamFetcher::new()), - cache_dir, + cache_dir: Arc::new(RwLock::new(cache_dir)), limit_bytes: Arc::new(Mutex::new(Some(2 * 1024 * 1024 * 1024u64))), cache_semaphore: Arc::new(Semaphore::new(3)), } @@ -198,6 +198,10 @@ impl OfflineCacheState { let new_db = OfflineCacheDb::new(&db_path)?; let mut guard = self.db.lock().await; *guard = Some(new_db); + // Update cache_dir to user-scoped path + if let Ok(mut dir_guard) = self.cache_dir.write() { + *dir_guard = cache_dir.clone(); + } log::info!("Offline cache initialized at: {:?}", cache_dir); Ok(()) } @@ -209,16 +213,19 @@ impl OfflineCacheState { /// Get the path for a track's audio file pub fn track_file_path(&self, track_id: u64, format: &str) -> PathBuf { - self.cache_dir.join("tracks").join(format!("{}.{}", track_id, format)) + let dir = self.cache_dir.read().unwrap(); + dir.join("tracks").join(format!("{}.{}", track_id, format)) } /// Get the path for an album's artwork pub fn artwork_path(&self, album_id: &str) -> PathBuf { - self.cache_dir.join("artwork").join(format!("{}.jpg", album_id)) + let dir = self.cache_dir.read().unwrap(); + dir.join("artwork").join(format!("{}.jpg", album_id)) } /// Get the cache directory path pub fn get_cache_path(&self) -> String { - self.cache_dir.to_string_lossy().to_string() + let dir = self.cache_dir.read().unwrap(); + dir.to_string_lossy().to_string() } }