Merge pull request #11576 from yannhodiesne/convert-prevent-file-deletion

Check the input and destination paths before converting a game file onto itself
This commit is contained in:
Admiral H. Curtiss 2023-02-17 18:49:15 +01:00 committed by GitHub
commit 74abf48234
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -4,6 +4,7 @@
#include "DolphinQt/ConvertDialog.h" #include "DolphinQt/ConvertDialog.h"
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <functional> #include <functional>
#include <future> #include <future>
#include <memory> #include <memory>
@ -22,6 +23,7 @@
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "DiscIO/Blob.h" #include "DiscIO/Blob.h"
#include "DiscIO/DiscUtils.h" #include "DiscIO/DiscUtils.h"
#include "DiscIO/ScrubbedBlob.h" #include "DiscIO/ScrubbedBlob.h"
@ -385,6 +387,8 @@ void ConvertDialog::Convert()
return; return;
} }
int success_count = 0;
for (const auto& file : m_files) for (const auto& file : m_files)
{ {
const auto original_path = file->GetFilePath(); const auto original_path = file->GetFilePath();
@ -410,6 +414,21 @@ void ConvertDialog::Convert()
} }
} }
if (std::filesystem::exists(StringToPath(dst_path.toStdString())))
{
std::error_code ec;
if (std::filesystem::equivalent(StringToPath(dst_path.toStdString()),
StringToPath(original_path), ec))
{
ModalMessageBox::critical(
this, tr("Error"),
tr("The destination file cannot be the same as the source file\n\n"
"Please select another destination path for \"%1\"")
.arg(QString::fromStdString(original_path)));
continue;
}
}
ParallelProgressDialog progress_dialog(tr("Converting..."), tr("Abort"), 0, 100, this); ParallelProgressDialog progress_dialog(tr("Converting..."), tr("Abort"), 0, 100, this);
progress_dialog.GetRaw()->setWindowModality(Qt::WindowModal); progress_dialog.GetRaw()->setWindowModality(Qt::WindowModal);
progress_dialog.GetRaw()->setWindowTitle(tr("Progress")); progress_dialog.GetRaw()->setWindowTitle(tr("Progress"));
@ -507,11 +526,13 @@ void ConvertDialog::Convert()
tr("Dolphin failed to complete the requested action.")); tr("Dolphin failed to complete the requested action."));
return; return;
} }
success_count++;
} }
} }
ModalMessageBox::information(this, tr("Success"), ModalMessageBox::information(this, tr("Success"),
tr("Successfully converted %n image(s).", "", m_files.size())); tr("Successfully converted %n image(s).", "", success_count));
close(); close();
} }