From 792b1d6648fa8868298c076810e5280f132bf6b9 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 26 Nov 2025 19:12:13 +0200 Subject: [PATCH] apply suggestions Signed-off-by: Trial97 --- launcher/archive/ArchiveReader.cpp | 10 ++++-- launcher/archive/ArchiveWriter.cpp | 53 +++++++++++++++--------------- launcher/archive/ArchiveWriter.h | 1 + 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/launcher/archive/ArchiveReader.cpp b/launcher/archive/ArchiveReader.cpp index c6602d50f..638d4b6d5 100644 --- a/launcher/archive/ArchiveReader.cpp +++ b/launcher/archive/ArchiveReader.cpp @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0-only +// SPDX-License-Identifier: GPL-3.0-only AND LicenseRef-PublicDomain /* * Prism Launcher - Minecraft Launcher * Copyright (c) 2025 Trial97 @@ -14,6 +14,9 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Additional note: Portions of this file are released into the public domain + * under LicenseRef-PublicDomain. */ #include "ArchiveReader.h" #include @@ -144,7 +147,7 @@ bool ArchiveReader::File::writeFile(archive* out, QString targetFileName, bool n } auto r = archive_write_finish_entry(out); if (r < ARCHIVE_OK) - qCritical() << "Failed dinish entry:" << archive_error_string(out); + qCritical() << "Failed to finish writing entry:" << archive_error_string(out); return (r > ARCHIVE_WARN); } @@ -155,7 +158,8 @@ bool ArchiveReader::parse(std::function doStuff) archive_read_support_format_all(a); archive_read_support_filter_all(a); auto fileName = m_archivePath.toUtf8(); - if (archive_read_open_filename(a, fileName.constData(), 10240) != ARCHIVE_OK) { + const auto blockSize = 10240; + if (archive_read_open_filename(a, fileName.constData(), blockSize) != ARCHIVE_OK) { qCritical() << "Failed to open archive file:" << m_archivePath << "-" << f->error(); return false; } diff --git a/launcher/archive/ArchiveWriter.cpp b/launcher/archive/ArchiveWriter.cpp index 73f8b7da5..f69ee3f74 100644 --- a/launcher/archive/ArchiveWriter.cpp +++ b/launcher/archive/ArchiveWriter.cpp @@ -47,7 +47,8 @@ bool ArchiveWriter::open() return false; } - archive_write_set_format_zip(m_archive); + auto format = m_format.toUtf8(); + archive_write_set_format_by_name(m_archive, format.constData()); if (archive_write_set_options(m_archive, "hdrcharset=UTF-8") != ARCHIVE_OK) { qCritical() << "Failed to open archive file:" << m_filename << "-" << archive_error_string(m_archive); @@ -84,7 +85,7 @@ bool ArchiveWriter::addFile(const QString& fileName, const QString& fileDest) { QFileInfo fileInfo(fileName); if (!fileInfo.exists()) { - qCritical() << "File does not exists:" << fileInfo.filePath(); + qCritical() << "File does not exist:" << fileInfo.filePath(); return false; } @@ -101,16 +102,15 @@ bool ArchiveWriter::addFile(const QString& fileName, const QString& fileDest) QByteArray utf8 = fileInfo.absoluteFilePath().toUtf8(); const char* cpath = utf8.constData(); struct stat st; - if (stat(cpath, &st) == 0) { - archive_entry_copy_stat(entry, &st); + if (stat(cpath, &st) != 0) { + qCritical() << "Failed to stat file:" << fileInfo.filePath(); } + archive_entry_copy_stat(entry, &st); archive_entry_set_perm(entry, fileInfo.permissions()); if (fileInfo.isSymLink()) { auto target = fileInfo.symLinkTarget().toUtf8(); - archive_entry_set_filetype(entry, AE_IFLNK); archive_entry_set_symlink(entry, target.constData()); - archive_entry_set_size(entry, 0); if (archive_write_header(m_archive, entry) != ARCHIVE_OK) { qCritical() << "Failed to write symlink header for:" << fileDest << "-" << archive_error_string(m_archive); @@ -119,39 +119,38 @@ bool ArchiveWriter::addFile(const QString& fileName, const QString& fileDest) return true; } - if (!fileInfo.isFile()) { + if (!fileInfo.isFile() && !fileInfo.isDir()) { qCritical() << "Unsupported file type:" << fileInfo.filePath(); return false; } - QFile file(fileInfo.absoluteFilePath()); - if (!file.open(QIODevice::ReadOnly)) { - qCritical() << "Failed to open file: " << fileInfo.filePath(); - return false; - } - - archive_entry_set_filetype(entry, AE_IFREG); - archive_entry_set_size(entry, file.size()); - if (archive_write_header(m_archive, entry) != ARCHIVE_OK) { qCritical() << "Failed to write header for: " << fileDest << "-" << archive_error_string(m_archive); return false; } - constexpr qint64 chunkSize = 8192; - QByteArray buffer; - buffer.resize(chunkSize); - - while (!file.atEnd()) { - auto bytesRead = file.read(buffer.data(), chunkSize); - if (bytesRead < 0) { - qCritical() << "Read error in file: " << fileInfo.filePath(); + if (fileInfo.isFile()) { + QFile file(fileInfo.absoluteFilePath()); + if (!file.open(QIODevice::ReadOnly)) { + qCritical() << "Failed to open file: " << fileInfo.filePath(); return false; } - if (archive_write_data(m_archive, buffer.constData(), bytesRead) < 0) { - qCritical() << "Write error in archive for: " << fileDest; - return false; + constexpr qint64 chunkSize = 8192; + QByteArray buffer; + buffer.resize(chunkSize); + + while (!file.atEnd()) { + auto bytesRead = file.read(buffer.data(), chunkSize); + if (bytesRead < 0) { + qCritical() << "Read error in file: " << fileInfo.filePath(); + return false; + } + + if (archive_write_data(m_archive, buffer.constData(), bytesRead) < 0) { + qCritical() << "Write error in archive for: " << fileDest; + return false; + } } } return true; diff --git a/launcher/archive/ArchiveWriter.h b/launcher/archive/ArchiveWriter.h index c351d4bb8..807bb297c 100644 --- a/launcher/archive/ArchiveWriter.h +++ b/launcher/archive/ArchiveWriter.h @@ -41,5 +41,6 @@ class ArchiveWriter { private: struct archive* m_archive = nullptr; QString m_filename; + QString m_format = "zip"; }; } // namespace MMCZip \ No newline at end of file