2016-10-03 04:25:54 +05:30
|
|
|
#include "InstanceCopyTask.h"
|
2023-03-21 05:08:40 +05:30
|
|
|
#include <QDebug>
|
|
|
|
#include <QtConcurrentRun>
|
2016-10-03 04:25:54 +05:30
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "NullInstance.h"
|
|
|
|
#include "pathmatcher/RegexpMatcher.h"
|
2023-03-21 05:08:40 +05:30
|
|
|
#include "settings/INISettingsObject.h"
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2022-10-23 08:34:36 +05:30
|
|
|
InstanceCopyTask::InstanceCopyTask(InstancePtr origInstance, const InstanceCopyPrefs& prefs)
|
2016-10-03 04:25:54 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_origInstance = origInstance;
|
2022-10-29 10:25:33 +05:30
|
|
|
m_keepPlaytime = prefs.isKeepPlaytimeEnabled();
|
2023-02-10 04:49:38 +05:30
|
|
|
m_useLinks = prefs.isUseSymLinksEnabled();
|
2023-02-09 07:09:17 +05:30
|
|
|
m_linkRecursively = prefs.isLinkRecursivelyEnabled();
|
|
|
|
m_useHardLinks = prefs.isLinkRecursivelyEnabled() && prefs.isUseHardLinksEnabled();
|
|
|
|
m_copySaves = prefs.isLinkRecursivelyEnabled() && prefs.isDontLinkSavesEnabled() && prefs.isCopySavesEnabled();
|
2023-02-09 14:32:40 +05:30
|
|
|
m_useClone = prefs.isUseCloneEnabled();
|
2023-03-21 05:08:40 +05:30
|
|
|
|
2023-02-10 08:18:40 +05:30
|
|
|
QString filters = prefs.getSelectedFiltersAsRegex();
|
|
|
|
if (m_useLinks || m_useHardLinks) {
|
2023-03-21 05:08:40 +05:30
|
|
|
if (!filters.isEmpty())
|
|
|
|
filters += "|";
|
2023-02-10 08:18:40 +05:30
|
|
|
filters += "instance.cfg";
|
2023-03-21 05:08:40 +05:30
|
|
|
}
|
2023-02-10 08:18:40 +05:30
|
|
|
|
|
|
|
qDebug() << "CopyFilters:" << filters;
|
2023-02-09 04:00:45 +05:30
|
|
|
|
2023-03-21 05:08:40 +05:30
|
|
|
if (!filters.isEmpty()) {
|
2022-10-26 09:50:36 +05:30
|
|
|
// 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 18:21:05 +05:30
|
|
|
}
|
2016-12-09 02:28:31 +05:30
|
|
|
}
|
2016-10-03 04:25:54 +05:30
|
|
|
|
2016-12-09 02:28:31 +05:30
|
|
|
void InstanceCopyTask::executeTask()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setStatus(tr("Copying instance %1").arg(m_origInstance->name()));
|
2017-09-06 03:08:17 +05:30
|
|
|
|
2023-03-21 05:08:40 +05:30
|
|
|
auto copySaves = [&]() {
|
2023-06-03 04:18:02 +05:30
|
|
|
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 07:09:17 +05:30
|
|
|
savesCopy.followSymlinks(true);
|
2023-02-09 04:00:45 +05:30
|
|
|
|
|
|
|
return savesCopy();
|
|
|
|
};
|
|
|
|
|
2023-03-21 05:08:40 +05:30
|
|
|
m_copyFuture = QtConcurrent::run(QThreadPool::globalInstance(), [this, copySaves] {
|
2023-02-09 14:32:40 +05:30
|
|
|
if (m_useClone) {
|
|
|
|
FS::clone folderClone(m_origInstance->instanceRoot(), m_stagingPath);
|
|
|
|
folderClone.matcher(m_matcher.get());
|
|
|
|
|
|
|
|
return folderClone();
|
2023-02-10 08:18:40 +05:30
|
|
|
} else if (m_useLinks || m_useHardLinks) {
|
2023-02-09 04:00:45 +05:30
|
|
|
FS::create_link folderLink(m_origInstance->instanceRoot(), m_stagingPath);
|
2023-03-21 05:08:40 +05:30
|
|
|
int depth = m_linkRecursively ? -1 : 0; // we need to at least link the top level instead of the instance folder
|
2023-02-10 08:18:40 +05:30
|
|
|
folderLink.linkRecursively(true).setMaxDepth(depth).useHardLinks(m_useHardLinks).matcher(m_matcher.get());
|
2023-02-09 04:00:45 +05:30
|
|
|
|
|
|
|
bool there_were_errors = false;
|
|
|
|
|
2023-03-21 05:08:40 +05:30
|
|
|
if (!folderLink()) {
|
2023-02-09 04:00:45 +05:30
|
|
|
#if defined Q_OS_WIN32
|
|
|
|
if (!m_useHardLinks) {
|
|
|
|
qDebug() << "EXPECTED: Link failure, Windows requires permissions for symlinks";
|
|
|
|
|
2023-03-21 05:08:40 +05:30
|
|
|
qDebug() << "attempting to run with privelage";
|
2023-02-09 04:00:45 +05:30
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
bool got_priv_results = false;
|
|
|
|
|
2023-03-21 05:08:40 +05:30
|
|
|
connect(&folderLink, &FS::create_link::finishedPrivileged, this, [&](bool gotResults) {
|
2023-02-09 04:00:45 +05:30
|
|
|
if (!gotResults) {
|
2023-02-23 06:10:07 +05:30
|
|
|
qDebug() << "Privileged run exited without results!";
|
2023-02-09 04:00:45 +05:30
|
|
|
}
|
|
|
|
got_priv_results = gotResults;
|
|
|
|
loop.quit();
|
|
|
|
});
|
2023-02-23 06:10:07 +05:30
|
|
|
folderLink.runPrivileged();
|
2023-02-09 04:00:45 +05:30
|
|
|
|
2023-03-21 05:08:40 +05:30
|
|
|
loop.exec(); // wait for the finished signal
|
2023-02-09 04:00:45 +05:30
|
|
|
|
|
|
|
for (auto result : folderLink.getResults()) {
|
|
|
|
if (result.err_value != 0) {
|
|
|
|
there_were_errors = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_copySaves) {
|
|
|
|
there_were_errors |= !copySaves();
|
|
|
|
}
|
2023-03-21 05:08:40 +05:30
|
|
|
|
2023-02-09 04:00:45 +05:30
|
|
|
return got_priv_results && !there_were_errors;
|
|
|
|
} else {
|
|
|
|
qDebug() << "Link Failed!" << folderLink.getOSError().value() << folderLink.getOSError().message().c_str();
|
2023-03-21 05:08:40 +05:30
|
|
|
}
|
2023-02-09 04:00:45 +05:30
|
|
|
#else
|
|
|
|
qDebug() << "Link Failed!" << folderLink.getOSError().value() << folderLink.getOSError().message().c_str();
|
2023-03-21 05:08:40 +05:30
|
|
|
#endif
|
2023-02-09 14:32:40 +05:30
|
|
|
return false;
|
2023-02-09 04:00:45 +05:30
|
|
|
}
|
2023-03-21 05:08:40 +05:30
|
|
|
|
2023-02-09 04:00:45 +05:30
|
|
|
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 21:42:33 +05:30
|
|
|
|
2023-02-09 04:00:45 +05:30
|
|
|
return folderCopy();
|
|
|
|
}
|
2022-10-23 03:06:47 +05:30
|
|
|
});
|
2018-07-15 18:21:05 +05:30
|
|
|
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 21:42:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void InstanceCopyTask::copyFinished()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto successful = m_copyFuture.result();
|
2023-03-21 05:08:40 +05:30
|
|
|
if (!successful) {
|
2018-07-15 18:21:05 +05:30
|
|
|
emitFailed(tr("Instance folder copy failed."));
|
|
|
|
return;
|
|
|
|
}
|
2023-06-03 04:18:02 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// 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-15 00:43:23 +05:30
|
|
|
inst->setName(name());
|
2018-07-15 18:21:05 +05:30
|
|
|
inst->setIconKey(m_instIcon);
|
2023-03-21 05:08:40 +05:30
|
|
|
if (!m_keepPlaytime) {
|
2020-01-09 20:01:32 +05:30
|
|
|
inst->resetTimePlayed();
|
|
|
|
}
|
2023-02-16 10:31:27 +05:30
|
|
|
if (m_useLinks)
|
|
|
|
inst->addLinkedInstanceId(m_origInstance->id());
|
2023-06-03 04:18:02 +05:30
|
|
|
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 04:44:38 +05:30
|
|
|
if (allowed_symlinks_file.isSymLink())
|
2023-06-03 04:18:02 +05:30
|
|
|
FS::deletePath(allowed_symlinks_file
|
2023-06-03 20:58:49 +05:30
|
|
|
.path()); // we dont want to modify the original. also make sure the resulting file is not itself a link.
|
2023-06-03 04:18:02 +05:30
|
|
|
|
|
|
|
FS::write(allowed_symlinks_file.path(), allowed_symlinks);
|
|
|
|
}
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
emitSucceeded();
|
2016-10-03 04:25:54 +05:30
|
|
|
}
|
2016-10-26 21:42:33 +05:30
|
|
|
|
|
|
|
void InstanceCopyTask::copyAborted()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emitFailed(tr("Instance folder copy has been aborted."));
|
|
|
|
return;
|
2016-10-26 21:42:33 +05:30
|
|
|
}
|