apply suggestions
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
@@ -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 <alexandru.tripon97@gmail.com>
|
||||
@@ -14,6 +14,9 @@
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Additional note: Portions of this file are released into the public domain
|
||||
* under LicenseRef-PublicDomain.
|
||||
*/
|
||||
#include "ArchiveReader.h"
|
||||
#include <archive.h>
|
||||
@@ -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<bool(File*, bool&)> 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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -41,5 +41,6 @@ class ArchiveWriter {
|
||||
private:
|
||||
struct archive* m_archive = nullptr;
|
||||
QString m_filename;
|
||||
QString m_format = "zip";
|
||||
};
|
||||
} // namespace MMCZip
|
||||
Reference in New Issue
Block a user