From ece6fc97ff7a5678918254aa568f9aa9a6739142 Mon Sep 17 00:00:00 2001 From: Riccardo Mottola Date: Tue, 2 Feb 2021 23:30:18 +0100 Subject: [PATCH] Add check for statbuf.st_mtim if available and use statbuf.st_mtim to get nanosecond precision in modification date, also fix conversion of nanoseconds in creation date. --- ChangeLog | 10 ++++++++++ Headers/GNUstepBase/config.h.in | 3 +++ Source/NSFileManager.m | 18 +++++++++++++----- configure | 12 +++++++++++- configure.ac | 4 ++-- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 483bfab0c..14d331633 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2021-02-02 Riccardo Mottola + + * configure + * configure.ac + Add check for statbuf.st_mtim if available + + * Source/NSFileManager.m + Use statbuf.st_mtim to get nanosecond precision in modification + date, also fix conversion of nanoseconds in creation date. + 2021-01-21 Fred Kiefer * Tools/pl2link.m (main): Set key StartupWMClass and allow file diff --git a/Headers/GNUstepBase/config.h.in b/Headers/GNUstepBase/config.h.in index 52583b71d..17c011350 100644 --- a/Headers/GNUstepBase/config.h.in +++ b/Headers/GNUstepBase/config.h.in @@ -592,6 +592,9 @@ /* Define to 1 if `st_birthtimespec' is a member of `struct stat'. */ #undef HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC +/* Define to 1 if `st_mtim' is a member of `struct stat'. */ +#undef HAVE_STRUCT_STAT_ST_MTIM + /* Define to 1 if you have the `symlink' function. */ #undef HAVE_SYMLINK diff --git a/Source/NSFileManager.m b/Source/NSFileManager.m index 602546b0d..3c2c6b632 100644 --- a/Source/NSFileManager.m +++ b/Source/NSFileManager.m @@ -611,8 +611,8 @@ static NSStringEncoding defaultEncoding; struct timespec ub[2]; ub[0].tv_sec = 0; ub[0].tv_nsec = UTIME_OMIT; // we don't touch access time - ub[1].tv_sec = truncl(ti); - ub[1].tv_nsec = (ti - (double)ub[1].tv_sec) * 1.0e6; + ub[1].tv_sec = (time_t)trunc(ti); + ub[1].tv_nsec = (long)trunc((ti - trunc(ti)) * 1.0e9); ok = (utimensat(AT_FDCWD, lpath, ub, 0) == 0); #elif defined(_POSIX_VERSION) @@ -661,8 +661,8 @@ static NSStringEncoding defaultEncoding; struct timespec ub[2]; ub[0].tv_sec = 0; ub[0].tv_nsec = UTIME_OMIT; // we don't touch access time - ub[1].tv_sec = truncl(ti); - ub[1].tv_nsec = (ti - (double)ub[1].tv_sec) * 1.0e6; + ub[1].tv_sec = (time_t)trunc(ti); + ub[1].tv_nsec = (long)trunc((ti - trunc(ti)) * 1.0e9); ok = (utimensat(AT_FDCWD, lpath, ub, 0) == 0); #elif defined(_WIN32) || defined(_POSIX_VERSION) @@ -3874,7 +3874,15 @@ static NSSet *fileKeys = nil; - (NSDate*) fileModificationDate { - return [NSDate dateWithTimeIntervalSince1970: statbuf.st_mtime]; + NSTimeInterval ti; + +#if defined (HAVE_STRUCT_STAT_ST_MTIM) + ti = statbuf.st_mtim.tv_sec + (double)statbuf.st_mtim.tv_nsec / 1.0e9; +#else + ti = (double)statbuf.st_mtime; +#endif + + return [NSDate dateWithTimeIntervalSince1970: ti]; } - (NSUInteger) filePosixPermissions diff --git a/configure b/configure index 4bd775f0d..f9ea658f4 100755 --- a/configure +++ b/configure @@ -9492,7 +9492,17 @@ fi fi -# Look for file creation date on NetBSD, FreeBSD, Darwin +# Look for file creation and modification date on NetBSD, FreeBSD, Darwin +ac_fn_c_check_member "$LINENO" "struct stat" "st_mtim" "ac_cv_member_struct_stat_st_mtim" "#include +" +if test "x$ac_cv_member_struct_stat_st_mtim" = xyes; then : + +cat >>confdefs.h <<_ACEOF +#define HAVE_STRUCT_STAT_ST_MTIM 1 +_ACEOF + + +fi ac_fn_c_check_member "$LINENO" "struct stat" "st_birthtime" "ac_cv_member_struct_stat_st_birthtime" "#include " if test "x$ac_cv_member_struct_stat_st_birthtime" = xyes; then : diff --git a/configure.ac b/configure.ac index f3a8c21e8..9da871629 100644 --- a/configure.ac +++ b/configure.ac @@ -2399,8 +2399,8 @@ fi AC_CHECK_FUNCS(getcwd) AC_HEADER_DIRENT -# Look for file creation date on NetBSD, FreeBSD, Darwin -AC_CHECK_MEMBERS([struct stat.st_birthtime, struct stat.st_birthtimespec, struct stat.st_birthtim, struct stat64.st_birthtimespec], +# Look for file creation and modification date on NetBSD, FreeBSD, Darwin +AC_CHECK_MEMBERS([struct stat.st_mtim, struct stat.st_birthtime, struct stat.st_birthtimespec, struct stat.st_birthtim, struct stat64.st_birthtimespec], ,,[#include ]) #--------------------------------------------------------------------