Make delete_other_versions_log_folders more reliable (#10760)

This commit is contained in:
Mykhailo Pylyp 2021-04-16 15:21:21 +03:00 committed by GitHub
parent 71cf94b330
commit 6e65f9cbd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,13 +28,40 @@ namespace LoggerHelpers
return false;
}
inline bool dir_exists(std::filesystem::path dir)
{
std::error_code err;
auto entry = std::filesystem::directory_entry(dir, err);
if (err.value())
{
Logger::error("Failed to create directory entry. {}", err.message());
return false;
}
return entry.exists();
}
inline bool delete_other_versions_log_folders(std::wstring_view appPath, const std::filesystem::path& currentVersionLogFolder)
{
bool result = true;
std::filesystem::path logFolderPath(appPath);
logFolderPath.append(LogSettings::logPath);
for (const auto& dir : std::filesystem::directory_iterator(logFolderPath))
if (!dir_exists(logFolderPath))
{
Logger::trace("Directory {} does not exist", logFolderPath.string());
return true;
}
std::error_code err;
auto folders = std::filesystem::directory_iterator(logFolderPath, err);
if (err.value())
{
Logger::error("Failed to create directory iterator for {}. {}", logFolderPath.string(), err.message());
return false;
}
for (const auto& dir : folders)
{
if (dir != currentVersionLogFolder)
{