In a function below I have an exception:
src/obfuscator.cpp (682), get_cmdline_options: get_filepaths:
Error during program execution: filesystem error: directory iterator
cannot open directory: Too many open files
[/home/robert/Projekty/kohana-cms/cms/cms/media/images/themes/clean/]
although in mentioned folder there is only one file.
#include <experimental/filesystem> //
http://en.cppreference.com/w/cpp/experimental/fs
namespace fs = std::experimental::filesystem;
wstrvector get_filepaths(fs::path path, string extensions, wstring
except_subdir_path, wstring prefix_dir) {
wstring except_subdir_path_no_prefix = except_subdir_path;
if (starts_with(except_subdir_path, prefix_dir)) {
except_subdir_path_no_prefix =
except_subdir_path.substr(prefix_dir.length());
}
string p = path.string();
p = wstr2str(normalize_path(str2wstr(p), wstr2str(dir_separator)[0]));
path = fs::path(p);
const fs::directory_iterator end {};
wstrvector filepaths;
if (exists(p)) {
if (ends_with(p, wstr2str(dir_separator + L".")) ||
ends_with(p, wstr2str(dir_separator + L"." + dir_separator))) {
for (fs::directory_iterator iter {path}; iter != end; ++iter) {
string e = iter->path().extension().string();
if (safe_substr(e, 0, 1) == ".") {
e = safe_substr(e, 1);
}
if (is_regular_file(iter->path()) && (extensions == ""
|| index_of_string(explode(",", extensions), e) >= 0)) {
filepaths.push_back(str2wstr(iter->path().string()));
} else if (is_directory(iter->path()) &&
(except_subdir_path_no_prefix == L"" ||
str2wstr(iter->path().string()).find(except_subdir_path_no_prefix) ==
wstring::npos)) {
wstrvector subfiles = get_filepaths(iter->path(),
extensions, except_subdir_path, prefix_dir); // recursion
filepaths.insert(filepaths.end(), subfiles.begin(),
subfiles.end());
}
}
} else {
for (fs::directory_iterator iter {path}; iter != end; ++iter) {
string e = iter->path().extension().string();
if (safe_substr(e, 0, 1) == ".") {
e = safe_substr(e, 1);
}
if (is_regular_file(iter->path()) && (extensions == ""
|| index_of_string(explode(",", extensions), e) >= 0)) {
filepaths.push_back(str2wstr(iter->path().string()));
} else if (is_directory(iter->path()) &&
(except_subdir_path_no_prefix == L"" ||
str2wstr(iter->path().string()).find(except_subdir_path_no_prefix) ==
wstring::npos)) {
wstrvector subfiles = get_filepaths(iter->path(),
extensions, except_subdir_path, prefix_dir); // recursion
filepaths.insert(filepaths.end(), subfiles.begin(),
subfiles.end());
}
}
}
}
return filepaths;
}