2013-12-06 01:22:55 +05:30
|
|
|
/* Copyright 2013 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DownloadUpdateTask.h"
|
|
|
|
|
|
|
|
#include "MultiMC.h"
|
|
|
|
#include "logic/updater/UpdateChecker.h"
|
|
|
|
#include "logic/net/NetJob.h"
|
|
|
|
#include "pathutils.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTemporaryDir>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
2013-12-06 08:02:12 +05:30
|
|
|
#include <QDomDocument>
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
DownloadUpdateTask::DownloadUpdateTask(QString repoUrl, int versionId, QObject *parent)
|
|
|
|
: Task(parent)
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
|
|
|
m_cVersionId = MMC->version().build;
|
|
|
|
|
|
|
|
m_nRepoUrl = repoUrl;
|
|
|
|
m_nVersionId = versionId;
|
|
|
|
|
|
|
|
m_updateFilesDir.setAutoRemove(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::executeTask()
|
|
|
|
{
|
|
|
|
// GO!
|
|
|
|
// This will call the next step when it's done.
|
|
|
|
findCurrentVersionInfo();
|
|
|
|
}
|
|
|
|
|
2013-12-15 16:48:42 +05:30
|
|
|
void DownloadUpdateTask::processChannels()
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
|
|
|
auto checker = MMC->updateChecker();
|
|
|
|
|
2013-12-15 16:48:42 +05:30
|
|
|
// Now, check the channel list again.
|
|
|
|
if (!checker->hasChannels())
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-15 16:48:42 +05:30
|
|
|
// We still couldn't load the channel list. Give up. Call loadVersionInfo and return.
|
|
|
|
QLOG_INFO() << "Reloading the channel list didn't work. Giving up.";
|
|
|
|
loadVersionInfo();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 01:22:55 +05:30
|
|
|
|
2013-12-15 16:48:42 +05:30
|
|
|
QList<UpdateChecker::ChannelListEntry> channels = checker->getChannelList();
|
|
|
|
QString channelId = MMC->version().channel;
|
2013-12-06 01:22:55 +05:30
|
|
|
|
2013-12-15 16:48:42 +05:30
|
|
|
// Search through the channel list for a channel with the correct ID.
|
|
|
|
for (auto channel : channels)
|
|
|
|
{
|
|
|
|
if (channel.id == channelId)
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-15 16:48:42 +05:30
|
|
|
QLOG_INFO() << "Found matching channel.";
|
2013-12-28 06:33:53 +05:30
|
|
|
m_cRepoUrl = fixPathForTests(channel.url);
|
2013-12-15 16:48:42 +05:30
|
|
|
break;
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
2013-12-15 16:48:42 +05:30
|
|
|
}
|
2013-12-06 01:22:55 +05:30
|
|
|
|
2013-12-15 16:48:42 +05:30
|
|
|
// Now that we've done that, load version info.
|
|
|
|
loadVersionInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::findCurrentVersionInfo()
|
|
|
|
{
|
2013-12-23 21:16:01 +05:30
|
|
|
setStatus(tr("Finding information about the current version..."));
|
2013-12-15 16:48:42 +05:30
|
|
|
|
|
|
|
auto checker = MMC->updateChecker();
|
2013-12-06 01:22:55 +05:30
|
|
|
|
2013-12-15 16:48:42 +05:30
|
|
|
if (!checker->hasChannels())
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
|
|
|
// Load the channel list and wait for it to finish loading.
|
|
|
|
QLOG_INFO() << "No channel list entries found. Will try reloading it.";
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
QObject::connect(checker.get(), &UpdateChecker::channelListLoaded, this,
|
|
|
|
&DownloadUpdateTask::processChannels);
|
2013-12-06 01:22:55 +05:30
|
|
|
checker->updateChanList();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-15 16:48:42 +05:30
|
|
|
processChannels();
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::loadVersionInfo()
|
|
|
|
{
|
2013-12-23 21:16:01 +05:30
|
|
|
setStatus(tr("Loading version information..."));
|
2013-12-06 01:22:55 +05:30
|
|
|
|
|
|
|
// Create the net job for loading version info.
|
2013-12-17 06:39:58 +05:30
|
|
|
NetJob *netJob = new NetJob("Version Info");
|
|
|
|
|
2013-12-06 01:22:55 +05:30
|
|
|
// Find the index URL.
|
|
|
|
QUrl newIndexUrl = QUrl(m_nRepoUrl).resolved(QString::number(m_nVersionId) + ".json");
|
2013-12-17 06:39:58 +05:30
|
|
|
QLOG_DEBUG() << m_nRepoUrl << " turns into " << newIndexUrl;
|
|
|
|
|
2013-12-06 01:22:55 +05:30
|
|
|
// Add a net action to download the version info for the version we're updating to.
|
|
|
|
netJob->addNetAction(ByteArrayDownload::make(newIndexUrl));
|
|
|
|
|
|
|
|
// If we have a current version URL, get that one too.
|
|
|
|
if (!m_cRepoUrl.isEmpty())
|
|
|
|
{
|
|
|
|
QUrl cIndexUrl = QUrl(m_cRepoUrl).resolved(QString::number(m_cVersionId) + ".json");
|
|
|
|
netJob->addNetAction(ByteArrayDownload::make(cIndexUrl));
|
2013-12-17 06:39:58 +05:30
|
|
|
QLOG_DEBUG() << m_cRepoUrl << " turns into " << cIndexUrl;
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Connect slots so we know when it's done.
|
2013-12-17 06:39:58 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this,
|
|
|
|
&DownloadUpdateTask::vinfoDownloadFinished);
|
2013-12-06 01:22:55 +05:30
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &DownloadUpdateTask::vinfoDownloadFailed);
|
|
|
|
|
|
|
|
// Store the NetJob in a class member. We don't want to lose it!
|
|
|
|
m_vinfoNetJob.reset(netJob);
|
|
|
|
|
|
|
|
// Finally, we start the network job and the thread's event loop to wait for it to finish.
|
|
|
|
netJob->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::vinfoDownloadFinished()
|
|
|
|
{
|
|
|
|
// Both downloads succeeded. OK. Parse stuff.
|
|
|
|
parseDownloadedVersionInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::vinfoDownloadFailed()
|
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
// Something failed. We really need the second download (current version info), so parse
|
|
|
|
// downloads anyways as long as the first one succeeded.
|
2013-12-06 01:22:55 +05:30
|
|
|
if (m_vinfoNetJob->first()->m_status != Job_Failed)
|
|
|
|
{
|
|
|
|
parseDownloadedVersionInfo();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Give a more detailed error message.
|
|
|
|
QLOG_ERROR() << "Failed to download version info files.";
|
|
|
|
emitFailed(tr("Failed to download version info files."));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::parseDownloadedVersionInfo()
|
|
|
|
{
|
2013-12-23 21:16:01 +05:30
|
|
|
setStatus(tr("Reading file list for new version..."));
|
|
|
|
QLOG_DEBUG() << "Reading file list for new version...";
|
2013-12-15 16:48:42 +05:30
|
|
|
QString error;
|
2013-12-17 06:39:58 +05:30
|
|
|
if (!parseVersionInfo(
|
|
|
|
std::dynamic_pointer_cast<ByteArrayDownload>(m_vinfoNetJob->first())->m_data,
|
|
|
|
&m_nVersionFileList, &error))
|
2013-12-15 16:48:42 +05:30
|
|
|
{
|
|
|
|
emitFailed(error);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-06 01:22:55 +05:30
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
// If there is a second entry in the network job's list, load it as the current version's
|
|
|
|
// info.
|
2013-12-06 01:22:55 +05:30
|
|
|
if (m_vinfoNetJob->size() >= 2 && m_vinfoNetJob->operator[](1)->m_status != Job_Failed)
|
|
|
|
{
|
2013-12-23 21:16:01 +05:30
|
|
|
setStatus(tr("Reading file list for current version..."));
|
|
|
|
QLOG_DEBUG() << "Reading file list for current version...";
|
2013-12-15 16:48:42 +05:30
|
|
|
QString error;
|
2013-12-17 06:39:58 +05:30
|
|
|
parseVersionInfo(
|
|
|
|
std::dynamic_pointer_cast<ByteArrayDownload>(m_vinfoNetJob->operator[](1))->m_data,
|
|
|
|
&m_cVersionFileList, &error);
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need this any more.
|
|
|
|
m_vinfoNetJob.reset();
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
// Now that we're done loading version info, we can move on to the next step. Process file
|
|
|
|
// lists and download files.
|
2013-12-06 01:22:55 +05:30
|
|
|
processFileLists();
|
|
|
|
}
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
bool DownloadUpdateTask::parseVersionInfo(const QByteArray &data, VersionFileList *list,
|
|
|
|
QString *error)
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
*error = QString("Failed to parse version info JSON: %1 at %2")
|
|
|
|
.arg(jsonError.errorString())
|
|
|
|
.arg(jsonError.offset);
|
2013-12-15 16:48:42 +05:30
|
|
|
QLOG_ERROR() << error;
|
|
|
|
return false;
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json = jsonDoc.object();
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
QLOG_DEBUG() << data;
|
2013-12-06 01:22:55 +05:30
|
|
|
QLOG_DEBUG() << "Loading version info from JSON.";
|
|
|
|
QJsonArray filesArray = json.value("Files").toArray();
|
|
|
|
for (QJsonValue fileValue : filesArray)
|
|
|
|
{
|
|
|
|
QJsonObject fileObj = fileValue.toObject();
|
|
|
|
|
2013-12-28 06:33:53 +05:30
|
|
|
QString file_path = fileObj.value("Path").toString();
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
// On OSX, the paths for the updater need to be fixed.
|
|
|
|
// basically, anything that isn't in the .app folder is ignored.
|
|
|
|
// everything else is changed so the code that processes the files actually finds
|
|
|
|
// them and puts the replacements in the right spots.
|
2013-12-29 05:43:57 +05:30
|
|
|
if (!fixPathForOSX(file_path))
|
2013-12-28 06:33:53 +05:30
|
|
|
continue;
|
|
|
|
#endif
|
2013-12-29 05:43:57 +05:30
|
|
|
VersionFileEntry file{file_path, fileObj.value("Perms").toVariant().toInt(),
|
|
|
|
FileSourceList(), fileObj.value("MD5").toString(), };
|
2013-12-06 08:02:12 +05:30
|
|
|
QLOG_DEBUG() << "File" << file.path << "with perms" << file.mode;
|
2013-12-17 06:39:58 +05:30
|
|
|
|
2013-12-06 01:22:55 +05:30
|
|
|
QJsonArray sourceArray = fileObj.value("Sources").toArray();
|
|
|
|
for (QJsonValue val : sourceArray)
|
|
|
|
{
|
|
|
|
QJsonObject sourceObj = val.toObject();
|
|
|
|
|
|
|
|
QString type = sourceObj.value("SourceType").toString();
|
|
|
|
if (type == "http")
|
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
file.sources.append(
|
2013-12-28 06:33:53 +05:30
|
|
|
FileSource("http", fixPathForTests(sourceObj.value("Url").toString())));
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
else if (type == "httpc")
|
|
|
|
{
|
2013-12-29 05:43:57 +05:30
|
|
|
file.sources.append(
|
|
|
|
FileSource("httpc", fixPathForTests(sourceObj.value("Url").toString()),
|
|
|
|
sourceObj.value("CompressionType").toString()));
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QLOG_WARN() << "Unknown source type" << type << "ignored.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QLOG_DEBUG() << "Loaded info for" << file.path;
|
|
|
|
|
|
|
|
list->append(file);
|
|
|
|
}
|
2013-12-15 16:48:42 +05:30
|
|
|
|
|
|
|
return true;
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::processFileLists()
|
2013-12-15 23:20:56 +05:30
|
|
|
{
|
|
|
|
// Create a network job for downloading files.
|
2013-12-17 06:39:58 +05:30
|
|
|
NetJob *netJob = new NetJob("Update Files");
|
2013-12-15 23:20:56 +05:30
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
if (!processFileLists(netJob, m_cVersionFileList, m_nVersionFileList, m_operationList))
|
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to process update lists..."));
|
|
|
|
return;
|
|
|
|
}
|
2013-12-15 23:20:56 +05:30
|
|
|
|
|
|
|
// Add listeners to wait for the downloads to finish.
|
2013-12-17 06:39:58 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this,
|
|
|
|
&DownloadUpdateTask::fileDownloadFinished);
|
|
|
|
QObject::connect(netJob, &NetJob::progress, this,
|
|
|
|
&DownloadUpdateTask::fileDownloadProgressChanged);
|
2013-12-15 23:20:56 +05:30
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &DownloadUpdateTask::fileDownloadFailed);
|
|
|
|
|
|
|
|
// Now start the download.
|
|
|
|
setStatus(tr("Downloading %1 update files.").arg(QString::number(netJob->size())));
|
|
|
|
QLOG_DEBUG() << "Begin downloading update files to" << m_updateFilesDir.path();
|
|
|
|
m_filesNetJob.reset(netJob);
|
|
|
|
netJob->start();
|
|
|
|
|
|
|
|
writeInstallScript(m_operationList, PathCombine(m_updateFilesDir.path(), "file_list.xml"));
|
|
|
|
}
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
bool
|
|
|
|
DownloadUpdateTask::processFileLists(NetJob *job,
|
|
|
|
const DownloadUpdateTask::VersionFileList ¤tVersion,
|
|
|
|
const DownloadUpdateTask::VersionFileList &newVersion,
|
|
|
|
DownloadUpdateTask::UpdateOperationList &ops)
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-23 21:16:01 +05:30
|
|
|
setStatus(tr("Processing file lists - figuring out how to install the update..."));
|
2013-12-06 01:22:55 +05:30
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
// First, if we've loaded the current version's file list, we need to iterate through it and
|
2013-12-06 01:22:55 +05:30
|
|
|
// delete anything in the current one version's list that isn't in the new version's list.
|
2013-12-15 23:20:56 +05:30
|
|
|
for (VersionFileEntry entry : currentVersion)
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
QFileInfo toDelete(entry.path);
|
|
|
|
if (!toDelete.exists())
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "Expected file " << toDelete.absoluteFilePath()
|
|
|
|
<< " doesn't exist!";
|
|
|
|
QLOG_ERROR() << "CWD: " << QDir::currentPath();
|
|
|
|
}
|
2013-12-13 07:22:54 +05:30
|
|
|
bool keep = false;
|
2013-12-17 06:39:58 +05:30
|
|
|
|
|
|
|
//
|
2013-12-15 23:20:56 +05:30
|
|
|
for (VersionFileEntry newEntry : newVersion)
|
2013-12-13 07:22:54 +05:30
|
|
|
{
|
2013-12-06 01:22:55 +05:30
|
|
|
if (newEntry.path == entry.path)
|
2013-12-13 04:40:43 +05:30
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
QLOG_DEBUG() << "Not deleting" << entry.path
|
|
|
|
<< "because it is still present in the new version.";
|
2013-12-13 07:22:54 +05:30
|
|
|
keep = true;
|
|
|
|
break;
|
2013-12-13 04:40:43 +05:30
|
|
|
}
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
2013-12-17 06:39:58 +05:30
|
|
|
|
2013-12-13 07:22:54 +05:30
|
|
|
// If the loop reaches the end and we didn't find a match, delete the file.
|
2013-12-17 06:39:58 +05:30
|
|
|
if (!keep)
|
|
|
|
{
|
|
|
|
QFileInfo toDelete(entry.path);
|
|
|
|
if (toDelete.exists())
|
|
|
|
ops.append(UpdateOperation::DeleteOp(entry.path));
|
|
|
|
}
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Next, check each file in MultiMC's folder and see if we need to update them.
|
2013-12-15 23:20:56 +05:30
|
|
|
for (VersionFileEntry entry : newVersion)
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
// TODO: Let's not MD5sum a ton of files on the GUI thread. We should probably find a
|
|
|
|
// way to do this in the background.
|
2013-12-06 01:22:55 +05:30
|
|
|
QString fileMD5;
|
|
|
|
QFile entryFile(entry.path);
|
2013-12-17 06:39:58 +05:30
|
|
|
QFileInfo entryInfo(entry.path);
|
|
|
|
|
|
|
|
bool needs_upgrade = false;
|
|
|
|
if (!entryFile.exists())
|
|
|
|
{
|
|
|
|
needs_upgrade = true;
|
|
|
|
}
|
|
|
|
else
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
bool pass = true;
|
|
|
|
if (!entryInfo.isReadable())
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "File " << entry.path << " is not readable.";
|
|
|
|
pass = false;
|
|
|
|
}
|
|
|
|
if (!entryInfo.isWritable())
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "File " << entry.path << " is not writable.";
|
|
|
|
pass = false;
|
|
|
|
}
|
|
|
|
if (!entryFile.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "File " << entry.path << " cannot be opened for reading.";
|
|
|
|
pass = false;
|
|
|
|
}
|
|
|
|
if (!pass)
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "CWD: " << QDir::currentPath();
|
|
|
|
ops.clear();
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
QCryptographicHash hash(QCryptographicHash::Md5);
|
|
|
|
auto foo = entryFile.readAll();
|
|
|
|
|
|
|
|
hash.addData(foo);
|
|
|
|
fileMD5 = hash.result().toHex();
|
|
|
|
if ((fileMD5 != entry.md5))
|
|
|
|
{
|
|
|
|
QLOG_DEBUG() << "MD5Sum does not match!";
|
|
|
|
QLOG_DEBUG() << "Expected:'" << entry.md5 << "'";
|
|
|
|
QLOG_DEBUG() << "Got: '" << fileMD5 << "'";
|
|
|
|
needs_upgrade = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip file. it doesn't need an upgrade.
|
|
|
|
if (!needs_upgrade)
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
QLOG_DEBUG() << "File" << entry.path << " does not need updating.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// yep. this file actually needs an upgrade. PROCEED.
|
|
|
|
QLOG_DEBUG() << "Found file" << entry.path << " that needs updating.";
|
2013-12-06 01:22:55 +05:30
|
|
|
|
2013-12-23 15:04:43 +05:30
|
|
|
// if it's the updater we want to treat it separately
|
|
|
|
bool isUpdater = entry.path.endsWith("updater") || entry.path.endsWith("updater.exe");
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
// Go through the sources list and find one to use.
|
|
|
|
// TODO: Make a NetAction that takes a source list and tries each of them until one
|
|
|
|
// works. For now, we'll just use the first http one.
|
|
|
|
for (FileSource source : entry.sources)
|
|
|
|
{
|
|
|
|
if (source.type == "http")
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
QLOG_DEBUG() << "Will download" << entry.path << "from" << source.url;
|
|
|
|
|
|
|
|
// Download it to updatedir/<filepath>-<md5> where filepath is the file's
|
|
|
|
// path with slashes replaced by underscores.
|
|
|
|
QString dlPath =
|
|
|
|
PathCombine(m_updateFilesDir.path(), QString(entry.path).replace("/", "_"));
|
|
|
|
|
2013-12-29 05:43:57 +05:30
|
|
|
if (isUpdater)
|
|
|
|
{
|
|
|
|
auto cache_entry = MMC->metacache()->resolveEntry("root", entry.path);
|
|
|
|
QLOG_DEBUG() << "Updater will be in " << cache_entry->getFullPath();
|
|
|
|
if(cache_entry->stale)
|
|
|
|
{
|
|
|
|
auto download = CacheDownload::make(QUrl(source.url), cache_entry);
|
|
|
|
job->addNetAction(download);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-12-06 01:22:55 +05:30
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
// We need to download the file to the updatefiles folder and add a task
|
|
|
|
// to copy it to its install path.
|
|
|
|
auto download = MD5EtagDownload::make(source.url, dlPath);
|
|
|
|
download->m_expected_md5 = entry.md5;
|
|
|
|
job->addNetAction(download);
|
2013-12-23 15:04:43 +05:30
|
|
|
ops.append(UpdateOperation::CopyOp(dlPath, entry.path, entry.mode));
|
|
|
|
}
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 06:39:58 +05:30
|
|
|
return true;
|
2013-12-06 08:02:12 +05:30
|
|
|
}
|
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
bool DownloadUpdateTask::writeInstallScript(UpdateOperationList &opsList, QString scriptFile)
|
2013-12-06 08:02:12 +05:30
|
|
|
{
|
|
|
|
// Build the base structure of the XML document.
|
|
|
|
QDomDocument doc;
|
|
|
|
|
|
|
|
QDomElement root = doc.createElement("update");
|
|
|
|
root.setAttribute("version", "3");
|
|
|
|
doc.appendChild(root);
|
|
|
|
|
|
|
|
QDomElement installFiles = doc.createElement("install");
|
|
|
|
root.appendChild(installFiles);
|
|
|
|
|
|
|
|
QDomElement removeFiles = doc.createElement("uninstall");
|
|
|
|
root.appendChild(removeFiles);
|
|
|
|
|
|
|
|
// Write the operation list to the XML document.
|
|
|
|
for (UpdateOperation op : opsList)
|
|
|
|
{
|
|
|
|
QDomElement file = doc.createElement("file");
|
|
|
|
|
|
|
|
switch (op.type)
|
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
case UpdateOperation::OP_COPY:
|
|
|
|
{
|
|
|
|
// Install the file.
|
|
|
|
QDomElement name = doc.createElement("source");
|
|
|
|
QDomElement path = doc.createElement("dest");
|
|
|
|
QDomElement mode = doc.createElement("mode");
|
2013-12-25 07:16:06 +05:30
|
|
|
name.appendChild(doc.createTextNode(op.file));
|
|
|
|
path.appendChild(doc.createTextNode(op.dest));
|
2013-12-17 06:39:58 +05:30
|
|
|
// We need to add a 0 at the beginning here, because Qt doesn't convert to octal
|
|
|
|
// correctly.
|
|
|
|
mode.appendChild(doc.createTextNode("0" + QString::number(op.mode, 8)));
|
|
|
|
file.appendChild(name);
|
|
|
|
file.appendChild(path);
|
|
|
|
file.appendChild(mode);
|
|
|
|
installFiles.appendChild(file);
|
2013-12-25 07:16:06 +05:30
|
|
|
QLOG_DEBUG() << "Will install file " << op.file << " to " << op.dest;
|
2013-12-17 06:39:58 +05:30
|
|
|
}
|
|
|
|
break;
|
2013-12-06 08:02:12 +05:30
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
case UpdateOperation::OP_DELETE:
|
|
|
|
{
|
|
|
|
// Delete the file.
|
2013-12-25 07:16:06 +05:30
|
|
|
file.appendChild(doc.createTextNode(op.file));
|
2013-12-17 06:39:58 +05:30
|
|
|
removeFiles.appendChild(file);
|
2013-12-25 07:16:06 +05:30
|
|
|
QLOG_DEBUG() << "Will remove file" << op.file;
|
2013-12-17 06:39:58 +05:30
|
|
|
}
|
|
|
|
break;
|
2013-12-06 08:02:12 +05:30
|
|
|
|
2013-12-17 06:39:58 +05:30
|
|
|
default:
|
|
|
|
QLOG_WARN() << "Can't write update operation of type" << op.type
|
|
|
|
<< "to file. Not implemented.";
|
|
|
|
continue;
|
2013-12-06 08:02:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the XML document to the file.
|
|
|
|
QFile outFile(scriptFile);
|
|
|
|
|
|
|
|
if (outFile.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
outFile.write(doc.toByteArray());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emitFailed(tr("Failed to write update script file."));
|
2013-12-15 16:48:42 +05:30
|
|
|
return false;
|
2013-12-06 08:02:12 +05:30
|
|
|
}
|
2013-12-15 16:48:42 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-28 06:33:53 +05:30
|
|
|
QString DownloadUpdateTask::fixPathForTests(const QString &path)
|
2013-12-15 16:48:42 +05:30
|
|
|
{
|
2013-12-29 05:43:57 +05:30
|
|
|
if (path.startsWith("$PWD"))
|
2013-12-21 18:55:16 +05:30
|
|
|
{
|
|
|
|
QString foo = path;
|
|
|
|
foo.replace("$PWD", qApp->applicationDirPath());
|
|
|
|
return QUrl::fromLocalFile(foo).toString(QUrl::FullyEncoded);
|
|
|
|
}
|
|
|
|
return path;
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
2013-12-28 06:33:53 +05:30
|
|
|
bool DownloadUpdateTask::fixPathForOSX(QString &path)
|
|
|
|
{
|
2013-12-29 05:43:57 +05:30
|
|
|
if (path.startsWith("MultiMC.app/"))
|
2013-12-28 06:33:53 +05:30
|
|
|
{
|
|
|
|
// remove the prefix and add a new, more appropriate one.
|
2013-12-29 05:43:57 +05:30
|
|
|
path.remove(0, 12);
|
2013-12-28 06:33:53 +05:30
|
|
|
path = QString("../../") + path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QLOG_ERROR() << "Update path not within .app: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:22:55 +05:30
|
|
|
void DownloadUpdateTask::fileDownloadFinished()
|
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::fileDownloadFailed()
|
|
|
|
{
|
|
|
|
// TODO: Give more info about the failure.
|
|
|
|
QLOG_ERROR() << "Failed to download update files.";
|
|
|
|
emitFailed(tr("Failed to download update files."));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadUpdateTask::fileDownloadProgressChanged(qint64 current, qint64 total)
|
|
|
|
{
|
2013-12-17 06:39:58 +05:30
|
|
|
setProgress((int)(((float)current / (float)total) * 100));
|
2013-12-06 01:22:55 +05:30
|
|
|
}
|
|
|
|
|
2013-12-07 00:29:58 +05:30
|
|
|
QString DownloadUpdateTask::updateFilesDir()
|
|
|
|
{
|
|
|
|
return m_updateFilesDir.path();
|
|
|
|
}
|