[Bug libstdc++/99533] New: "operation not permitted" error on recursive_directory_iterator despite skip_permission_denied
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99533 Bug ID: 99533 Summary: "operation not permitted" error on recursive_directory_iterator despite skip_permission_denied Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: ssh at pobox dot com Target Milestone: --- On POSIX filesystem backend type systems the std::filesystem::recursive_directory_iterator throws a filesystem_error exception with "operation not permitted" when the opendir/readdir call returns EPERM instead of EACCES even if std::filesystem::directory_options::skip_permission_denied is set. Given the following code: #include #include int main(int argc, char* argv[]) { fs::path dir{"."}; if(argc == 2) { dir = fs::u8path(argv[1]); } int totalDirs = 0; int totalFiles = 0; try { for(const auto& de : fs::recursive_directory_iterator(dir, fs::directory_options::skip_permission_denied)) { if(de.is_regular_file()) { ++totalFiles; } else if(de.is_directory()) { ++totalDirs; } } } catch(fs::filesystem_error fe) { std::cerr << "Error: " << fe.what() << std::endl; exit(1); } std::cout << totalFiles << " files in " << totalDirs << " directories" << std::endl; return 0; } This fails for example on macOS when called on the user home directory with: Error: filesystem error: cannot increment recursive directory iterator: Operation not permitted This is due to System Integrity Protection (since macOS 10.14) on the "/Users//Library/Application Support/MobileSync" folder leading to EPERM. On Linux, called with / it stops when hitting for example a "/proc/1/task/1/cwd", resulting in EPERM too. I don't have examples from other POSIX systems, but I would say handling only EACCES for the skip_permission_denied option is not enough.
[Bug libstdc++/99533] "operation not permitted" error on recursive_directory_iterator despite skip_permission_denied
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99533 --- Comment #5 from Steffen Schuemann --- Thank you for looking into this and sorry for the namespace issue, I was to greedy when deleting the preprocessor parts that switched between my implementation and std::filesystem. First of all, I'm with you in thinking the macOS issue is more of an os problem, but knowing the chances that this gets fixed on Apples side, especially for anything older than Big Sur, I hoped that a fix/workaround on GCC/LLVM side would be a much better chance. Thanks for the changes, I'll try to test macOS later this day. And thank you for pointing out that the situation under Linux is in fact not the same. You are right. Sadly I was fooled to believe it is, while comparing the behavior with my implementation but after some debugging the reason mine iterates through /proc/1/task/1/cwd without an error is actually a bug on my side. So I have to thank you for helping me find that one. So to recap: EPERM issue is indeed macOS only, not Linux!