Skip to content

Commit

Permalink
[runner]Fix issue on files cleanup when directory doesn't exist (#20063)
Browse files Browse the repository at this point in the history
* Fix issue when directory doesn't exist

* Address PR comment
  • Loading branch information
stefansjfw committed Aug 24, 2022
1 parent a3b7c70 commit e7d3aad
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/runner/main.cpp
Expand Up @@ -300,37 +300,44 @@ 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());
}
}
}
}

// 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());
}
}
}
}
Expand Down

0 comments on commit e7d3aad

Please sign in to comment.