13
0

Prevent endless recursive directory scans

This can happen when a folder symlinks to itself or subfolders
in some similar recursive pattern.
This commit is contained in:
Robin Gareus 2024-09-08 01:33:57 +02:00
parent e8162a421d
commit d257c8c0ce
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04

View File

@ -22,6 +22,7 @@
#include <algorithm> #include <algorithm>
#include <climits> #include <climits>
#include <set>
#include <vector> #include <vector>
#include <glib.h> #include <glib.h>
@ -80,7 +81,8 @@ run_functor_for_paths (vector<string>& result,
void *arg, void *arg,
bool pass_files_only, bool pass_files_only,
bool pass_fullpath, bool return_fullpath, bool pass_fullpath, bool return_fullpath,
bool recurse) bool recurse,
set<string>& scanned_paths)
{ {
for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) { for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
try try
@ -102,11 +104,12 @@ run_functor_for_paths (vector<string>& result,
bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR); bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
if (is_dir && recurse) { if (is_dir && recurse) {
DEBUG_TRACE (DEBUG::FileUtils, if (scanned_paths.find (fullpath) == scanned_paths.end ()) {
string_compose("Descending into directory: %1\n", scanned_paths.insert (fullpath);
fullpath)); DEBUG_TRACE (DEBUG::FileUtils, string_compose("Descending into directory: %1\n", fullpath));
run_functor_for_paths (result, fullpath, functor, arg, pass_files_only, run_functor_for_paths (result, fullpath, functor, arg, pass_files_only,
pass_fullpath, return_fullpath, recurse); pass_fullpath, return_fullpath, recurse, scanned_paths);
}
} }
if (is_dir && pass_files_only) { if (is_dir && pass_files_only) {
@ -163,8 +166,9 @@ get_paths (vector<string>& result,
bool files_only, bool files_only,
bool recurse) bool recurse)
{ {
set<string> scanned_path;
run_functor_for_paths (result, paths, accept_all_files, 0, run_functor_for_paths (result, paths, accept_all_files, 0,
files_only, true, true, recurse); files_only, true, true, recurse, scanned_path);
} }
void void
@ -186,9 +190,10 @@ find_files_matching_pattern (vector<string>& result,
const Searchpath& paths, const Searchpath& paths,
const Glib::PatternSpec& pattern) const Glib::PatternSpec& pattern)
{ {
set<string> unused;
run_functor_for_paths (result, paths, pattern_filter, run_functor_for_paths (result, paths, pattern_filter,
const_cast<Glib::PatternSpec*>(&pattern), const_cast<Glib::PatternSpec*>(&pattern),
true, false, true, false); true, false, true, false, unused);
} }
void void
@ -281,7 +286,8 @@ find_paths_matching_filter (vector<string>& result,
bool pass_fullpath, bool return_fullpath, bool pass_fullpath, bool return_fullpath,
bool recurse) bool recurse)
{ {
run_functor_for_paths (result, paths, filter, arg, false, pass_fullpath, return_fullpath, recurse); set<string> scanned_path;
run_functor_for_paths (result, paths, filter, arg, false, pass_fullpath, return_fullpath, recurse, scanned_path);
} }
void void
@ -292,7 +298,8 @@ find_files_matching_filter (vector<string>& result,
bool pass_fullpath, bool return_fullpath, bool pass_fullpath, bool return_fullpath,
bool recurse) bool recurse)
{ {
run_functor_for_paths (result, paths, filter, arg, true, pass_fullpath, return_fullpath, recurse); set<string> scanned_path;
run_functor_for_paths (result, paths, filter, arg, true, pass_fullpath, return_fullpath, recurse, scanned_path);
} }
bool bool