fix(libarchive): use multibyte encoding aware libarchive functions when dealing with filenames
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
@@ -25,6 +25,16 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#if defined Q_OS_WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
// clang-format off
|
||||
#include <windows.h>
|
||||
#include <fileapi.h>
|
||||
// clang-format on
|
||||
#endif
|
||||
|
||||
namespace MMCZip {
|
||||
|
||||
ArchiveWriter::ArchiveWriter(const QString& archiveName) : m_filename(archiveName) {}
|
||||
@@ -55,8 +65,8 @@ bool ArchiveWriter::open()
|
||||
return false;
|
||||
}
|
||||
|
||||
auto archiveNameUtf8 = m_filename.toUtf8();
|
||||
if (archive_write_open_filename(m_archive, archiveNameUtf8.constData()) != ARCHIVE_OK) {
|
||||
auto archiveNameW = m_filename.toStdWString();
|
||||
if (archive_write_open_filename_w(m_archive, archiveNameW.data()) != ARCHIVE_OK) {
|
||||
qCritical() << "Failed to open archive file:" << m_filename << "-" << archive_error_string(m_archive);
|
||||
return false;
|
||||
}
|
||||
@@ -97,16 +107,46 @@ bool ArchiveWriter::addFile(const QString& fileName, const QString& fileDest)
|
||||
}
|
||||
|
||||
auto fileDestUtf8 = fileDest.toUtf8();
|
||||
archive_entry_set_pathname(entry, fileDestUtf8.constData());
|
||||
archive_entry_set_pathname_utf8(entry, fileDestUtf8.constData());
|
||||
|
||||
QByteArray utf8 = fileInfo.absoluteFilePath().toUtf8();
|
||||
const char* cpath = utf8.constData();
|
||||
struct stat st;
|
||||
if (stat(cpath, &st) != 0) {
|
||||
qCritical() << "Failed to stat file:" << fileInfo.filePath();
|
||||
#if defined Q_OS_WIN32
|
||||
{
|
||||
// Windows needs to use this method, thanks I hate it.
|
||||
|
||||
auto widePath = fileInfo.absoluteFilePath().toStdWString();
|
||||
HANDLE file_handle = CreateFileW(widePath.data(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (file_handle == INVALID_HANDLE_VALUE) {
|
||||
qCritical() << "Failed to stat file:" << fileInfo.filePath();
|
||||
return false;
|
||||
}
|
||||
|
||||
BY_HANDLE_FILE_INFORMATION file_info;
|
||||
if (!GetFileInformationByHandle(file_handle, &file_info)) {
|
||||
qCritical() << "Failed to stat file:" << fileInfo.filePath();
|
||||
CloseHandle(file_handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
archive_entry_copy_bhfi(entry, &file_info);
|
||||
CloseHandle(file_handle);
|
||||
}
|
||||
// This should handle the copying of most attributes
|
||||
archive_entry_copy_stat(entry, &st);
|
||||
#else
|
||||
{
|
||||
// this only works for myltibyte encoded filenames if the local is properly set,
|
||||
// a wide charater version doens't seem to exist: here's hopeing...
|
||||
|
||||
QByteArray utf8 = fileInfo.absoluteFilePath().toUtf8();
|
||||
const char* cpath = utf8.constData();
|
||||
struct stat st;
|
||||
if (stat(cpath, &st) != 0) {
|
||||
qCritical() << "Failed to stat file:" << fileInfo.filePath();
|
||||
return false;
|
||||
}
|
||||
|
||||
// This should handle the copying of most attributes
|
||||
archive_entry_copy_stat(entry, &st);
|
||||
}
|
||||
#endif
|
||||
|
||||
// However:
|
||||
// "The [filetype] constants used by stat(2) may have different numeric values from the corresponding [libarchive constants]."
|
||||
@@ -116,11 +156,11 @@ bool ArchiveWriter::addFile(const QString& fileName, const QString& fileDest)
|
||||
|
||||
// We also need to manually copy some attributes from the link itself, as `stat` above operates on its target
|
||||
auto target = fileInfo.symLinkTarget().toUtf8();
|
||||
archive_entry_set_symlink(entry, target.constData());
|
||||
archive_entry_set_symlink_utf8(entry, target.constData());
|
||||
archive_entry_set_size(entry, 0);
|
||||
archive_entry_set_perm(entry, fileInfo.permissions());
|
||||
} else if (fileInfo.isFile()) {
|
||||
archive_entry_set_filetype(entry, AE_IFREG);
|
||||
archive_entry_set_filetype(entry, AE_IFREG);
|
||||
} else {
|
||||
qCritical() << "Unsupported file type:" << fileInfo.filePath();
|
||||
return false;
|
||||
@@ -169,7 +209,7 @@ bool ArchiveWriter::addFile(const QString& fileDest, const QByteArray& data)
|
||||
}
|
||||
|
||||
auto fileDestUtf8 = fileDest.toUtf8();
|
||||
archive_entry_set_pathname(entry, fileDestUtf8.constData());
|
||||
archive_entry_set_pathname_utf8(entry, fileDestUtf8.constData());
|
||||
archive_entry_set_perm(entry, 0644);
|
||||
|
||||
archive_entry_set_filetype(entry, AE_IFREG);
|
||||
@@ -210,4 +250,4 @@ std::unique_ptr<archive, void (*)(archive*)> ArchiveWriter::createDiskWriter()
|
||||
|
||||
return extPtr;
|
||||
}
|
||||
} // namespace MMCZip
|
||||
} // namespace MMCZip
|
||||
|
||||
Reference in New Issue
Block a user