2015-02-03 03:55:30 +05:30
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-11-04 07:23:05 +05:30
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "Env.h"
|
2013-09-30 06:04:46 +05:30
|
|
|
#include "ForgeXzDownload.h"
|
|
|
|
#include <pathutils.h>
|
|
|
|
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDateTime>
|
2013-12-17 06:39:58 +05:30
|
|
|
#include <QDir>
|
2015-02-02 06:44:14 +05:30
|
|
|
#include <QDebug>
|
2013-09-30 06:04:46 +05:30
|
|
|
|
2013-11-17 16:14:18 +05:30
|
|
|
ForgeXzDownload::ForgeXzDownload(QString relative_path, MetaEntryPtr entry) : NetAction()
|
2013-09-30 06:04:46 +05:30
|
|
|
{
|
|
|
|
m_entry = entry;
|
|
|
|
m_target_path = entry->getFullPath();
|
2013-11-19 05:17:59 +05:30
|
|
|
m_pack200_xz_file.setFileTemplate("./dl_temp.XXXXXX");
|
2013-09-30 06:04:46 +05:30
|
|
|
m_status = Job_NotStarted;
|
2013-11-17 16:14:18 +05:30
|
|
|
m_url_path = relative_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::setMirrors(QList<ForgeMirror> &mirrors)
|
|
|
|
{
|
|
|
|
m_mirror_index = 0;
|
|
|
|
m_mirrors = mirrors;
|
|
|
|
updateUrl();
|
2013-09-30 06:04:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::start()
|
|
|
|
{
|
2013-11-17 16:14:18 +05:30
|
|
|
m_status = Job_InProgress;
|
2013-09-30 06:04:46 +05:30
|
|
|
if (!m_entry->stale)
|
|
|
|
{
|
2013-11-17 16:14:18 +05:30
|
|
|
m_status = Job_Finished;
|
2013-12-15 19:30:09 +05:30
|
|
|
emit succeeded(m_index_within_job);
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
// can we actually create the real, final file?
|
|
|
|
if (!ensureFilePathExists(m_target_path))
|
|
|
|
{
|
2013-11-17 16:14:18 +05:30
|
|
|
m_status = Job_Failed;
|
2013-12-15 19:30:09 +05:30
|
|
|
emit failed(m_index_within_job);
|
2013-11-17 16:14:18 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_mirrors.empty())
|
|
|
|
{
|
|
|
|
m_status = Job_Failed;
|
2013-12-15 19:30:09 +05:30
|
|
|
emit failed(m_index_within_job);
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
2013-11-17 16:14:18 +05:30
|
|
|
|
2015-02-02 06:44:14 +05:30
|
|
|
qDebug() << "Downloading " << m_url.toString();
|
2013-09-30 06:04:46 +05:30
|
|
|
QNetworkRequest request(m_url);
|
|
|
|
request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->etag.toLatin1());
|
2013-11-04 07:23:05 +05:30
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
|
2013-09-30 06:04:46 +05:30
|
|
|
|
2015-01-31 21:29:03 +05:30
|
|
|
auto worker = ENV.qnam();
|
2013-09-30 06:04:46 +05:30
|
|
|
QNetworkReply *rep = worker->get(request);
|
|
|
|
|
2015-02-08 22:26:14 +05:30
|
|
|
m_reply.reset(rep);
|
2013-09-30 06:04:46 +05:30
|
|
|
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)
|
|
|
|
{
|
2013-12-15 19:30:09 +05:30
|
|
|
m_total_progress = bytesTotal;
|
|
|
|
m_progress = bytesReceived;
|
2015-07-26 21:25:29 +05:30
|
|
|
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
|
2013-09-30 06:04:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
|
|
|
// error happened during download.
|
|
|
|
// TODO: log the reason why
|
|
|
|
m_status = Job_Failed;
|
|
|
|
}
|
|
|
|
|
2013-11-17 16:14:18 +05:30
|
|
|
void ForgeXzDownload::failAndTryNextMirror()
|
|
|
|
{
|
|
|
|
m_status = Job_Failed;
|
|
|
|
int next = m_mirror_index + 1;
|
|
|
|
if(m_mirrors.size() == next)
|
|
|
|
m_mirror_index = 0;
|
|
|
|
else
|
|
|
|
m_mirror_index = next;
|
|
|
|
|
|
|
|
updateUrl();
|
2013-12-15 19:30:09 +05:30
|
|
|
emit failed(m_index_within_job);
|
2013-11-17 16:14:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::updateUrl()
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qDebug() << "Updating URL for " << m_url_path;
|
2013-11-17 16:14:18 +05:30
|
|
|
for (auto possible : m_mirrors)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qDebug() << "Possible: " << possible.name << " : " << possible.mirror_url;
|
2013-11-17 16:14:18 +05:30
|
|
|
}
|
|
|
|
QString aggregate = m_mirrors[m_mirror_index].mirror_url + m_url_path + ".pack.xz";
|
|
|
|
m_url = QUrl(aggregate);
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:04:46 +05:30
|
|
|
void ForgeXzDownload::downloadFinished()
|
|
|
|
{
|
2013-11-17 16:14:18 +05:30
|
|
|
//TEST: defer to other possible mirrors (autofail the first one)
|
|
|
|
/*
|
2015-02-02 06:44:14 +05:30
|
|
|
qDebug() <<"dl " << index_within_job << " mirror " << m_mirror_index;
|
2013-11-17 16:14:18 +05:30
|
|
|
if( m_mirror_index == 0)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qDebug() <<"dl " << index_within_job << " AUTOFAIL";
|
2013-11-17 16:14:18 +05:30
|
|
|
m_status = Job_Failed;
|
|
|
|
m_pack200_xz_file.close();
|
|
|
|
m_pack200_xz_file.remove();
|
|
|
|
m_reply.reset();
|
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2013-09-30 06:04:46 +05:30
|
|
|
// if the download succeeded
|
|
|
|
if (m_status != Job_Failed)
|
|
|
|
{
|
|
|
|
// nothing went wrong...
|
|
|
|
m_status = Job_Finished;
|
2013-11-17 16:14:18 +05:30
|
|
|
if (m_pack200_xz_file.isOpen())
|
2013-09-30 06:04:46 +05:30
|
|
|
{
|
|
|
|
// we actually downloaded something! process and isntall it
|
|
|
|
decompressAndInstall();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-17 16:14:18 +05:30
|
|
|
// something bad happened -- on the local machine!
|
2013-11-17 01:06:01 +05:30
|
|
|
m_status = Job_Failed;
|
2013-09-30 06:04:46 +05:30
|
|
|
m_pack200_xz_file.remove();
|
2013-10-06 04:43:40 +05:30
|
|
|
m_reply.reset();
|
2013-12-15 19:30:09 +05:30
|
|
|
emit failed(m_index_within_job);
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else the download failed
|
|
|
|
else
|
|
|
|
{
|
2013-11-17 16:14:18 +05:30
|
|
|
m_status = Job_Failed;
|
2013-09-30 06:04:46 +05:30
|
|
|
m_pack200_xz_file.close();
|
|
|
|
m_pack200_xz_file.remove();
|
2013-10-06 04:43:40 +05:30
|
|
|
m_reply.reset();
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::downloadReadyRead()
|
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-11-17 16:14:18 +05:30
|
|
|
if (!m_pack200_xz_file.isOpen())
|
2013-09-30 06:04:46 +05:30
|
|
|
{
|
|
|
|
if (!m_pack200_xz_file.open())
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Can't open the file... the job failed
|
|
|
|
*/
|
|
|
|
m_reply->abort();
|
2013-12-15 19:30:09 +05:30
|
|
|
emit failed(m_index_within_job);
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_pack200_xz_file.write(m_reply->readAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "xz.h"
|
|
|
|
#include "unpack200.h"
|
2013-10-01 12:35:58 +05:30
|
|
|
#include <stdexcept>
|
2013-09-30 06:04:46 +05:30
|
|
|
|
|
|
|
const size_t buffer_size = 8196;
|
|
|
|
|
|
|
|
void ForgeXzDownload::decompressAndInstall()
|
|
|
|
{
|
|
|
|
// rewind the downloaded temp file
|
|
|
|
m_pack200_xz_file.seek(0);
|
|
|
|
// de-xz'd file
|
2013-11-19 05:17:59 +05:30
|
|
|
QTemporaryFile pack200_file("./dl_temp.XXXXXX");
|
2013-09-30 06:04:46 +05:30
|
|
|
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);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
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)
|
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
b.in_size = m_pack200_xz_file.read((char *)in, sizeof(in));
|
2013-09-30 06:04:46 +05:30
|
|
|
b.in_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = xz_dec_run(s, &b);
|
|
|
|
|
|
|
|
if (b.out_pos == sizeof(out))
|
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
if (pack200_file.write((char *)out, b.out_pos) != b.out_pos)
|
2013-09-30 06:04:46 +05:30
|
|
|
{
|
|
|
|
// msg = "Write error\n";
|
|
|
|
xz_dec_end(s);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
if (pack200_file.write((char *)out, b.out_pos) != b.out_pos)
|
2013-09-30 06:04:46 +05:30
|
|
|
{
|
|
|
|
// 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:
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Memory allocation failed\n";
|
2013-09-30 06:04:46 +05:30
|
|
|
xz_dec_end(s);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_MEMLIMIT_ERROR:
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Memory usage limit reached\n";
|
2013-09-30 06:04:46 +05:30
|
|
|
xz_dec_end(s);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_FORMAT_ERROR:
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Not a .xz file\n";
|
2013-09-30 06:04:46 +05:30
|
|
|
xz_dec_end(s);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_OPTIONS_ERROR:
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Unsupported options in the .xz headers\n";
|
2013-09-30 06:04:46 +05:30
|
|
|
xz_dec_end(s);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_DATA_ERROR:
|
|
|
|
case XZ_BUF_ERROR:
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "File is corrupt\n";
|
2013-09-30 06:04:46 +05:30
|
|
|
xz_dec_end(s);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Bug!\n";
|
2013-09-30 06:04:46 +05:30
|
|
|
xz_dec_end(s);
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 05:17:59 +05:30
|
|
|
m_pack200_xz_file.remove();
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-09-30 06:04:46 +05:30
|
|
|
// revert pack200
|
2013-12-22 23:19:52 +05:30
|
|
|
pack200_file.seek(0);
|
|
|
|
int handle_in = pack200_file.handle();
|
|
|
|
// FIXME: dispose of file handles, pointers and the like. Ideally wrap in objects.
|
|
|
|
if(handle_in == -1)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Error reopening " << pack200_file.fileName();
|
2013-12-22 23:19:52 +05:30
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FILE * file_in = fdopen(handle_in,"r");
|
|
|
|
if(!file_in)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Error reopening " << pack200_file.fileName();
|
2013-12-22 23:19:52 +05:30
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QFile qfile_out(m_target_path);
|
|
|
|
if(!qfile_out.open(QIODevice::WriteOnly))
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Error opening " << qfile_out.fileName();
|
2013-12-22 23:19:52 +05:30
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int handle_out = qfile_out.handle();
|
|
|
|
if(handle_out == -1)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Error opening " << qfile_out.fileName();
|
2013-12-22 23:19:52 +05:30
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FILE * file_out = fdopen(handle_out,"w");
|
|
|
|
if(!file_out)
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Error opening " << qfile_out.fileName();
|
2013-12-22 23:19:52 +05:30
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
2013-09-30 06:04:46 +05:30
|
|
|
try
|
|
|
|
{
|
2013-12-22 23:19:52 +05:30
|
|
|
unpack_200(file_in, file_out);
|
2013-09-30 06:04:46 +05:30
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
catch (std::runtime_error &err)
|
2013-09-30 06:04:46 +05:30
|
|
|
{
|
2013-11-17 01:06:01 +05:30
|
|
|
m_status = Job_Failed;
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Error unpacking " << pack200_file.fileName() << " : " << err.what();
|
2013-09-30 06:04:46 +05:30
|
|
|
QFile f(m_target_path);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (f.exists())
|
2013-09-30 06:04:46 +05:30
|
|
|
f.remove();
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
2013-11-19 05:17:59 +05:30
|
|
|
pack200_file.remove();
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-09-30 06:04:46 +05:30
|
|
|
QFile jar_file(m_target_path);
|
|
|
|
|
|
|
|
if (!jar_file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
jar_file.remove();
|
2013-11-17 16:14:18 +05:30
|
|
|
failAndTryNextMirror();
|
2013-09-30 06:04:46 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_entry->md5sum = QCryptographicHash::hash(jar_file.readAll(), QCryptographicHash::Md5)
|
2013-11-04 07:23:05 +05:30
|
|
|
.toHex()
|
|
|
|
.constData();
|
2013-09-30 06:04:46 +05:30
|
|
|
jar_file.close();
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-09-30 06:04:46 +05:30
|
|
|
QFileInfo output_file_info(m_target_path);
|
|
|
|
m_entry->etag = m_reply->rawHeader("ETag").constData();
|
2013-10-18 04:30:46 +05:30
|
|
|
m_entry->local_changed_timestamp =
|
2013-09-30 06:04:46 +05:30
|
|
|
output_file_info.lastModified().toUTC().toMSecsSinceEpoch();
|
|
|
|
m_entry->stale = false;
|
2015-01-31 21:29:03 +05:30
|
|
|
ENV.metacache()->updateEntry(m_entry);
|
2013-09-30 06:04:46 +05:30
|
|
|
|
2013-10-06 04:43:40 +05:30
|
|
|
m_reply.reset();
|
2013-12-15 19:30:09 +05:30
|
|
|
emit succeeded(m_index_within_job);
|
2013-09-30 06:04:46 +05:30
|
|
|
}
|