Introduce QFileInfo::birthTime and metadataChangeTime

[ChangeLog][QtCore][QFileInfo] Deprecated created() because it could
return one of three different file times depending on the OS and
filesystem type, without the ability to determine which one is which. It
is replaced by metadataChangeTime() and birthTime().

[ChangeLog][QtCore][QFileInfo] Added QFileInfo::metadataChangeTime(),
which returns the time the file's metadata was last changed, if it is
known, and falling back to the same value as lastModified() otherwise.
On Unix systems, this corresponds to the file's ctime.

[ChangeLog][QtCore][QFileInfo] Added QFileInfo::birthTime(), which
returns the file's birth time if it is known, an invalid QDateTime
otherwise. This function is supported on Windows and on some Unix
systems.

Change-Id: I0031aa609e714ae983c3fffd1467bd8b3e3a593d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Thiago Macieira 2016-08-04 16:10:09 -07:00
parent 88c30618d5
commit 261c6713bd
3 changed files with 65 additions and 17 deletions

View File

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

View File

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

View File

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