Implement update installer

This commit is contained in:
Andrew 2013-12-05 20:32:12 -06:00
parent 48ec8e67b8
commit e90f1a2756
12 changed files with 133 additions and 73 deletions

View File

@ -63,6 +63,15 @@ void FileDownload::start()
request.setRawHeader(QString("If-None-Match").toLatin1(), m_expected_md5.toLatin1()); request.setRawHeader(QString("If-None-Match").toLatin1(), m_expected_md5.toLatin1());
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
// Go ahead and try to open the file.
// If we don't do this, empty files won't be created, which breaks the updater.
// Plus, this way, we don't end up starting a download for a file we can't open.
if (!m_output_file.open(QIODevice::WriteOnly))
{
emit failed(index_within_job);
return;
}
auto worker = MMC->qnam(); auto worker = MMC->qnam();
QNetworkReply *rep = worker->get(request); QNetworkReply *rep = worker->get(request);

View File

@ -24,6 +24,8 @@
#include <QTemporaryDir> #include <QTemporaryDir>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QDomDocument>
DownloadUpdateTask::DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent) : DownloadUpdateTask::DownloadUpdateTask(QString repoUrl, int versionId, QObject* parent) :
Task(parent) Task(parent)
@ -197,10 +199,11 @@ void DownloadUpdateTask::parseVersionInfo(VersionInfoFileEnum vfile, VersionFile
VersionFileEntry file{ VersionFileEntry file{
fileObj.value("Path").toString(), fileObj.value("Path").toString(),
fileObj.value("Executable").toBool(false), fileObj.value("Perms").toVariant().toInt(),
FileSourceList(), FileSourceList(),
fileObj.value("MD5").toString(), fileObj.value("MD5").toString(),
}; };
QLOG_DEBUG() << "File" << file.path << "with perms" << file.mode;
QJsonArray sourceArray = fileObj.value("Sources").toArray(); QJsonArray sourceArray = fileObj.value("Sources").toArray();
for (QJsonValue val : sourceArray) for (QJsonValue val : sourceArray)
@ -274,7 +277,7 @@ void DownloadUpdateTask::processFileLists()
QLOG_DEBUG() << "Will download" << entry.path << "from" << source.url; 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. // 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. // 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); FileDownloadPtr download = FileDownload::make(source.url, dlPath);
@ -283,7 +286,7 @@ void DownloadUpdateTask::processFileLists()
netJob->addNetAction(download); netJob->addNetAction(download);
// Now add a copy operation to our operations list to install the file. // 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); m_filesNetJob.reset(netJob);
netJob->start(); 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() void DownloadUpdateTask::fileDownloadFinished()

View File

@ -57,7 +57,7 @@ protected:
struct VersionFileEntry struct VersionFileEntry
{ {
QString path; QString path;
bool isExecutable; int mode;
FileSourceList sources; FileSourceList sources;
QString md5; QString md5;
}; };
@ -70,9 +70,9 @@ protected:
*/ */
struct UpdateOperation struct UpdateOperation
{ {
static UpdateOperation CopyOp(QString fsource, QString fdest) { return UpdateOperation{OP_COPY, fsource, fdest, 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) { return UpdateOperation{OP_MOVE, fsource, fdest, 644}; } 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, "", 644}; } static UpdateOperation DeleteOp(QString file) { return UpdateOperation{OP_DELETE, file, "", 0644}; }
static UpdateOperation ChmodOp(QString file, int fmode) { return UpdateOperation{OP_CHMOD, file, "", fmode}; } static UpdateOperation ChmodOp(QString file, int fmode) { return UpdateOperation{OP_CHMOD, file, "", fmode}; }
//! Specifies the type of operation that this is. //! Specifies the type of operation that this is.
@ -84,8 +84,8 @@ protected:
OP_CHMOD, OP_CHMOD,
} type; } type;
//! The source file. If this is a DELETE or CHMOD operation, this is the file that will be modified. //! The file to operate on. If this is a DELETE or CHMOD operation, this is the file that will be modified.
QString source; QString file;
//! The destination file. If this is a DELETE or CHMOD operation, this field will be ignored. //! The destination file. If this is a DELETE or CHMOD operation, this field will be ignored.
QString dest; QString dest;
@ -145,6 +145,11 @@ protected:
*/ */
virtual void processFileLists(); 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; VersionFileList m_downloadList;
UpdateOperationList m_operationList; UpdateOperationList m_operationList;

View File

@ -51,6 +51,11 @@ void UpdateInstaller::setForceElevated(bool elevated)
m_forceElevated = elevated; m_forceElevated = elevated;
} }
void UpdateInstaller::setFinishCmd(const std::string& cmd)
{
m_finishCmd = cmd;
}
std::list<std::string> UpdateInstaller::updaterArgs() const std::list<std::string> UpdateInstaller::updaterArgs() const
{ {
std::list<std::string> args; std::list<std::string> args;
@ -266,7 +271,7 @@ void UpdateInstaller::revert()
void UpdateInstaller::installFile(const UpdateScriptFile& file) void UpdateInstaller::installFile(const UpdateScriptFile& file)
{ {
std::string destPath = m_installDir + '/' + file.path; std::string destPath = file.dest;
std::string target = file.linkTarget; std::string target = file.linkTarget;
// backup the existing file if any // backup the existing file if any
@ -279,9 +284,7 @@ void UpdateInstaller::installFile(const UpdateScriptFile& file)
FileUtils::mkpath(destDir.c_str()); FileUtils::mkpath(destDir.c_str());
} }
if (target.empty()) std::string sourceFile = file.source;
{
std::string sourceFile = m_packageDir + '/' + FileUtils::fileName(file.path.c_str());
if (!FileUtils::fileExists(sourceFile.c_str())) if (!FileUtils::fileExists(sourceFile.c_str()))
{ {
throw "Source file does not exist: " + sourceFile; throw "Source file does not exist: " + sourceFile;
@ -291,12 +294,6 @@ void UpdateInstaller::installFile(const UpdateScriptFile& file)
// set the permissions on the newly extracted file // set the permissions on the newly extracted file
FileUtils::chmod(destPath.c_str(),file.permissions); FileUtils::chmod(destPath.c_str(),file.permissions);
} }
else
{
// create the symlink
FileUtils::createSymLink(destPath.c_str(),target.c_str());
}
}
void UpdateInstaller::installFiles() void UpdateInstaller::installFiles()
{ {
@ -391,19 +388,9 @@ void UpdateInstaller::restartMainApp()
{ {
try try
{ {
std::string command; std::string command = m_installDir + '/' + m_finishCmd;
std::list<std::string> args; std::list<std::string> args;
for (std::vector<UpdateScriptFile>::const_iterator iter = m_script->filesToInstall().begin();
iter != m_script->filesToInstall().end();
iter++)
{
if (iter->isMainBinary)
{
command = m_installDir + '/' + iter->path;
}
}
if (!command.empty()) if (!command.empty())
{ {
LOG(Info,"Starting main application " + command); LOG(Info,"Starting main application " + command);
@ -411,7 +398,7 @@ void UpdateInstaller::restartMainApp()
} }
else else
{ {
LOG(Error,"No main binary specified in update script"); LOG(Error,"No main binary specified");
} }
} }
catch (const std::exception& ex) catch (const std::exception& ex)

View File

@ -33,6 +33,7 @@ class UpdateInstaller
void setWaitPid(PLATFORM_PID pid); void setWaitPid(PLATFORM_PID pid);
void setForceElevated(bool elevated); void setForceElevated(bool elevated);
void setAutoClose(bool autoClose); void setAutoClose(bool autoClose);
void setFinishCmd(const std::string& cmd);
void setObserver(UpdateObserver* observer); void setObserver(UpdateObserver* observer);
@ -60,6 +61,7 @@ class UpdateInstaller
std::string m_installDir; std::string m_installDir;
std::string m_packageDir; std::string m_packageDir;
std::string m_backupDir; std::string m_backupDir;
std::string m_finishCmd;
PLATFORM_PID m_waitPid; PLATFORM_PID m_waitPid;
UpdateScript* m_script; UpdateScript* m_script;
UpdateObserver* m_observer; UpdateObserver* m_observer;

View File

@ -71,13 +71,14 @@ void UpdateScript::parseUpdate(const TiXmlElement* updateNode)
UpdateScriptFile UpdateScript::parseFile(const TiXmlElement* element) UpdateScriptFile UpdateScript::parseFile(const TiXmlElement* element)
{ {
UpdateScriptFile file; UpdateScriptFile file;
file.path = elementText(element->FirstChildElement("name")); // The name within the update files folder.
file.source = elementText(element->FirstChildElement("source"));
// The path to install to.
file.dest = elementText(element->FirstChildElement("dest"));
std::string modeString = elementText(element->FirstChildElement("permissions")); std::string modeString = elementText(element->FirstChildElement("mode"));
sscanf(modeString.c_str(),"%i",&file.permissions); sscanf(modeString.c_str(),"%i",&file.permissions);
file.linkTarget = elementText(element->FirstChildElement("target"));
file.isMainBinary = strToBool(elementText(element->FirstChildElement("is-main-binary")));
return file; return file;
} }

View File

@ -35,10 +35,12 @@ class UpdateScriptFile
public: public:
UpdateScriptFile() UpdateScriptFile()
: permissions(0) : permissions(0)
, isMainBinary(false)
{} {}
std::string path; /// Path to copy from.
std::string source;
/// The path to copy to.
std::string dest;
std::string linkTarget; std::string linkTarget;
/** The permissions for this file, specified /** The permissions for this file, specified
@ -46,14 +48,11 @@ class UpdateScriptFile
*/ */
int permissions; int permissions;
bool isMainBinary;
bool operator==(const UpdateScriptFile& other) const bool operator==(const UpdateScriptFile& other) const
{ {
return path == other.path && return source == other.source &&
permissions == other.permissions && dest == other.dest &&
linkTarget == other.linkTarget && permissions == other.permissions;
isMainBinary == other.isMainBinary;
} }
}; };

View File

@ -111,6 +111,7 @@ void UpdaterOptions::parse(int argc, char** argv)
AnyOption parser; AnyOption parser;
parser.setOption("install-dir"); parser.setOption("install-dir");
parser.setOption("package-dir"); parser.setOption("package-dir");
parser.setOption("finish-cmd");
parser.setOption("script"); parser.setOption("script");
parser.setOption("wait"); parser.setOption("wait");
parser.setOption("mode"); parser.setOption("mode");
@ -140,6 +141,10 @@ void UpdaterOptions::parse(int argc, char** argv)
{ {
waitPid = static_cast<PLATFORM_PID>(atoll(parser.getValue("wait"))); waitPid = static_cast<PLATFORM_PID>(atoll(parser.getValue("wait")));
} }
if (parser.getValue("finish-cmd"))
{
finishCmd = parser.getValue("finish-cmd");
}
showVersion = parser.getFlag("version"); showVersion = parser.getFlag("version");
forceElevated = parser.getFlag("force-elevated"); forceElevated = parser.getFlag("force-elevated");

View File

@ -14,6 +14,7 @@ class UpdaterOptions
std::string installDir; std::string installDir;
std::string packageDir; std::string packageDir;
std::string scriptPath; std::string scriptPath;
std::string finishCmd;
PLATFORM_PID waitPid; PLATFORM_PID waitPid;
std::string logFile; std::string logFile;
bool showVersion; bool showVersion;

View File

@ -137,7 +137,8 @@ int main(int argc, char** argv)
+ ", package-dir: " + options.packageDir + ", package-dir: " + options.packageDir
+ ", wait-pid: " + intToStr(options.waitPid) + ", wait-pid: " + intToStr(options.waitPid)
+ ", script-path: " + options.scriptPath + ", script-path: " + options.scriptPath
+ ", mode: " + intToStr(options.mode)); + ", mode: " + intToStr(options.mode)
+ ", finish-cmd: " + options.finishCmd);
installer.setMode(options.mode); installer.setMode(options.mode);
installer.setInstallDir(options.installDir); installer.setInstallDir(options.installDir);
@ -146,6 +147,7 @@ int main(int argc, char** argv)
installer.setWaitPid(options.waitPid); installer.setWaitPid(options.waitPid);
installer.setForceElevated(options.forceElevated); installer.setForceElevated(options.forceElevated);
installer.setAutoClose(options.autoClose); installer.setAutoClose(options.autoClose);
installer.setFinishCmd(options.finishCmd);
if (options.mode == UpdateInstaller::Main) if (options.mode == UpdateInstaller::Main)
{ {

View File

@ -18,31 +18,10 @@ void TestUpdateScript::testV2Script()
TEST_COMPARE(newFormat.filesToUninstall(),oldFormat.filesToUninstall()); TEST_COMPARE(newFormat.filesToUninstall(),oldFormat.filesToUninstall());
} }
void TestUpdateScript::testPermissions()
{
UpdateScript script;
script.parse("file_list.xml");
for (std::vector<UpdateScriptFile>::const_iterator iter = script.filesToInstall().begin();
iter != script.filesToInstall().end();
iter++)
{
if (iter->isMainBinary)
{
TEST_COMPARE(iter->permissions,0755);
}
if (!iter->linkTarget.empty())
{
TEST_COMPARE(iter->permissions,0);
}
}
}
int main(int,char**) int main(int,char**)
{ {
TestList<TestUpdateScript> tests; TestList<TestUpdateScript> tests;
tests.addTest(&TestUpdateScript::testV2Script); tests.addTest(&TestUpdateScript::testV2Script);
tests.addTest(&TestUpdateScript::testPermissions);
return TestUtils::runTest(tests); return TestUtils::runTest(tests);
} }

View File

@ -4,6 +4,5 @@ class TestUpdateScript
{ {
public: public:
void testV2Script(); void testV2Script();
void testPermissions();
}; };