0

I want to retrieve all files/directorys path (nested) in the C:/ drive, at least if they are not currently used or where I have the permission to retrive.

In order to do it recursively, I used recursive_directory_iterator from std::filesystem.

I managed to avoid permission error thanks to std::filesystem::directory_options::skip_permission_denied

However, when my program enter the C:/drivers, I get _Error _Sharing_violation (32) __std_win_error because the file it tried to access was probably open.

I have added condition to avoid C:/Windows and C:/drivers but I want to be sure to avoid the error mentioned previously. Unfortunately, there is no option in filesystem to prevent this error like skip_permission_denied for permission, or I don't know it.

Here is my current code :

fs::path rootPath = "C:";
fs::path exclPath = rootPath / "\\Windows";
fs::path drivrPath = rootPath / "\\drivers";

const std::filesystem::directory_options options = (std::filesystem::directory_options::skip_permission_denied);

for (fs::directory_entry p : std::filesystem::recursive_directory_iterator("C:\\", options))
        {
          string filePath = p.path().string();
          // Vérification du type de l'objet
                        
          if (!fs::is_regular_file(p.path()) && filePath.find(drivrPath.string()) == string::npos && filePath.find(exclPath.string()) == string::npos)
                        {


                            cout << filePath << endl;


                        }

        }

Don't pay attention to !fs::is_regular_file(p.path()), I change it manually to test and retrieve directory or file. I get sharing violation error for both.

After some testing I saw that my program fail when checking C:\DumpStack.log.tmp. The issue comes from !fs::is_regular_file(p.path()). When I removed this verification, the program worked. Now, I don't know how to check object type without triggering Share Violation

Sad1que
  • 37
  • 6
  • I see nothing in this code that should be causing sharing violations, since you are not actually accessing the files themselves, only enumerating filesystem metadata. Which exact line of code is throwing the error? Have you tried simply `catch`ing the error so you can continue the enumeration? – Remy Lebeau Aug 28 '21 at 19:03
  • @RemyLebeau, yes I don't even open files and I get Sharing Violation. The error comes from filesystem lib. I tried to catch error like in python with try, except, continue but when I get sharing violation the program just stop. I will try to check if the file is open before perform any check from filesystem lib – Sad1que Aug 28 '21 at 19:54
  • but, again, which **specific** statement in your code is throwing the error? What does the call stack look like when the error is thrown? What does your `try/catch` look like that is not working for you? Please [edit] your question with those details. – Remy Lebeau Aug 28 '21 at 20:06
  • @RemyLebeau Sorry, I don't know how to check this on VS 2019 but I found where the error comes from. I have edited my question – Sad1que Aug 28 '21 at 21:56
  • I can't imagine what `is_regular_file()` does (why not use `p.is_regular_file()` instead?) that requires accessing the file. But whatever. Have you tried using the overload of `is_regular_file()` that takes a `std::error_code&` parameter? That overload does not throw an exception on failure, it fills the `error_code` with details. – Remy Lebeau Aug 28 '21 at 22:37
  • @RemyLebeau, Ohh nice it seems to work now with ```p.is_regular_file()```. I used ```fs::is_regular_file(p.path())``` because I saw in filesystem docs (code exemple) – Sad1que Aug 28 '21 at 22:59

0 Answers0