diff options
author | Petr Mrázek <peterix@gmail.com> | 2013-12-07 03:57:40 +0100 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2013-12-07 03:57:40 +0100 |
commit | 220e07aef4a34dca9f31ae0c5bb994e5c594125d (patch) | |
tree | 70821c6dbe7e6c2f66f231dcb1bad62a763638d7 /logic/updater | |
parent | 858916b951e92127c22503826b746d1e70c433f6 (diff) | |
parent | 7a6705030c36b2d6b94da1acd5bdf2311e4efeb7 (diff) | |
download | MultiMC-220e07aef4a34dca9f31ae0c5bb994e5c594125d.tar MultiMC-220e07aef4a34dca9f31ae0c5bb994e5c594125d.tar.gz MultiMC-220e07aef4a34dca9f31ae0c5bb994e5c594125d.tar.lz MultiMC-220e07aef4a34dca9f31ae0c5bb994e5c594125d.tar.xz MultiMC-220e07aef4a34dca9f31ae0c5bb994e5c594125d.zip |
Merge branch 'feature_updater' of github.com:MultiMC/MultiMC5 into feature_updater
Diffstat (limited to 'logic/updater')
-rw-r--r-- | logic/updater/DownloadUpdateTask.cpp | 84 | ||||
-rw-r--r-- | logic/updater/DownloadUpdateTask.h | 22 |
2 files changed, 96 insertions, 10 deletions
diff --git a/logic/updater/DownloadUpdateTask.cpp b/logic/updater/DownloadUpdateTask.cpp index fc4d321f..ef975c93 100644 --- a/logic/updater/DownloadUpdateTask.cpp +++ b/logic/updater/DownloadUpdateTask.cpp @@ -24,6 +24,8 @@ #include <QTemporaryDir> #include <QCryptographicHash> +#include <QDomDocument> + DownloadUpdateTask::DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent) : Task(parent) @@ -197,10 +199,11 @@ void DownloadUpdateTask::parseVersionInfo(VersionInfoFileEnum vfile, VersionFile VersionFileEntry file{ fileObj.value("Path").toString(), - fileObj.value("Executable").toBool(false), + fileObj.value("Perms").toVariant().toInt(), FileSourceList(), fileObj.value("MD5").toString(), }; + QLOG_DEBUG() << "File" << file.path << "with perms" << file.mode; QJsonArray sourceArray = fileObj.value("Sources").toArray(); for (QJsonValue val : sourceArray) @@ -274,7 +277,7 @@ void DownloadUpdateTask::processFileLists() 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(), entry.path.replace("/", "_")); + QString dlPath = PathCombine(m_updateFilesDir.path(), QString(entry.path).replace("/", "_")); // We need to download the file to the updatefiles folder and add a task to copy it to its install path. FileDownloadPtr download = FileDownload::make(source.url, dlPath); @@ -283,7 +286,7 @@ void DownloadUpdateTask::processFileLists() netJob->addNetAction(download); // Now add a copy operation to our operations list to install the file. - m_operationList.append(UpdateOperation::CopyOp(dlPath, entry.path)); + m_operationList.append(UpdateOperation::CopyOp(dlPath, entry.path, entry.mode)); } } } @@ -300,7 +303,75 @@ void DownloadUpdateTask::processFileLists() m_filesNetJob.reset(netJob); netJob->start(); - // TODO: Write update operations to a file for the update installer. + writeInstallScript(m_operationList, PathCombine(m_updateFilesDir.path(), "file_list.xml")); +} + +void DownloadUpdateTask::writeInstallScript(UpdateOperationList& opsList, QString scriptFile) +{ + // 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) + { + case UpdateOperation::OP_COPY: + { + // Install the file. + QDomElement name = doc.createElement("source"); + QDomElement path = doc.createElement("dest"); + QDomElement mode = doc.createElement("mode"); + name.appendChild(doc.createTextNode(op.file)); + path.appendChild(doc.createTextNode(op.dest)); + // 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); + QLOG_DEBUG() << "Will install file" << op.file; + } + break; + + case UpdateOperation::OP_DELETE: + { + // Delete the file. + file.appendChild(doc.createTextNode(op.file)); + removeFiles.appendChild(file); + QLOG_DEBUG() << "Will remove file" << op.file; + } + break; + + default: + QLOG_WARN() << "Can't write update operation of type" << op.type << "to file. Not implemented."; + continue; + } + } + + // 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.")); + } } void DownloadUpdateTask::fileDownloadFinished() @@ -320,3 +391,8 @@ void DownloadUpdateTask::fileDownloadProgressChanged(qint64 current, qint64 tota setProgress((int)(((float)current / (float)total)*100)); } +QString DownloadUpdateTask::updateFilesDir() +{ + return m_updateFilesDir.path(); +} + diff --git a/logic/updater/DownloadUpdateTask.h b/logic/updater/DownloadUpdateTask.h index 6fb745ab..f5b23d12 100644 --- a/logic/updater/DownloadUpdateTask.h +++ b/logic/updater/DownloadUpdateTask.h @@ -28,6 +28,11 @@ class DownloadUpdateTask : public Task public: explicit DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent=0); + + /*! + * Gets the directory that contains the update files. + */ + QString updateFilesDir(); protected: // TODO: We should probably put these data structures into a separate header... @@ -57,7 +62,7 @@ protected: struct VersionFileEntry { QString path; - bool isExecutable; + int mode; FileSourceList sources; QString md5; }; @@ -70,9 +75,9 @@ protected: */ struct UpdateOperation { - static UpdateOperation CopyOp(QString fsource, QString fdest) { return UpdateOperation{OP_COPY, fsource, fdest, 644}; } - static UpdateOperation MoveOp(QString fsource, QString fdest) { return UpdateOperation{OP_MOVE, fsource, fdest, 644}; } - static UpdateOperation DeleteOp(QString file) { return UpdateOperation{OP_DELETE, file, "", 644}; } + static UpdateOperation CopyOp(QString fsource, QString fdest, int fmode=0644) { return UpdateOperation{OP_COPY, fsource, fdest, fmode}; } + static UpdateOperation MoveOp(QString fsource, QString fdest, int fmode=0644) { return UpdateOperation{OP_MOVE, fsource, fdest, fmode}; } + static UpdateOperation DeleteOp(QString file) { return UpdateOperation{OP_DELETE, file, "", 0644}; } static UpdateOperation ChmodOp(QString file, int fmode) { return UpdateOperation{OP_CHMOD, file, "", fmode}; } //! Specifies the type of operation that this is. @@ -84,8 +89,8 @@ protected: OP_CHMOD, } type; - //! The source file. If this is a DELETE or CHMOD operation, this is the file that will be modified. - QString source; + //! The file to operate on. If this is a DELETE or CHMOD operation, this is the file that will be modified. + QString file; //! The destination file. If this is a DELETE or CHMOD operation, this field will be ignored. QString dest; @@ -145,6 +150,11 @@ protected: */ virtual void processFileLists(); + /*! + * Takes the operations list and writes an install script for the updater to the update files directory. + */ + virtual void writeInstallScript(UpdateOperationList& opsList, QString scriptFile); + VersionFileList m_downloadList; UpdateOperationList m_operationList; |