Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

The data folder is worked out once per run, not on every tick (#565) (#582)

Ahmed Nefzaouif3c11394f3d1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 88fdfbc4..588d8830 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- A freeze when the disk was slow: MDM looked on the disk for a portable data folder on every tick of the window; it now works the data folder out once ([#565](https://github.com/anefzaoui/misterdownloadmanager/issues/565)).
- macOS 15 and later: Chinese names and text find the PingFang font again, which moved into the system's font assets, with Hiragino Sans GB and STHeiti as fallbacks ([#541](https://github.com/anefzaoui/misterdownloadmanager/issues/541)).
- The video button follows **Options → General → Customize MDM Download panels in browsers** in the pages already open: switched off, or its site listed, it goes within a few seconds, without reloading the page (it stayed until a reload, and on YouTube, which never reloads, for good). A site listed as `example.com` now takes `www.example.com` and its other subdomains too, and a player embedded from another site follows the site in the address bar ([#532](https://github.com/anefzaoui/misterdownloadmanager/issues/532)).
- macOS: file names in Persian, Arabic, Urdu, Hebrew or Armenian show their letters in the download list and windows instead of boxes, whatever the interface language: these scripts now get the font for them that comes with macOS (Arial Unicode), the first time one is needed ([#533](https://github.com/anefzaoui/misterdownloadmanager/issues/533)).
diff --git a/ROADMAP.md b/ROADMAP.md
index 7d9e5ab9..0bfc0819 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -522,6 +522,8 @@ These ship in a later version, when they're ready.
### Bugs being looked into
+- 🔄 A freeze on a slow disk, the data folder looked up on every tick
+ ([#565](https://github.com/anefzaoui/misterdownloadmanager/issues/565)): fix in review
- 🔄 A progress window's title bar buttons not answering
([#540](https://github.com/anefzaoui/misterdownloadmanager/issues/540)): a window with a title of
256 characters or more got the system's title bar above MDM's own; that part is fixed, in review.
diff --git a/crates/ipc/src/lib.rs b/crates/ipc/src/lib.rs
index 202dd0a3..6124989e 100644
--- a/crates/ipc/src/lib.rs
+++ b/crates/ipc/src/lib.rs
@@ -221,23 +221,35 @@ pub fn set_data_dir(dir: PathBuf) {
///
/// `MDM_DATA_DIR` overrides it (a separate instance with its own list and channel: tests,
/// screenshots). Portable mode: a writable `data` folder next to the program holds it all.
+///
+/// Worked out once per run and kept (#565): the window asks for it on every tick (torrents'
+/// state, the list), and the portable check below reads and writes the disk, so a slow disk
+/// held the window up. A folder given on the command line is still taken first.
pub fn data_dir() -> Option<PathBuf> {
if let Some(dir) = DATA_DIR.get() {
return Some(dir.clone());
}
- if let Some(dir) = std::env::var_os("MDM_DATA_DIR") {
- return Some(PathBuf::from(dir));
- }
- if let Some(dir) = portable_dir() {
- return Some(dir);
- }
- let base = dirs::data_dir()?;
- let dir = base.join("misterdownloadmanager");
- let legacy = base.join("idmnext");
- if !dir.exists() && legacy.is_dir() {
- let _ = std::fs::rename(&legacy, &dir);
- }
- Some(dir)
+ static RESOLVED: std::sync::OnceLock<Option<PathBuf>> = std::sync::OnceLock::new();
+ once(&RESOLVED, || {
+ if let Some(dir) = std::env::var_os("MDM_DATA_DIR") {
+ return Some(PathBuf::from(dir));
+ }
+ if let Some(dir) = portable_dir() {
+ return Some(dir);
+ }
+ let base = dirs::data_dir()?;
+ let dir = base.join("misterdownloadmanager");
+ let legacy = base.join("idmnext");
+ if !dir.exists() && legacy.is_dir() {
+ let _ = std::fs::rename(&legacy, &dir);
+ }
+ Some(dir)
+ })
+}
+
+/// `cell`'s value, worked out by `make` the first time only.
+fn once<T: Clone>(cell: &std::sync::OnceLock<T>, make: impl FnOnce() -> T) -> T {
+ cell.get_or_init(make).clone()
}
/// Create the data folder `dir`, readable by its owner only on Linux and macOS (it holds the
@@ -262,7 +274,13 @@ pub fn private_write(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()
/// Portable mode: the `data` folder next to the program, when it exists and can be written
/// (else the profile folder is used). Nothing is written to the registry in this mode.
+/// Asked once per run and kept, like [`data_dir`] (#565).
pub fn portable_dir() -> Option<PathBuf> {
+ static PORTABLE: std::sync::OnceLock<Option<PathBuf>> = std::sync::OnceLock::new();
+ once(&PORTABLE, portable_dir_now)
+}
+
+fn portable_dir_now() -> Option<PathBuf> {
let dir = std::env::current_exe().ok()?.parent()?.join("data");
if !dir.is_dir() {
return None;
@@ -625,6 +643,23 @@ pub fn parse_args(args: &[String]) -> Option<Command> {
mod tests {
use super::*;
+ /// #565: the data folder is worked out once; later calls (every tick of the window) don't
+ /// look at the disk again.
+ #[test]
+ fn a_kept_value_is_worked_out_once() {
+ let cell = std::sync::OnceLock::new();
+ let calls = std::cell::Cell::new(0);
+ let make = || {
+ calls.set(calls.get() + 1);
+ Some(PathBuf::from("data"))
+ };
+ assert_eq!(once(&cell, make), Some(PathBuf::from("data")));
+ assert_eq!(once(&cell, make), Some(PathBuf::from("data")));
+ assert_eq!(once(&cell, make), Some(PathBuf::from("data")));
+ assert_eq!(calls.get(), 1);
+ assert_eq!(data_dir(), data_dir());
+ }
+
#[test]
fn bare_magnets_and_torrent_files() {
let c = parse_args(&["magnet:?xt=urn:btih:abc".into()]).unwrap();