diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index 77349d511d0..bb4eb5aeb21 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -1300,20 +1300,50 @@ qint64 QFileInfo::size() const return d->fileSize; } +#if QT_DEPRECATED_SINCE(5, 10) /*! - Returns the date and local time when the file was created. + \deprecated - On most Unix systems, this function returns the time of the last - status change. A status change occurs when the file is created, - but it also occurs whenever the user writes or sets inode - information (for example, changing the file permissions). + Returns the date and time when the file was created, the time its metadata + was last changed or the time of last modification, whichever one of the + three is available (in that order). - If neither creation time nor "last status change" time are not - available, returns the same as lastModified(). + This function is deprecated. Instead, use the birthTime() function to get + the time the file was created, metadataChangeTime() to get the time its + metadata was last changed, or lastModified() to get the time it was last modified. + + \sa birthTime(), metadataChangeTime(), lastModified(), lastRead() +*/ +QDateTime QFileInfo::created() const +{ + return fileTime(QFile::FileCreationTime); +} +#endif + +/*! + \since 5.10 + Returns the date and time when the file was created / born. + + If the file birth time is not available, this function returns an invalid + QDateTime. + + \sa lastModified(), lastRead(), metadataChangeTime() +*/ +QDateTime QFileInfo::birthTime() const +{ + return fileTime(QFile::FileCreationTime); +} + +/*! + \since 5.10 + Returns the date and time when the file metadata was changed. A metadata + change occurs when the file is created, but it also occurs whenever the + user writes or sets inode information (for example, changing the file + permissions). \sa lastModified(), lastRead() */ -QDateTime QFileInfo::created() const +QDateTime QFileInfo::metadataChangeTime() const { return fileTime(QFile::FileCreationTime); } @@ -1321,7 +1351,7 @@ QDateTime QFileInfo::created() const /*! Returns the date and local time when the file was last modified. - \sa created(), lastRead(), fileTime() + \sa birthTime(), lastRead(), metadataChangeTime(), fileTime() */ QDateTime QFileInfo::lastModified() const { @@ -1334,7 +1364,7 @@ QDateTime QFileInfo::lastModified() const On platforms where this information is not available, returns the same as lastModified(). - \sa created(), lastModified(), fileTime() + \sa birthTime(), lastModified(), metadataChangeTime(), fileTime() */ QDateTime QFileInfo::lastRead() const { diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h index 845fdefac76..f295a86015b 100644 --- a/src/corelib/io/qfileinfo.h +++ b/src/corelib/io/qfileinfo.h @@ -130,7 +130,12 @@ public: qint64 size() const; // ### Qt6: inline these functions +#if QT_DEPRECATED_SINCE(5, 10) + QT_DEPRECATED_X("Use either birthTime() or metadataChangeTime()") QDateTime created() const; +#endif + QDateTime birthTime() const; + QDateTime metadataChangeTime() const; QDateTime lastModified() const; QDateTime lastRead() const; QDateTime fileTime(QFile::FileTime time) const; diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index 4c015be5aad..b269e3e1e40 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -1034,6 +1034,11 @@ void tst_QFileInfo::systemFiles() QVERIFY2(fi.exists(), msgDoesNotExist(fi.absoluteFilePath()).constData()); QVERIFY(fi.size() > 0); QVERIFY(fi.lastModified().isValid()); + QVERIFY(fi.metadataChangeTime().isValid()); + QCOMPARE(fi.metadataChangeTime(), fi.lastModified()); // On Windows, they're the same + QVERIFY(fi.birthTime().isValid()); + QVERIFY(fi.birthTime() <= fi.lastModified()); + QCOMPARE(fi.created(), fi.birthTime()); // On Windows, they're the same } void tst_QFileInfo::compare_data() @@ -1145,6 +1150,8 @@ void tst_QFileInfo::fileTimes() QTest::qSleep(sleepTime); { QFileInfo fileInfo(fileName); + QVERIFY(!fileInfo.birthTime().isValid() || fileInfo.birthTime() < beforeWrite); + QVERIFY(fileInfo.metadataChangeTime() < beforeWrite); QVERIFY(fileInfo.created() < beforeWrite); QFile file(fileName); QVERIFY(file.open(QFile::ReadWrite | QFile::Text)); @@ -1156,10 +1163,8 @@ void tst_QFileInfo::fileTimes() QTest::qSleep(sleepTime); { QFileInfo fileInfo(fileName); -// On unix created() returns the same as lastModified(). -#if !defined(Q_OS_UNIX) - QVERIFY(fileInfo.created() < beforeWrite); -#endif + QVERIFY(!fileInfo.birthTime().isValid() || fileInfo.birthTime() < beforeWrite); + QVERIFY(fileInfo.metadataChangeTime() > beforeWrite); QVERIFY(fileInfo.lastModified() > beforeWrite); QFile file(fileName); QVERIFY(file.open(QFile::ReadOnly | QFile::Text)); @@ -1169,9 +1174,9 @@ void tst_QFileInfo::fileTimes() } QFileInfo fileInfo(fileName); -#if !defined(Q_OS_UNIX) - QVERIFY(fileInfo.created() < beforeWrite); -#endif + QVERIFY(!fileInfo.birthTime().isValid() || fileInfo.birthTime() < beforeWrite); + QVERIFY(fileInfo.metadataChangeTime() > beforeWrite); + //In Vista the last-access timestamp is not updated when the file is accessed/touched (by default). //To enable this the HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\NtfsDisableLastAccessUpdate //is set to 0, in the test machine. @@ -1901,6 +1906,8 @@ void tst_QFileInfo::invalidState() info.setCaching(false); info.created(); + info.birthTime(); + info.metadataChangeTime(); info.lastRead(); info.lastModified(); } @@ -1913,6 +1920,8 @@ void tst_QFileInfo::invalidState() info.setCaching(false); info.created(); + info.birthTime(); + info.metadataChangeTime(); info.lastRead(); info.lastModified(); } @@ -1925,6 +1934,8 @@ void tst_QFileInfo::invalidState() info.setCaching(false); info.created(); + info.birthTime(); + info.metadataChangeTime(); info.lastRead(); info.lastModified(); } @@ -1937,6 +1948,8 @@ void tst_QFileInfo::nonExistingFileDates() QFileInfo info("non-existing-file.foobar"); QVERIFY(!info.exists()); QVERIFY(!info.created().isValid()); + QVERIFY(!info.birthTime().isValid()); + QVERIFY(!info.metadataChangeTime().isValid()); QVERIFY(!info.lastRead().isValid()); QVERIFY(!info.lastModified().isValid()); }