-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.cpp
424 lines (369 loc) · 10.5 KB
/
bundle.cpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "bundle.h"
#include "patcher.h"
#include "version.h"
#include "information.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QtConcurrent>
#include <QFuture>
#include <QFutureWatcher>
#include <QCryptographicHash>
#include <QNetworkReply>
#include <zlib.h>
Bundle::Bundle(QString install_path, Patcher *patcher)
: QObject(), mZstreamBufferSize(1024*1024), mInstallPath(install_path), mPatcher(patcher), mNeedsDownloading(false)
{
}
Bundle::~Bundle()
{
}
void Bundle::verify(QJsonObject* array)
{
Q_ASSERT(array != NULL);
mNumVerified = 0;
mNumToVerify = 0;
mChecksum = (*array)["checksum"].toString();
mTotalSize = (*array)["size"].toString().toULong();
QJsonArray entries = (*array)["entries"].toArray();
for(QJsonArray::const_iterator e = entries.constBegin(); e != entries.constEnd(); ++e)
{
File file_entry;
file_entry.filename = (*e).toObject()["filename"].toString();
file_entry.checksum = (*e).toObject()["checksum"].toString();
file_entry.offset = (*e).toObject()["offset"].toString().toULong();
file_entry.size = (*e).toObject()["size"].toString().toULong();
file_entry.sizeZ = (*e).toObject()["sizeZ"].toString().toULong();
file_entry.executable = (*e).toObject()["executable"].toBool();
file_entry.file = NULL;
file_entry.compressed = (file_entry.sizeZ != 0);
file_entry.zbuffer = NULL;
if(file_entry.size == 0)
{
createEmptyFile(file_entry.filename);
continue;
}
bool was_symlinked = false;
for(QList<File>::const_iterator prevfile = mFiles.constBegin(); prevfile != mFiles.constEnd(); ++prevfile)
{
if(prevfile->offset == file_entry.offset && prevfile->size != 0 && file_entry.size != 0)
{
createSymbolicLink(prevfile->filename, file_entry.filename);
was_symlinked = true;
break;
}
}
if(was_symlinked)
continue;
if(file_entry.size == 0)
{
// Zero byte files. Just create those.
QFile file(mPatcher->getFile(mInstallPath + "/" +file_entry.filename));
if(!file.open(QFile::WriteOnly))
{
error_occurred = true;
emit error(tr("Couldn't create empty file \"%1\"").arg(file.fileName()));
}
file.close();
continue;
}
if(mNeedsDownloading)
{
// No need to check files if we already know we need to download.
mFiles.push_back(file_entry);
continue;
}
mNumToVerify++;
// Check the SHA1 checksum.
file_entry.future = QtConcurrent::run(verifySHA1, file_entry, &mNeedsDownloading, mPatcher, mInstallPath);
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>(this);
watcher->setFuture(file_entry.future);
connect(watcher, SIGNAL(finished()), SLOT(verifyFinished()));
mFiles.push_back(file_entry);
}
}
bool Bundle::verifySHA1(Bundle::File file_entry, bool* downloading, Patcher *patcher, QString install_path)
{
if(*downloading)
// No need to verify the SHA1 if we are already downloading;
return false;
QByteArray data_on_disk(patcher->getFile(install_path + "/" +file_entry.filename));
if(data_on_disk.isNull())
{
// File does not exist. Therefore this bundle needs to be downloaded.
*downloading = true;
return false;
}
QCryptographicHash hash(QCryptographicHash::Sha1);
QBuffer buffer(&data_on_disk);
if(buffer.open(QBuffer::ReadOnly))
{
while(!buffer.atEnd())
{
if(*downloading || error_occurred)
return false;
hash.addData(buffer.read(4096));
}
buffer.close();
if(hash.result().toHex() == file_entry.checksum)
{
return true;
}
else
{
info.log("Verification failed", file_entry.filename, true);
*downloading = true;
return false;
}
}
else
return false;//FATAL
}
void Bundle::verifyFinished()
{
mNumVerified++;
if(mNumToVerify == mNumVerified)
{
emit verifyDone();
if(mNeedsDownloading)
emit downloadMe();
else
emit finished();
}
}
void Bundle::downloadAndExtract(QNetworkAccessManager* network_access_manager, QString download_url)
{
info.log("Download bundle", mChecksum);
QNetworkRequest request(QUrl(download_url.arg(mChecksum)));
request.setRawHeader("X-Clacks-Overhead", "GNU Terry Pratchett");
request.setRawHeader("User-Agent", QString("PAAlternativeLauncher/%1").arg(VERSION).toUtf8());
mBytesDownloaded = 0;
mBytesProgress = 0;
QNetworkReply *reply = network_access_manager->get(request);
connect(reply, SIGNAL(finished()), SLOT(downloadFinished()));
connect(reply, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64)));
}
void Bundle::downloadReadyRead()
{
QNetworkReply *reply = dynamic_cast<QNetworkReply *>(sender());
if(reply)
{
if(error_occurred)
{
reply->abort();
return;
}
if(reply->error() == QNetworkReply::NoError)
{
qint64 bytes_available = reply->bytesAvailable();
if(bytes_available > 0)
{
processData(reply, bytes_available);
}
mBytesDownloaded += bytes_available;
}
else
{
if(reply->error() != QNetworkReply::OperationCanceledError)
{
emit error(tr("Error while getting Bundle (1).\n%1").arg(reply->errorString()));
error_occurred = true;
}
}
}
}
void Bundle::processData(QNetworkReply *reply, qint64 bytes_available)
{
if(!reply)
{
error_occurred = true;
emit error("NULL reply in processData");
return;
}
QByteArray data = reply->readAll();
for(QList<File>::iterator f = mFiles.begin(); f != mFiles.end(); ++f)
{
// Three cases to visualize the calculations
//
// |.......*.....|.............|..*...........|
// 0 6 10 20 22 30
//
// first block: mBytesDownloaded = 0
// bytes_available = 10
// f->offset = 6
// endoffset = 22
//
// second block: mBytesDownloaded = 10
// bytes_available = 10
// f->offset = 6
// endoffset = 22
//
// third block: mBytesDownloaded = 20
// bytes_available = 10
// f->offset = 6
// endoffset = 22
// Within block case
//
// |..........*....*........|
// 10 18 22 30
//
// mBytesDownloaded = 10
// bytes_available = 20
// f->offset = 18
// endoffset = 22
ulong endoffset = (f->compressed ? f->offset + f->sizeZ : f->offset + f->size);
if((ulong)mBytesDownloaded + bytes_available > f->offset && (ulong)mBytesDownloaded <= endoffset)
{
// Our file has data in this block.
if(f->file == NULL)
{
prepareFile(&(*f));
}
ulong start_in_data = 0;
if(f->offset > (ulong)mBytesDownloaded)
start_in_data = f->offset - mBytesDownloaded;
ulong length_in_data = bytes_available - start_in_data;
if((ulong)mBytesDownloaded + bytes_available > endoffset)
{
Q_ASSERT(endoffset >= mBytesDownloaded + start_in_data);
length_in_data = endoffset - mBytesDownloaded - start_in_data;
}
if(f->compressed)
{
int res;
QByteArray snippet(data.mid(start_in_data, length_in_data).constData(), length_in_data);
f->zstream.next_in = (Bytef *)snippet.constData();
f->zstream.avail_in = length_in_data;
do
{
uInt old_avail_out = f->zstream.avail_out;
res = inflate(&(f->zstream), Z_SYNC_FLUSH);
if(res != Z_OK && res != Z_STREAM_END)
{
error_occurred = true;
reply->abort();
emit error(QString("ZLib ReadyRead error (%1)\n%2").arg(mChecksum).arg(f->zstream.msg));
return;
}
f->file->write((const char *)f->zbuffer, old_avail_out - f->zstream.avail_out);
f->zstream.avail_out = mZstreamBufferSize;
f->zstream.next_out = f->zbuffer;
}
while(f->zstream.avail_in > 0);
}
else
f->file->write(data.mid(start_in_data, length_in_data));
}
else
{
// Our file has no business here. So if it's open, close it.
closeFile(&(*f));
}
}
}
void Bundle::downloadFinished()
{
if(error_occurred)
return;
QNetworkReply *reply = dynamic_cast<QNetworkReply *>(sender());
if(reply)
{
if(reply->error() != QNetworkReply::NoError)
{
if(reply->error() != QNetworkReply::OperationCanceledError)
{
error_occurred = true;
emit error(tr("Error while getting Bundle (2).\n%1").arg(reply->errorString()));
}
reply->deleteLater();
return;
}
for(QList<File>::iterator f = mFiles.begin(); f != mFiles.end(); ++f)
{
closeFile(&(*f));
}
reply->deleteLater();
emit finished();
}
}
void Bundle::downloadProgress(qint64 downloaded, qint64)
{
emit downloadProgress(downloaded - mBytesProgress);
mBytesProgress = downloaded;
}
void Bundle::prepareFile(File *file)
{
// Create the necessary directories.
QFileInfo file_info(mInstallPath + "/" + file->filename);
file_info.absoluteDir().mkpath(".");
file->file = new QFile(file_info.absoluteFilePath());
if(!file->file->open(QFile::WriteOnly))
{
error_occurred = true;
emit error(QString("Can't write to \"%1\"\n%2").arg(mInstallPath + "/" + file->filename).arg(file->file->errorString()));
return;
}
if(file->compressed)
{
file->zbuffer = new Bytef[mZstreamBufferSize];
file->zstream.next_out = file->zbuffer;
file->zstream.avail_out = mZstreamBufferSize;
file->zstream.next_in = Z_NULL;
file->zstream.avail_in = 0;
file->zstream.zalloc = Z_NULL;
file->zstream.zfree = Z_NULL;
file->zstream.opaque = Z_NULL;
int res = inflateInit2(&(file->zstream), 16 + MAX_WBITS);
if(res != Z_OK)
{
error_occurred = true;
emit error(QString("prepareZLib error (%1)\n%2").arg(res).arg(file->zstream.msg));
return;
}
}
if(file->executable)
file->file->setPermissions(file->file->permissions() | QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther);
}
void Bundle::closeFile(File* file)
{
if(file->file != NULL)
{
file->file->close();
delete file->file;
file->file = NULL;
if(file->zbuffer)
{
delete[] file->zbuffer;
file->zbuffer = NULL;
int res = inflateEnd(&file->zstream);
if(res != Z_OK && res != Z_STREAM_END)
{
error_occurred = true;
emit error(QString("ZLib closeFile error (%1)\n%2").arg(res).arg(file->zstream.msg));
return;
}
}
}
}
bool Bundle::createSymbolicLink(const QString& from, const QString& to)
{
// Add to the list, which is retrieved by Patcher later to make the actual links.
mSymLinkLater[mInstallPath + "/" + to] = mInstallPath + "/" + from;
return true;
}
bool Bundle::createEmptyFile(const QString& file_name)
{
QFileInfo(mInstallPath + '/' + file_name).absoluteDir().mkpath(".");
QFile file(mInstallPath + '/' + file_name);
if(file.open(QFile::WriteOnly))
{
file.close();
return true;
}
else
return false;
}
#include "moc_bundle.cpp"