1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "updater.hxx"
#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)
{
OUString aSourceURL = rOldDir + "/" + rFileName;
OUString aDestURL = rTempDirURL + "/" + rFileName;
osl::File::RC eError = osl::File::copy(aSourceURL, aDestURL);
if (eError != osl::File::E_None)
{
SAL_WARN("desktop.updater", "could not copy the file to a temp directory: " << rFileName);
throw std::exception();
}
}
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);
CopyFileToDir(rTempDirURL, aUpdaterName, rInstallDirURL);
}
void createStr(const char* pSrc, char** pArgs, size_t i)
{
size_t nLength = std::strlen(pSrc);
char* pFinalStr = new char[nLength + 1];
std::strncpy(pFinalStr, pSrc, nLength);
pFinalStr[nLength] = '\0';
pArgs[i] = pFinalStr;
}
void createStr(const OUString& rStr, char** pArgs, size_t i)
{
OString aStr = OUStringToOString(rStr, RTL_TEXTENCODING_UTF8);
char* pStr = new char[aStr.getLength() + 1];
std::strncpy(pStr, aStr.getStr(), aStr.getLength());
pStr[aStr.getLength()] = '\0';
pArgs[i] = pStr;
}
char** createCommandLine(const OUString& rInstallDir)
{
size_t nArgs = 6;
char** pArgs = new char*[nArgs];
{
createStr(pUpdaterName, pArgs, 0);
}
{
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);
}
{
OUString aWorkingDir = rInstallDir + "/updated";
createStr(aWorkingDir, pArgs, 3);
}
{
const char* pPID = "/replace";
createStr(pPID, pArgs, 4);
}
pArgs[nArgs - 1] = nullptr;
return pArgs;
}
}
void Update(const OUString& rInstallDirURL)
{
utl::TempFile aTempDir(nullptr, true);
OUString aTempDirURL = aTempDir.GetURL();
CopyUpdaterToTempDir(rInstallDirURL, aTempDirURL);
OUString aTempDirPath = getPathFromURL(aTempDirURL);
OString aPath = OUStringToOString(aTempDirPath + "/" + OUString::fromUtf8(pUpdaterName), RTL_TEXTENCODING_UTF8);
char** pArgs = createCommandLine(rInstallDirURL);
if (execv(aPath.getStr(), pArgs))
{
printf("execv failed with error %d %s\n",errno,strerror(errno));
}
for (size_t i = 0; i < 6; ++i)
{
delete[] pArgs[i];
}
delete[] pArgs;
}
void CreateValidUpdateDir(const OUString& rInstallDir)
{
OUString aInstallPath = getPathFromURL(rInstallDir);
OUString aWorkdirPath = getPathFromURL(rInstallDir + "/updated");
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));
OUString aCommand = aUpdaterPath + " " + aPatchDir + " " + aInstallPath + " " + aWorkdirPath + " -1";
OString aOCommand = OUStringToOString(aCommand, RTL_TEXTENCODING_UTF8);
int nResult = std::system(aOCommand.getStr());
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: */
|