Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 59 additions & 15 deletions src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "export.h"
#include "logic.h"

#include <atomic>
#include <memory>
#include <thread>

// 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}};
Expand All @@ -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<bool> 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");

Expand Down Expand Up @@ -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<std::string> & 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;
Expand Down Expand Up @@ -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<std::string> 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<std::vector<std::string>>();
auto walkInfo = std::make_shared<std::string>();
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() {
Expand Down
47 changes: 33 additions & 14 deletions src/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ static bool list_dir_entries(const std::string & dir, std::vector<std::string> &
return !ec;
}

std::vector<std::string> scan_dead_directories(std::string *diag) {
std::vector<std::string> candidates;
size_t enumFailures = 0;
std::vector<std::string> library_scan_roots(std::string *info) {
static_api_ptr_t<library_manager> lm;
metadb_handle_list all;
lm->get_all_items(all);
Expand All @@ -234,9 +232,26 @@ std::vector<std::string> 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<std::string> 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<std::string> scan_dead_directories(const std::vector<std::string> & roots, std::string *diag,
const std::function<void(const std::string &)> & onFound) {
std::vector<std::string> candidates;
size_t enumFailures = 0;
std::unordered_set<std::string> visited;

// For each root, enumerate all subdirectories recursively.
for (const auto & root : roots) {
std::vector<std::pair<std::string, bool>> stack;
stack.emplace_back(root, true);
Expand Down Expand Up @@ -264,6 +279,8 @@ std::vector<std::string> 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.
Expand All @@ -279,20 +296,22 @@ std::vector<std::string> 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<std::string> scan_dead_directories(std::string *diag) {
std::string rootInfo;
const std::vector<std::string> roots = library_scan_roots(&rootInfo);
std::string walkInfo;
std::vector<std::string> dirs = scan_dead_directories(roots, &walkInfo);
if (diag)
*diag = rootInfo + ", " + walkInfo;
return dirs;
}

void find_playlists_for(metadb_handle_list_cref sel, std::vector<track_report> & out) {
out.clear();
const t_size selCount = sel.get_count();
Expand Down
15 changes: 12 additions & 3 deletions src/scanner.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <pfc/pfc.h>
#include <functional>
#include <string>
#include <vector>
#include <unordered_set>
Expand Down Expand Up @@ -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<dead_entry> 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<std::string> 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<std::string> scan_dead_directories(const std::vector<std::string> & roots, std::string *diag = nullptr,
const std::function<void(const std::string &)> & 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<std::string> scan_dead_directories(std::string *diag = nullptr);

// Playlists this component manages. Excluded from every scan and from export:
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading