From cdc9bed83f9110bdf41133a3489ed2fca543ee32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 29 May 2017 01:32:28 +0200 Subject: [PATCH] NOISSUE move some zip utility functions back to quazip It's not entirely clean yet. --- api/logic/InstanceImportTask.cpp | 2 +- api/logic/MMCZip.cpp | 163 ++---------------- api/logic/MMCZip.h | 18 +- api/logic/minecraft/launch/ExtractNatives.cpp | 3 +- libraries/quazip | 2 +- 5 files changed, 19 insertions(+), 169 deletions(-) diff --git a/api/logic/InstanceImportTask.cpp b/api/logic/InstanceImportTask.cpp index 13ad863e..f1b3d5aa 100644 --- a/api/logic/InstanceImportTask.cpp +++ b/api/logic/InstanceImportTask.cpp @@ -99,7 +99,7 @@ void InstanceImportTask::extractAndTweak() QDir extractDir(m_stagingPath); qDebug() << "Attempting to create instance from" << m_archivePath; - m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, m_archivePath, extractDir.absolutePath(), MMCZip::Option::NoPermissions); + m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractDir, m_archivePath, extractDir.absolutePath()); connect(&m_extractFutureWatcher, &QFutureWatcher::finished, this, &InstanceImportTask::extractFinished); connect(&m_extractFutureWatcher, &QFutureWatcher::canceled, this, &InstanceImportTask::extractAborted); m_extractFutureWatcher.setFuture(m_extractFuture); diff --git a/api/logic/MMCZip.cpp b/api/logic/MMCZip.cpp index 6b4a9ff8..725aea71 100644 --- a/api/logic/MMCZip.cpp +++ b/api/logic/MMCZip.cpp @@ -24,67 +24,14 @@ see quazip/(un)MMCZip.h files for details. Basically it's the zlib license. */ #include -#include #include +#include +#include #include "MMCZip.h" #include "FileSystem.h" #include -bool copyData(QIODevice &inFile, QIODevice &outFile) -{ - while (!inFile.atEnd()) - { - char buf[4096]; - qint64 readLen = inFile.read(buf, 4096); - if (readLen <= 0) - return false; - if (outFile.write(buf, readLen) != readLen) - return false; - } - return true; -} - -bool compressFile(QuaZip *zip, QString fileName, QString fileDest) -{ - if (!zip) - { - return false; - } - if (zip->getMode() != QuaZip::mdCreate && zip->getMode() != QuaZip::mdAppend && - zip->getMode() != QuaZip::mdAdd) - { - return false; - } - - QFile inFile; - inFile.setFileName(fileName); - if (!inFile.open(QIODevice::ReadOnly)) - { - return false; - } - - QuaZipFile outFile(zip); - if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, inFile.fileName()))) - { - return false; - } - - if (!copyData(inFile, outFile) || outFile.getZipError() != UNZ_OK) - { - return false; - } - - outFile.close(); - if (outFile.getZipError() != UNZ_OK) - { - return false; - } - inFile.close(); - - return true; -} - bool MMCZip::compressSubDir(QuaZip* zip, QString dir, QString origDir, QSet& added, QString prefix, const SeparatorPrefixTree <'/'> * blacklist) { if (!zip) return false; @@ -151,7 +98,7 @@ bool MMCZip::compressSubDir(QuaZip* zip, QString dir, QString origDir, QSet &contained, - std::function filter) +bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet &contained, std::function filter) { QuaZip modZip(from.filePath()); modZip.open(QuaZip::mdUnzip); @@ -199,7 +145,7 @@ bool MMCZip::mergeZipFiles(QuaZip *into, QFileInfo from, QSet &containe fileInsideMod.close(); return false; } - if (!copyData(fileInsideMod, zipOutFile)) + if (!JlCompress::copyData(fileInsideMod, zipOutFile)) { zipOutFile.close(); fileInsideMod.close(); @@ -247,8 +193,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const else if (mod.type() == Mod::MOD_SINGLEFILE) { auto filename = mod.filename(); - if (!compressFile(&zipOut, filename.absoluteFilePath(), - filename.fileName())) + if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName())) { zipOut.close(); QFile::remove(targetJarPath); @@ -264,7 +209,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QDir dir(what_to_zip); dir.cdUp(); QString parent_dir = dir.absolutePath(); - if (!compressSubDir(&zipOut, what_to_zip, parent_dir, addedFiles)) + if (!MMCZip::compressSubDir(&zipOut, what_to_zip, parent_dir, addedFiles)) { zipOut.close(); QFile::remove(targetJarPath); @@ -328,7 +273,7 @@ bool MMCZip::compressDir(QString zipFile, QString dir, QString prefix, const Sep } QSet added; - if (!compressSubDir(&zip, dir, dir, added, prefix, blacklist)) + if (!MMCZip::compressSubDir(&zip, dir, dir, added, prefix, blacklist)) { QFile::remove(zipFile); return false; @@ -379,89 +324,7 @@ bool MMCZip::findFilesInZip(QuaZip * zip, const QString & what, QStringList & re return !result.isEmpty(); } -bool removeFile(QStringList listFile) -{ - bool ret = true; - for (int i = 0; i < listFile.count(); i++) - { - ret &= QFile::remove(listFile.at(i)); - } - return ret; -} - -bool MMCZip::extractFile(QuaZip *zip, const QString &fileName, const QString &fileDest, MMCZip::Options opts) -{ - if(!zip) - return false; - - if (zip->getMode() != QuaZip::mdUnzip) - return false; - - if (!fileName.isEmpty()) - zip->setCurrentFile(fileName); - - QuaZipFile inFile(zip); - if (!inFile.open(QIODevice::ReadOnly) || inFile.getZipError() != UNZ_OK) - return false; - - QDir curDir; - if (fileDest.endsWith('/')) - { - if (!curDir.mkpath(fileDest)) - { - return false; - } - } - else - { - if (!curDir.mkpath(QFileInfo(fileDest).absolutePath())) - { - return false; - } - } - - QuaZipFileInfo64 info; - if (!zip->getCurrentFileInfo(&info)) - return false; - - QFile::Permissions srcPerm = info.getPermissions(); - if (fileDest.endsWith('/') && QFileInfo(fileDest).isDir()) - { - if (!opts.testFlag(Option::NoPermissions) && srcPerm != 0) - { - QFile(fileDest).setPermissions(srcPerm); - } - return true; - } - - QFile outFile; - outFile.setFileName(fileDest); - if (!outFile.open(QIODevice::WriteOnly)) - return false; - - if (!copyData(inFile, outFile) || inFile.getZipError() != UNZ_OK) - { - outFile.close(); - removeFile(QStringList(fileDest)); - return false; - } - outFile.close(); - - inFile.close(); - if (inFile.getZipError() != UNZ_OK) - { - removeFile(QStringList(fileDest)); - return false; - } - - if (!opts.testFlag(Option::NoPermissions) && srcPerm != 0) - { - outFile.setPermissions(srcPerm); - } - return true; -} - -QStringList MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target, MMCZip::Options opts) +QStringList MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target) { QDir directory(target); QStringList extracted; @@ -482,9 +345,9 @@ QStringList MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QSt { absFilePath += "/"; } - if (!MMCZip::extractFile(zip, "", absFilePath, opts)) + if (!JlCompress::extractFile(zip, "", absFilePath)) { - removeFile(extracted); + JlCompress::removeFile(extracted); return QStringList(); } extracted.append(absFilePath); @@ -492,12 +355,12 @@ QStringList MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QSt return extracted; } -QStringList MMCZip::extractDir(QString fileCompressed, QString dir, MMCZip::Options opts) +QStringList MMCZip::extractDir(QString fileCompressed, QString dir) { QuaZip zip(fileCompressed); if (!zip.open(QuaZip::mdUnzip)) { return {}; } - return MMCZip::extractSubDir(&zip, "", dir, opts); + return MMCZip::extractSubDir(&zip, "", dir); } diff --git a/api/logic/MMCZip.h b/api/logic/MMCZip.h index 7608352c..ca8b349f 100644 --- a/api/logic/MMCZip.h +++ b/api/logic/MMCZip.h @@ -9,7 +9,7 @@ #include "multimc_logic_export.h" -class QuaZip; +#include namespace MMCZip { @@ -64,22 +64,10 @@ namespace MMCZip */ bool MULTIMC_LOGIC_EXPORT findFilesInZip(QuaZip * zip, const QString & what, QStringList & result, const QString &root = QString()); - enum Option { - NoPermissions = 1 - }; - Q_DECLARE_FLAGS(Options, Option) - - /** - * Extract a single file to a destination - * - * \return true if it succeeds - */ - bool MULTIMC_LOGIC_EXPORT extractFile(QuaZip *zip, const QString &fileName, const QString &fileDest, Options opts = 0); - /** * Extract a subdirectory from an archive */ - QStringList MULTIMC_LOGIC_EXPORT extractSubDir(QuaZip *zip, const QString & subdir, const QString &target, Options opts = 0); + QStringList MULTIMC_LOGIC_EXPORT extractSubDir(QuaZip *zip, const QString & subdir, const QString &target); /** * Extract a whole archive. @@ -89,6 +77,6 @@ namespace MMCZip * \param opts Extra options. * \return The list of the full paths of the files extracted, empty on failure. */ - QStringList MULTIMC_LOGIC_EXPORT extractDir(QString fileCompressed, QString dir, Options opts = 0); + QStringList MULTIMC_LOGIC_EXPORT extractDir(QString fileCompressed, QString dir); } diff --git a/api/logic/minecraft/launch/ExtractNatives.cpp b/api/logic/minecraft/launch/ExtractNatives.cpp index 9f36f8f4..f30dea89 100644 --- a/api/logic/minecraft/launch/ExtractNatives.cpp +++ b/api/logic/minecraft/launch/ExtractNatives.cpp @@ -18,7 +18,6 @@ #include #include -#include #include #include "MMCZip.h" #include "FileSystem.h" @@ -54,7 +53,7 @@ static bool unzipNatives(QString source, QString targetFolder, bool applyJnilibH name = replaceSuffix(name, ".jnilib", ".dylib"); } QString absFilePath = directory.absoluteFilePath(name); - if (!MMCZip::extractFile(&zip, "", absFilePath)) + if (!JlCompress::extractFile(&zip, "", absFilePath)) { return false; } diff --git a/libraries/quazip b/libraries/quazip index 2c40fc85..88375553 160000 --- a/libraries/quazip +++ b/libraries/quazip @@ -1 +1 @@ -Subproject commit 2c40fc85b465b9af33424322de30e4fd09c5c0ba +Subproject commit 8837555312a7f95dc5c4ce6474860b1c159d8105