Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up codebase and fix bug #1575

Merged
merged 3 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 20 additions & 15 deletions launcher/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,24 +414,29 @@ bool createShortCut(QString location, QString dest, QStringList args, QString na
#endif
}

bool overrideFolder(QString overwritten_path, QString override_path)
bool mergeFolders(QString dstpath, QString srcpath)
{
using copy_opts = fs::copy_options;

if (!FS::ensureFolderPathExists(overwritten_path))
return false;

std::error_code err;
fs::copy_options opt = copy_opts::recursive | copy_opts::overwrite_existing;

fs::copy(toStdString(override_path), toStdString(overwritten_path), opt, err);

if (err) {
qCritical() << QString("Failed to apply override from %1 to %2").arg(override_path, overwritten_path);
qCritical() << "Reason:" << QString::fromStdString(err.message());
std::error_code ec;
fs::path fullSrcPath = srcpath.toStdString();
fs::path fullDstPath = dstpath.toStdString();
for (auto& entry : fs::recursive_directory_iterator(fullSrcPath))
{
fs::path relativeChild = fs::relative(entry, fullSrcPath);
if (entry.is_directory())
if (!fs::exists(fullDstPath / relativeChild))
fs::create_directory(fullDstPath / relativeChild);
if (entry.is_regular_file())
{
fs::path childDst = fullDstPath / relativeChild;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this declared here instead of higher up

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's declared inside the if's scope, since it's not used outside of that scope, why declare it anywhere else?

if (fs::exists(childDst))
fs::remove(childDst);
fs::copy(entry, childDst, fs::copy_options::none, ec);
if (ec.value() != 0)
qCritical() << QString("File copy failed with: %1. File was %2 -> %3").arg(QString::fromStdString(ec.message()), entry.path().c_str(), childDst.c_str());
}
}

return err.value() == 0;
return ec.value() == 0;
}

}
2 changes: 1 addition & 1 deletion launcher/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,5 @@ QString getDesktopDir();

// Overrides one folder with the contents of another, preserving items exclusive to the first folder
// Equivalent to doing QDir::rename, but allowing for overrides
bool overrideFolder(QString overwritten_path, QString override_path);
bool mergeFolders(QString dstpath, QString srcpath);
}
2 changes: 1 addition & 1 deletion launcher/InstanceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ bool InstanceList::commitStagedInstance(const QString& path, InstanceName const&
QString destination = FS::PathCombine(m_instDir, instID);

if (should_override) {
if (!FS::overrideFolder(destination, path)) {
if (!FS::mergeFolders(destination, path)) {
qWarning() << "Failed to override" << path << "to" << destination;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ bool ModrinthCreationTask::createInstance()
Override::createOverrides("client-overrides", parent_folder, client_override_path);

// Apply the overrides
if (!FS::overrideFolder(mcPath, client_override_path)) {
setError(tr("Could not rename the client overrides folder:\n") + "client overrides");
if (!FS::mergeFolders(mcPath, client_override_path)) {
setError(tr("Could not overwrite / create new files:\n") + "client overrides");
return false;
}
}
Expand Down