summaryrefslogtreecommitdiffstats
path: root/logic/net/ForgeXzDownload.cpp
blob: dde36f4e5f051aa3621d1abb82ce1a650b032164 (plain)
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
#include "MultiMC.h"
#include "ForgeXzDownload.h"
#include <pathutils.h>

#include <QCryptographicHash>
#include <QFileInfo>
#include <QDateTime>
#include <QDebug>

ForgeXzDownload::ForgeXzDownload(QUrl url, MetaEntryPtr entry)
	: Download()
{
	QString urlstr = url.toString();
	urlstr.append(".pack.xz");
	m_url = QUrl(urlstr);
	m_entry = entry;
	m_target_path = entry->getFullPath();
	m_status = Job_NotStarted;
	m_opened_for_saving = false;
}

void ForgeXzDownload::start()
{
	if (!m_entry->stale)
	{
		emit succeeded(index_within_job);
		return;
	}
	// can we actually create the real, final file?
	if (!ensureFilePathExists(m_target_path))
	{
		emit failed(index_within_job);
		return;
	}
	qDebug() << "Downloading " << m_url.toString();
	QNetworkRequest request(m_url);
	request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->etag.toLatin1());

	auto worker = MMC->qnam();
	QNetworkReply *rep = worker->get(request);

	m_reply = QSharedPointer<QNetworkReply>(rep, &QObject::deleteLater);
	connect(rep, SIGNAL(downloadProgress(qint64, qint64)),
			SLOT(downloadProgress(qint64, qint64)));
	connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
	connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
			SLOT(downloadError(QNetworkReply::NetworkError)));
	connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
}

void ForgeXzDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
	emit progress(index_within_job, bytesReceived, bytesTotal);
}

void ForgeXzDownload::downloadError(QNetworkReply::NetworkError error)
{
	// error happened during download.
	// TODO: log the reason why
	m_status = Job_Failed;
}

void ForgeXzDownload::downloadFinished()
{
	// if the download succeeded
	if (m_status != Job_Failed)
	{
		// nothing went wrong...
		m_status = Job_Finished;
		if (m_opened_for_saving)
		{
			// we actually downloaded something! process and isntall it
			decompressAndInstall();
			return;
		}
		else
		{
			// something bad happened
			m_pack200_xz_file.remove();
			m_reply.clear();
			emit failed(index_within_job);
			return;
		}
	}
	// else the download failed
	else
	{
		m_pack200_xz_file.close();
		m_pack200_xz_file.remove();
		m_reply.clear();
		emit failed(index_within_job);
		return;
	}
}

void ForgeXzDownload::downloadReadyRead()
{
	
	if (!m_opened_for_saving)
	{
		if (!m_pack200_xz_file.open())
		{
			/*
			* Can't open the file... the job failed
			*/
			m_reply->abort();
			emit failed(index_within_job);
			return;
		}
		m_opened_for_saving = true;
	}
	m_pack200_xz_file.write(m_reply->readAll());
}

#include "xz.h"
#include "unpack200.h"
#include <stdexcept>

const size_t buffer_size = 8196;

void ForgeXzDownload::decompressAndInstall()
{
	// rewind the downloaded temp file
	m_pack200_xz_file.seek(0);
	// de-xz'd file
	QTemporaryFile pack200_file;
	pack200_file.open();

	bool xz_success = false;
	// first, de-xz
	{
		uint8_t in[buffer_size];
		uint8_t out[buffer_size];
		struct xz_buf b;
		struct xz_dec *s;
		enum xz_ret ret;
		xz_crc32_init();
		xz_crc64_init();
		s = xz_dec_init(XZ_DYNALLOC, 1 << 26);
		if (s == nullptr)
		{
			xz_dec_end(s);
			emit failed(index_within_job);
			return;
		}
		b.in = in;
		b.in_pos = 0;
		b.in_size = 0;
		b.out = out;
		b.out_pos = 0;
		b.out_size = buffer_size;
		while (!xz_success)
		{
			if (b.in_pos == b.in_size)
			{
				b.in_size = m_pack200_xz_file.read((char*)in, sizeof(in));
				b.in_pos = 0;
			}

			ret = xz_dec_run(s, &b);

			if (b.out_pos == sizeof(out))
			{
				if (pack200_file.write((char*)out, b.out_pos) != b.out_pos)
				{
					// msg = "Write error\n";
					xz_dec_end(s);
					emit failed(index_within_job);
					return;
				}

				b.out_pos = 0;
			}

			if (ret == XZ_OK)
				continue;

			if (ret == XZ_UNSUPPORTED_CHECK)
			{
				// unsupported check. this is OK, but we should log this
				continue;
			}

			if (pack200_file.write((char*)out, b.out_pos) != b.out_pos )
			{
				// write error
				pack200_file.close();
				xz_dec_end(s);
				return;
			}

			switch (ret)
			{
			case XZ_STREAM_END:
				xz_dec_end(s);
				xz_success = true;
				break;

			case XZ_MEM_ERROR:
				qDebug() << "Memory allocation failed\n";
				xz_dec_end(s);
				emit failed(index_within_job);
				return;

			case XZ_MEMLIMIT_ERROR:
				qDebug() << "Memory usage limit reached\n";
				xz_dec_end(s);
				emit failed(index_within_job);
				return;

			case XZ_FORMAT_ERROR:
				qDebug() << "Not a .xz file\n";
				xz_dec_end(s);
				emit failed(index_within_job);
				return;

			case XZ_OPTIONS_ERROR:
				qDebug() << "Unsupported options in the .xz headers\n";
				xz_dec_end(s);
				emit failed(index_within_job);
				return;

			case XZ_DATA_ERROR:
			case XZ_BUF_ERROR:
				qDebug() << "File is corrupt\n";
				xz_dec_end(s);
				emit failed(index_within_job);
				return;

			default:
				qDebug() << "Bug!\n";
				xz_dec_end(s);
				emit failed(index_within_job);
				return;
			}
		}
	}
	
	// revert pack200
	pack200_file.close();
	QString pack_name = pack200_file.fileName();
	try
	{
		unpack_200(pack_name.toStdString(), m_target_path.toStdString());
	}
	catch(std::runtime_error & err)
	{
		qDebug() << "Error unpacking " << pack_name.toUtf8() << " : " << err.what();
		QFile f(m_target_path);
		if(f.exists())
			f.remove();
		emit failed(index_within_job);
		return;
	}
	
	QFile jar_file(m_target_path);

	if (!jar_file.open(QIODevice::ReadOnly))
	{
		jar_file.remove();
		emit failed(index_within_job);
		return;
	}
	m_entry->md5sum = QCryptographicHash::hash(jar_file.readAll(), QCryptographicHash::Md5)
			.toHex()
			.constData();
	jar_file.close();
	
	QFileInfo output_file_info(m_target_path);
	m_entry->etag = m_reply->rawHeader("ETag").constData();
	m_entry->last_changed_timestamp =
		output_file_info.lastModified().toUTC().toMSecsSinceEpoch();
	m_entry->stale = false;
	MMC->metacache()->updateEntry(m_entry);

	m_reply.clear();
	emit succeeded(index_within_job);
}