QFileInfo::birthTime() returns nothing (on Ubuntu)
-
I'm using Qt 5.15.2 on Ubuntu 21.10
When I callQFileInfo fileInfo(PATH_TO_A_FILE); LOG() << fileInfo.birthTime().toString("dd/MM/yy HH:mm:ss"); LOG() << fileInfo.created().toString("dd/MM/yy HH:mm:ss");
then
birthTime()
's result is an empty string, whereascreated()
returns the correct time. However, according to the docscreated()
is deprecated andbirthTime()
should be used instead...Any ideas why do I observe such behavior?
-
@Pawel_Kleczek
For code browser for Qt5, https://codebrowser.dev/qt5/qtbase/src/corelib/io/qfileinfo.cpp.htmlQDateTime QFileInfo::created() const { QDateTime d = fileTime(QFile::FileBirthTime); if (d.isValid()) return d; return fileTime(QFile::FileMetadataChangeTime); } QDateTime QFileInfo::birthTime() const { return fileTime(QFile::FileBirthTime); }
So at some level
fileTime(QFile::FileBirthTime)
is returning invalidQDateTime
while forcreated()
thefileTime(QFile::FileMetadataChangeTime)
is returning the result you get.You would have to follow https://codebrowser.dev/qt5/qtbase/src/corelib/io/qfileinfo.cpp.html#_ZNK9QFileInfo8fileTimeEN11QFileDevice8FileTimeE,
QDateTime QFileInfo::fileTime(QFile::FileTime time) const
, to find out why.I believe Qt caches
QFileInfo
stuff, that could be an issue somehow?Additionally per https://forum.qt.io/topic/133954/either-qfileinfo-caching-does-not-work-or-just-its-methods-are-unreasonable-very-slow/19 I believe at Qt 5.15
birthTime()
(created()
too?) may be "inefficient", though that is a separate matter, unless it interferes with the result here.It may be that Qt/OS does not regard Linux fs as actually storing a "creation" time, hence the call to
QFile::FileMetadataChangeTime
. Google for e.g.linux file created
, read what is said, how it depends on the filing system in use, etc. Thec
flag on a Linux file is not named "created" time, it is named "Change - the last time meta data of the file was changed (e.g. permissions)", e.g. https://unix.stackexchange.com/a/2465/104736. So you're not going to get a Linux "created/born" time unless you use a native Linux call and appropriate to the file system.You may also use QDateTime QFileInfo::metadataChangeTime() const directly.
-
Storage of actual file creation time will also depend on the file system involved. For example, EXT4 has provision for a create time while EXT2/3 do not.
Even then, the standard stat() call has no place to return it in struct stat, so you need the statx() call. Not sure what Qt ultimately uses.
-
-
@ChrisW67 said in QFileInfo::birthTime() returns nothing (on Ubuntu):
Not sure what Qt ultimately uses.
Qt uses statx (see e.g. https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/io/qfilesystemengine_unix.cpp).
But yeah, not all file systems support birth time. @Pawel_Kleczek , you should be able to check this yourself on the command line with
stat PATH_TO_A_FILE
does it print a 'Birth: ' row?
-
OP's Ubuntu should support
stat file
forBirth
. See e.g. How to Find Out When a File Was Created in Linux for a recent discussion of the issues.