23 #include "updateinstaller.h"
25 #include "application.h"
26 #include "configuration/doomseekerconfig.h"
27 #include "datapaths.h"
30 #include "strings.hpp"
31 #include "updater/autoupdater.h"
36 #include <QTemporaryFile>
39 const QString UPDATER_EXECUTABLE_FILENAME =
"updater.exe";
41 const QString UPDATER_EXECUTABLE_FILENAME =
"updater";
44 UpdateInstaller::UpdateInstaller(QObject *pParent)
49 UpdateInstaller::~UpdateInstaller()
53 QString UpdateInstaller::copyUpdaterExecutableToTemporarySpace()
59 QCoreApplication::applicationDirPath(),
60 UPDATER_EXECUTABLE_FILENAME);
66 QString updaterCloneFilename = QString(
"%1-%2").arg(
67 DataPaths::UPDATE_PACKAGE_FILENAME_PREFIX).arg(
68 UPDATER_EXECUTABLE_FILENAME);
70 AutoUpdater::updateStorageDirPath(), updaterCloneFilename);
72 if (QFile::copy(updaterProgramPath, clonePath))
74 bool bPermissionsSet = QFile::setPermissions(clonePath,
75 QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner
76 | QFile::ExeGroup | QFile::ReadGroup | QFile::ReadOther);
82 gLog << tr(
"Failed to copy the updater executable to a temporary"
83 " space: \"%1\" -> \"%2\".").arg(updaterProgramPath, clonePath);
87 QString UpdateInstaller::errorCodeToStr(ErrorCode code)
94 return tr(
"Nothing to update.");
95 case EC_UpdatePackageMissing:
96 return tr(
"Update package or script are not found. Check log for details.");
97 case EC_ProcessStartFailure:
98 return tr(
"Failed to start updater process.");
100 return tr(
"Unknown error: %1.").arg(code);
104 QString UpdateInstaller::getPercentEncodedCurrentProcessArgs()
106 const QStringList &originalArgs = gApp->originalArgs();
107 QStringList argsEncoded;
108 for (
int i = 1; i < originalArgs.size(); ++i)
110 const QString &arg = originalArgs[i];
112 if (arg ==
"--update-failed")
117 argsEncoded << QUrl::toPercentEncoding(arg);
119 return argsEncoded.join(
" ");
122 QString UpdateInstaller::processErrorCodeToStr(ProcessErrorCode code)
128 case PEC_UnableToReadUpdateScript:
129 return tr(
"Unable to read the update script.");
130 case PEC_NoInstallationDirectorySpecified:
131 return tr(
"No installation directory specified.");
132 case PEC_UnableToDeterminePathOfUpdater:
133 return tr(
"Unable to determine the path of the updater.");
134 case PEC_GeneralFailure:
135 return tr(
"General failure.");
137 return tr(
"Unknown process error code: %1.").arg(code);
144 QFile fileScript(scriptPath);
145 if (fileScript.exists())
147 gLog << tr(
"Installing update.");
148 QString packagesDirPath = AutoUpdater::updateStorageDirPath();
149 QDir packagesDir(packagesDirPath);
152 bool isPackageOk = packagesDir.exists();
155 if (!startUpdaterProcess(packagesDirPath, scriptPath))
157 return EC_ProcessStartFailure;
162 gLog << tr(
"Package directory \"%1\" doesn't exist.").arg(packagesDirPath);
163 return EC_UpdatePackageMissing;
168 gLog << tr(
"Update was about to be installed but "
169 "update script \"%1\" is missing.").arg(scriptPath);
170 return EC_UpdatePackageMissing;
175 bool UpdateInstaller::startUpdaterProcess(
const QString &packagesDir,
176 const QString &scriptFilePath)
178 QString updaterProgramPath = copyUpdaterExecutableToTemporarySpace();
179 if (updaterProgramPath.isEmpty())
183 qDebug() <<
"Updater program is located at path: " << updaterProgramPath;
184 QFile updaterProgramFile(updaterProgramPath);
185 QFileInfo programFileInfo(QCoreApplication::applicationFilePath());
189 args <<
"--install-dir" << (QCoreApplication::applicationDirPath() +
"/../..");
191 args <<
"--install-dir" << QCoreApplication::applicationDirPath();
193 args <<
"--package-dir" << packagesDir;
194 args <<
"--script" << scriptFilePath;
195 args <<
"--log" << QDir::toNativeSeparators(AutoUpdater::updaterLogPath());
196 args <<
"--exec" << QDir::toNativeSeparators(programFileInfo.absoluteFilePath());
197 QString currentProcessArgs = getPercentEncodedCurrentProcessArgs();
198 if (!currentProcessArgs.isEmpty())
200 args <<
"--args" << currentProcessArgs;
202 bool bStarted = QProcess::startDetached(updaterProgramPath, args);
205 gLog << tr(
"Failed to start updater process: %1 %2")
206 .arg(updaterProgramPath).arg(args.join(
" "));