Commit 25989cbf authored by Eike Ziller's avatar Eike Ziller

MimeDatabase: Avoid opening files even if already file name matches

Change-Id: If669985f8ef852c31ecbaae76c533c7286e5a294
Reviewed-by: default avatarDaniel Teske <daniel.teske@theqtcompany.com>
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 1e29d5c0
......@@ -175,12 +175,16 @@ MimeType MimeDatabasePrivate::mimeTypeForFileNameAndData(const QString &fileName
// Extension is unknown, or matches multiple mimetypes.
// Pass 2) Match on content, if we can read the data
const bool openedByUs = !device->isOpen() && device->open(QIODevice::ReadOnly);
if (device->isOpen()) {
// Read 16K in one go (QIODEVICE_BUFFERSIZE in qiodevice_p.h).
// This is much faster than seeking back and forth into QIODevice.
const QByteArray data = device->peek(16384);
if (openedByUs)
device->close();
int magicAccuracy = 0;
MimeType candidateByData(findByData(data, &magicAccuracy));
......@@ -485,7 +489,6 @@ MimeType MimeDatabase::mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode
int priority = 0;
switch (mode) {
case MatchDefault:
file.open(QIODevice::ReadOnly); // isOpen() will be tested by method below
return d->mimeTypeForFileNameAndData(fileInfo.absoluteFilePath(), &file, &priority);
case MatchExtension:
locker.unlock();
......@@ -655,10 +658,7 @@ MimeType MimeDatabase::mimeTypeForUrl(const QUrl &url) const
MimeType MimeDatabase::mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const
{
int accuracy = 0;
const bool openedByUs = !device->isOpen() && device->open(QIODevice::ReadOnly);
const MimeType result = d->mimeTypeForFileNameAndData(fileName, device, &accuracy);
if (openedByUs)
device->close();
return result;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment