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