Use QFileInfo's file times in UTC for file timestamps

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 <thiago.macieira@intel.com>
Reviewed-by: Kai Köhne <kai.koehne@qt.io>
This commit is contained in:
Ahmad Samir 2022-12-17 13:09:33 +02:00
parent 7eff78e8a5
commit 41867c25f8
11 changed files with 38 additions and 38 deletions

View File

@ -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));
}
};

View File

@ -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;
}

View File

@ -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

View File

@ -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<const char*>(m_data + offset))).lastModified()) {
+ QString::fromUtf8(reinterpret_cast<const char*>(m_data + offset))).lastModified(QTimeZone::UTC)) {
m_isValid = false;
return;
}

View File

@ -714,7 +714,7 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers
QString key = "qt_pixmap"_L1
% info.absoluteFilePath()
% HexString<uint>(info.lastModified().toSecsSinceEpoch())
% HexString<uint>(info.lastModified(QTimeZone::UTC).toSecsSinceEpoch())
% HexString<quint64>(info.size())
% HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);

View File

@ -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();
}
}

View File

@ -195,7 +195,7 @@ static bool writeJsonFiles(const QList<QString> &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;
}

View File

@ -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());

View File

@ -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()

View File

@ -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);
}
}

View File

@ -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());