diff options
author | Ivan Komissarov <abbapoh@gmail.com> | 2021-09-22 00:44:59 +0300 |
---|---|---|
committer | Ivan Komissarov <ABBAPOH@gmail.com> | 2021-09-27 11:48:01 +0000 |
commit | 9eb524e81adc6403f28951d42bc9377d422181f5 (patch) | |
tree | 2a1baecc5149b5a1eec7ef487f6e5cfb5a7aefff /src | |
parent | 60cbf03195e3a5424c2104d2e4493826d8354a53 (diff) | |
download | qbs-9eb524e81adc6403f28951d42bc9377d422181f5.tar.gz |
Simplify the FileTime implementation
The mode without the clock_gettime was added to support "older Unixes"
such as macOS < 10.12 (see aba193d23420777a309597fe64a9398076b6ff25).
Later, custom clock_gettime version was added for the
same reason (see 2aa3131bef6444a800f6e6259cecf44851eb0454).
MacOS < 10.12 is not supported anymore as probably all other Unixes
that do not support clock_gettime.
Another argument for that is that Qt 5.13 and above uses high-precision
time structs unconditionally - so, if Unix does not support such
structs, Qt won't compile as well.
Thus, copy the workaround for clock_gettime problem from Qt 5.13 and
remove the branch for low-precision file time at all.
Change-Id: I8aaa630f87d9a845578e7701c627ea190d4e17ac
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/corelib/tools/fileinfo.cpp | 8 | ||||
-rw-r--r-- | src/lib/corelib/tools/filetime.cpp | 76 | ||||
-rw-r--r-- | src/lib/corelib/tools/filetime.h | 24 |
3 files changed, 35 insertions, 73 deletions
diff --git a/src/lib/corelib/tools/fileinfo.cpp b/src/lib/corelib/tools/fileinfo.cpp index 382843a33..afd125df5 100644 --- a/src/lib/corelib/tools/fileinfo.cpp +++ b/src/lib/corelib/tools/fileinfo.cpp @@ -354,10 +354,8 @@ FileTime FileInfo::lastModified() const { #if APPLE_STAT_TIMESPEC return m_stat.st_mtimespec; -#elif HAS_CLOCK_GETTIME - return m_stat.st_mtim; #else - return m_stat.st_mtime; + return m_stat.st_mtim; #endif } @@ -365,10 +363,8 @@ FileTime FileInfo::lastStatusChange() const { #if APPLE_STAT_TIMESPEC return m_stat.st_ctimespec; -#elif HAS_CLOCK_GETTIME - return m_stat.st_ctim; #else - return m_stat.st_ctime; + return m_stat.st_ctim; #endif } diff --git a/src/lib/corelib/tools/filetime.cpp b/src/lib/corelib/tools/filetime.cpp index d115075a7..3f8ef1d69 100644 --- a/src/lib/corelib/tools/filetime.cpp +++ b/src/lib/corelib/tools/filetime.cpp @@ -50,23 +50,32 @@ namespace qbs { namespace Internal { -#ifdef APPLE_CUSTOM_CLOCK_GETTIME -#include <sys/time.h> +#if !defined(Q_OS_WIN) -#ifndef CLOCK_REALTIME -#define CLOCK_REALTIME 0 -#endif - -// clk_id isn't used, only the CLOCK_REALTIME case is implemented. -int clock_gettime(int /*clk_id*/, struct timespec *t) +// based on https://github.com/qt/qtbase/blob/5.13/src/corelib/kernel/qelapsedtimer_unix.cpp +// for details why it is implemented this way, see Qt source code +# if !defined(CLOCK_REALTIME) +# define CLOCK_REALTIME 0 +static inline void qbs_clock_gettime(int, struct timespec *ts) { + // support clock_gettime with gettimeofday struct timeval tv; - // Resolution of gettimeofday is 1000nsecs = 1 microsecond. - int ret = gettimeofday(&tv, NULL); - t->tv_sec = tv.tv_sec; - t->tv_nsec = tv.tv_usec * 1000; - return ret; + gettimeofday(&tv, 0); + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; +} + +# ifdef _POSIX_MONOTONIC_CLOCK +# undef _POSIX_MONOTONIC_CLOCK +# define _POSIX_MONOTONIC_CLOCK -1 +# endif +# else +static inline void qbs_clock_gettime(clockid_t clock, struct timespec *ts) +{ + clock_gettime(clock, ts); } +# endif + #endif FileTime::FileTime() @@ -75,16 +84,14 @@ FileTime::FileTime() static_assert(sizeof(FileTime::InternalType) == sizeof(FILETIME), "FileTime::InternalType has wrong size."); m_fileTime = 0; -#elif HAS_CLOCK_GETTIME - m_fileTime = {0, 0}; #else - m_fileTime = 0; + m_fileTime = {0, 0}; #endif } FileTime::FileTime(const FileTime::InternalType &ft) : m_fileTime(ft) { -#if HAS_CLOCK_GETTIME +#if !defined(Q_OS_WIN) if (m_fileTime.tv_sec == 0) m_fileTime.tv_nsec = 0; // stat() sets only the first member to 0 for non-existing files. #endif @@ -96,7 +103,7 @@ int FileTime::compare(const FileTime &other) const auto const t1 = reinterpret_cast<const FILETIME *>(&m_fileTime); auto const t2 = reinterpret_cast<const FILETIME *>(&other.m_fileTime); return CompareFileTime(t1, t2); -#elif HAS_CLOCK_GETTIME +#else if (m_fileTime.tv_sec < other.m_fileTime.tv_sec) return -1; if (m_fileTime.tv_sec > other.m_fileTime.tv_sec) @@ -106,18 +113,12 @@ int FileTime::compare(const FileTime &other) const if (m_fileTime.tv_nsec > other.m_fileTime.tv_nsec) return 1; return 0; -#else - if (m_fileTime < other.m_fileTime) - return -1; - if (m_fileTime > other.m_fileTime) - return 1; - return 0; #endif } void FileTime::clear() { -#if HAS_CLOCK_GETTIME +#if !defined(Q_OS_WIN) m_fileTime = { 0, 0 }; #else m_fileTime = 0; @@ -138,19 +139,10 @@ FileTime FileTime::currentTime() auto const ft = reinterpret_cast<FILETIME *>(&result.m_fileTime); SystemTimeToFileTime(&st, ft); return result; -#elif defined APPLE_CUSTOM_CLOCK_GETTIME - InternalType t; - // Explicitly use our custom version, so that we don't get an additional unresolved symbol on a - // system that actually provides one, but isn't used due to the minimium deployment target - // being lower. - qbs::Internal::clock_gettime(CLOCK_REALTIME, &t); - return t; -#elif HAS_CLOCK_GETTIME +#else InternalType t; - clock_gettime(CLOCK_REALTIME, &t); + qbs_clock_gettime(CLOCK_REALTIME, &t); return t; -#else - return time(nullptr); #endif } @@ -171,16 +163,14 @@ FileTime FileTime::oldestTime() auto const ft = reinterpret_cast<FILETIME *>(&result.m_fileTime); SystemTimeToFileTime(&st, ft); return result; -#elif HAS_CLOCK_GETTIME - return FileTime({1, 0}); #else - return 1; + return FileTime({1, 0}); #endif } double FileTime::asDouble() const { -#if HAS_CLOCK_GETTIME +#if !defined(Q_OS_WIN) return static_cast<double>(m_fileTime.tv_sec); #else return static_cast<double>(m_fileTime); @@ -200,11 +190,7 @@ QString FileTime::toString() const return result; #else QDateTime dt; -#if HAS_CLOCK_GETTIME - dt.setMSecsSinceEpoch(m_fileTime.tv_sec * 1000 + m_fileTime.tv_nsec / 1000000); -#else - dt.setTime_t(m_fileTime); -#endif + dt.setMSecsSinceEpoch(m_fileTime.tv_sec * 1000 + m_fileTime.tv_nsec / 1000000); return dt.toString(Qt::ISODateWithMs); #endif } diff --git a/src/lib/corelib/tools/filetime.h b/src/lib/corelib/tools/filetime.h index 70f4f4461..c656608e0 100644 --- a/src/lib/corelib/tools/filetime.h +++ b/src/lib/corelib/tools/filetime.h @@ -45,28 +45,12 @@ #include <QtCore/qdatastream.h> #include <QtCore/qdebug.h> -#if defined(Q_OS_UNIX) && !defined(__APPLE__) +#if defined(Q_OS_UNIX) #include <time.h> -# if defined(Q_OS_FREEBSD) -// FreeBSD 9.0 and above supports m_tim -# define HAS_CLOCK_GETTIME (__FreeBSD_version >= 900000) -# else -# define HAS_CLOCK_GETTIME (_POSIX_C_SOURCE >= 199309L) -# endif #endif // Q_OS_UNIX #ifdef __APPLE__ -#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200 -// macOS 10.12+ ships clock_gettime. -#else -// We implement our own clock_gettime. -#define APPLE_CUSTOM_CLOCK_GETTIME 1 -#endif // __MAC_OS_X_VERSION_MIN_REQUIRED - -// Either way we have a clock_gettime in the end. -#define HAS_CLOCK_GETTIME 1 - // Apple stat struct has slightly different names for time fields. #define APPLE_STAT_TIMESPEC 1 @@ -79,11 +63,7 @@ class QBS_AUTOTEST_EXPORT FileTime { public: #if defined(Q_OS_UNIX) -#if HAS_CLOCK_GETTIME using InternalType = timespec; -#else - using InternalType = time_t; -#endif // HAS_CLOCK_GETTIME #elif defined(Q_OS_WIN) using InternalType = quint64; #else @@ -112,7 +92,7 @@ public: template<PersistentPool::OpType opType> void completeSerializationOp(PersistentPool &pool) { -#if HAS_CLOCK_GETTIME +#if !defined(Q_OS_WIN) pool.serializationOp<opType>(m_fileTime.tv_sec, m_fileTime.tv_nsec); #else pool.serializationOp<opType>(m_fileTime); |