diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ad3f6a..ff77ac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Versions match `FIP_VERSION` in `src/version.h`. ## [Unreleased] +## [1.8.5] - 2026-11-23 + +### Changed + +- Find Dead/Empty Directories now runs off the UI thread: results stream into the foobar2000 console as they are found, and the completion popup includes the report file path. Repeated clicks while a scan is running are ignored. + ## [1.8.4] - 2026-11-23 ### Fixed diff --git a/src/main_menu.cpp b/src/main_menu.cpp index 7fcebf5..08e1c41 100644 --- a/src/main_menu.cpp +++ b/src/main_menu.cpp @@ -4,6 +4,10 @@ #include "export.h" #include "logic.h" +#include +#include +#include + // Command GUIDs. static const GUID guid_main_cmd = {0xbc83531a, 0x186a, 0x4de2, {0x97, 0x31, 0x94, 0x19, 0x80, 0xdb, 0xad, 0x1d}}; static const GUID guid_cmd_dupes = {0x79ae2e61, 0x1513, 0x4aca, {0xb8, 0x36, 0x31, 0x64, 0xca, 0x9c, 0x65, 0xaa}}; @@ -23,6 +27,9 @@ static const GUID guid_cmd_invalid_tags = { static const GUID guid_main_group = {0x23adac65, 0xf18a, 0x4e1c, {0xa2, 0xef, 0xc0, 0x3a, 0x75, 0x0d, 0x96, 0x88}}; +// Guards against overlapping dead-directory scans from repeated menu clicks. +static std::atomic g_deadDirScanRunning{false}; + static mainmenu_group_popup_factory g_main_group(guid_main_group, mainmenu_groups::library, mainmenu_commands::sort_priority_dontcare, "Find in Playlists"); @@ -186,12 +193,37 @@ class main_find_playing : public mainmenu_commands { } private: - static void report(const char *title, const pfc::string_base & msg) { + static std::string report_path(const char *title, const pfc::string_base & msg) { const std::string path(write_report_file(title, msg)); FB2K_console_formatter() << title << " (report: " << path.c_str() << ")"; + return path; } + static void report(const char *title, const pfc::string_base & msg) { report_path(title, msg); } static void confirm(const char *title, const pfc::string_base & summary) { popup_message::g_show(summary, title); } + // Console writes are marshalled to the main thread; the scan itself runs off-thread. + static void console_line(const std::string & line) { + fb2k::inMainThread([line]() { FB2K_console_formatter() << line.c_str(); }); + } + + static void show_dead_dirs_results(const std::vector & dirs, const std::string & diag) { + pfc::string_formatter msg; + msg << "Dead/empty directories found: " << dirs.size() << "\n"; + msg << "Scan stats: " << diag.c_str() << "\n\n"; + for (const auto & d : dirs) + msg << d.c_str() << "\n"; + const std::string path = report_path("Find in Playlists: dead directories", msg); + FB2K_console_formatter() << "Find in Playlists: scan complete - " << dirs.size() << " found."; + + pfc::string_formatter sum; + if (dirs.empty()) + sum << "No dead/empty directories found."; + else + sum << dirs.size() << " dead/empty directories found.\nReport: " << path.c_str() + << "\nFull list also printed to the console."; + confirm("Find in Playlists", sum); + } + static void run_find_playing() { pfc::string_formatter msg; metadb_handle_ptr now; @@ -338,20 +370,32 @@ class main_find_playing : public mainmenu_commands { } static void run_dead_dirs() { - std::string diag; - auto dirs = scan_dead_directories(&diag); - pfc::string_formatter msg; - msg << "Dead/empty directories found: " << dirs.size() << "\n"; - msg << "Scan stats: " << diag.c_str() << "\n\n"; - for (const auto & d : dirs) - msg << d.c_str() << "\n"; - report("Find in Playlists: dead directories", msg); - pfc::string_formatter sum; - if (dirs.empty()) - sum << "No dead/empty directories found."; - else - sum << dirs.size() << " dead/empty directories found.\nFull list in the report file."; - confirm("Find in Playlists", sum); + if (g_deadDirScanRunning.exchange(true)) { + pfc::string_formatter busy; + busy << "A dead-directory scan is already running."; + confirm("Find in Playlists", busy); + return; + } + + // library_manager is main-thread-only; collect roots before going off-thread. + std::string rootInfo; + const std::vector roots = library_scan_roots(&rootInfo); + if (roots.empty()) { + g_deadDirScanRunning = false; + show_dead_dirs_results({}, rootInfo + ", dirs visited: 0, unreadable dirs: 0"); + return; + } + + FB2K_console_formatter() << "Find in Playlists: scanning " << roots.size() << " root(s)..."; + auto found = std::make_shared>(); + auto walkInfo = std::make_shared(); + std::thread([roots, found, walkInfo, rootInfo]() { + *found = scan_dead_directories(roots, walkInfo.get(), console_line); + fb2k::inMainThread([found, walkInfo, rootInfo]() { + g_deadDirScanRunning = false; + show_dead_dirs_results(*found, rootInfo + ", " + *walkInfo); + }); + }).detach(); } static void run_remove_dead_dirs() { diff --git a/src/scanner.cpp b/src/scanner.cpp index b5e33f0..daa6e6f 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -211,9 +211,7 @@ static bool list_dir_entries(const std::string & dir, std::vector & return !ec; } -std::vector scan_dead_directories(std::string *diag) { - std::vector candidates; - size_t enumFailures = 0; +std::vector library_scan_roots(std::string *info) { static_api_ptr_t lm; metadb_handle_list all; lm->get_all_items(all); @@ -234,9 +232,26 @@ std::vector scan_dead_directories(std::string *diag) { // Scan from the deepest common ancestor per volume, not from leaf dirs // only - empty folders sitting beside album folders would else be missed. const std::vector roots = common_scan_roots(dirOfTrack); + if (info) { + std::string rootsJoined; + for (const auto & r : roots) { + if (!rootsJoined.empty()) + rootsJoined += "; "; + rootsJoined += r; + } + *info = "library items: " + std::to_string(all.get_count()) + + ", track dirs: " + std::to_string(dirOfTrack.size()) + ", roots: [" + rootsJoined + "]"; + } + return roots; +} - // For each root, enumerate all subdirectories recursively. +std::vector scan_dead_directories(const std::vector & roots, std::string *diag, + const std::function & onFound) { + std::vector candidates; + size_t enumFailures = 0; std::unordered_set visited; + + // For each root, enumerate all subdirectories recursively. for (const auto & root : roots) { std::vector> stack; stack.emplace_back(root, true); @@ -264,6 +279,8 @@ std::vector scan_dead_directories(std::string *diag) { // folders, and RemoveDirectoryA would fail on them anyway. if (!isTop && subdirs.empty() && classify_directory(filenames) == dir_status::empty_or_dead) { candidates.push_back(dir); + if (onFound) + onFound(dir); } // Push subdirs for traversal. @@ -279,20 +296,22 @@ std::vector scan_dead_directories(std::string *diag) { candidates.erase(std::unique(candidates.begin(), candidates.end()), candidates.end()); if (diag) { - std::string rootsJoined; - for (const auto & r : roots) { - if (!rootsJoined.empty()) - rootsJoined += "; "; - rootsJoined += r; - } - *diag = "library items: " + std::to_string(all.get_count()) + - ", track dirs: " + std::to_string(dirOfTrack.size()) + ", roots: [" + rootsJoined + - "], dirs visited: " + std::to_string(visited.size()) + - ", unreadable dirs: " + std::to_string(enumFailures); + *diag = + "dirs visited: " + std::to_string(visited.size()) + ", unreadable dirs: " + std::to_string(enumFailures); } return candidates; } +std::vector scan_dead_directories(std::string *diag) { + std::string rootInfo; + const std::vector roots = library_scan_roots(&rootInfo); + std::string walkInfo; + std::vector dirs = scan_dead_directories(roots, &walkInfo); + if (diag) + *diag = rootInfo + ", " + walkInfo; + return dirs; +} + void find_playlists_for(metadb_handle_list_cref sel, std::vector & out) { out.clear(); const t_size selCount = sel.get_count(); diff --git a/src/scanner.h b/src/scanner.h index 56e2e40..e01c8b4 100644 --- a/src/scanner.h +++ b/src/scanner.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -33,9 +34,17 @@ void goto_track_in_playlist(const std::string & playlist, const std::string & ke // Groups results by playlist for reporting/cleanup. std::vector scan_invalid_tags(); -// Directories under the media library that are empty or contain only ignorable files. -// Returns a list of directory paths that are candidates for deletion. When diag is -// given, it receives a one-line scan summary (items seen, roots, dirs visited, errors). +// Media-library scan roots (one per volume). MUST be called on the main thread: +// it queries library_manager. info receives library item/dir counts. +std::vector library_scan_roots(std::string *info = nullptr); + +// Walk the given roots off-thread. onFound, when given, is called once per +// candidate as it is discovered (results are sorted at the end). +std::vector scan_dead_directories(const std::vector & roots, std::string *diag = nullptr, + const std::function & onFound = {}); + +// Convenience wrapper: main-thread roots + filesystem walk in one call. +// diag receives a one-line summary (items seen, roots, dirs visited, errors). std::vector scan_dead_directories(std::string *diag = nullptr); // Playlists this component manages. Excluded from every scan and from export: diff --git a/src/version.h b/src/version.h index bcca495..d66a11a 100644 --- a/src/version.h +++ b/src/version.h @@ -2,4 +2,4 @@ // Single source of truth for the component version. // Automatically updated by GitHub Actions semantic release. -#define FIP_VERSION "1.8.4" +#define FIP_VERSION "1.8.5"