ScanDirectory with std::filesystem incomplete if an exception is raised
arnaud-neny opened this issue · 3 comments
arnaud-neny commented
Hi!
It seems the try-catch in the ScanDirectory of the std::filesystem should be inside the for loop instead of outside. This way, if a file or directory is not accessible, the loop is stopped and doesn't process the other entries.
Thank you!
arnaud-neny commented
Well, in fact it's a little bit more complicated (as it can fail on the directory_iterator request, so here how the code might be:
std::vector<IGFD::FileInfos> ScanDirectory(const std::string& vPath) override {
std::vector<IGFD::FileInfos> res;
try {
const std::filesystem::path fspath(vPath);
IGFD::FileType fstype = IGFD::FileType(IGFD::FileType::ContentType::Directory, std::filesystem::is_symlink(std::filesystem::status(fspath)));
{
IGFD::FileInfos file_two_dot;
file_two_dot.filePath = vPath;
file_two_dot.fileNameExt = "..";
file_two_dot.fileType = fstype;
res.push_back(file_two_dot);
}
const auto dir_iter = std::filesystem::directory_iterator(fspath);
for (const auto& file : dir_iter) {
try {
IGFD::FileType fileType;
if (file.is_symlink()) {
fileType.SetSymLink(file.is_symlink());
fileType.SetContent(IGFD::FileType::ContentType::LinkToUnknown);
}
if (file.is_directory()) {
fileType.SetContent(IGFD::FileType::ContentType::Directory);
} // directory or symlink to directory
else if (file.is_regular_file()) {
fileType.SetContent(IGFD::FileType::ContentType::File);
}
if (fileType.isValid()) {
auto fileNameExt = file.path().filename().string();
{
IGFD::FileInfos _file;
_file.filePath = vPath;
_file.fileNameExt = fileNameExt;
_file.fileType = fileType;
res.push_back(_file);
}
}
} catch (const std::exception& ex) {
printf("%s", ex.what());
}
}
} catch (const std::exception& ex) {
printf("%s", ex.what());
}
return res;
}
aiekick commented
hello,
you are right thanks
aiekick commented
fixed