From 41867c25f8438eb34dddba9d058e32c73638b4f5 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 17 Dec 2022 13:09:33 +0200 Subject: [PATCH] Use QFileInfo's file times in UTC for file timestamps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is inherently faster than getting it in UTC from the underlying native API stat call, then converting it to the Local Time Zone just to compare them. The same goes for any use-case where you get a QDateTime then the first thing you do is call t.to{Msec,Secs}SinceEpoch(). Change-Id: Ic13bcfd99b937c9f10f102ea7741832950a553c6 Reviewed-by: Thiago Macieira Reviewed-by: Kai Köhne --- src/corelib/io/qfilesystemwatcher_polling_p.h | 4 +- src/corelib/io/qlockfile.cpp | 4 +- src/corelib/mimetypes/qmimeprovider.cpp | 4 +- src/gui/image/qiconloader.cpp | 6 +-- src/gui/image/qpixmap.cpp | 2 +- src/network/access/qnetworkdiskcache.cpp | 4 +- src/tools/cmake_automoc_parser/main.cpp | 2 +- tests/auto/corelib/io/qdir/tst_qdir.cpp | 2 +- .../corelib/io/qfileinfo/tst_qfileinfo.cpp | 42 +++++++++---------- .../qmimedatabase/tst_qmimedatabase.cpp | 2 +- tests/auto/gui/image/qicon/tst_qicon.cpp | 4 +- 11 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/corelib/io/qfilesystemwatcher_polling_p.h b/src/corelib/io/qfilesystemwatcher_polling_p.h index a12ff4b540..2d7423b39d 100644 --- a/src/corelib/io/qfilesystemwatcher_polling_p.h +++ b/src/corelib/io/qfilesystemwatcher_polling_p.h @@ -46,7 +46,7 @@ class QPollingFileSystemWatcherEngine : public QFileSystemWatcherEngine : ownerId(fileInfo.ownerId()), groupId(fileInfo.groupId()), permissions(fileInfo.permissions()), - lastModified(fileInfo.lastModified()) + lastModified(fileInfo.lastModified(QTimeZone::UTC)) { if (fileInfo.isDir()) { entries = fileInfo.absoluteDir().entryList(QDir::AllEntries); @@ -65,7 +65,7 @@ class QPollingFileSystemWatcherEngine : public QFileSystemWatcherEngine return (ownerId != fileInfo.ownerId() || groupId != fileInfo.groupId() || permissions != fileInfo.permissions() - || lastModified != fileInfo.lastModified()); + || lastModified != fileInfo.lastModified(QTimeZone::UTC)); } }; diff --git a/src/corelib/io/qlockfile.cpp b/src/corelib/io/qlockfile.cpp index 522bb081ee..5a452226e0 100644 --- a/src/corelib/io/qlockfile.cpp +++ b/src/corelib/io/qlockfile.cpp @@ -255,7 +255,7 @@ bool QLockFile::tryLock(int timeout) return false; case LockFailedError: if (!d->isLocked && d->isApparentlyStale()) { - if (Q_UNLIKELY(QFileInfo(d->fileName).lastModified() > QDateTime::currentDateTimeUtc())) + if (Q_UNLIKELY(QFileInfo(d->fileName).lastModified(QTimeZone::UTC) > QDateTime::currentDateTimeUtc())) qInfo("QLockFile: Lock file '%ls' has a modification time in the future", qUtf16Printable(d->fileName)); // Stale lock from another thread/process // Ensure two processes don't remove it at the same time @@ -413,7 +413,7 @@ bool QLockFilePrivate::isApparentlyStale() const } } - const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTimeUtc()); + const qint64 age = QFileInfo(fileName).lastModified(QTimeZone::UTC).msecsTo(QDateTime::currentDateTimeUtc()); return staleLockTime > 0 && qAbs(age) > staleLockTime; } diff --git a/src/corelib/mimetypes/qmimeprovider.cpp b/src/corelib/mimetypes/qmimeprovider.cpp index 5b7bec336e..8732d4c915 100644 --- a/src/corelib/mimetypes/qmimeprovider.cpp +++ b/src/corelib/mimetypes/qmimeprovider.cpp @@ -110,7 +110,7 @@ bool QMimeBinaryProvider::CacheFile::load() const int minor = getUint16(2); m_valid = (major == 1 && minor >= 1 && minor <= 2); } - m_mtime = QFileInfo(file).lastModified(); + m_mtime = QFileInfo(file).lastModified(QTimeZone::UTC); return m_valid; } @@ -155,7 +155,7 @@ enum { bool QMimeBinaryProvider::checkCacheChanged() { QFileInfo fileInfo(m_cacheFile->file); - if (fileInfo.lastModified() > m_cacheFile->m_mtime) { + if (fileInfo.lastModified(QTimeZone::UTC) > m_cacheFile->m_mtime) { // Deletion can't happen by just running update-mime-database. // But the user could use rm -rf :-) m_cacheFile->reload(); // will mark itself as invalid on failure diff --git a/src/gui/image/qiconloader.cpp b/src/gui/image/qiconloader.cpp index 98c0527dd5..f5ae864746 100644 --- a/src/gui/image/qiconloader.cpp +++ b/src/gui/image/qiconloader.cpp @@ -215,7 +215,7 @@ QIconCacheGtkReader::QIconCacheGtkReader(const QString &dirName) : m_isValid(false) { QFileInfo info(dirName + "/icon-theme.cache"_L1); - if (!info.exists() || info.lastModified() < QFileInfo(dirName).lastModified()) + if (!info.exists() || info.lastModified(QTimeZone::UTC) < QFileInfo(dirName).lastModified(QTimeZone::UTC)) return; m_file.setFileName(info.absoluteFilePath()); if (!m_file.open(QFile::ReadOnly)) @@ -230,13 +230,13 @@ QIconCacheGtkReader::QIconCacheGtkReader(const QString &dirName) m_isValid = true; // Check that all the directories are older than the cache - auto lastModified = info.lastModified(); + const QDateTime lastModified = info.lastModified(QTimeZone::UTC); quint32 dirListOffset = read32(8); quint32 dirListLen = read32(dirListOffset); for (uint i = 0; i < dirListLen; ++i) { quint32 offset = read32(dirListOffset + 4 + 4 * i); if (!m_isValid || offset >= m_size || lastModified < QFileInfo(dirName + u'/' - + QString::fromUtf8(reinterpret_cast(m_data + offset))).lastModified()) { + + QString::fromUtf8(reinterpret_cast(m_data + offset))).lastModified(QTimeZone::UTC)) { m_isValid = false; return; } diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index 4accabacc1..8e98a4f254 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -714,7 +714,7 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers QString key = "qt_pixmap"_L1 % info.absoluteFilePath() - % HexString(info.lastModified().toSecsSinceEpoch()) + % HexString(info.lastModified(QTimeZone::UTC).toSecsSinceEpoch()) % HexString(info.size()) % HexString(data ? data->pixelType() : QPlatformPixmap::PixmapType); diff --git a/src/network/access/qnetworkdiskcache.cpp b/src/network/access/qnetworkdiskcache.cpp index 6b52b8d49a..72923543b5 100644 --- a/src/network/access/qnetworkdiskcache.cpp +++ b/src/network/access/qnetworkdiskcache.cpp @@ -493,9 +493,9 @@ qint64 QNetworkDiskCache::expire() QString path = info.filePath(); QString fileName = info.fileName(); if (fileName.endsWith(CACHE_POSTFIX)) { - const QDateTime birthTime = info.fileTime(QFile::FileBirthTime); + const QDateTime birthTime = info.birthTime(QTimeZone::UTC); cacheItems.insert(birthTime.isValid() ? birthTime - : info.fileTime(QFile::FileMetadataChangeTime), path); + : info.metadataChangeTime(QTimeZone::UTC), path); totalSize += info.size(); } } diff --git a/src/tools/cmake_automoc_parser/main.cpp b/src/tools/cmake_automoc_parser/main.cpp index 801483cdd7..86e10d48ad 100644 --- a/src/tools/cmake_automoc_parser/main.cpp +++ b/src/tools/cmake_automoc_parser/main.cpp @@ -195,7 +195,7 @@ static bool writeJsonFiles(const QList &fileList, const QString &fileLi // processed by cmake_automoc parser for (const auto &jsonFile : fileList) { const qint64 jsonFileLastModified = - QFileInfo(jsonFile).lastModified().toMSecsSinceEpoch(); + QFileInfo(jsonFile).lastModified(QTimeZone::UTC).toMSecsSinceEpoch(); if (jsonFileLastModified > timestamp) { timestamp = jsonFileLastModified; } diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp index 65a7daaf7d..39bf938c1b 100644 --- a/tests/auto/corelib/io/qdir/tst_qdir.cpp +++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp @@ -999,7 +999,7 @@ void tst_QDir::entryListTimedSort() QFileInfo aFileInfo(aFile); QFileInfo bFileInfo(bFile); - QVERIFY(bFileInfo.lastModified().msecsTo(aFileInfo.lastModified()) < 0); + QVERIFY(bFileInfo.lastModified(QTimeZone::UTC).msecsTo(aFileInfo.lastModified(QTimeZone::UTC)) < 0); QCOMPARE(actual.size(), 2); QCOMPARE(actual.first(), bFileInfo.fileName()); diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index 4a2e5cb5e3..8ddbe75809 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -1109,8 +1109,8 @@ void tst_QFileInfo::fileTimes() { // try to guess if file times on this filesystem round to the second QFileInfo cwd("."); - if (cwd.lastModified().toMSecsSinceEpoch() % 1000 == 0 - && cwd.lastRead().toMSecsSinceEpoch() % 1000 == 0) { + if (cwd.lastModified(QTimeZone::UTC).toMSecsSinceEpoch() % 1000 == 0 + && cwd.lastRead(QTimeZone::UTC).toMSecsSinceEpoch() % 1000 == 0) { fsClockSkew = sleepTime = 1000; noAccessTime = qIsLikelyToBeFat(fileName); @@ -1130,12 +1130,12 @@ void tst_QFileInfo::fileTimes() QDateTime birthTime, writeTime, metadataChangeTime, readTime; // --- Create file and write to it - beforeBirth = QDateTime::currentDateTime().addMSecs(-fsClockSkew); + beforeBirth = QDateTime::currentDateTime(QTimeZone::UTC).addMSecs(-fsClockSkew); { QFile file(fileName); QVERIFY(file.open(QFile::WriteOnly | QFile::Text)); QFileInfo fileInfo(fileName); - birthTime = fileInfo.birthTime(); + birthTime = fileInfo.birthTime(QTimeZone::UTC); QVERIFY2(!birthTime.isValid() || birthTime > beforeBirth, datePairString(birthTime, beforeBirth)); @@ -1146,30 +1146,30 @@ void tst_QFileInfo::fileTimes() } { QFileInfo fileInfo(fileName); - writeTime = fileInfo.lastModified(); + writeTime = fileInfo.lastModified(QTimeZone::UTC); QVERIFY2(writeTime > beforeWrite, datePairString(writeTime, beforeWrite)); - QCOMPARE(fileInfo.birthTime(), birthTime); // mustn't have changed + QCOMPARE(fileInfo.birthTime(QTimeZone::UTC), birthTime); // mustn't have changed } // --- Change the file's metadata QTest::qSleep(sleepTime); - beforeMetadataChange = QDateTime::currentDateTime().addMSecs(-fsClockSkew); + beforeMetadataChange = QDateTime::currentDateTime(QTimeZone::UTC).addMSecs(-fsClockSkew); { QFile file(fileName); file.setPermissions(file.permissions()); } { QFileInfo fileInfo(fileName); - metadataChangeTime = fileInfo.metadataChangeTime(); + metadataChangeTime = fileInfo.metadataChangeTime(QTimeZone::UTC); QVERIFY2(metadataChangeTime > beforeMetadataChange, datePairString(metadataChangeTime, beforeMetadataChange)); QVERIFY(metadataChangeTime >= writeTime); // not all filesystems can store both times - QCOMPARE(fileInfo.birthTime(), birthTime); // mustn't have changed + QCOMPARE(fileInfo.birthTime(QTimeZone::UTC), birthTime); // mustn't have changed } // --- Read the file QTest::qSleep(sleepTime); - beforeRead = QDateTime::currentDateTime().addMSecs(-fsClockSkew); + beforeRead = QDateTime::currentDateTime(QTimeZone::UTC).addMSecs(-fsClockSkew); { QFile file(fileName); QVERIFY(file.open(QFile::ReadOnly | QFile::Text)); @@ -1179,9 +1179,9 @@ void tst_QFileInfo::fileTimes() } QFileInfo fileInfo(fileName); - readTime = fileInfo.lastRead(); - QCOMPARE(fileInfo.lastModified(), writeTime); // mustn't have changed - QCOMPARE(fileInfo.birthTime(), birthTime); // mustn't have changed + readTime = fileInfo.lastRead(QTimeZone::UTC); + QCOMPARE(fileInfo.lastModified(QTimeZone::UTC), writeTime); // mustn't have changed + QCOMPARE(fileInfo.birthTime(QTimeZone::UTC), birthTime); // mustn't have changed QVERIFY(readTime.isValid()); #if defined(Q_OS_QNX) || defined(Q_OS_ANDROID) @@ -1251,7 +1251,7 @@ void tst_QFileInfo::fakeFileTimes() file.close(); if (ok) - QCOMPARE(QFileInfo(file.fileName()).lastModified(), when); + QCOMPARE(QFileInfo(file.fileName()).lastModified(QTimeZone::UTC), when); else QSKIP("Unable to set file metadata to contrived values"); } @@ -1642,14 +1642,14 @@ void tst_QFileInfo::refresh() file.flush(); QFileInfo info(file); - QDateTime lastModified = info.lastModified(); + QDateTime lastModified = info.lastModified(QTimeZone::UTC); QCOMPARE(info.size(), qint64(7)); QTest::qSleep(sleepTime); QCOMPARE(file.write("JOJOJO"), qint64(6)); file.flush(); - QCOMPARE(info.lastModified(), lastModified); + QCOMPARE(info.lastModified(QTimeZone::UTC), lastModified); QCOMPARE(info.size(), qint64(7)); #if defined(Q_OS_WIN) @@ -1657,7 +1657,7 @@ void tst_QFileInfo::refresh() #endif info.refresh(); QCOMPARE(info.size(), qint64(13)); - QVERIFY(info.lastModified() > lastModified); + QVERIFY(info.lastModified(QTimeZone::UTC) > lastModified); QFileInfo info2 = info; QCOMPARE(info2.size(), info.size()); @@ -2259,10 +2259,10 @@ static void stateCheck(const QFileInfo &info, const QString &dirname, const QStr QCOMPARE(info.permissions(), QFile::Permissions()); - QVERIFY(!info.birthTime().isValid()); - QVERIFY(!info.metadataChangeTime().isValid()); - QVERIFY(!info.lastRead().isValid()); - QVERIFY(!info.lastModified().isValid()); + QVERIFY(!info.birthTime(QTimeZone::UTC).isValid()); + QVERIFY(!info.metadataChangeTime(QTimeZone::UTC).isValid()); + QVERIFY(!info.lastRead(QTimeZone::UTC).isValid()); + QVERIFY(!info.lastModified(QTimeZone::UTC).isValid()); }; void tst_QFileInfo::invalidState_data() diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 8c6bc73cdf..4bfb305ef0 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -886,7 +886,7 @@ static bool waitAndRunUpdateMimeDatabase(const QString &path) QFileInfo mimeCacheInfo(path + QString::fromLatin1("/mime.cache")); if (mimeCacheInfo.exists()) { // Wait until the beginning of the next second - while (mimeCacheInfo.lastModified().secsTo(QDateTime::currentDateTime()) == 0) { + while (mimeCacheInfo.lastModified(QTimeZone::UTC).secsTo(QDateTime::currentDateTime()) == 0) { QTest::qSleep(200); } } diff --git a/tests/auto/gui/image/qicon/tst_qicon.cpp b/tests/auto/gui/image/qicon/tst_qicon.cpp index 514a2d9bcf..ebb0f1e9d2 100644 --- a/tests/auto/gui/image/qicon/tst_qicon.cpp +++ b/tests/auto/gui/image/qicon/tst_qicon.cpp @@ -785,7 +785,7 @@ void tst_QIcon::fromThemeCache() QTest::qWait(1000); // wait enough to have a different modification time in seconds QVERIFY(QFile(QStringLiteral(":/styles/commonstyle/images/standardbutton-save-16.png")) .copy(dir.path() + QLatin1String("/testcache/16x16/actions/button-save.png"))); - QVERIFY(QFileInfo(cacheName).lastModified() < QFileInfo(dir.path() + QLatin1String("/testcache/16x16/actions")).lastModified()); + QVERIFY(QFileInfo(cacheName).lastModified(QTimeZone::UTC) < QFileInfo(dir.path() + QLatin1String("/testcache/16x16/actions")).lastModified(QTimeZone::UTC)); QIcon::setThemeSearchPaths(QStringList() << dir.path()); // reload themes QVERIFY(!QIcon::fromTheme("button-open").isNull()); @@ -806,7 +806,7 @@ void tst_QIcon::fromThemeCache() QCOMPARE(process.exitStatus(), QProcess::NormalExit); QCOMPARE(process.exitCode(), 0); #endif // QT_CONFIG(process) - QVERIFY(QFileInfo(cacheName).lastModified() >= QFileInfo(dir.path() + QLatin1String("/testcache/16x16/actions")).lastModified()); + QVERIFY(QFileInfo(cacheName).lastModified(QTimeZone::UTC) >= QFileInfo(dir.path() + QLatin1String("/testcache/16x16/actions")).lastModified(QTimeZone::UTC)); QIcon::setThemeSearchPaths(QStringList() << dir.path()); // reload themes QVERIFY(!QIcon::fromTheme("button-open").isNull()); QVERIFY(!QIcon::fromTheme("button-open-fallback").isNull());