From d257c8c0ceeb57cff3cafcbb4526462839de887e Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sun, 8 Sep 2024 01:33:57 +0200 Subject: [PATCH] Prevent endless recursive directory scans This can happen when a folder symlinks to itself or subfolders in some similar recursive pattern. --- libs/pbd/file_utils.cc | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/libs/pbd/file_utils.cc b/libs/pbd/file_utils.cc index 4714bbc741..cf1f8ec832 100644 --- a/libs/pbd/file_utils.cc +++ b/libs/pbd/file_utils.cc @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -80,7 +81,8 @@ run_functor_for_paths (vector& result, void *arg, bool pass_files_only, bool pass_fullpath, bool return_fullpath, - bool recurse) + bool recurse, + set& scanned_paths) { for (vector::const_iterator i = paths.begin(); i != paths.end(); ++i) { try @@ -102,11 +104,12 @@ run_functor_for_paths (vector& result, bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR); if (is_dir && recurse) { - DEBUG_TRACE (DEBUG::FileUtils, - string_compose("Descending into directory: %1\n", - fullpath)); - run_functor_for_paths (result, fullpath, functor, arg, pass_files_only, - pass_fullpath, return_fullpath, recurse); + if (scanned_paths.find (fullpath) == scanned_paths.end ()) { + scanned_paths.insert (fullpath); + DEBUG_TRACE (DEBUG::FileUtils, string_compose("Descending into directory: %1\n", fullpath)); + run_functor_for_paths (result, fullpath, functor, arg, pass_files_only, + pass_fullpath, return_fullpath, recurse, scanned_paths); + } } if (is_dir && pass_files_only) { @@ -163,8 +166,9 @@ get_paths (vector& result, bool files_only, bool recurse) { + set scanned_path; run_functor_for_paths (result, paths, accept_all_files, 0, - files_only, true, true, recurse); + files_only, true, true, recurse, scanned_path); } void @@ -186,9 +190,10 @@ find_files_matching_pattern (vector& result, const Searchpath& paths, const Glib::PatternSpec& pattern) { + set unused; run_functor_for_paths (result, paths, pattern_filter, const_cast(&pattern), - true, false, true, false); + true, false, true, false, unused); } void @@ -281,7 +286,8 @@ find_paths_matching_filter (vector& result, bool pass_fullpath, bool return_fullpath, bool recurse) { - run_functor_for_paths (result, paths, filter, arg, false, pass_fullpath, return_fullpath, recurse); + set scanned_path; + run_functor_for_paths (result, paths, filter, arg, false, pass_fullpath, return_fullpath, recurse, scanned_path); } void @@ -292,7 +298,8 @@ find_files_matching_filter (vector& result, bool pass_fullpath, bool return_fullpath, bool recurse) { - run_functor_for_paths (result, paths, filter, arg, true, pass_fullpath, return_fullpath, recurse); + set scanned_path; + run_functor_for_paths (result, paths, filter, arg, true, pass_fullpath, return_fullpath, recurse, scanned_path); } bool