QMimeDatabase: introduce QMimeMagicResult to accumulate magic results

Better than two pointer parameters. However it can't be a return
value as we initially thought, it's an in/out parameter, in order to
compare the results in the current directory with those found in earlier
directories.

Change-Id: Ic6137f2627703f8d6930c3f7dd1a1102187eef30
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2023-09-12 19:32:52 +02:00
parent 1f0bc1ade3
commit 071a9c9061
3 changed files with 29 additions and 18 deletions

View File

@ -337,13 +337,14 @@ QMimeType QMimeDatabasePrivate::findByData(const QByteArray &data, int *accuracy
return mimeTypeForName(QStringLiteral("application/x-zerosize"));
}
*accuracyPtr = 0;
QString candidate;
QMimeMagicResult result;
for (const auto &provider : providers())
provider->findByMagic(data, accuracyPtr, &candidate);
provider->findByMagic(data, result);
if (!candidate.isEmpty())
return QMimeType(QMimeTypePrivate(candidate));
if (result.isValid()) {
*accuracyPtr = result.accuracy;
return QMimeType(QMimeTypePrivate(result.candidate));
}
if (isTextFile(data)) {
*accuracyPtr = 5;

View File

@ -348,7 +348,7 @@ bool QMimeBinaryProvider::matchMagicRule(QMimeBinaryProvider::CacheFile *cacheFi
return false;
}
void QMimeBinaryProvider::findByMagic(const QByteArray &data, int *accuracyPtr, QString *candidate)
void QMimeBinaryProvider::findByMagic(const QByteArray &data, QMimeMagicResult &result)
{
const int magicListOffset = m_cacheFile->getUint32(PosMagicListOffset);
const int numMatches = m_cacheFile->getUint32(magicListOffset);
@ -362,11 +362,13 @@ void QMimeBinaryProvider::findByMagic(const QByteArray &data, int *accuracyPtr,
if (matchMagicRule(m_cacheFile.get(), numMatchlets, firstMatchletOffset, data)) {
const int mimeTypeOffset = m_cacheFile->getUint32(off + 4);
const char *mimeType = m_cacheFile->getCharStar(mimeTypeOffset);
*accuracyPtr = m_cacheFile->getUint32(off);
// Return the first match. We have no rules for conflicting magic data...
// (mime.cache itself is sorted, but what about local overrides with a lower prio?)
*candidate = QString::fromLatin1(mimeType);
return;
const int accuracy = static_cast<int>(m_cacheFile->getUint32(off));
if (accuracy > result.accuracy) {
result.accuracy = accuracy;
result.candidate = QString::fromLatin1(mimeType);
// Return the first match, mime.cache is sorted
return;
}
}
}
}
@ -718,14 +720,14 @@ void QMimeXMLProvider::addFileNameMatches(const QString &fileName, QMimeGlobMatc
m_mimeTypeGlobs.matchingGlobs(fileName, result, filterFunc);
}
void QMimeXMLProvider::findByMagic(const QByteArray &data, int *accuracyPtr, QString *candidate)
void QMimeXMLProvider::findByMagic(const QByteArray &data, QMimeMagicResult &result)
{
for (const QMimeMagicRuleMatcher &matcher : std::as_const(m_magicMatchers)) {
if (matcher.matches(data)) {
const int priority = matcher.priority();
if (priority > *accuracyPtr) {
*accuracyPtr = priority;
*candidate = matcher.mimetype();
if (priority > result.accuracy) {
result.accuracy = priority;
result.candidate = matcher.mimetype();
}
}
}

View File

@ -31,6 +31,14 @@ class QMimeMagicRuleMatcher;
class QMimeTypeXMLData;
class QMimeProviderBase;
struct QMimeMagicResult
{
bool isValid() const { return !candidate.isEmpty(); }
QString candidate;
int accuracy = 0;
};
class QMimeProviderBase
{
Q_DISABLE_COPY(QMimeProviderBase)
@ -46,7 +54,7 @@ public:
virtual void addParents(const QString &mime, QStringList &result) = 0;
virtual QString resolveAlias(const QString &name) = 0;
virtual void addAliases(const QString &name, QStringList &result) = 0;
virtual void findByMagic(const QByteArray &data, int *accuracyPtr, QString *candidate) = 0;
virtual void findByMagic(const QByteArray &data, QMimeMagicResult &result) = 0;
virtual void addAllMimeTypes(QList<QMimeType> &result) = 0;
virtual QMimeTypePrivate::LocaleHash localeComments(const QString &name) = 0;
virtual bool hasGlobDeleteAll(const QString &name) = 0;
@ -82,7 +90,7 @@ public:
void addParents(const QString &mime, QStringList &result) override;
QString resolveAlias(const QString &name) override;
void addAliases(const QString &name, QStringList &result) override;
void findByMagic(const QByteArray &data, int *accuracyPtr, QString *candidate) override;
void findByMagic(const QByteArray &data, QMimeMagicResult &result) override;
void addAllMimeTypes(QList<QMimeType> &result) override;
QMimeTypePrivate::LocaleHash localeComments(const QString &name) override;
bool hasGlobDeleteAll(const QString &name) override;
@ -144,7 +152,7 @@ public:
void addParents(const QString &mime, QStringList &result) override;
QString resolveAlias(const QString &name) override;
void addAliases(const QString &name, QStringList &result) override;
void findByMagic(const QByteArray &data, int *accuracyPtr, QString *candidate) override;
void findByMagic(const QByteArray &data, QMimeMagicResult &result) override;
void addAllMimeTypes(QList<QMimeType> &result) override;
void ensureLoaded() override;
QMimeTypePrivate::LocaleHash localeComments(const QString &name) override;