diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2016-08-24 23:34:42 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2017-05-19 03:43:19 +0200 |
commit | 9c3b05f2d571b58ee2322a942162ecec654544dc (patch) | |
tree | 33f791c61e708bef41a4ddc1716e793d16a564d4 /desktop | |
parent | ed7360235f039b1df17ea743058e59d6c4ee947a (diff) |
improve update checker and update downloader code
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/Library_sofficeapp.mk | 4 | ||||
-rw-r--r-- | desktop/inc/app.hxx | 3 | ||||
-rw-r--r-- | desktop/source/app/app.cxx | 38 | ||||
-rw-r--r-- | desktop/source/app/updater.cxx | 128 | ||||
-rw-r--r-- | desktop/source/app/updater.hxx | 2 |
5 files changed, 150 insertions, 25 deletions
diff --git a/desktop/Library_sofficeapp.mk b/desktop/Library_sofficeapp.mk index 90faf8516b51..aabe8705fae4 100644 --- a/desktop/Library_sofficeapp.mk +++ b/desktop/Library_sofficeapp.mk @@ -31,6 +31,7 @@ $(eval $(call gb_Library_use_externals,sofficeapp, \ icu_headers \ icui18n \ icuuc \ + curl \ )) $(eval $(call gb_Library_use_custom_headers,sofficeapp,\ @@ -106,7 +107,8 @@ $(eval $(call gb_Library_add_exception_objects,sofficeapp,\ desktop/source/app/officeipcthread \ desktop/source/app/opencl \ desktop/source/app/sofficemain \ - desktop/source/app/updater \ + $(if $(ENABLE_ONLINE_UPDATE_MAR),\ + desktop/source/app/updater )\ desktop/source/app/userinstall \ desktop/source/migration/migration \ )) diff --git a/desktop/inc/app.hxx b/desktop/inc/app.hxx index 91dde49439ab..56ec44ac89db 100644 --- a/desktop/inc/app.hxx +++ b/desktop/inc/app.hxx @@ -31,7 +31,9 @@ #include <com/sun/star/task/XStatusIndicator.hpp> #include <com/sun/star/uno/Reference.h> #include <osl/mutex.hxx> + #include <memory> +#include <thread> namespace com { namespace sun { namespace star { namespace uno { class XComponentContext; @@ -174,6 +176,7 @@ class Desktop : public Application std::unique_ptr<Lockfile> m_xLockfile; Timer m_firstRunTimer; + std::thread m_aUpdateThread; static ResMgr* pResMgr; }; diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx index 7b797d077755..8ca66903b9ce 100644 --- a/desktop/source/app/app.cxx +++ b/desktop/source/app/app.cxx @@ -38,7 +38,9 @@ #include "userinstall.hxx" #include "desktopcontext.hxx" #include "migration.hxx" +#if HAVE_FEATURE_UPDATE_MAR #include "updater.hxx" +#endif #include <o3tl/runtimetooustring.hxx> #include <svl/languageoptions.hxx> @@ -1234,6 +1236,11 @@ void restartOnMac(bool passArguments) { #endif } +bool isTimeForUpdateCheck() +{ + return true; +} + } void Desktop::Exception(ExceptionCategory nCategory) @@ -1452,22 +1459,35 @@ int Desktop::Main() if ( !InitializeConfiguration() ) return EXIT_FAILURE; +#if HAVE_FEATURE_UPDATE_MAR if (officecfg::Office::Update::Update::Enabled::get()) { + OUString aPatchDirPath("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/patch/"); + rtl::Bootstrap::expandMacros(aPatchDirPath); - OUString aInstallationDir( "$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER ); - - CreateValidUpdateDir(aInstallationDir); + osl::DirectoryItem aPatchInfo; + osl::DirectoryItem::get(aPatchDirPath + "/update.info", aPatchInfo); - osl::DirectoryItem aDirectoryItem; - osl::DirectoryItem::get("file:///lo/users/moggi/test-inst/updated"/*aInstallationDir + "/updated"*/, aDirectoryItem); - bool bValidUpdateDirExists = aDirectoryItem.is(); - if (bValidUpdateDirExists) + if (aPatchInfo.is()) { + OUString aInstallationDir( "$BRAND_BASE_DIR/" LIBO_LIBEXEC_FOLDER ); rtl::Bootstrap::expandMacros(aInstallationDir); - Update(aInstallationDir); + CreateValidUpdateDir(aInstallationDir); + + osl::DirectoryItem aDirectoryItem; + osl::DirectoryItem::get(aInstallationDir + "/updated", aDirectoryItem); + bool bValidUpdateDirExists = aDirectoryItem.is(); + if (bValidUpdateDirExists) + { + rtl::Bootstrap::expandMacros(aInstallationDir); + Update(aInstallationDir); + } } + + if (isTimeForUpdateCheck()) + m_aUpdateThread = std::thread(update_checker); } +#endif SetSplashScreenProgress(30); @@ -1718,6 +1738,8 @@ int Desktop::doShutdown() if( ! pExecGlobals ) return EXIT_SUCCESS; + m_aUpdateThread.join(); + pExecGlobals->bRestartRequested = pExecGlobals->bRestartRequested || OfficeRestartManager::get(comphelper::getProcessComponentContext())-> isRestartRequested(true); diff --git a/desktop/source/app/updater.cxx b/desktop/source/app/updater.cxx index c4c953232084..b6bac978fb21 100644 --- a/desktop/source/app/updater.cxx +++ b/desktop/source/app/updater.cxx @@ -12,12 +12,22 @@ #include <unistd.h> #include <errno.h> +#include <config_folders.h> +#include <rtl/bootstrap.hxx> + +#include <officecfg/Office/Update.hxx> + #include <rtl/ustring.hxx> #include <unotools/tempfile.hxx> +#include <unotools/configmgr.hxx> #include <osl/file.hxx> +#include <curl/curl.h> + namespace { +static const char kUserAgent[] = "UpdateChecker/1.0 (Linux)"; + const char* pUpdaterName = "updater"; void CopyFileToDir(const OUString& rTempDirURL, const OUString rFileName, const OUString& rOldDir) @@ -33,6 +43,14 @@ void CopyFileToDir(const OUString& rTempDirURL, const OUString rFileName, const } } +OUString getPathFromURL(const OUString& rURL) +{ + OUString aPath; + osl::FileBase::getSystemPathFromFileURL(rURL, aPath); + + return aPath; +} + void CopyUpdaterToTempDir(const OUString& rInstallDirURL, const OUString& rTempDirURL) { OUString aUpdaterName = OUString::fromUtf8(pUpdaterName); @@ -65,8 +83,10 @@ char** createCommandLine(const OUString& rInstallDir) createStr(pUpdaterName, pArgs, 0); } { - const char* pPatchDir = "/lo/users/moggi/patch"; - createStr(pPatchDir, pArgs, 1); + OUString aPatchDir("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/patch/"); + rtl::Bootstrap::expandMacros(aPatchDir); + OUString aTempDirPath = getPathFromURL(aPatchDir); + createStr(aPatchDir, pArgs, 1); } { createStr(rInstallDir, pArgs, 2); @@ -84,14 +104,6 @@ char** createCommandLine(const OUString& rInstallDir) return pArgs; } -OUString getPathFromURL(const OUString& rURL) -{ - OUString aPath; - osl::FileBase::getSystemPathFromFileURL(rURL, aPath); - - return aPath; -} - } void Update(const OUString& rInstallDirURL) @@ -103,7 +115,7 @@ void Update(const OUString& rInstallDirURL) OUString aTempDirPath = getPathFromURL(aTempDirURL); OString aPath = OUStringToOString(aTempDirPath + "/" + OUString::fromUtf8(pUpdaterName), RTL_TEXTENCODING_UTF8); - char** pArgs = createCommandLine("/lo/users/moggi/test-inst"); + char** pArgs = createCommandLine(rInstallDirURL); if (execv(aPath.getStr(), pArgs)) { @@ -116,13 +128,14 @@ void Update(const OUString& rInstallDirURL) delete[] pArgs; } -void CreateValidUpdateDir(const OUString& /*rInstallDir*/) +void CreateValidUpdateDir(const OUString& rInstallDir) { - OUString rInstallDir = "file:///lo/users/moggi/test-inst"; OUString aInstallPath = getPathFromURL(rInstallDir); OUString aWorkdirPath = getPathFromURL(rInstallDir + "/updated"); - // OUString aPatchDir = getPathFromURL(rUserProfileDir + "/patch"); - OUString aPatchDir = getPathFromURL("file:///lo/users/moggi/patch"); + + OUString aPatchDirPath("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/patch/"); + rtl::Bootstrap::expandMacros(aPatchDirPath); + OUString aPatchDir = getPathFromURL(aPatchDirPath); OUString aUpdaterPath = getPathFromURL(rInstallDir + "/program/" + OUString::fromUtf8(pUpdaterName)); @@ -131,7 +144,90 @@ void CreateValidUpdateDir(const OUString& /*rInstallDir*/) OString aOCommand = OUStringToOString(aCommand, RTL_TEXTENCODING_UTF8); int nResult = std::system(aOCommand.getStr()); - SAL_WARN_IF(nResult, "desktop.updater", "failed to update"); + if (nResult) + { + // TODO: remove the update directory + SAL_WARN("desktop.updater", "failed to update"); + } +} + +namespace { + +// Callback to get the response data from server. +static size_t WriteCallback(void *ptr, size_t size, + size_t nmemb, void *userp) +{ + if (!userp) + return 0; + + std::string* response = static_cast<std::string *>(userp); + size_t real_size = size * nmemb; + response->append(static_cast<char *>(ptr), real_size); + return real_size; +} + +} + +void update_checker() +{ + OUString aDownloadCheckBaseURL = officecfg::Office::Update::Update::URL::get(); + + OUString aProductName = utl::ConfigManager::getProductName(); + OUString aBuildID("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("version") ":buildid}"); + rtl::Bootstrap::expandMacros(aBuildID); + OUString aVersion = "5.3.0.0.alpha0+"; + OUString aBuildTarget = "${_OS}-${_ARCH}"; + rtl::Bootstrap::expandMacros(aBuildTarget); + OUString aLocale = "en-US"; + OUString aChannel = officecfg::Office::Update::Update::UpdateChannel::get(); + OUString aOSVersion = "0"; + + OUString aDownloadCheckURL = aDownloadCheckBaseURL + "update/3/" + aProductName + + "/" + aVersion + "/" + aBuildID + "/" + aBuildTarget + "/" + aLocale + + "/" + aChannel + "/" + aOSVersion + "/default/default/update.xml?force=1"; + OString aURL = OUStringToOString(aDownloadCheckURL, RTL_TEXTENCODING_UTF8); + SAL_DEBUG(aDownloadCheckURL); + SAL_DEBUG("update_checker"); + + CURL* curl = curl_easy_init(); + + if (!curl) + return; + + curl_easy_setopt(curl, CURLOPT_URL, aURL.getStr()); + curl_easy_setopt(curl, CURLOPT_USERAGENT, kUserAgent); + bool bUseProxy = false; + if (bUseProxy) + { + /* + curl_easy_setopt(curl, CURLOPT_PROXY, proxy.c_str()); + curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str()); + */ + } + + char buf[] = "Expect:"; + curl_slist* headerlist = nullptr; + headerlist = curl_slist_append(headerlist, buf); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); + std::string response_body; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, + static_cast<void *>(&response_body)); + + // Fail if 400+ is returned from the web server. + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); + + CURLcode cc = curl_easy_perform(curl); + long http_code = 0; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + SAL_DEBUG(http_code); + SAL_DEBUG(cc); + SAL_DEBUG(response_body); + if (cc == CURLE_OK) + { + SAL_DEBUG(response_body); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/desktop/source/app/updater.hxx b/desktop/source/app/updater.hxx index dbd53dac36a3..c73e302eea50 100644 --- a/desktop/source/app/updater.hxx +++ b/desktop/source/app/updater.hxx @@ -15,6 +15,8 @@ void CreateValidUpdateDir(const OUString& rInstallDir); void Update(const OUString& rInstallDir); +void update_checker(); + #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |