From 6aa9bd0f77dcb5128167fae62e32aa5252fe85c6 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Mon, 2 Dec 2013 00:55:24 +0100 Subject: Renew the updater branch Now with some actual consensus on what the updater will do! --- mmc_updater/src/UpdateInstaller.cpp | 439 ++++++++++++++++++++++++++++++++++++ 1 file changed, 439 insertions(+) create mode 100644 mmc_updater/src/UpdateInstaller.cpp (limited to 'mmc_updater/src/UpdateInstaller.cpp') diff --git a/mmc_updater/src/UpdateInstaller.cpp b/mmc_updater/src/UpdateInstaller.cpp new file mode 100644 index 00000000..23e1a4ca --- /dev/null +++ b/mmc_updater/src/UpdateInstaller.cpp @@ -0,0 +1,439 @@ +#include "UpdateInstaller.h" + +#include "AppInfo.h" +#include "FileUtils.h" +#include "Log.h" +#include "ProcessUtils.h" +#include "UpdateObserver.h" + +UpdateInstaller::UpdateInstaller() +: m_mode(Setup) +, m_waitPid(0) +, m_script(0) +, m_observer(0) +, m_forceElevated(false) +, m_autoClose(false) +{ +} + +void UpdateInstaller::setWaitPid(PLATFORM_PID pid) +{ + m_waitPid = pid; +} + +void UpdateInstaller::setInstallDir(const std::string& path) +{ + m_installDir = path; +} + +void UpdateInstaller::setPackageDir(const std::string& path) +{ + m_packageDir = path; +} + +void UpdateInstaller::setBackupDir(const std::string& path) +{ + m_backupDir = path; +} + +void UpdateInstaller::setMode(Mode mode) +{ + m_mode = mode; +} + +void UpdateInstaller::setScript(UpdateScript* script) +{ + m_script = script; +} + +void UpdateInstaller::setForceElevated(bool elevated) +{ + m_forceElevated = elevated; +} + +std::list UpdateInstaller::updaterArgs() const +{ + std::list args; + args.push_back("--install-dir"); + args.push_back(m_installDir); + args.push_back("--package-dir"); + args.push_back(m_packageDir); + args.push_back("--script"); + args.push_back(m_script->path()); + if (m_autoClose) + { + args.push_back("--auto-close"); + } + return args; +} + +void UpdateInstaller::reportError(const std::string& error) +{ + if (m_observer) + { + m_observer->updateError(error); + m_observer->updateFinished(); + } +} + +std::string UpdateInstaller::friendlyErrorForError(const FileUtils::IOException& exception) const +{ + std::string friendlyError; + + switch (exception.type()) + { + case FileUtils::IOException::ReadOnlyFileSystem: +#ifdef PLATFORM_MAC + friendlyError = AppInfo::appName() + " was started from a read-only location. " + "Copy it to the Applications folder on your Mac and run " + "it from there."; +#else + friendlyError = AppInfo::appName() + " was started from a read-only location. " + "Re-install it to a location that can be updated and run it from there."; +#endif + break; + case FileUtils::IOException::DiskFull: + friendlyError = "The disk is full. Please free up some space and try again."; + break; + default: + break; + } + + return friendlyError; +} + +void UpdateInstaller::run() throw () +{ + if (!m_script || !m_script->isValid()) + { + reportError("Unable to read update script"); + return; + } + if (m_installDir.empty()) + { + reportError("No installation directory specified"); + return; + } + + std::string updaterPath; + try + { + updaterPath = ProcessUtils::currentProcessPath(); + } + catch (const FileUtils::IOException&) + { + LOG(Error,"error reading process path with mode " + intToStr(m_mode)); + reportError("Unable to determine path of updater"); + return; + } + + if (m_mode == Setup) + { + if (m_waitPid != 0) + { + LOG(Info,"Waiting for main app process to finish"); + ProcessUtils::waitForProcess(m_waitPid); + } + + std::list args = updaterArgs(); + args.push_back("--mode"); + args.push_back("main"); + args.push_back("--wait"); + args.push_back(intToStr(ProcessUtils::currentProcessId())); + + int installStatus = 0; + if (m_forceElevated || !checkAccess()) + { + LOG(Info,"Insufficient rights to install app to " + m_installDir + " requesting elevation"); + + // start a copy of the updater with admin rights + installStatus = ProcessUtils::runElevated(updaterPath,args,AppInfo::name()); + } + else + { + LOG(Info,"Sufficient rights to install app - restarting with same permissions"); + installStatus = ProcessUtils::runSync(updaterPath,args); + } + + if (installStatus == 0) + { + LOG(Info,"Update install completed"); + } + else + { + LOG(Error,"Update install failed with status " + intToStr(installStatus)); + } + + // restart the main application - this is currently done + // regardless of whether the installation succeeds or not + restartMainApp(); + + // clean up files created by the updater + cleanup(); + } + else if (m_mode == Main) + { + LOG(Info,"Starting update installation"); + + // the detailed error string returned by the OS + std::string error; + // the message to present to the user. This may be the same + // as 'error' or may be different if a more helpful suggestion + // can be made for a particular problem + std::string friendlyError; + + try + { + LOG(Info,"Installing new and updated files"); + installFiles(); + + LOG(Info,"Uninstalling removed files"); + uninstallFiles(); + + LOG(Info,"Removing backups"); + removeBackups(); + + postInstallUpdate(); + } + catch (const FileUtils::IOException& exception) + { + error = exception.what(); + friendlyError = friendlyErrorForError(exception); + } + catch (const std::string& genericError) + { + error = genericError; + } + + if (!error.empty()) + { + LOG(Error,std::string("Error installing update ") + error); + + try + { + revert(); + } + catch (const FileUtils::IOException& exception) + { + LOG(Error,"Error reverting partial update " + std::string(exception.what())); + } + + if (m_observer) + { + if (friendlyError.empty()) + { + friendlyError = error; + } + m_observer->updateError(friendlyError); + } + } + + if (m_observer) + { + m_observer->updateFinished(); + } + } +} + +void UpdateInstaller::cleanup() +{ + try + { + FileUtils::rmdirRecursive(m_packageDir.c_str()); + } + catch (const FileUtils::IOException& ex) + { + LOG(Error,"Error cleaning up updater " + std::string(ex.what())); + } + LOG(Info,"Updater files removed"); +} + +void UpdateInstaller::revert() +{ + std::map::const_iterator iter = m_backups.begin(); + for (;iter != m_backups.end();iter++) + { + const std::string& installedFile = iter->first; + const std::string& backupFile = iter->second; + + if (FileUtils::fileExists(installedFile.c_str())) + { + FileUtils::removeFile(installedFile.c_str()); + } + FileUtils::moveFile(backupFile.c_str(),installedFile.c_str()); + } +} + +void UpdateInstaller::installFile(const UpdateScriptFile& file) +{ + std::string destPath = m_installDir + '/' + file.path; + std::string target = file.linkTarget; + + // backup the existing file if any + backupFile(destPath); + + // create the target directory if it does not exist + std::string destDir = FileUtils::dirname(destPath.c_str()); + if (!FileUtils::fileExists(destDir.c_str())) + { + FileUtils::mkpath(destDir.c_str()); + } + + if (target.empty()) + { + std::string sourceFile = m_packageDir + '/' + FileUtils::fileName(file.path.c_str()); + if (!FileUtils::fileExists(sourceFile.c_str())) + { + throw "Source file does not exist: " + sourceFile; + } + FileUtils::copyFile(sourceFile.c_str(),destPath.c_str()); + + // set the permissions on the newly extracted file + FileUtils::chmod(destPath.c_str(),file.permissions); + } + else + { + // create the symlink + FileUtils::createSymLink(destPath.c_str(),target.c_str()); + } +} + +void UpdateInstaller::installFiles() +{ + std::vector::const_iterator iter = m_script->filesToInstall().begin(); + int filesInstalled = 0; + for (;iter != m_script->filesToInstall().end();iter++) + { + installFile(*iter); + ++filesInstalled; + if (m_observer) + { + int toInstallCount = static_cast(m_script->filesToInstall().size()); + double percentage = ((1.0 * filesInstalled) / toInstallCount) * 100.0; + m_observer->updateProgress(static_cast(percentage)); + } + } +} + +void UpdateInstaller::uninstallFiles() +{ + std::vector::const_iterator iter = m_script->filesToUninstall().begin(); + for (;iter != m_script->filesToUninstall().end();iter++) + { + std::string path = m_installDir + '/' + iter->c_str(); + if (FileUtils::fileExists(path.c_str())) + { + FileUtils::removeFile(path.c_str()); + } + else + { + LOG(Warn,"Unable to uninstall file " + path + " because it does not exist."); + } + } +} + +void UpdateInstaller::backupFile(const std::string& path) +{ + if (!FileUtils::fileExists(path.c_str())) + { + // no existing file to backup + return; + } + + std::string backupPath = path + ".bak"; + FileUtils::removeFile(backupPath.c_str()); + FileUtils::moveFile(path.c_str(), backupPath.c_str()); + m_backups[path] = backupPath; +} + +void UpdateInstaller::removeBackups() +{ + std::map::const_iterator iter = m_backups.begin(); + for (;iter != m_backups.end();iter++) + { + const std::string& backupFile = iter->second; + FileUtils::removeFile(backupFile.c_str()); + } +} + +bool UpdateInstaller::checkAccess() +{ + std::string testFile = m_installDir + "/update-installer-test-file"; + + try + { + FileUtils::removeFile(testFile.c_str()); + } + catch (const FileUtils::IOException& error) + { + LOG(Info,"Removing existing access check file failed " + std::string(error.what())); + } + + try + { + FileUtils::touch(testFile.c_str()); + FileUtils::removeFile(testFile.c_str()); + return true; + } + catch (const FileUtils::IOException& error) + { + LOG(Info,"checkAccess() failed " + std::string(error.what())); + return false; + } +} + +void UpdateInstaller::setObserver(UpdateObserver* observer) +{ + m_observer = observer; +} + +void UpdateInstaller::restartMainApp() +{ + try + { + std::string command; + std::list args; + + for (std::vector::const_iterator iter = m_script->filesToInstall().begin(); + iter != m_script->filesToInstall().end(); + iter++) + { + if (iter->isMainBinary) + { + command = m_installDir + '/' + iter->path; + } + } + + if (!command.empty()) + { + LOG(Info,"Starting main application " + command); + ProcessUtils::runAsync(command,args); + } + else + { + LOG(Error,"No main binary specified in update script"); + } + } + catch (const std::exception& ex) + { + LOG(Error,"Unable to restart main app " + std::string(ex.what())); + } +} + +void UpdateInstaller::postInstallUpdate() +{ + // perform post-install actions + +#ifdef PLATFORM_MAC + // touch the application's bundle directory so that + // OS X' Launch Services notices any changes in the application's + // Info.plist file. + FileUtils::touch(m_installDir.c_str()); +#endif +} + +void UpdateInstaller::setAutoClose(bool autoClose) +{ + m_autoClose = autoClose; +} + -- cgit From e90f1a27569ac6b9e9782646c9de92fc9534b1d2 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 5 Dec 2013 20:32:12 -0600 Subject: Implement update installer --- logic/net/FileDownload.cpp | 9 ++++ logic/updater/DownloadUpdateTask.cpp | 79 ++++++++++++++++++++++++++++-- logic/updater/DownloadUpdateTask.h | 17 ++++--- mmc_updater/src/UpdateInstaller.cpp | 43 ++++++---------- mmc_updater/src/UpdateInstaller.h | 2 + mmc_updater/src/UpdateScript.cpp | 9 ++-- mmc_updater/src/UpdateScript.h | 15 +++--- mmc_updater/src/UpdaterOptions.cpp | 5 ++ mmc_updater/src/UpdaterOptions.h | 1 + mmc_updater/src/main.cpp | 4 +- mmc_updater/src/tests/TestUpdateScript.cpp | 21 -------- mmc_updater/src/tests/TestUpdateScript.h | 1 - 12 files changed, 133 insertions(+), 73 deletions(-) (limited to 'mmc_updater/src/UpdateInstaller.cpp') diff --git a/logic/net/FileDownload.cpp b/logic/net/FileDownload.cpp index 239af351..38f0b9c2 100644 --- a/logic/net/FileDownload.cpp +++ b/logic/net/FileDownload.cpp @@ -63,6 +63,15 @@ void FileDownload::start() request.setRawHeader(QString("If-None-Match").toLatin1(), m_expected_md5.toLatin1()); 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(); QNetworkReply *rep = worker->get(request); diff --git a/logic/updater/DownloadUpdateTask.cpp b/logic/updater/DownloadUpdateTask.cpp index fc4d321f..7c5eb84a 100644 --- a/logic/updater/DownloadUpdateTask.cpp +++ b/logic/updater/DownloadUpdateTask.cpp @@ -24,6 +24,8 @@ #include #include +#include + 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/- 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() diff --git a/logic/updater/DownloadUpdateTask.h b/logic/updater/DownloadUpdateTask.h index 6fb745ab..dc30ca15 100644 --- a/logic/updater/DownloadUpdateTask.h +++ b/logic/updater/DownloadUpdateTask.h @@ -57,7 +57,7 @@ protected: struct VersionFileEntry { QString path; - bool isExecutable; + int mode; FileSourceList sources; QString md5; }; @@ -70,9 +70,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 +84,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 +145,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; diff --git a/mmc_updater/src/UpdateInstaller.cpp b/mmc_updater/src/UpdateInstaller.cpp index 23e1a4ca..3ddc1ec0 100644 --- a/mmc_updater/src/UpdateInstaller.cpp +++ b/mmc_updater/src/UpdateInstaller.cpp @@ -51,6 +51,11 @@ void UpdateInstaller::setForceElevated(bool elevated) m_forceElevated = elevated; } +void UpdateInstaller::setFinishCmd(const std::string& cmd) +{ + m_finishCmd = cmd; +} + std::list UpdateInstaller::updaterArgs() const { std::list args; @@ -266,7 +271,7 @@ void UpdateInstaller::revert() void UpdateInstaller::installFile(const UpdateScriptFile& file) { - std::string destPath = m_installDir + '/' + file.path; + std::string destPath = file.dest; std::string target = file.linkTarget; // backup the existing file if any @@ -279,23 +284,15 @@ void UpdateInstaller::installFile(const UpdateScriptFile& file) FileUtils::mkpath(destDir.c_str()); } - if (target.empty()) - { - std::string sourceFile = m_packageDir + '/' + FileUtils::fileName(file.path.c_str()); - if (!FileUtils::fileExists(sourceFile.c_str())) - { - throw "Source file does not exist: " + sourceFile; - } - FileUtils::copyFile(sourceFile.c_str(),destPath.c_str()); - - // set the permissions on the newly extracted file - FileUtils::chmod(destPath.c_str(),file.permissions); - } - else + std::string sourceFile = file.source; + if (!FileUtils::fileExists(sourceFile.c_str())) { - // create the symlink - FileUtils::createSymLink(destPath.c_str(),target.c_str()); + throw "Source file does not exist: " + sourceFile; } + FileUtils::copyFile(sourceFile.c_str(),destPath.c_str()); + + // set the permissions on the newly extracted file + FileUtils::chmod(destPath.c_str(),file.permissions); } void UpdateInstaller::installFiles() @@ -391,19 +388,9 @@ void UpdateInstaller::restartMainApp() { try { - std::string command; + std::string command = m_installDir + '/' + m_finishCmd; std::list args; - for (std::vector::const_iterator iter = m_script->filesToInstall().begin(); - iter != m_script->filesToInstall().end(); - iter++) - { - if (iter->isMainBinary) - { - command = m_installDir + '/' + iter->path; - } - } - if (!command.empty()) { LOG(Info,"Starting main application " + command); @@ -411,7 +398,7 @@ void UpdateInstaller::restartMainApp() } else { - LOG(Error,"No main binary specified in update script"); + LOG(Error,"No main binary specified"); } } catch (const std::exception& ex) diff --git a/mmc_updater/src/UpdateInstaller.h b/mmc_updater/src/UpdateInstaller.h index 5dfa263e..1eca0bc7 100644 --- a/mmc_updater/src/UpdateInstaller.h +++ b/mmc_updater/src/UpdateInstaller.h @@ -33,6 +33,7 @@ class UpdateInstaller void setWaitPid(PLATFORM_PID pid); void setForceElevated(bool elevated); void setAutoClose(bool autoClose); + void setFinishCmd(const std::string& cmd); void setObserver(UpdateObserver* observer); @@ -60,6 +61,7 @@ class UpdateInstaller std::string m_installDir; std::string m_packageDir; std::string m_backupDir; + std::string m_finishCmd; PLATFORM_PID m_waitPid; UpdateScript* m_script; UpdateObserver* m_observer; diff --git a/mmc_updater/src/UpdateScript.cpp b/mmc_updater/src/UpdateScript.cpp index 606163dd..849a2217 100644 --- a/mmc_updater/src/UpdateScript.cpp +++ b/mmc_updater/src/UpdateScript.cpp @@ -71,13 +71,14 @@ void UpdateScript::parseUpdate(const TiXmlElement* updateNode) UpdateScriptFile UpdateScript::parseFile(const TiXmlElement* element) { 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); - file.linkTarget = elementText(element->FirstChildElement("target")); - file.isMainBinary = strToBool(elementText(element->FirstChildElement("is-main-binary"))); return file; } diff --git a/mmc_updater/src/UpdateScript.h b/mmc_updater/src/UpdateScript.h index fec463f3..c825e35d 100644 --- a/mmc_updater/src/UpdateScript.h +++ b/mmc_updater/src/UpdateScript.h @@ -35,10 +35,12 @@ class UpdateScriptFile public: UpdateScriptFile() : 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; /** The permissions for this file, specified @@ -46,14 +48,11 @@ class UpdateScriptFile */ int permissions; - bool isMainBinary; - bool operator==(const UpdateScriptFile& other) const { - return path == other.path && - permissions == other.permissions && - linkTarget == other.linkTarget && - isMainBinary == other.isMainBinary; + return source == other.source && + dest == other.dest && + permissions == other.permissions; } }; diff --git a/mmc_updater/src/UpdaterOptions.cpp b/mmc_updater/src/UpdaterOptions.cpp index 1ea820d2..c8f76f6e 100644 --- a/mmc_updater/src/UpdaterOptions.cpp +++ b/mmc_updater/src/UpdaterOptions.cpp @@ -111,6 +111,7 @@ void UpdaterOptions::parse(int argc, char** argv) AnyOption parser; parser.setOption("install-dir"); parser.setOption("package-dir"); + parser.setOption("finish-cmd"); parser.setOption("script"); parser.setOption("wait"); parser.setOption("mode"); @@ -140,6 +141,10 @@ void UpdaterOptions::parse(int argc, char** argv) { waitPid = static_cast(atoll(parser.getValue("wait"))); } + if (parser.getValue("finish-cmd")) + { + finishCmd = parser.getValue("finish-cmd"); + } showVersion = parser.getFlag("version"); forceElevated = parser.getFlag("force-elevated"); diff --git a/mmc_updater/src/UpdaterOptions.h b/mmc_updater/src/UpdaterOptions.h index a8496d9f..b4473a82 100644 --- a/mmc_updater/src/UpdaterOptions.h +++ b/mmc_updater/src/UpdaterOptions.h @@ -14,6 +14,7 @@ class UpdaterOptions std::string installDir; std::string packageDir; std::string scriptPath; + std::string finishCmd; PLATFORM_PID waitPid; std::string logFile; bool showVersion; diff --git a/mmc_updater/src/main.cpp b/mmc_updater/src/main.cpp index e23cf16d..fb072ab5 100644 --- a/mmc_updater/src/main.cpp +++ b/mmc_updater/src/main.cpp @@ -137,7 +137,8 @@ int main(int argc, char** argv) + ", package-dir: " + options.packageDir + ", wait-pid: " + intToStr(options.waitPid) + ", script-path: " + options.scriptPath - + ", mode: " + intToStr(options.mode)); + + ", mode: " + intToStr(options.mode) + + ", finish-cmd: " + options.finishCmd); installer.setMode(options.mode); installer.setInstallDir(options.installDir); @@ -146,6 +147,7 @@ int main(int argc, char** argv) installer.setWaitPid(options.waitPid); installer.setForceElevated(options.forceElevated); installer.setAutoClose(options.autoClose); + installer.setFinishCmd(options.finishCmd); if (options.mode == UpdateInstaller::Main) { diff --git a/mmc_updater/src/tests/TestUpdateScript.cpp b/mmc_updater/src/tests/TestUpdateScript.cpp index 9e8c1392..30a7572a 100644 --- a/mmc_updater/src/tests/TestUpdateScript.cpp +++ b/mmc_updater/src/tests/TestUpdateScript.cpp @@ -18,31 +18,10 @@ void TestUpdateScript::testV2Script() TEST_COMPARE(newFormat.filesToUninstall(),oldFormat.filesToUninstall()); } -void TestUpdateScript::testPermissions() -{ - UpdateScript script; - script.parse("file_list.xml"); - - for (std::vector::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**) { TestList tests; tests.addTest(&TestUpdateScript::testV2Script); - tests.addTest(&TestUpdateScript::testPermissions); return TestUtils::runTest(tests); } diff --git a/mmc_updater/src/tests/TestUpdateScript.h b/mmc_updater/src/tests/TestUpdateScript.h index fd0994fe..513513d5 100644 --- a/mmc_updater/src/tests/TestUpdateScript.h +++ b/mmc_updater/src/tests/TestUpdateScript.h @@ -4,6 +4,5 @@ class TestUpdateScript { public: void testV2Script(); - void testPermissions(); }; -- cgit From 6ac94ddcb6f64ffae3948bed778bccc33a92f0fd Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 6 Dec 2013 12:59:58 -0600 Subject: Finish implementing update installation. Also add the option to update on exit. --- MultiMC.cpp | 69 +++++++++++++++++++++++++++++++++++- MultiMC.h | 18 ++++++++++ gui/MainWindow.cpp | 27 +++++++++----- gui/MainWindow.h | 5 +++ gui/dialogs/UpdateDialog.ui | 7 ++++ logic/updater/DownloadUpdateTask.cpp | 5 +++ logic/updater/DownloadUpdateTask.h | 5 +++ mmc_updater/src/UpdateInstaller.cpp | 2 +- 8 files changed, 128 insertions(+), 10 deletions(-) (limited to 'mmc_updater/src/UpdateInstaller.cpp') diff --git a/MultiMC.cpp b/MultiMC.cpp index 128e71f3..e3107ac4 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -2,10 +2,12 @@ #include "MultiMC.h" #include #include +#include #include #include #include #include +#include #include "gui/MainWindow.h" #include "gui/dialogs/VersionSelectDialog.h" @@ -409,6 +411,65 @@ std::shared_ptr MultiMC::javalist() return m_javalist; } +#ifdef WINDOWS +#define UPDATER_BIN "updater.exe" +#elif LINUX +#define UPDATER_BIN "updater" +#elif OSX +#define UPDATER_BIN "updater" +#else +#error Unsupported operating system. +#endif + +void MultiMC::installUpdates(const QString& updateFilesDir, bool restartOnFinish) +{ + QLOG_INFO() << "Installing updates."; +#if LINUX + // On Linux, the MultiMC executable file is actually in the bin folder inside the installation directory. + // This means that MultiMC's *actual* install path is the parent folder. + // We need to tell the updater to run with this directory as the install path, rather than the bin folder where the executable is. + // On other operating systems, we'll just use the path to the executable. + QString appDir = QFileInfo(MMC->applicationDirPath()).dir().path(); + + // On Linux, we also need to set the finish command to the launch script, rather than the binary. + QString finishCmd = PathCombine(appDir, "MultiMC"); +#else + QString appDir = MMC->applicationDirPath(); + QString finishCmd = MMC->applicationFilePath(); +#endif + + // Build the command we'll use to run the updater. + // Note, the above comment about the app dir path on Linux is irrelevant here because the updater binary is always in the + // same folder as the main binary. + QString updaterBinary = PathCombine(MMC->applicationDirPath(), UPDATER_BIN); + QStringList args; + // ./updater --install-dir $INSTALL_DIR --package-dir $UPDATEFILES_DIR --script $UPDATEFILES_DIR/file_list.xml --wait $PID --mode main + args << "--install-dir" << appDir; + args << "--package-dir" << updateFilesDir; + args << "--script" << PathCombine(updateFilesDir, "file_list.xml"); + args << "--wait" << QString::number(MMC->applicationPid()); + + if (restartOnFinish) + args << "--finish-cmd" << finishCmd; + + QLOG_INFO() << "Running updater with command" << updaterBinary << args.join(" "); + + QProcess::startDetached(updaterBinary, args); + + // Now that we've started the updater, quit MultiMC. + MMC->quit(); +} + +void MultiMC::setUpdateOnExit(const QString& updateFilesDir) +{ + m_updateOnExitPath = updateFilesDir; +} + +QString MultiMC::getExitUpdatePath() const +{ + return m_updateOnExitPath; +} + int main_gui(MultiMC &app) { // show main window @@ -417,7 +478,13 @@ int main_gui(MultiMC &app) mainWin.restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("MainWindowGeometry").toByteArray())); mainWin.show(); mainWin.checkSetDefaultJava(); - return app.exec(); + auto exitCode = app.exec(); + + // Update if necessary. + if (!app.getExitUpdatePath().isEmpty()) + app.installUpdates(app.getExitUpdatePath(), false); + + return exitCode; } int main(int argc, char *argv[]) diff --git a/MultiMC.h b/MultiMC.h index 659104ba..22cea029 100644 --- a/MultiMC.h +++ b/MultiMC.h @@ -98,6 +98,22 @@ public: std::shared_ptr javalist(); + /*! + * Installs update from the given update files directory. + */ + void installUpdates(const QString& updateFilesDir, bool restartOnFinish=false); + + /*! + * Sets MultiMC to install updates from the given directory when it exits. + */ + void setUpdateOnExit(const QString& updateFilesDir); + + /*! + * Gets the path to install updates from on exit. + * If this is an empty string, no updates should be installed on exit. + */ + QString getExitUpdatePath() const; + private: void initLogger(); @@ -124,6 +140,8 @@ private: QsLogging::DestinationPtr m_fileDestination; QsLogging::DestinationPtr m_debugDestination; + QString m_updateOnExitPath; + Status m_status = MultiMC::Failed; MultiMCVersion m_version; }; diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 618884ef..7ea5c291 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -439,20 +439,31 @@ void MainWindow::updateAvailable(QString repo, QString versionName, int versionI QLOG_INFO() << "Update will be installed later."; break; case UPDATE_NOW: - { - QLOG_INFO() << "Installing update."; - ProgressDialog updateDlg(this); - DownloadUpdateTask updateTask(repo, versionId, &updateDlg); - updateDlg.exec(&updateTask); - } + downloadUpdates(repo, versionId); break; case UPDATE_ONEXIT: - // TODO: Implement installing updates on exit. - QLOG_INFO() << "Installing on exit is not implemented yet."; + downloadUpdates(repo, versionId, true); break; } } +void MainWindow::downloadUpdates(QString repo, int versionId, bool installOnExit) +{ + QLOG_INFO() << "Downloading updates."; + // TODO: If the user chooses to update on exit, we should download updates in the background. + // Doing so is a bit complicated, because we'd have to make sure it finished downloading before actually exiting MultiMC. + ProgressDialog updateDlg(this); + DownloadUpdateTask updateTask(repo, versionId, &updateDlg); + // If the task succeeds, install the updates. + if (updateDlg.exec(&updateTask)) + { + if (installOnExit) + MMC->setUpdateOnExit(updateTask.updateFilesDir()); + else + MMC->installUpdates(updateTask.updateFilesDir()); + } +} + void MainWindow::onCatToggled(bool state) { setCatBackground(state); diff --git a/gui/MainWindow.h b/gui/MainWindow.h index 1f498eca..b99c54ee 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -168,6 +168,11 @@ slots: void changeActiveAccount(); void repopulateAccountsMenu(); + + /*! + * Runs the DownloadUpdateTask and installs updates. + */ + void downloadUpdates(QString repo, int versionId, bool installOnExit=false); protected: bool eventFilter(QObject *obj, QEvent *ev); diff --git a/gui/dialogs/UpdateDialog.ui b/gui/dialogs/UpdateDialog.ui index f2361bd3..1fe65e62 100644 --- a/gui/dialogs/UpdateDialog.ui +++ b/gui/dialogs/UpdateDialog.ui @@ -41,6 +41,13 @@ + + + + Update after MultiMC closes + + + diff --git a/logic/updater/DownloadUpdateTask.cpp b/logic/updater/DownloadUpdateTask.cpp index 7c5eb84a..ef975c93 100644 --- a/logic/updater/DownloadUpdateTask.cpp +++ b/logic/updater/DownloadUpdateTask.cpp @@ -391,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 dc30ca15..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... diff --git a/mmc_updater/src/UpdateInstaller.cpp b/mmc_updater/src/UpdateInstaller.cpp index 3ddc1ec0..ced6ff39 100644 --- a/mmc_updater/src/UpdateInstaller.cpp +++ b/mmc_updater/src/UpdateInstaller.cpp @@ -388,7 +388,7 @@ void UpdateInstaller::restartMainApp() { try { - std::string command = m_installDir + '/' + m_finishCmd; + std::string command = m_finishCmd; std::list args; if (!command.empty()) -- cgit