QMimeDatabase: Fix handling of duplicate mimetype definitions.

When both freedesktop.org.xml and kde.xml define text/x-qml (*.qml),
the XML provider would look up *.qml, see two mimetypes, and treat that
as a glob conflict, and proceed with contents-based-determination,
which for this sample file, would find "C source" due to the C comment.

Fixed by ignoring duplicate pattern-mimetype associations.

The binary-cache provider doesn't have this problem, update-mime-database
already filters out duplicates when generating the on-disk extension tree.

Change-Id: Ie335b0b419e7413fa0550779709513f68c2bfc68
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2012-10-22 12:42:05 +02:00 committed by The Qt Project
parent 9346cb9552
commit 7721c3d27c
5 changed files with 80 additions and 6 deletions

View File

@ -165,14 +165,14 @@ void QMimeAllGlobPatterns::addGlob(const QMimeGlobPattern &glob)
// The bulk of the patterns is *.foo with weight 50 --> those go into the fast patterns hash.
const QString extension = pattern.mid(2).toLower();
QStringList &patterns = m_fastPatterns[extension]; // find or create
// This would just slow things down: if (!patterns.contains(glob.mimeType()))
if (!patterns.contains(glob.mimeType()))
patterns.append(glob.mimeType());
} else {
if (glob.weight() > 50) {
// This would just slow things down: if (!m_highWeightGlobs.hasPattern(glob.mimeType(), glob.pattern()))
if (!m_highWeightGlobs.hasPattern(glob.mimeType(), glob.pattern()))
m_highWeightGlobs.append(glob);
} else {
//This would just slow things down: if (!m_lowWeightGlobs.hasPattern(glob.mimeType(), glob.pattern()))
if (!m_lowWeightGlobs.hasPattern(glob.mimeType(), glob.pattern()))
m_lowWeightGlobs.append(glob);
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="text/x-qml">
<comment>Qt Markup Language file</comment>
<magic priority="80">
<match type="string" value="import Qt " offset="0:256"/>
</magic>
<glob pattern="*.qml"/>
</mime-type>
</mime-info>

View File

@ -0,0 +1,44 @@
/****************************************************************************
**
** Copyright (C) 2012 David Faure <faure@kde.org>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 1.1
Item {
}

View File

@ -52,6 +52,7 @@
#include <QtTest/QtTest>
static const char yastFileName[] ="yast2-metapackage-handler-mimetypes.xml";
static const char qmlAgainFileName[] ="qml-again.xml";
void initializeLang()
{
@ -121,6 +122,10 @@ void tst_QMimeDatabase::initTestCase()
QVERIFY2(!m_yastMimeTypes.isEmpty(),
qPrintable(QString::fromLatin1("Cannot find '%1' starting from '%2'").
arg(yastFileName, QDir::currentPath())));
m_qmlAgainFileName = QFINDTESTDATA(qmlAgainFileName);
QVERIFY2(!m_qmlAgainFileName.isEmpty(),
qPrintable(QString::fromLatin1("Cannot find '%1' starting from '%2'").
arg(qmlAgainFileName, QDir::currentPath())));
init();
}
@ -796,11 +801,14 @@ void tst_QMimeDatabase::installNewGlobalMimeType()
const QString destDir = mimeDir + QLatin1String("/packages/");
const QString destFile = destDir + QLatin1String(yastFileName);
QFile::remove(destFile);
const QString destQmlFile = destDir + QLatin1String(qmlAgainFileName);
QFile::remove(destQmlFile);
//qDebug() << destFile;
if (!QFileInfo(destDir).isDir())
QVERIFY(QDir(m_globalXdgDir).mkpath(destDir));
QVERIFY(QFile::copy(m_yastMimeTypes, destFile));
QVERIFY(QFile::copy(m_qmlAgainFileName, destQmlFile));
if (!waitAndRunUpdateMimeDatabase(mimeDir))
QSKIP("shared-mime-info not found, skipping mime.cache test");
@ -809,8 +817,17 @@ void tst_QMimeDatabase::installNewGlobalMimeType()
QVERIFY(db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid());
checkHasMimeType("text/x-suse-ymp");
// Test that a double-definition of a mimetype doesn't lead to sniffing ("conflicting globs").
const QString qmlTestFile = QFINDTESTDATA("test.qml");
QVERIFY2(!qmlTestFile.isEmpty(),
qPrintable(QString::fromLatin1("Cannot find '%1' starting from '%2'").
arg("test.qml", QDir::currentPath())));
QCOMPARE(db.mimeTypeForFile(qmlTestFile).name(),
QString::fromLatin1("text/x-qml"));
// Now test removing it again
QFile::remove(destFile);
QFile::remove(destQmlFile);
if (!waitAndRunUpdateMimeDatabase(mimeDir))
QSKIP("shared-mime-info not found, skipping mime.cache test");
QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(),

View File

@ -97,6 +97,7 @@ private:
QString m_globalXdgDir;
QString m_localXdgDir;
QString m_yastMimeTypes;
QString m_qmlAgainFileName;
QTemporaryDir m_temporaryDir;
QString m_testSuite;
};