UpdateManager.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "QsLog.h"
  2. #include <QDir>
  3. #include <QStandardPaths>
  4. #include <QCoreApplication>
  5. #include <QProcess>
  6. #include "UpdateManager.h"
  7. #include "utils/Utils.h"
  8. #include "utils/HelperLauncher.h"
  9. #include "system/SystemComponent.h"
  10. #ifdef KONVERGO_OPENELEC
  11. #include "OEUpdateManager.h"
  12. #endif
  13. #ifdef Q_OS_WIN
  14. #include "UpdateManagerWin32.h"
  15. #endif
  16. UpdateManager* g_updateManager;
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////
  18. UpdateManager* UpdateManager::Get()
  19. {
  20. if (!g_updateManager)
  21. {
  22. #if defined(KONVERGO_OPENELEC)
  23. g_updateManager = new OEUpdateManager(nullptr);
  24. #elif defined(Q_OS_WIN)
  25. g_updateManager = new UpdateManagerWin32(nullptr);
  26. #else
  27. g_updateManager = new UpdateManager(nullptr);
  28. #endif
  29. }
  30. return g_updateManager;
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////////////////////////
  33. QString UpdateManager::HaveUpdate()
  34. {
  35. QDir updateDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/updates/");
  36. if (!updateDir.exists())
  37. {
  38. QLOG_DEBUG() << "No Update directory found, exiting";
  39. return "";
  40. }
  41. QStringList nonAppliedUpdates;
  42. // sort update directories, sort by the newest directory first, that way
  43. // we apply the latest update downloaded.
  44. //
  45. foreach (const QString& dir, updateDir.entryList(QDir::NoDotAndDotDot | QDir::Dirs, QDir::Time))
  46. {
  47. // check if this version has been applied
  48. QString readyFile(GetPath("_readyToApply", dir, false));
  49. QLOG_DEBUG() << "Checking for:" << readyFile;
  50. QDir packageDir(GetPath("packages", dir, false));
  51. if (QFile::exists(readyFile))
  52. {
  53. QLOG_DEBUG() << dir << "is not applied";
  54. return dir;
  55. }
  56. else if (packageDir.exists())
  57. {
  58. QLOG_DEBUG() << "Removing old update packages in dir:" << dir;
  59. if (!packageDir.removeRecursively())
  60. QLOG_WARN() << "Failed to remove old update packages in dir:" << dir;
  61. }
  62. }
  63. QLOG_DEBUG() << "No valid/applicable update found.";
  64. return "";
  65. }
  66. ///////////////////////////////////////////////////////////////////////////////////////////////////
  67. bool UpdateManager::applyUpdate(const QString& version)
  68. {
  69. // make sure we have a manifest for this version.
  70. QLOG_DEBUG() << "Applying Update to version" << version;
  71. QString manifestPath = GetPath("manifest.xml.bz2", version, false);
  72. QString packagePath = QFileInfo(manifestPath).dir().absolutePath() + "/packages";
  73. if (!QFile(manifestPath).exists() || !QDir(packagePath).exists())
  74. {
  75. QLOG_ERROR() << "Could not find:" << manifestPath << "or" << packagePath;
  76. return false;
  77. }
  78. QString updaterName("updater");
  79. #ifdef Q_OS_WIN
  80. updaterName = "updater.exe";
  81. #endif
  82. QFile updaterFile(Paths::resourceDir(updaterName));
  83. if (!updaterFile.exists())
  84. {
  85. QLOG_ERROR() << "Missing updater:" << updaterFile.fileName();
  86. return false;
  87. }
  88. // copy the updater to a temporary directory so that we don't overwrite it.
  89. QString updaterPath = QDir::temp().absoluteFilePath(updaterName);
  90. // we need to remove it first, since QFile::copy() will fail otherwise.
  91. if (QFile::exists(updaterPath))
  92. {
  93. if (!QFile::remove(updaterPath))
  94. {
  95. QLOG_DEBUG() << "Failed to remove updater from " << updaterPath;
  96. }
  97. }
  98. if (!QFile::copy(updaterFile.fileName(), updaterPath))
  99. {
  100. QLOG_ERROR() << "Failed to copy the updater to:" << updaterPath;
  101. return false;
  102. }
  103. QStringList args;
  104. args << "--script=" + manifestPath;
  105. args << "--package-dir=" + packagePath;
  106. args << "--auto-close";
  107. #ifdef Q_OS_MAC
  108. args << "--install-dir=" + QDir(QCoreApplication::applicationDirPath() + "/../../").absolutePath();
  109. #else
  110. args << "--install-dir=" + QDir(QCoreApplication::applicationDirPath()).absolutePath();
  111. #endif
  112. args << "--wait=" + QString::number(QCoreApplication::applicationPid());
  113. // beyond this point we don't want to try to install this update again.
  114. QFile::remove(GetPath("_readyToApply", version, false));
  115. QLOG_DEBUG() << "Executing:" << updaterPath << args;
  116. QProcess* process = new QProcess(NULL);
  117. if (process->startDetached(updaterPath, args, QDir::temp().absolutePath()))
  118. {
  119. QLOG_DEBUG() << "Updater running, shutting down Plex Media Player";
  120. return true;
  121. }
  122. QLOG_ERROR() << "Failed to execute the updater";
  123. return false;
  124. }
  125. ///////////////////////////////////////////////////////////////////////////////////////////////////
  126. QString UpdateManager::GetPath(const QString& file, const QString& version, bool package)
  127. {
  128. QString filePath(file);
  129. // put the packages in the packages subdir.
  130. if (package)
  131. filePath = "packages/" + file;
  132. return QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/updates/" + version + "/" + filePath;
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////
  135. void UpdateManager::doUpdate(const QString& version)
  136. {
  137. QLOG_DEBUG() << "Update competed, restarting system";
  138. if (!applyUpdate(version))
  139. {
  140. QLOG_ERROR() "Failed to apply update";
  141. }
  142. else
  143. {
  144. QLOG_DEBUG() "Update was applied successfully";
  145. }
  146. HelperLauncher::Get().stop();
  147. SystemComponent::Get().exit();
  148. }
  149. /////////////////////////////////////////////////////////////////////////////////////////
  150. bool UpdateManager::CheckForUpdates()
  151. {
  152. QString updateVersion = Get()->HaveUpdate();
  153. if (!updateVersion.isEmpty())
  154. {
  155. QLOG_DEBUG() << "We want to apply update:" << updateVersion;
  156. // if this call succeeds we need to shut down and let
  157. // the updater do it's work. Otherwise we'll just
  158. // start the application as normal and pretend that
  159. // nothing happened
  160. //
  161. if (Get()->applyUpdate(updateVersion))
  162. return true;
  163. }
  164. return false;
  165. }