Skip to content

Commit

Permalink
SporeModManager: cleanup installed files when installation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Nov 5, 2023
1 parent b03dd1b commit dc38b77
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions SporeModManager/SporeModManagerHelpers/SporeMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,30 @@ bool SporeMod::InstallSporeMod(std::filesystem::path path)
{
std::cout << "-> Installing " << installedFile.FileName.string() << std::endl;

std::filesystem::path sourcePath = installedFile.FileName;
std::filesystem::path sourcePath = installedFile.FileName;
std::filesystem::path installPath = Path::GetFullInstallPath(installedFile.InstallLocation, installedFile.FileName);

if (!Zip::ExtractFile(zipFile, sourcePath, installPath))
{
std::cerr << "Zip::ExtractFile() Failed!" << std::endl;
Zip::CloseFile(zipFile);
// cleanup installed files that were left over
for (const auto& installedFileToRemove : installedSporeMod.InstalledFiles)
{
installPath = Path::GetFullInstallPath(installedFileToRemove.InstallLocation, installedFileToRemove.FileName);
if (std::filesystem::is_regular_file(installPath))
{
try
{
std::cout << "-> Removing " << installPath.filename().string() << std::endl;
std::filesystem::remove(installPath);
}
catch(...)
{
std::cerr << "std::filesystem::remove(" << installPath << "\") Failed!" << std::endl;
}
}
}
return false;
}
}
Expand Down Expand Up @@ -363,7 +380,7 @@ bool SporeMod::InstallPackage(std::filesystem::path path)
{
std::cout << "-> Installing " << installedFile.FileName.string() << std::endl;

std::filesystem::path sourcePath = path;
std::filesystem::path sourcePath = path;
std::filesystem::path installPath = Path::GetFullInstallPath(installedFile.InstallLocation, installedFile.FileName);

try
Expand Down

0 comments on commit dc38b77

Please sign in to comment.