2016-10-02 23:55:54 +01:00
|
|
|
#include "InstanceCopyTask.h"
|
2023-03-20 23:38:40 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QtConcurrentRun>
|
2016-10-02 23:55:54 +01:00
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "NullInstance.h"
|
|
|
|
#include "pathmatcher/RegexpMatcher.h"
|
2023-03-20 23:38:40 +00:00
|
|
|
#include "settings/INISettingsObject.h"
|
2016-10-02 23:55:54 +01:00
|
|
|
|
2022-10-23 04:04:36 +01:00
|
|
|
InstanceCopyTask::InstanceCopyTask(InstancePtr origInstance, const InstanceCopyPrefs& prefs)
|
2016-10-02 23:55:54 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_origInstance = origInstance;
|
2022-10-29 05:55:33 +01:00
|
|
|
m_keepPlaytime = prefs.isKeepPlaytimeEnabled();
|
2023-02-09 23:19:38 +00:00
|
|
|
m_useLinks = prefs.isUseSymLinksEnabled();
|
2023-02-09 01:39:17 +00:00
|
|
|
m_linkRecursively = prefs.isLinkRecursivelyEnabled();
|
|
|
|
m_useHardLinks = prefs.isLinkRecursivelyEnabled() && prefs.isUseHardLinksEnabled();
|
|
|
|
m_copySaves = prefs.isLinkRecursivelyEnabled() && prefs.isDontLinkSavesEnabled() && prefs.isCopySavesEnabled();
|
2023-02-09 09:02:40 +00:00
|
|
|
m_useClone = prefs.isUseCloneEnabled();
|
2023-03-20 23:38:40 +00:00
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
QString filters = prefs.getSelectedFiltersAsRegex();
|
|
|
|
if (m_useLinks || m_useHardLinks) {
|
2023-03-20 23:38:40 +00:00
|
|
|
if (!filters.isEmpty())
|
|
|
|
filters += "|";
|
2023-02-10 02:48:40 +00:00
|
|
|
filters += "instance.cfg";
|
2023-03-20 23:38:40 +00:00
|
|
|
}
|
2023-02-10 02:48:40 +00:00
|
|
|
|
|
|
|
qDebug() << "CopyFilters:" << filters;
|
2023-02-08 22:30:45 +00:00
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
if (!filters.isEmpty()) {
|
2022-10-26 05:20:36 +01:00
|
|
|
// Set regex filter:
|
|
|
|
// FIXME: get this from the original instance type...
|
|
|
|
auto matcherReal = new RegexpMatcher(filters);
|
|
|
|
matcherReal->caseSensitive(false);
|
|
|
|
m_matcher.reset(matcherReal);
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2016-12-08 20:58:31 +00:00
|
|
|
}
|
2016-10-02 23:55:54 +01:00
|
|
|
|
2016-12-08 20:58:31 +00:00
|
|
|
void InstanceCopyTask::executeTask()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
setStatus(tr("Copying instance %1").arg(m_origInstance->name()));
|
2017-09-05 22:38:17 +01:00
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
auto copySaves = [&]() {
|
2023-06-02 23:48:02 +01:00
|
|
|
QFileInfo mcDir(FS::PathCombine(m_stagingPath, "minecraft"));
|
|
|
|
QFileInfo dotMCDir(FS::PathCombine(m_stagingPath, ".minecraft"));
|
|
|
|
|
|
|
|
QString staging_mc_dir;
|
|
|
|
if (mcDir.exists() && !dotMCDir.exists())
|
|
|
|
staging_mc_dir = mcDir.filePath();
|
|
|
|
else
|
|
|
|
staging_mc_dir = dotMCDir.filePath();
|
|
|
|
|
|
|
|
FS::copy savesCopy(FS::PathCombine(m_origInstance->gameRoot(), "saves"), FS::PathCombine(staging_mc_dir, "saves"));
|
2023-02-09 01:39:17 +00:00
|
|
|
savesCopy.followSymlinks(true);
|
2023-02-08 22:30:45 +00:00
|
|
|
|
|
|
|
return savesCopy();
|
|
|
|
};
|
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
m_copyFuture = QtConcurrent::run(QThreadPool::globalInstance(), [this, copySaves] {
|
2023-02-09 09:02:40 +00:00
|
|
|
if (m_useClone) {
|
|
|
|
FS::clone folderClone(m_origInstance->instanceRoot(), m_stagingPath);
|
|
|
|
folderClone.matcher(m_matcher.get());
|
|
|
|
|
|
|
|
return folderClone();
|
2023-02-10 02:48:40 +00:00
|
|
|
} else if (m_useLinks || m_useHardLinks) {
|
2023-02-08 22:30:45 +00:00
|
|
|
FS::create_link folderLink(m_origInstance->instanceRoot(), m_stagingPath);
|
2023-03-20 23:38:40 +00:00
|
|
|
int depth = m_linkRecursively ? -1 : 0; // we need to at least link the top level instead of the instance folder
|
2023-02-10 02:48:40 +00:00
|
|
|
folderLink.linkRecursively(true).setMaxDepth(depth).useHardLinks(m_useHardLinks).matcher(m_matcher.get());
|
2023-02-08 22:30:45 +00:00
|
|
|
|
|
|
|
bool there_were_errors = false;
|
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
if (!folderLink()) {
|
2023-02-08 22:30:45 +00:00
|
|
|
#if defined Q_OS_WIN32
|
|
|
|
if (!m_useHardLinks) {
|
|
|
|
qDebug() << "EXPECTED: Link failure, Windows requires permissions for symlinks";
|
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
qDebug() << "attempting to run with privelage";
|
2023-02-08 22:30:45 +00:00
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
bool got_priv_results = false;
|
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
connect(&folderLink, &FS::create_link::finishedPrivileged, this, [&](bool gotResults) {
|
2023-02-08 22:30:45 +00:00
|
|
|
if (!gotResults) {
|
2023-02-23 00:40:07 +00:00
|
|
|
qDebug() << "Privileged run exited without results!";
|
2023-02-08 22:30:45 +00:00
|
|
|
}
|
|
|
|
got_priv_results = gotResults;
|
|
|
|
loop.quit();
|
|
|
|
});
|
2023-02-23 00:40:07 +00:00
|
|
|
folderLink.runPrivileged();
|
2023-02-08 22:30:45 +00:00
|
|
|
|
2023-03-20 23:38:40 +00:00
|
|
|
loop.exec(); // wait for the finished signal
|
2023-02-08 22:30:45 +00:00
|
|
|
|
|
|
|
for (auto result : folderLink.getResults()) {
|
|
|
|
if (result.err_value != 0) {
|
|
|
|
there_were_errors = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_copySaves) {
|
|
|
|
there_were_errors |= !copySaves();
|
|
|
|
}
|
2023-03-20 23:38:40 +00:00
|
|
|
|
2023-02-08 22:30:45 +00:00
|
|
|
return got_priv_results && !there_were_errors;
|
|
|
|
} else {
|
|
|
|
qDebug() << "Link Failed!" << folderLink.getOSError().value() << folderLink.getOSError().message().c_str();
|
2023-03-20 23:38:40 +00:00
|
|
|
}
|
2023-02-08 22:30:45 +00:00
|
|
|
#else
|
|
|
|
qDebug() << "Link Failed!" << folderLink.getOSError().value() << folderLink.getOSError().message().c_str();
|
2023-03-20 23:38:40 +00:00
|
|
|
#endif
|
2023-02-09 09:02:40 +00:00
|
|
|
return false;
|
2023-02-08 22:30:45 +00:00
|
|
|
}
|
2023-03-20 23:38:40 +00:00
|
|
|
|
2023-02-08 22:30:45 +00:00
|
|
|
if (m_copySaves) {
|
|
|
|
there_were_errors |= !copySaves();
|
|
|
|
}
|
|
|
|
|
|
|
|
return !there_were_errors;
|
|
|
|
} else {
|
|
|
|
FS::copy folderCopy(m_origInstance->instanceRoot(), m_stagingPath);
|
|
|
|
folderCopy.followSymlinks(false).matcher(m_matcher.get());
|
2016-10-26 17:12:33 +01:00
|
|
|
|
2023-02-08 22:30:45 +00:00
|
|
|
return folderCopy();
|
|
|
|
}
|
2022-10-22 22:36:47 +01:00
|
|
|
});
|
2018-07-15 13:51:05 +01:00
|
|
|
connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::finished, this, &InstanceCopyTask::copyFinished);
|
|
|
|
connect(&m_copyFutureWatcher, &QFutureWatcher<bool>::canceled, this, &InstanceCopyTask::copyAborted);
|
|
|
|
m_copyFutureWatcher.setFuture(m_copyFuture);
|
2016-10-26 17:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceCopyTask::copyFinished()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto successful = m_copyFuture.result();
|
2023-03-20 23:38:40 +00:00
|
|
|
if (!successful) {
|
2018-07-15 13:51:05 +01:00
|
|
|
emitFailed(tr("Instance folder copy failed."));
|
|
|
|
return;
|
|
|
|
}
|
2023-06-02 23:48:02 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
// FIXME: shouldn't this be able to report errors?
|
|
|
|
auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(m_stagingPath, "instance.cfg"));
|
|
|
|
|
|
|
|
InstancePtr inst(new NullInstance(m_globalSettings, instanceSettings, m_stagingPath));
|
2022-07-14 20:13:23 +01:00
|
|
|
inst->setName(name());
|
2018-07-15 13:51:05 +01:00
|
|
|
inst->setIconKey(m_instIcon);
|
2023-03-20 23:38:40 +00:00
|
|
|
if (!m_keepPlaytime) {
|
2020-01-09 14:31:32 +00:00
|
|
|
inst->resetTimePlayed();
|
|
|
|
}
|
2023-02-16 05:01:27 +00:00
|
|
|
if (m_useLinks)
|
|
|
|
inst->addLinkedInstanceId(m_origInstance->id());
|
2023-06-02 23:48:02 +01:00
|
|
|
if (m_useLinks) {
|
|
|
|
auto allowed_symlinks_file = QFileInfo(FS::PathCombine(inst->gameRoot(), "allowed_symlinks.txt"));
|
|
|
|
|
|
|
|
QByteArray allowed_symlinks;
|
|
|
|
if (allowed_symlinks_file.exists()) {
|
|
|
|
allowed_symlinks.append(FS::read(allowed_symlinks_file.path()));
|
|
|
|
if (allowed_symlinks.right(1) != "\n")
|
|
|
|
allowed_symlinks.append("\n"); // we want to be on a new line
|
|
|
|
}
|
|
|
|
allowed_symlinks.append(m_origInstance->gameRoot().toUtf8());
|
|
|
|
allowed_symlinks.append("\n");
|
2023-06-03 00:14:38 +01:00
|
|
|
if (allowed_symlinks_file.isSymLink())
|
2023-06-02 23:48:02 +01:00
|
|
|
FS::deletePath(allowed_symlinks_file
|
2023-06-03 16:28:49 +01:00
|
|
|
.path()); // we dont want to modify the original. also make sure the resulting file is not itself a link.
|
2023-06-02 23:48:02 +01:00
|
|
|
|
|
|
|
FS::write(allowed_symlinks_file.path(), allowed_symlinks);
|
|
|
|
}
|
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
emitSucceeded();
|
2016-10-02 23:55:54 +01:00
|
|
|
}
|
2016-10-26 17:12:33 +01:00
|
|
|
|
|
|
|
void InstanceCopyTask::copyAborted()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
emitFailed(tr("Instance folder copy has been aborted."));
|
|
|
|
return;
|
2016-10-26 17:12:33 +01:00
|
|
|
}
|