kutils: implement KArchive::data() argument to limit the data size

to be used (for example) in MIME type determination, i.e. read only a small
chunk of the data

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-05-18 05:13:02 +03:00
parent 6f968d6f03
commit 7697b90bb2
2 changed files with 16 additions and 6 deletions

View file

@ -201,7 +201,7 @@ public:
bool copyData(struct archive* readarchive, struct archive* writearchive);
bool writeFile(struct archive* writearchive, QFile *file);
bool readData(struct archive* readarchive, QByteArray *buffer);
bool readData(struct archive* readarchive, QByteArray *buffer, const int maxsize);
QString tempFilePath() const;
@ -413,7 +413,7 @@ bool KArchivePrivate::writeFile(struct archive* writearchive, QFile *file)
return (readsize >= 0);
}
bool KArchivePrivate::readData(struct archive* readarchive, QByteArray *buffer)
bool KArchivePrivate::readData(struct archive* readarchive, QByteArray *buffer, const int maxsize)
{
char readbuffer[KARCHIVE_BUFFSIZE];
ssize_t readsize = archive_read_data(readarchive, readbuffer, sizeof(readbuffer));
@ -424,11 +424,17 @@ bool KArchivePrivate::readData(struct archive* readarchive, QByteArray *buffer)
if (result != ARCHIVE_OK) {
m_error = archive_error_string(readarchive);
kDebug() << "archive_read_data" << m_error;
buffer->clear();
return false;
}
buffer->append(readbuffer, readsize);
if (maxsize > 0 && buffer->size() >= maxsize) {
buffer->resize(maxsize);
return true;
}
readsize = archive_read_data(readarchive, readbuffer, sizeof(readbuffer));
}
@ -1151,7 +1157,7 @@ KArchiveEntry KArchive::entry(const QString &path) const
}
QByteArray KArchive::data(const QString &path) const
QByteArray KArchive::data(const QString &path, const int maxsize) const
{
QByteArray result;
@ -1185,7 +1191,7 @@ QByteArray KArchive::data(const QString &path) const
const QByteArray pathname = archive_entry_pathname(entry);
const QString pathnamestring = QFile::decodeName(pathname);
if (pathnamestring == path) {
d->readData(readarchive, &result);
d->readData(readarchive, &result, maxsize);
found = true;
break;

View file

@ -135,8 +135,12 @@ public:
QList<KArchiveEntry> list(const QString &path = QString()) const;
//! @brief Get entry information for path in archive
KArchiveEntry entry(const QString &path) const;
//! @brief Get data for path in archive
QByteArray data(const QString &path) const;
/*!
@brief Get data for path in archive
@param path path to get data for
@param maxsize the limit for data, if zero then it is as if there is no limit
*/
QByteArray data(const QString &path, const int maxsize = 0) const;
//! @brief Returns if path is readable archive
bool isReadable() const;