Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

too many open files (experimental/filesystem)

37 views
Skip to first unread message

Jivanmukta

unread,
Jul 1, 2023, 2:08:10 PM7/1/23
to
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;
}


Jivanmukta

unread,
Jul 1, 2023, 10:49:32 PM7/1/23
to
I change function's code. Still problem.

wstrvector get_filepaths(fs::path path, string extensions, wstring
except_subdir_path, wstring prefix_dir) {
if (path.string().find("vendor") != string::npos &&
except_subdir_path.find(L"kohana-multi-site") != string::npos) {
path = path;
}
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);
wstrvector filepaths;
if (exists(p)) {
/*
if (ends_with(p, wstr2str(dir_separator + L".")) ||
ends_with(p, wstr2str(dir_separator + L"." + dir_separator))) {
*/
const fs::directory_iterator end {};

Paavo Helde

unread,
Jul 2, 2023, 3:12:15 AM7/2/23
to
01.07.2023 21:07 Jivanmukta kirjutas:
> 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.
>

You are hitting the file descriptor limit in a single process.

As your function is recursive, the most obvious suspect is infinite
recursion.


Mut...@dastardlyhq.com

unread,
Jul 2, 2023, 3:58:42 AM7/2/23
to
On Sat, 1 Jul 2023 20:07:51 +0200
Jivanmukta <jivan...@poczta.onet.pl> wrote:
>In a function below I have an exception:

Well good luck! There are better things to do in life than wade through
that mess. Pay me and I'll do it.

Jivanmukta

unread,
Jul 3, 2023, 12:51:58 PM7/3/23
to
W dniu 2.07.2023 o 09:11, Paavo Helde pisze:
I solved the problem by adding
&& (except_subdir_path_no_prefix == L"" ||
str2wstr(iter->path().string()).find(except_subdir_path_no_prefix) ==
wstring::npos)
to the IF is_regular_file and by using fs::recursive_directory_iterator.
There was no infinite recursion but tested direcotries structure was
very large.

wstrvector get_filepaths(fs::path path, string extensions, wstring
except_subdir_path, wstring prefix_dir) {
if (path.string().find("vendor") != string::npos &&
except_subdir_path.find(L"kohana-multi-site") != string::npos) {
path = path;
}
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);
wstrvector filepaths;
if (exists(p)) {
/*
if (ends_with(p, wstr2str(dir_separator + L".")) ||
ends_with(p, wstr2str(dir_separator + L"." + dir_separator))) {
*/
const fs::directory_iterator end {};

Jivanmukta

unread,
Jul 5, 2023, 10:16:14 PM7/5/23
to
W dniu 2.07.2023 o 09:58, Mut...@dastardlyhq.com pisze:
Do better things.
I will not pay you.

Jivanmukta

unread,
Jul 6, 2023, 12:52:21 AM7/6/23
to
Sorry.

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());
}
TRACE("except_subdir_path_no_prefix: " <<
except_subdir_path_no_prefix);
string p = path.string();
p = wstr2str(normalize_path(str2wstr(p), wstr2str(dir_separator)[0]));
path = fs::path(p);
wstrvector filepaths;
if (exists(p)) {
for (auto f : fs::recursive_directory_iterator(path)) {
string e = fs::path(f).extension().string();
if (safe_substr(e, 0, 1) == ".") {
e = safe_substr(e, 1);
}
if (is_regular_file(f) && (extensions == "" ||
index_of_string(explode(",", extensions), e) >= 0)
&& (except_subdir_path_no_prefix == L"" ||
str2wstr(fs::path(f).string()).find(except_subdir_path_no_prefix) ==
wstring::npos)) {
filepaths.push_back(str2wstr(fs::path(f).string()));
} else if (is_directory(f) && (except_subdir_path_no_prefix
== L"" ||
str2wstr(fs::path(f).string()).find(except_subdir_path_no_prefix) ==
wstring::npos)) {
filepaths.push_back(str2wstr(fs::path(f).string()));
}
}
}
return filepaths;
}
0 new messages