2015-02-02 22:25:30 +00:00
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-11-04 01:53:05 +00:00
|
|
|
*
|
|
|
|
* 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 00:51:14 +00:00
|
|
|
#include "Env.h"
|
2013-09-30 01:34:46 +01:00
|
|
|
#include "ForgeXzDownload.h"
|
2015-10-05 00:47:27 +01:00
|
|
|
#include <FileSystem.h>
|
2013-09-30 01:34:46 +01:00
|
|
|
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDateTime>
|
2013-12-17 01:09:58 +00:00
|
|
|
#include <QDir>
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2013-09-30 01:34:46 +01:00
|
|
|
|
2013-11-17 10:44:18 +00:00
|
|
|
ForgeXzDownload::ForgeXzDownload(QString relative_path, MetaEntryPtr entry) : NetAction()
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
|
|
|
m_entry = entry;
|
|
|
|
m_target_path = entry->getFullPath();
|
2013-11-18 23:47:59 +00:00
|
|
|
m_pack200_xz_file.setFileTemplate("./dl_temp.XXXXXX");
|
2013-09-30 01:34:46 +01:00
|
|
|
m_status = Job_NotStarted;
|
2013-11-17 10:44:18 +00:00
|
|
|
m_url_path = relative_path;
|
2016-03-26 15:56:57 +00:00
|
|
|
m_url = "http://files.minecraftforge.net/maven/" + m_url_path + ".pack.xz";
|
2013-09-30 01:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::start()
|
|
|
|
{
|
2016-08-14 01:33:31 +01:00
|
|
|
if(m_status == Job_Aborted)
|
|
|
|
{
|
|
|
|
qWarning() << "Attempt to start an aborted Download:" << m_url.toString();
|
|
|
|
emit aborted(m_index_within_job);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-17 10:44:18 +00:00
|
|
|
m_status = Job_InProgress;
|
2016-03-26 15:56:57 +00:00
|
|
|
if (!m_entry->isStale())
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
2013-11-17 10:44:18 +00:00
|
|
|
m_status = Job_Finished;
|
2013-12-15 14:00:09 +00:00
|
|
|
emit succeeded(m_index_within_job);
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// can we actually create the real, final file?
|
2015-10-05 00:47:27 +01:00
|
|
|
if (!FS::ensureFilePathExists(m_target_path))
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
2013-11-17 10:44:18 +00:00
|
|
|
m_status = Job_Failed;
|
2013-12-15 14:00:09 +00:00
|
|
|
emit failed(m_index_within_job);
|
2013-11-17 10:44:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-02 01:14:14 +00:00
|
|
|
qDebug() << "Downloading " << m_url.toString();
|
2013-09-30 01:34:46 +01:00
|
|
|
QNetworkRequest request(m_url);
|
2016-03-26 15:56:57 +00:00
|
|
|
request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->getETag().toLatin1());
|
2013-11-04 01:53:05 +00:00
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)");
|
2013-09-30 01:34:46 +01:00
|
|
|
|
2016-11-06 20:58:54 +00:00
|
|
|
QNetworkReply *rep = ENV.qnam().get(request);
|
2013-09-30 01:34:46 +01:00
|
|
|
|
2015-02-08 16:56:14 +00:00
|
|
|
m_reply.reset(rep);
|
2016-11-06 20:58:54 +00:00
|
|
|
connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64)));
|
2013-09-30 01:34:46 +01:00
|
|
|
connect(rep, SIGNAL(finished()), SLOT(downloadFinished()));
|
2016-11-06 20:58:54 +00:00
|
|
|
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
|
2013-09-30 01:34:46 +01:00
|
|
|
connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
2013-12-15 14:00:09 +00:00
|
|
|
m_total_progress = bytesTotal;
|
|
|
|
m_progress = bytesReceived;
|
2015-07-26 16:55:29 +01:00
|
|
|
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
|
2013-09-30 01:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2016-08-14 01:33:31 +01:00
|
|
|
if(error == QNetworkReply::OperationCanceledError)
|
|
|
|
{
|
|
|
|
qCritical() << "Aborted " << m_url.toString();
|
|
|
|
m_status = Job_Aborted;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// error happened during download.
|
|
|
|
qCritical() << "Failed " << m_url.toString() << " with reason " << error;
|
|
|
|
m_status = Job_Failed;
|
|
|
|
}
|
2013-09-30 01:34:46 +01:00
|
|
|
}
|
|
|
|
|
2013-11-17 10:44:18 +00:00
|
|
|
void ForgeXzDownload::failAndTryNextMirror()
|
|
|
|
{
|
|
|
|
m_status = Job_Failed;
|
2013-12-15 14:00:09 +00:00
|
|
|
emit failed(m_index_within_job);
|
2013-11-17 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 01:34:46 +01:00
|
|
|
void ForgeXzDownload::downloadFinished()
|
|
|
|
{
|
|
|
|
// if the download succeeded
|
2016-08-14 01:33:31 +01:00
|
|
|
if (m_status != Job_Failed && m_status != Job_Aborted)
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
|
|
|
// nothing went wrong...
|
|
|
|
m_status = Job_Finished;
|
2013-11-17 10:44:18 +00:00
|
|
|
if (m_pack200_xz_file.isOpen())
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
|
|
|
// we actually downloaded something! process and isntall it
|
|
|
|
decompressAndInstall();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-17 10:44:18 +00:00
|
|
|
// something bad happened -- on the local machine!
|
2013-11-16 19:36:01 +00:00
|
|
|
m_status = Job_Failed;
|
2013-09-30 01:34:46 +01:00
|
|
|
m_pack200_xz_file.remove();
|
2013-10-06 00:13:40 +01:00
|
|
|
m_reply.reset();
|
2013-12-15 14:00:09 +00:00
|
|
|
emit failed(m_index_within_job);
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 01:33:31 +01:00
|
|
|
else if(m_status == Job_Aborted)
|
|
|
|
{
|
|
|
|
m_pack200_xz_file.remove();
|
|
|
|
m_reply.reset();
|
|
|
|
emit failed(m_index_within_job);
|
|
|
|
emit aborted(m_index_within_job);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-30 01:34:46 +01:00
|
|
|
// else the download failed
|
|
|
|
else
|
|
|
|
{
|
2013-11-17 10:44:18 +00:00
|
|
|
m_status = Job_Failed;
|
2013-09-30 01:34:46 +01:00
|
|
|
m_pack200_xz_file.close();
|
|
|
|
m_pack200_xz_file.remove();
|
2013-10-06 00:13:40 +01:00
|
|
|
m_reply.reset();
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgeXzDownload::downloadReadyRead()
|
|
|
|
{
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2013-11-17 10:44:18 +00:00
|
|
|
if (!m_pack200_xz_file.isOpen())
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
|
|
|
if (!m_pack200_xz_file.open())
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Can't open the file... the job failed
|
|
|
|
*/
|
|
|
|
m_reply->abort();
|
2013-12-15 14:00:09 +00:00
|
|
|
emit failed(m_index_within_job);
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_pack200_xz_file.write(m_reply->readAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "xz.h"
|
|
|
|
#include "unpack200.h"
|
2013-10-01 08:05:58 +01:00
|
|
|
#include <stdexcept>
|
2016-03-28 19:52:14 +01:00
|
|
|
#include <unistd.h>
|
2013-09-30 01:34:46 +01:00
|
|
|
|
|
|
|
const size_t buffer_size = 8196;
|
|
|
|
|
2016-08-14 01:33:31 +01:00
|
|
|
// NOTE: once this gets here, it can't be aborted anymore. we don't care.
|
2013-09-30 01:34:46 +01:00
|
|
|
void ForgeXzDownload::decompressAndInstall()
|
|
|
|
{
|
|
|
|
// rewind the downloaded temp file
|
|
|
|
m_pack200_xz_file.seek(0);
|
|
|
|
// de-xz'd file
|
2013-11-18 23:47:59 +00:00
|
|
|
QTemporaryFile pack200_file("./dl_temp.XXXXXX");
|
2013-09-30 01:34:46 +01:00
|
|
|
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 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
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 01:53:05 +00:00
|
|
|
b.in_size = m_pack200_xz_file.read((char *)in, sizeof(in));
|
2013-09-30 01:34:46 +01:00
|
|
|
b.in_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = xz_dec_run(s, &b);
|
|
|
|
|
|
|
|
if (b.out_pos == sizeof(out))
|
|
|
|
{
|
2013-11-04 01:53:05 +00:00
|
|
|
if (pack200_file.write((char *)out, b.out_pos) != b.out_pos)
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
|
|
|
// msg = "Write error\n";
|
|
|
|
xz_dec_end(s);
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
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 01:53:05 +00:00
|
|
|
if (pack200_file.write((char *)out, b.out_pos) != b.out_pos)
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
|
|
|
// 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 01:14:14 +00:00
|
|
|
qCritical() << "Memory allocation failed\n";
|
2013-09-30 01:34:46 +01:00
|
|
|
xz_dec_end(s);
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_MEMLIMIT_ERROR:
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Memory usage limit reached\n";
|
2013-09-30 01:34:46 +01:00
|
|
|
xz_dec_end(s);
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_FORMAT_ERROR:
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Not a .xz file\n";
|
2013-09-30 01:34:46 +01:00
|
|
|
xz_dec_end(s);
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_OPTIONS_ERROR:
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Unsupported options in the .xz headers\n";
|
2013-09-30 01:34:46 +01:00
|
|
|
xz_dec_end(s);
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case XZ_DATA_ERROR:
|
|
|
|
case XZ_BUF_ERROR:
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "File is corrupt\n";
|
2013-09-30 01:34:46 +01:00
|
|
|
xz_dec_end(s);
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Bug!\n";
|
2013-09-30 01:34:46 +01:00
|
|
|
xz_dec_end(s);
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 23:47:59 +00:00
|
|
|
m_pack200_xz_file.remove();
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2013-09-30 01:34:46 +01:00
|
|
|
// revert pack200
|
2013-12-22 17:49:52 +00:00
|
|
|
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 01:14:14 +00:00
|
|
|
qCritical() << "Error reopening " << pack200_file.fileName();
|
2013-12-22 17:49:52 +00:00
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
2016-03-28 19:52:14 +01:00
|
|
|
int handle_in_dup = dup (handle_in);
|
|
|
|
if(handle_in_dup == -1)
|
|
|
|
{
|
|
|
|
qCritical() << "Error reopening " << pack200_file.fileName();
|
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FILE *file_in = fdopen (handle_in_dup, "rb");
|
2013-12-22 17:49:52 +00:00
|
|
|
if(!file_in)
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error reopening " << pack200_file.fileName();
|
2013-12-22 17:49:52 +00:00
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QFile qfile_out(m_target_path);
|
|
|
|
if(!qfile_out.open(QIODevice::WriteOnly))
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error opening " << qfile_out.fileName();
|
2013-12-22 17:49:52 +00:00
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int handle_out = qfile_out.handle();
|
|
|
|
if(handle_out == -1)
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error opening " << qfile_out.fileName();
|
2013-12-22 17:49:52 +00:00
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
2016-03-28 19:52:14 +01:00
|
|
|
int handle_out_dup = dup (handle_out);
|
|
|
|
if(handle_out_dup == -1)
|
|
|
|
{
|
|
|
|
qCritical() << "Error reopening " << qfile_out.fileName();
|
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FILE *file_out = fdopen (handle_out_dup, "wb");
|
2013-12-22 17:49:52 +00:00
|
|
|
if(!file_out)
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error opening " << qfile_out.fileName();
|
2013-12-22 17:49:52 +00:00
|
|
|
failAndTryNextMirror();
|
|
|
|
return;
|
|
|
|
}
|
2013-09-30 01:34:46 +01:00
|
|
|
try
|
|
|
|
{
|
2016-03-28 19:52:14 +01:00
|
|
|
// NOTE: this takes ownership of both FILE pointers. That's why we duplicate them above.
|
2013-12-22 17:49:52 +00:00
|
|
|
unpack_200(file_in, file_out);
|
2013-09-30 01:34:46 +01:00
|
|
|
}
|
2013-11-04 01:53:05 +00:00
|
|
|
catch (std::runtime_error &err)
|
2013-09-30 01:34:46 +01:00
|
|
|
{
|
2013-11-16 19:36:01 +00:00
|
|
|
m_status = Job_Failed;
|
2015-02-02 01:14:14 +00:00
|
|
|
qCritical() << "Error unpacking " << pack200_file.fileName() << " : " << err.what();
|
2013-09-30 01:34:46 +01:00
|
|
|
QFile f(m_target_path);
|
2013-11-04 01:53:05 +00:00
|
|
|
if (f.exists())
|
2013-09-30 01:34:46 +01:00
|
|
|
f.remove();
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-11-18 23:47:59 +00:00
|
|
|
pack200_file.remove();
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2013-09-30 01:34:46 +01:00
|
|
|
QFile jar_file(m_target_path);
|
|
|
|
|
|
|
|
if (!jar_file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
jar_file.remove();
|
2013-11-17 10:44:18 +00:00
|
|
|
failAndTryNextMirror();
|
2013-09-30 01:34:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-03-26 15:56:57 +00:00
|
|
|
auto hash = QCryptographicHash::hash(jar_file.readAll(), QCryptographicHash::Md5);
|
|
|
|
m_entry->setMD5Sum(hash.toHex().constData());
|
2013-09-30 01:34:46 +01:00
|
|
|
jar_file.close();
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2013-09-30 01:34:46 +01:00
|
|
|
QFileInfo output_file_info(m_target_path);
|
2016-03-26 15:56:57 +00:00
|
|
|
m_entry->setETag(m_reply->rawHeader("ETag").constData());
|
|
|
|
m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch());
|
|
|
|
m_entry->setStale(false);
|
2015-01-31 15:59:03 +00:00
|
|
|
ENV.metacache()->updateEntry(m_entry);
|
2013-09-30 01:34:46 +01:00
|
|
|
|
2013-10-06 00:13:40 +01:00
|
|
|
m_reply.reset();
|
2013-12-15 14:00:09 +00:00
|
|
|
emit succeeded(m_index_within_job);
|
2013-09-30 01:34:46 +01:00
|
|
|
}
|
2016-08-14 01:33:31 +01:00
|
|
|
|
|
|
|
bool ForgeXzDownload::abort()
|
|
|
|
{
|
|
|
|
if(m_reply)
|
|
|
|
m_reply->abort();
|
|
|
|
m_status = Job_Aborted;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ForgeXzDownload::canAbort()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|