[runner]Fix issue on files cleanup when directory doesn't exist (#20063)

* Fix issue when directory doesn't exist

* Address PR comment
This commit is contained in:
Stefan Markovic
2022-08-24 12:04:23 +02:00
committed by GitHub
parent a3b7c70fe0
commit e7d3aadec3

View File

@@ -300,19 +300,23 @@ void cleanup_updates()
return;
}
// Msi and exe files
for (const auto& entry : std::filesystem::directory_iterator(updating::get_pending_updates_path()))
auto update_dir = updating::get_pending_updates_path();
if (std::filesystem::exists(update_dir))
{
auto entryPath = entry.path().wstring();
std::transform(entryPath.begin(), entryPath.end(), entryPath.begin(), ::towlower);
if (entryPath.ends_with(L".msi") || entryPath.ends_with(L".exe"))
// Msi and exe files
for (const auto& entry : std::filesystem::directory_iterator(update_dir))
{
std::error_code err;
std::filesystem::remove(entry, err);
if (err.value())
auto entryPath = entry.path().wstring();
std::transform(entryPath.begin(), entryPath.end(), entryPath.begin(), ::towlower);
if (entryPath.ends_with(L".msi") || entryPath.ends_with(L".exe"))
{
Logger::warn("Failed to delete installer file {}. {}", entry.path().string(), err.message());
std::error_code err;
std::filesystem::remove(entry, err);
if (err.value())
{
Logger::warn("Failed to delete installer file {}. {}", entry.path().string(), err.message());
}
}
}
}
@@ -320,17 +324,20 @@ void cleanup_updates()
// Log files
auto rootPath{ PTSettingsHelper::get_root_save_folder_location() };
auto currentVersion = left_trim<wchar_t>(get_product_version(), L"v");
for (const auto& entry : std::filesystem::directory_iterator(rootPath))
if (std::filesystem::exists(rootPath))
{
auto entryPath = entry.path().wstring();
std::transform(entryPath.begin(), entryPath.end(), entryPath.begin(), ::towlower);
if (entry.is_regular_file() && entryPath.ends_with(L".log") && entryPath.find(currentVersion) == std::string::npos)
for (const auto& entry : std::filesystem::directory_iterator(rootPath))
{
std::error_code err;
std::filesystem::remove(entry, err);
if (err.value())
auto entryPath = entry.path().wstring();
std::transform(entryPath.begin(), entryPath.end(), entryPath.begin(), ::towlower);
if (entry.is_regular_file() && entryPath.ends_with(L".log") && entryPath.find(currentVersion) == std::string::npos)
{
Logger::warn("Failed to delete log file {}. {}", entry.path().string(), err.message());
std::error_code err;
std::filesystem::remove(entry, err);
if (err.value())
{
Logger::warn("Failed to delete log file {}. {}", entry.path().string(), err.message());
}
}
}
}