diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-07-02 11:46:43 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2017-08-06 23:14:26 +0000 |
commit | 5a9b1425e187bc77122354cf7446bcf6bb322ff4 (patch) | |
tree | a9db6878473eec577a373127f9cd41d4aea7f517 /tests/manual | |
parent | 04f4d87bda7e108817e80a7b16ba6fa9323ed3c4 (diff) |
QFileSystemEngine/Unix: rework the getting of nsec-precision file times
It was working on Linux because _GNU_SOURCE gets us POSIX.1-2008
compatibility, but not on macOS or the BSDs. There, we were still stuck
to full second precision.
This commit uses the template trick introduced by the futimes code
(which itself was inspired by commit 2fb42eb4af3444b11e7b1210323637937ef
in QtNetwork). Also note how it adds support for birth time, if the
system's stat struct has that information.
Tested to work on MacOS and FreeBSD. The manual filetest produces:
Name: .
Path: . (/usr/home/tjmaciei/src/qt/qt5)
Size: 1536 Type: Directory
Attrs: readable writable executable hidden nativepath
Mode: drwxr-xr-x
Owner: tjmaciei (1001) Group: tjmaciei (1001)
Access: 2017-07-13T20:03:47.916
Birth: 2017-07-13T20:03:47.916
Change: 2017-07-13T20:04:41.648
Modified: 2017-07-13T20:04:41.648
Linux will require support for statx(2).
Change-Id: I8d96dea9955d4c749b99fffd14cd97d7a8c6d45d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/manual')
-rw-r--r-- | tests/manual/filetest/main.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/manual/filetest/main.cpp b/tests/manual/filetest/main.cpp index 396fcdb24b..c278fd87ce 100644 --- a/tests/manual/filetest/main.cpp +++ b/tests/manual/filetest/main.cpp @@ -26,6 +26,7 @@ ** ****************************************************************************/ +#include <QDateTime> #include <QDebug> #include <QCoreApplication> #include <QFileInfo> @@ -40,12 +41,25 @@ static const char usage1[] = "Usage: "; static const char usage2[] =" [KEYWORD] [ARGUMENTS]\n\n" "Keywords: ls FILES list file information\n" +" stat FILES print detailed file information\n" " mv SOURCE TARGET rename files using QFile::rename\n" " cp SOURCE TARGET copy files using QFile::copy\n" " rm FILE remove file using QFile::remove\n" " rmr DIR remove directory recursively\n" " using QDir::removeRecursively\n"; +std::ostream &operator<<(std::ostream &o, const QString &str) +{ + return o << qPrintable(str); +} + +std::ostream &operator<<(std::ostream &o, const QDateTime &dt) +{ + if (dt.isValid()) + return o << dt.toString(Qt::ISODateWithMs); + return o << '-'; +} + static inline std::string permissions(QFile::Permissions permissions) { std::string result(10, '-'); @@ -102,6 +116,44 @@ static int ls(int argCount, char **args) return 0; } +static int stat(int argCount, char **args) +{ + for (int i = 0 ; i < argCount; ++i) { + const QFileInfo fi(QFile::decodeName(args[i])); + std::cout << "Name:\t" << fi.fileName() << std::endl; + std::cout << "Path:\t" << QDir::toNativeSeparators(fi.path()) + << " (" << QDir::toNativeSeparators(fi.absolutePath()) << ')' << std::endl; + std::cout << "Size:\t" << fi.size() + << "\tType: " + << (fi.isSymLink() && !fi.exists() ? "Broken symlink" : + !fi.exists() ? "Non-existent" : + fi.isSymLink() ? "Symlink to " : "") + << (!fi.exists() ? "" : + fi.isFile() ? "Regular file" : + fi.isDir() ? "Directory" : "Special node") + << std::endl; + if (fi.isSymLink()) + std::cout << "Target:\t" << fi.symLinkTarget() << std::endl; + std::cout << "Attrs: " + << (fi.isReadable() ? " readable" : "") + << (fi.isWritable() ? " writable" : "") + << (fi.isExecutable() ? " executable" : "") + << (fi.isHidden() ? " hidden" : "") + << (fi.isNativePath() ? " nativepath" : "") + << (fi.isRoot() ? " root" : "") + << (fi.isBundle() ? " bundle" : "") + << std::endl; + std::cout << "Mode:\t" << permissions(fi) << std::endl; + std::cout << "Owner:\t" << fi.owner() << " (" << fi.ownerId() + << ")\tGroup:\t" << fi.group() << " (" << fi.groupId() << ')' << std::endl; + std::cout << "Access:\t" << fi.lastRead() << std::endl; + std::cout << "Birth:\t" << fi.birthTime() << std::endl; + std::cout << "Change:\t" << fi.metadataChangeTime() << std::endl; + std::cout << "Modified: " << fi.lastModified() << std::endl; + } + return 0; +} + static int mv(const char *sourceFileName, const char *targetFileName) { QFile sourceFile(QString::fromLocal8Bit(sourceFileName)); @@ -155,6 +207,9 @@ int main(int argc, char *argv[]) if (argc >= 3 && !qstrcmp(argv[1], "ls")) return ls(argc -2, argv + 2); + if (argc >= 3 && !qstrcmp(argv[1], "stat")) + return stat(argc -2, argv + 2); + if (argc == 4 && !qstrcmp(argv[1], "mv")) return mv(argv[2], argv[3]); |