Skip to content

Commit

Permalink
Utility::Glob(): add option to suppress errors
Browse files Browse the repository at this point in the history
refs #7742
  • Loading branch information
Al2Klimov committed Mar 17, 2020
1 parent 8cf8838 commit 01ee2e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
41 changes: 30 additions & 11 deletions lib/base/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,10 @@ bool Utility::Glob(const String& pathSpec, const std::function<void (const Strin
* @param pattern The pattern.
* @param callback The callback which is invoked for each matching file.
* @param type The file type (a combination of GlobFile and GlobDirectory)
* @param onError If an actual function (!!onError) called on error, if returns true, that error is being suppressed.
*/
bool Utility::GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback, int type)
bool Utility::GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback,
int type, const std::function<bool(const std::exception&)>& onError)
{
std::vector<String> files, dirs, alldirs;

Expand All @@ -565,10 +567,18 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const std
if (errorCode == ERROR_FILE_NOT_FOUND)
return false;

BOOST_THROW_EXCEPTION(win32_error()
<< boost::errinfo_api_function("FindFirstFile")
<< errinfo_win32_error(errorCode)
<< boost::errinfo_file_name(pathSpec));
try {
BOOST_THROW_EXCEPTION(win32_error()
<< boost::errinfo_api_function("FindFirstFile")
<< errinfo_win32_error(errorCode)
<< boost::errinfo_file_name(pathSpec));
} catch (const std::exception& ex) {
if (onError && onError(ex)) {
return false;
}

throw;
}
}

do {
Expand Down Expand Up @@ -600,11 +610,20 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const std

dirp = opendir(path.CStr());

if (!dirp)
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("opendir")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(path));
if (!dirp) {
try {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("opendir")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(path));
} catch (const std::exception& ex) {
if (onError && onError(ex)) {
return false;
}

throw;
}
}

while (dirp) {
dirent *pent;
Expand Down Expand Up @@ -662,7 +681,7 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const std

std::sort(alldirs.begin(), alldirs.end());
for (const String& cpath : alldirs) {
GlobRecursive(cpath, pattern, callback, type);
GlobRecursive(cpath, pattern, callback, type, onError);
}

return true;
Expand Down
3 changes: 2 additions & 1 deletion lib/base/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class Utility
static String NewUniqueID();

static bool Glob(const String& pathSpec, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
static bool GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
static bool GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback,
int type = GlobFile | GlobDirectory, const std::function<bool(const std::exception&)>& onError = {});
static void MkDir(const String& path, int mode);
static void MkDirP(const String& path, int mode);
static bool SetFileOwnership(const String& file, const String& user, const String& group);
Expand Down

0 comments on commit 01ee2e7

Please sign in to comment.