1

I'm using the following loop to extract names of files and directories from a root path.

for (const auto& entry : fs::directory_iterator(dirToList, fs::directory_options::skip_permission_denied, ec)) {
.
Doing some work
.
}

I've use ec in order to avoid the exception but i get the following error

terminate called after throwing an instance of 'std::experimental::filesystem::v1::__cxx11::filesystem_error'
  what():  filesystem error: status: Too many levels of symbolic links [/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/proc/self/task/92639/fd/4/media/floppy]
Aborted (core dumped)

The above error seems to be due to symbolic link so i used the following to avoid it.

                if(fs::is_symlink(entry, ec)){
                    std::cerr << "Avoiding symlink!" << std::endl;
                    continue;
                }

still same exception.

  1. How to resolve the above issue.
  2. How to avoid all exceptions related to std::filesystem::experimental altogether. I can go with return error approach as I do not want to handle exceptions.
Evg
  • 25,259
  • 5
  • 41
  • 83
noob_user
  • 87
  • 7
  • 2
    Try `.increment(ec)` instead of `++` (meaning you can no longer use ranged-for). – HolyBlackCat Aug 07 '23 at 11:10
  • Unrelated to the question, but is your compiler outdated? I believe they all stopped using `experimental::` for the filesystem stuff. – HolyBlackCat Aug 07 '23 at 11:10
  • Yes, the compiler is old. I'm on ubuntu 16.04, g++ 5.4.0. I need to support some old Linux distribution therefor stuck with legacy compilers – noob_user Aug 07 '23 at 11:15
  • Kindly elaborate it a little, i didn't get this "Try .increment(ec) instead of ++" You means to say that i should use something like this. i.e auto it = fs::directory_iterator(path); for (auto i = fs::begin(it); fs::begin(i) != fs::end(it); ) { } – noob_user Aug 07 '23 at 11:16
  • 3
    The ranged-for (`for (... : ...)`) internally uses `++` to increment the iterator, and here `++` can throw. Rewrite the loop to use the old-school syntax (`for (...; ...; ...)` or `while`), and instead of using `++it` to increment the iterator, use `it.increment(ec)` and check the error code. – HolyBlackCat Aug 07 '23 at 11:20

0 Answers0