2022-03-19 11:46:56 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-07-16 21:47:47 +01:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-03-19 11:46:56 +00:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2023-07-16 18:57:57 +01:00
|
|
|
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
|
2017-05-31 08:17:45 +01:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2017-05-31 08:17:45 +01:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-31 08:17:45 +01:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* 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.
|
2017-05-31 08:17:45 +01:00
|
|
|
*/
|
2022-01-25 11:53:33 +00:00
|
|
|
|
2023-07-16 18:53:58 +01:00
|
|
|
#include "MMCZip.h"
|
2022-01-24 21:55:57 +00:00
|
|
|
#include <quazip/quazip.h>
|
|
|
|
#include <quazip/quazipdir.h>
|
|
|
|
#include <quazip/quazipfile.h>
|
2015-10-05 00:47:27 +01:00
|
|
|
#include "FileSystem.h"
|
2015-04-12 21:50:52 +01:00
|
|
|
|
2022-12-01 18:15:15 +00:00
|
|
|
#include <QCoreApplication>
|
2015-04-12 21:50:52 +01:00
|
|
|
#include <QDebug>
|
2023-11-03 20:55:10 +00:00
|
|
|
#include <QFileInfo>
|
2023-07-30 22:43:04 +01:00
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#if defined(LAUNCHER_APPLICATION)
|
2023-07-16 18:57:57 +01:00
|
|
|
#include <QtConcurrentRun>
|
2023-07-30 22:43:04 +01:00
|
|
|
#endif
|
2015-04-12 21:50:52 +01:00
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
namespace MMCZip {
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
bool mergeZipFiles(QuaZip* into, QFileInfo from, QSet<QString>& contained, const FilterFunction filter)
|
2015-04-12 21:50:52 +01:00
|
|
|
{
|
|
|
|
QuaZip modZip(from.filePath());
|
|
|
|
modZip.open(QuaZip::mdUnzip);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2015-04-12 21:50:52 +01:00
|
|
|
QuaZipFile fileInsideMod(&modZip);
|
|
|
|
QuaZipFile zipOutFile(into);
|
2023-07-16 18:53:58 +01:00
|
|
|
for (bool more = modZip.goToFirstFile(); more; more = modZip.goToNextFile()) {
|
2015-04-12 21:50:52 +01:00
|
|
|
QString filename = modZip.getCurrentFileName();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (filter && !filter(filename)) {
|
|
|
|
qDebug() << "Skipping file " << filename << " from " << from.fileName() << " - filtered";
|
2015-04-12 21:50:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
if (contained.contains(filename)) {
|
|
|
|
qDebug() << "Skipping already contained file " << filename << " from " << from.fileName();
|
2015-04-12 21:50:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
contained.insert(filename);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!fileInsideMod.open(QIODevice::ReadOnly)) {
|
2015-04-12 21:50:52 +01:00
|
|
|
qCritical() << "Failed to open " << filename << " from " << from.fileName();
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2015-04-12 21:50:52 +01:00
|
|
|
QuaZipNewInfo info_out(fileInsideMod.getActualFileName());
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!zipOutFile.open(QIODevice::WriteOnly, info_out)) {
|
2015-04-12 21:50:52 +01:00
|
|
|
qCritical() << "Failed to open " << filename << " in the jar";
|
|
|
|
fileInsideMod.close();
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!JlCompress::copyData(fileInsideMod, zipOutFile)) {
|
2015-04-12 21:50:52 +01:00
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
|
|
|
qCritical() << "Failed to copy data of " << filename << " into the jar";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
zipOutFile.close();
|
|
|
|
fileInsideMod.close();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
bool compressDirFiles(QuaZip* zip, QString dir, QFileInfoList files, bool followSymlinks)
|
2022-01-28 12:26:59 +00:00
|
|
|
{
|
|
|
|
QDir directory(dir);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!directory.exists())
|
|
|
|
return false;
|
2022-01-28 12:26:59 +00:00
|
|
|
|
|
|
|
for (auto e : files) {
|
|
|
|
auto filePath = directory.relativeFilePath(e.absoluteFilePath());
|
2023-02-09 01:52:50 +00:00
|
|
|
auto srcPath = e.absoluteFilePath();
|
2023-02-10 02:48:40 +00:00
|
|
|
if (followSymlinks) {
|
|
|
|
if (e.isSymLink()) {
|
|
|
|
srcPath = e.symLinkTarget();
|
|
|
|
} else {
|
|
|
|
srcPath = e.canonicalFilePath();
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!JlCompress::compressFile(zip, srcPath, filePath))
|
|
|
|
return false;
|
2022-01-28 12:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
bool compressDirFiles(QString fileCompressed, QString dir, QFileInfoList files, bool followSymlinks)
|
2022-01-28 12:26:59 +00:00
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
|
|
|
QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!zip.open(QuaZip::mdCreate)) {
|
2022-01-28 12:26:59 +00:00
|
|
|
QFile::remove(fileCompressed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-10 02:48:40 +00:00
|
|
|
auto result = compressDirFiles(&zip, dir, files, followSymlinks);
|
2022-01-28 12:26:59 +00:00
|
|
|
|
|
|
|
zip.close();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (zip.getZipError() != 0) {
|
2022-01-28 12:26:59 +00:00
|
|
|
QFile::remove(fileCompressed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:10:06 +01:00
|
|
|
#if defined(LAUNCHER_APPLICATION)
|
2022-01-24 22:02:06 +00:00
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
bool createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods)
|
2022-01-24 22:02:06 +00:00
|
|
|
{
|
|
|
|
QuaZip zipOut(targetJarPath);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!zipOut.open(QuaZip::mdCreate)) {
|
2022-01-24 22:02:06 +00:00
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to open the minecraft.jar for modding";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Files already added to the jar.
|
|
|
|
// These files will be skipped.
|
|
|
|
QSet<QString> addedFiles;
|
|
|
|
|
|
|
|
// Modify the jar
|
2022-07-23 18:21:28 +01:00
|
|
|
// This needs to be done in reverse-order to ensure we respect the loading order of components
|
2023-07-16 18:53:58 +01:00
|
|
|
for (auto i = mods.crbegin(); i != mods.crend(); i++) {
|
2022-07-23 18:21:28 +01:00
|
|
|
const auto* mod = *i;
|
2022-01-24 22:02:06 +00:00
|
|
|
// do not merge disabled mods.
|
2022-06-26 18:17:15 +01:00
|
|
|
if (!mod->enabled())
|
2022-01-24 22:02:06 +00:00
|
|
|
continue;
|
2023-07-16 18:53:58 +01:00
|
|
|
if (mod->type() == ResourceType::ZIPFILE) {
|
|
|
|
if (!mergeZipFiles(&zipOut, mod->fileinfo(), addedFiles)) {
|
2022-01-24 22:02:06 +00:00
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 18:17:15 +01:00
|
|
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
} else if (mod->type() == ResourceType::SINGLEFILE) {
|
2022-01-24 22:02:06 +00:00
|
|
|
// FIXME: buggy - does not work with addedFiles
|
2022-06-26 18:17:15 +01:00
|
|
|
auto filename = mod->fileinfo();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName())) {
|
2022-01-24 22:02:06 +00:00
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 18:17:15 +01:00
|
|
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
addedFiles.insert(filename.fileName());
|
2023-07-16 18:53:58 +01:00
|
|
|
} else if (mod->type() == ResourceType::FOLDER) {
|
2022-01-28 12:26:59 +00:00
|
|
|
// untested, but seems to be unused / not possible to reach
|
2022-01-24 22:02:06 +00:00
|
|
|
// FIXME: buggy - does not work with addedFiles
|
2022-06-26 18:17:15 +01:00
|
|
|
auto filename = mod->fileinfo();
|
2022-01-24 22:02:06 +00:00
|
|
|
QString what_to_zip = filename.absoluteFilePath();
|
|
|
|
QDir dir(what_to_zip);
|
|
|
|
dir.cdUp();
|
|
|
|
QString parent_dir = dir.absolutePath();
|
2022-01-28 12:26:59 +00:00
|
|
|
auto files = QFileInfoList();
|
2023-07-16 18:57:57 +01:00
|
|
|
collectFileListRecursively(what_to_zip, nullptr, &files, nullptr);
|
2022-01-28 12:26:59 +00:00
|
|
|
|
|
|
|
for (auto e : files) {
|
|
|
|
if (addedFiles.contains(e.filePath()))
|
|
|
|
files.removeAll(e);
|
|
|
|
}
|
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
if (!compressDirFiles(&zipOut, parent_dir, files)) {
|
2022-01-24 22:02:06 +00:00
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 18:17:15 +01:00
|
|
|
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
qDebug() << "Adding folder " << filename.fileName() << " from " << filename.absoluteFilePath();
|
|
|
|
} else {
|
2022-01-24 22:02:06 +00:00
|
|
|
// Make sure we do not continue launching when something is missing or undefined...
|
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
2022-06-26 18:17:15 +01:00
|
|
|
qCritical() << "Failed to add unknown mod type" << mod->fileinfo().fileName() << "to the jar.";
|
2022-01-24 22:02:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!mergeZipFiles(&zipOut, QFileInfo(sourceJarPath), addedFiles, [](const QString key) { return !key.contains("META-INF"); })) {
|
2022-01-24 22:02:06 +00:00
|
|
|
zipOut.close();
|
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to insert minecraft.jar contents.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompress the jar
|
|
|
|
zipOut.close();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (zipOut.getZipError() != 0) {
|
2022-01-24 22:02:06 +00:00
|
|
|
QFile::remove(targetJarPath);
|
|
|
|
qCritical() << "Failed to finalize minecraft.jar!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-26 00:10:06 +01:00
|
|
|
#endif
|
2022-01-24 22:02:06 +00:00
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
QString findFolderOfFileInZip(QuaZip* zip, const QString& what, const QStringList& ignore_paths, const QString& root)
|
2015-09-13 03:21:26 +01:00
|
|
|
{
|
|
|
|
QuaZipDir rootDir(zip, root);
|
2022-12-01 18:15:15 +00:00
|
|
|
for (auto&& fileName : rootDir.entryList(QDir::Files)) {
|
|
|
|
if (fileName == what)
|
2015-09-13 03:21:26 +01:00
|
|
|
return root;
|
2022-12-01 18:15:15 +00:00
|
|
|
|
|
|
|
QCoreApplication::processEvents();
|
2015-09-13 03:21:26 +01:00
|
|
|
}
|
2022-12-01 18:15:15 +00:00
|
|
|
|
|
|
|
// Recurse the search to non-ignored subfolders
|
|
|
|
for (auto&& fileName : rootDir.entryList(QDir::Dirs)) {
|
|
|
|
if (ignore_paths.contains(fileName))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QString result = findFolderOfFileInZip(zip, what, ignore_paths, root + fileName);
|
|
|
|
if (!result.isEmpty())
|
2015-09-13 03:21:26 +01:00
|
|
|
return result;
|
|
|
|
}
|
2022-12-01 18:15:15 +00:00
|
|
|
|
|
|
|
return {};
|
2015-09-13 03:21:26 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
bool findFilesInZip(QuaZip* zip, const QString& what, QStringList& result, const QString& root)
|
2015-09-13 03:21:26 +01:00
|
|
|
{
|
|
|
|
QuaZipDir rootDir(zip, root);
|
2023-07-16 18:53:58 +01:00
|
|
|
for (auto fileName : rootDir.entryList(QDir::Files)) {
|
|
|
|
if (fileName == what) {
|
2015-09-13 03:21:26 +01:00
|
|
|
result.append(root);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
for (auto fileName : rootDir.entryList(QDir::Dirs)) {
|
2015-09-13 03:21:26 +01:00
|
|
|
findFilesInZip(zip, what, result, root + fileName);
|
|
|
|
}
|
|
|
|
return !result.isEmpty();
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
std::optional<QStringList> extractSubDir(QuaZip* zip, const QString& subdir, const QString& target)
|
2015-09-13 03:21:26 +01:00
|
|
|
{
|
2023-02-24 23:08:29 +00:00
|
|
|
auto target_top_dir = QUrl::fromLocalFile(target);
|
2023-02-10 08:17:48 +00:00
|
|
|
|
2015-09-13 03:21:26 +01:00
|
|
|
QStringList extracted;
|
2021-02-09 04:04:23 +00:00
|
|
|
|
2018-04-07 21:33:26 +01:00
|
|
|
qDebug() << "Extracting subdir" << subdir << "from" << zip->getZipName() << "to" << target;
|
2021-02-09 04:04:23 +00:00
|
|
|
auto numEntries = zip->getEntriesCount();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (numEntries < 0) {
|
2021-02-09 04:04:23 +00:00
|
|
|
qWarning() << "Failed to enumerate files in archive";
|
2022-07-20 13:21:44 +01:00
|
|
|
return std::nullopt;
|
2023-07-16 18:53:58 +01:00
|
|
|
} else if (numEntries == 0) {
|
2021-02-09 04:04:23 +00:00
|
|
|
qDebug() << "Extracting empty archives seems odd...";
|
|
|
|
return extracted;
|
2023-07-16 18:53:58 +01:00
|
|
|
} else if (!zip->goToFirstFile()) {
|
2018-04-07 21:33:26 +01:00
|
|
|
qWarning() << "Failed to seek to first file in zip";
|
2022-07-20 13:21:44 +01:00
|
|
|
return std::nullopt;
|
2015-09-13 03:21:26 +01:00
|
|
|
}
|
2021-02-09 04:04:23 +00:00
|
|
|
|
2023-02-24 23:08:29 +00:00
|
|
|
do {
|
|
|
|
QString file_name = zip->getCurrentFileName();
|
|
|
|
if (!file_name.startsWith(subdir))
|
2015-09-13 03:21:26 +01:00
|
|
|
continue;
|
2022-05-16 14:50:46 +01:00
|
|
|
|
2023-02-24 23:08:29 +00:00
|
|
|
auto relative_file_name = QDir::fromNativeSeparators(file_name.remove(0, subdir.size()));
|
|
|
|
auto original_name = relative_file_name;
|
2022-05-16 14:50:46 +01:00
|
|
|
|
2023-02-10 09:46:21 +00:00
|
|
|
// Fix subdirs/files ending with a / getting transformed into absolute paths
|
2023-02-24 23:08:29 +00:00
|
|
|
if (relative_file_name.startsWith('/'))
|
|
|
|
relative_file_name = relative_file_name.mid(1);
|
2023-02-10 09:46:21 +00:00
|
|
|
|
2022-05-16 14:50:46 +01:00
|
|
|
// Fix weird "folders with a single file get squashed" thing
|
2023-02-24 23:08:29 +00:00
|
|
|
QString sub_path;
|
|
|
|
if (relative_file_name.contains('/') && !relative_file_name.endsWith('/')) {
|
|
|
|
sub_path = relative_file_name.section('/', 0, -2) + '/';
|
|
|
|
FS::ensureFolderPathExists(FS::PathCombine(target, sub_path));
|
2022-05-16 14:50:46 +01:00
|
|
|
|
2023-02-24 23:08:29 +00:00
|
|
|
relative_file_name = relative_file_name.split('/').last();
|
2022-05-16 14:50:46 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 23:08:29 +00:00
|
|
|
QString target_file_path;
|
|
|
|
if (relative_file_name.isEmpty()) {
|
|
|
|
target_file_path = target + '/';
|
|
|
|
} else {
|
2023-03-05 11:24:56 +00:00
|
|
|
target_file_path = FS::PathCombine(target_top_dir.toLocalFile(), sub_path, relative_file_name);
|
2023-02-24 23:08:29 +00:00
|
|
|
if (relative_file_name.endsWith('/') && !target_file_path.endsWith('/'))
|
|
|
|
target_file_path += '/';
|
2023-02-10 08:17:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 23:08:29 +00:00
|
|
|
if (!target_top_dir.isParentOf(QUrl::fromLocalFile(target_file_path))) {
|
2023-07-16 18:53:58 +01:00
|
|
|
qWarning() << "Extracting" << relative_file_name << "was cancelled, because it was effectively outside of the target path"
|
|
|
|
<< target;
|
2023-02-10 08:17:48 +00:00
|
|
|
return std::nullopt;
|
2022-05-16 14:50:46 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 23:08:29 +00:00
|
|
|
if (!JlCompress::extractFile(zip, "", target_file_path)) {
|
|
|
|
qWarning() << "Failed to extract file" << original_name << "to" << target_file_path;
|
2017-05-29 00:32:28 +01:00
|
|
|
JlCompress::removeFile(extracted);
|
2022-07-20 13:21:44 +01:00
|
|
|
return std::nullopt;
|
2015-09-13 03:21:26 +01:00
|
|
|
}
|
2022-05-16 14:50:46 +01:00
|
|
|
|
2023-02-24 23:08:29 +00:00
|
|
|
extracted.append(target_file_path);
|
2023-11-03 20:55:10 +00:00
|
|
|
auto fileInfo = QFileInfo(target_file_path);
|
|
|
|
if (fileInfo.isFile()) {
|
|
|
|
auto permissions = fileInfo.permissions();
|
|
|
|
auto maxPermisions = QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser | QFileDevice::Permission::ExeUser |
|
|
|
|
QFileDevice::Permission::ReadGroup | QFileDevice::Permission::ReadOther;
|
|
|
|
auto minPermisions = QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser;
|
|
|
|
|
|
|
|
auto newPermisions = (permissions & maxPermisions) | minPermisions;
|
|
|
|
if (newPermisions != permissions) {
|
|
|
|
if (!QFile::setPermissions(target_file_path, permissions)) {
|
|
|
|
qWarning() << (QObject::tr("Could not fix permissions for %1").arg(target_file_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 23:08:29 +00:00
|
|
|
qDebug() << "Extracted file" << relative_file_name << "to" << target_file_path;
|
2015-09-13 03:21:26 +01:00
|
|
|
} while (zip->goToNextFile());
|
2023-02-24 23:08:29 +00:00
|
|
|
|
2015-09-13 03:21:26 +01:00
|
|
|
return extracted;
|
|
|
|
}
|
2017-05-25 00:24:27 +01:00
|
|
|
|
2020-08-24 23:13:43 +01:00
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
bool extractRelFile(QuaZip* zip, const QString& file, const QString& target)
|
2020-08-24 23:13:43 +01:00
|
|
|
{
|
|
|
|
return JlCompress::extractFile(zip, file, target);
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:17:45 +01:00
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
std::optional<QStringList> extractDir(QString fileCompressed, QString dir)
|
2017-05-25 00:24:27 +01:00
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!zip.open(QuaZip::mdUnzip)) {
|
2021-02-09 04:04:23 +00:00
|
|
|
// check if this is a minimum size empty zip file...
|
|
|
|
QFileInfo fileInfo(fileCompressed);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (fileInfo.size() == 22) {
|
2021-02-09 04:04:23 +00:00
|
|
|
return QStringList();
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
|
|
|
;
|
2022-07-20 13:21:44 +01:00
|
|
|
return std::nullopt;
|
2017-05-25 00:24:27 +01:00
|
|
|
}
|
2023-07-16 18:57:57 +01:00
|
|
|
return extractSubDir(&zip, "", dir);
|
2017-05-25 00:24:27 +01:00
|
|
|
}
|
2020-08-24 23:13:43 +01:00
|
|
|
|
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
std::optional<QStringList> extractDir(QString fileCompressed, QString subdir, QString dir)
|
2020-08-24 23:13:43 +01:00
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!zip.open(QuaZip::mdUnzip)) {
|
2021-02-09 04:04:23 +00:00
|
|
|
// check if this is a minimum size empty zip file...
|
|
|
|
QFileInfo fileInfo(fileCompressed);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (fileInfo.size() == 22) {
|
2021-02-09 04:04:23 +00:00
|
|
|
return QStringList();
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
|
|
|
;
|
2022-07-20 13:21:44 +01:00
|
|
|
return std::nullopt;
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
2023-07-16 18:57:57 +01:00
|
|
|
return extractSubDir(&zip, subdir, dir);
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ours
|
2023-07-16 18:57:57 +01:00
|
|
|
bool extractFile(QString fileCompressed, QString file, QString target)
|
2020-08-24 23:13:43 +01:00
|
|
|
{
|
|
|
|
QuaZip zip(fileCompressed);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!zip.open(QuaZip::mdUnzip)) {
|
2021-02-09 04:04:23 +00:00
|
|
|
// check if this is a minimum size empty zip file...
|
|
|
|
QFileInfo fileInfo(fileCompressed);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (fileInfo.size() == 22) {
|
2021-02-09 04:04:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
|
|
|
|
return false;
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
2023-07-16 18:57:57 +01:00
|
|
|
return extractRelFile(&zip, file, target);
|
2020-08-24 23:13:43 +01:00
|
|
|
}
|
2022-01-28 11:37:22 +00:00
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
bool collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList* files, FilterFunction excludeFilter)
|
2023-07-16 18:53:58 +01:00
|
|
|
{
|
2022-01-28 11:37:22 +00:00
|
|
|
QDir rootDirectory(rootDir);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!rootDirectory.exists())
|
|
|
|
return false;
|
2022-01-28 11:37:22 +00:00
|
|
|
|
|
|
|
QDir directory;
|
|
|
|
if (subDir == nullptr)
|
|
|
|
directory = rootDirectory;
|
|
|
|
else
|
|
|
|
directory = QDir(subDir);
|
|
|
|
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!directory.exists())
|
|
|
|
return false; // shouldn't ever happen
|
2022-01-28 11:37:22 +00:00
|
|
|
|
|
|
|
// recurse directories
|
|
|
|
QFileInfoList entries = directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
|
2023-07-16 18:53:58 +01:00
|
|
|
for (const auto& e : entries) {
|
2022-01-28 11:37:22 +00:00
|
|
|
if (!collectFileListRecursively(rootDir, e.filePath(), files, excludeFilter))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect files
|
|
|
|
entries = directory.entryInfoList(QDir::Files);
|
2023-07-16 18:53:58 +01:00
|
|
|
for (const auto& e : entries) {
|
2022-01-28 11:37:22 +00:00
|
|
|
QString relativeFilePath = rootDirectory.relativeFilePath(e.absoluteFilePath());
|
|
|
|
if (excludeFilter && excludeFilter(relativeFilePath)) {
|
|
|
|
qDebug() << "Skipping file " << relativeFilePath;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
files->append(e); // we want the original paths for compressDirFiles
|
2022-01-28 11:37:22 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-16 18:57:57 +01:00
|
|
|
|
2023-07-30 22:43:04 +01:00
|
|
|
#if defined(LAUNCHER_APPLICATION)
|
2023-07-16 18:57:57 +01:00
|
|
|
void ExportToZipTask::executeTask()
|
|
|
|
{
|
2023-07-17 15:42:15 +01:00
|
|
|
setStatus("Adding files...");
|
|
|
|
setProgress(0, m_files.length());
|
|
|
|
m_build_zip_future = QtConcurrent::run(QThreadPool::globalInstance(), [this]() { return exportZip(); });
|
|
|
|
connect(&m_build_zip_watcher, &QFutureWatcher<ZipResult>::finished, this, &ExportToZipTask::finish);
|
|
|
|
m_build_zip_watcher.setFuture(m_build_zip_future);
|
2023-07-17 14:24:43 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 15:42:15 +01:00
|
|
|
auto ExportToZipTask::exportZip() -> ZipResult
|
2023-07-17 14:24:43 +01:00
|
|
|
{
|
|
|
|
if (!m_dir.exists()) {
|
2023-07-17 15:42:15 +01:00
|
|
|
return ZipResult(tr("Folder doesn't exist"));
|
2023-07-17 14:24:43 +01:00
|
|
|
}
|
|
|
|
if (!m_output.isOpen() && !m_output.open(QuaZip::mdCreate)) {
|
2023-07-17 15:42:15 +01:00
|
|
|
return ZipResult(tr("Could not create file"));
|
2023-07-17 14:24:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto fileName : m_extra_files.keys()) {
|
2023-07-17 15:42:15 +01:00
|
|
|
if (m_build_zip_future.isCanceled())
|
|
|
|
return ZipResult();
|
2023-07-17 14:24:43 +01:00
|
|
|
QuaZipFile indexFile(&m_output);
|
|
|
|
if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileName))) {
|
2023-07-17 15:42:15 +01:00
|
|
|
return ZipResult(tr("Could not create:") + fileName);
|
2023-07-16 18:57:57 +01:00
|
|
|
}
|
2023-07-17 14:24:43 +01:00
|
|
|
indexFile.write(m_extra_files[fileName]);
|
|
|
|
}
|
2023-07-16 18:57:57 +01:00
|
|
|
|
2023-07-17 14:24:43 +01:00
|
|
|
for (const QFileInfo& file : m_files) {
|
2023-07-17 15:42:15 +01:00
|
|
|
if (m_build_zip_future.isCanceled())
|
|
|
|
return ZipResult();
|
2023-07-16 18:57:57 +01:00
|
|
|
|
2023-07-17 14:24:43 +01:00
|
|
|
auto absolute = file.absoluteFilePath();
|
|
|
|
auto relative = m_dir.relativeFilePath(absolute);
|
|
|
|
setStatus("Compresing: " + relative);
|
|
|
|
setProgress(m_progress + 1, m_progressTotal);
|
|
|
|
if (m_follow_symlinks) {
|
|
|
|
if (file.isSymLink())
|
|
|
|
absolute = file.symLinkTarget();
|
|
|
|
else
|
|
|
|
absolute = file.canonicalFilePath();
|
2023-07-16 18:57:57 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 14:24:43 +01:00
|
|
|
if (!m_exclude_files.contains(relative) && !JlCompress::compressFile(&m_output, absolute, m_destination_prefix + relative)) {
|
2023-07-17 15:42:15 +01:00
|
|
|
return ZipResult(tr("Could not read and compress %1").arg(relative));
|
2023-07-16 18:57:57 +01:00
|
|
|
}
|
2023-07-17 14:24:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_output.close();
|
|
|
|
if (m_output.getZipError() != 0) {
|
2023-07-17 15:42:15 +01:00
|
|
|
return ZipResult(tr("A zip error occurred"));
|
|
|
|
}
|
|
|
|
return ZipResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExportToZipTask::finish()
|
|
|
|
{
|
|
|
|
if (m_build_zip_future.isCanceled()) {
|
|
|
|
QFile::remove(m_output_path);
|
|
|
|
emitAborted();
|
|
|
|
} else if (auto result = m_build_zip_future.result(); result.has_value()) {
|
|
|
|
QFile::remove(m_output_path);
|
|
|
|
emitFailed(result.value());
|
|
|
|
} else {
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExportToZipTask::abort()
|
|
|
|
{
|
|
|
|
if (m_build_zip_future.isRunning()) {
|
|
|
|
m_build_zip_future.cancel();
|
|
|
|
// NOTE: Here we don't do `emitAborted()` because it will be done when `m_build_zip_future` actually cancels, which may not occur
|
|
|
|
// immediately.
|
|
|
|
return true;
|
2023-07-17 14:24:43 +01:00
|
|
|
}
|
2023-07-17 15:42:15 +01:00
|
|
|
return false;
|
2023-07-16 18:57:57 +01:00
|
|
|
}
|
2023-07-17 14:24:43 +01:00
|
|
|
|
2023-07-19 09:58:27 +01:00
|
|
|
void ExtractZipTask::executeTask()
|
|
|
|
{
|
|
|
|
m_zip_future = QtConcurrent::run(QThreadPool::globalInstance(), [this]() { return extractZip(); });
|
|
|
|
connect(&m_zip_watcher, &QFutureWatcher<ZipResult>::finished, this, &ExtractZipTask::finish);
|
|
|
|
m_zip_watcher.setFuture(m_zip_future);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ExtractZipTask::extractZip() -> ZipResult
|
|
|
|
{
|
|
|
|
auto target = m_output_dir.absolutePath();
|
|
|
|
auto target_top_dir = QUrl::fromLocalFile(target);
|
|
|
|
|
|
|
|
QStringList extracted;
|
|
|
|
|
|
|
|
qDebug() << "Extracting subdir" << m_subdirectory << "from" << m_input->getZipName() << "to" << target;
|
|
|
|
auto numEntries = m_input->getEntriesCount();
|
|
|
|
if (numEntries < 0) {
|
|
|
|
return ZipResult(tr("Failed to enumerate files in archive"));
|
|
|
|
}
|
|
|
|
if (numEntries == 0) {
|
|
|
|
logWarning(tr("Extracting empty archives seems odd..."));
|
|
|
|
return ZipResult();
|
|
|
|
}
|
|
|
|
if (!m_input->goToFirstFile()) {
|
|
|
|
return ZipResult(tr("Failed to seek to first file in zip"));
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus("Extracting files...");
|
|
|
|
setProgress(0, numEntries);
|
|
|
|
do {
|
|
|
|
if (m_zip_future.isCanceled())
|
|
|
|
return ZipResult();
|
|
|
|
setProgress(m_progress + 1, m_progressTotal);
|
|
|
|
QString file_name = m_input->getCurrentFileName();
|
|
|
|
if (!file_name.startsWith(m_subdirectory))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto relative_file_name = QDir::fromNativeSeparators(file_name.remove(0, m_subdirectory.size()));
|
|
|
|
auto original_name = relative_file_name;
|
|
|
|
setStatus("Unziping: " + relative_file_name);
|
|
|
|
|
|
|
|
// Fix subdirs/files ending with a / getting transformed into absolute paths
|
|
|
|
if (relative_file_name.startsWith('/'))
|
|
|
|
relative_file_name = relative_file_name.mid(1);
|
|
|
|
|
|
|
|
// Fix weird "folders with a single file get squashed" thing
|
|
|
|
QString sub_path;
|
|
|
|
if (relative_file_name.contains('/') && !relative_file_name.endsWith('/')) {
|
|
|
|
sub_path = relative_file_name.section('/', 0, -2) + '/';
|
|
|
|
FS::ensureFolderPathExists(FS::PathCombine(target, sub_path));
|
|
|
|
|
|
|
|
relative_file_name = relative_file_name.split('/').last();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString target_file_path;
|
|
|
|
if (relative_file_name.isEmpty()) {
|
|
|
|
target_file_path = target + '/';
|
|
|
|
} else {
|
|
|
|
target_file_path = FS::PathCombine(target_top_dir.toLocalFile(), sub_path, relative_file_name);
|
|
|
|
if (relative_file_name.endsWith('/') && !target_file_path.endsWith('/'))
|
|
|
|
target_file_path += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target_top_dir.isParentOf(QUrl::fromLocalFile(target_file_path))) {
|
|
|
|
return ZipResult(tr("Extracting %1 was cancelled, because it was effectively outside of the target path %2")
|
|
|
|
.arg(relative_file_name, target));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JlCompress::extractFile(m_input.get(), "", target_file_path)) {
|
|
|
|
JlCompress::removeFile(extracted);
|
|
|
|
return ZipResult(tr("Failed to extract file %1 to %2").arg(original_name, target_file_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
extracted.append(target_file_path);
|
2023-11-03 20:55:10 +00:00
|
|
|
auto fileInfo = QFileInfo(target_file_path);
|
|
|
|
if (fileInfo.isFile()) {
|
|
|
|
auto permissions = fileInfo.permissions();
|
|
|
|
auto maxPermisions = QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser | QFileDevice::Permission::ExeUser |
|
|
|
|
QFileDevice::Permission::ReadGroup | QFileDevice::Permission::ReadOther;
|
|
|
|
auto minPermisions = QFileDevice::Permission::ReadUser | QFileDevice::Permission::WriteUser;
|
|
|
|
|
|
|
|
auto newPermisions = (permissions & maxPermisions) | minPermisions;
|
|
|
|
if (newPermisions != permissions) {
|
|
|
|
if (!QFile::setPermissions(target_file_path, permissions)) {
|
|
|
|
logWarning(tr("Could not fix permissions for %1").arg(target_file_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-19 09:58:27 +01:00
|
|
|
|
|
|
|
qDebug() << "Extracted file" << relative_file_name << "to" << target_file_path;
|
|
|
|
} while (m_input->goToNextFile());
|
|
|
|
|
|
|
|
return ZipResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtractZipTask::finish()
|
|
|
|
{
|
|
|
|
if (m_zip_future.isCanceled()) {
|
|
|
|
emitAborted();
|
|
|
|
} else if (auto result = m_zip_future.result(); result.has_value()) {
|
|
|
|
emitFailed(result.value());
|
|
|
|
} else {
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtractZipTask::abort()
|
|
|
|
{
|
|
|
|
if (m_zip_future.isRunning()) {
|
|
|
|
m_zip_future.cancel();
|
|
|
|
// NOTE: Here we don't do `emitAborted()` because it will be done when `m_build_zip_future` actually cancels, which may not occur
|
|
|
|
// immediately.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-16 21:32:24 +01:00
|
|
|
#endif
|
2023-07-30 22:43:04 +01:00
|
|
|
} // namespace MMCZip
|