From 0b2bf17cd7e1f3c484adeeceaa9ca14522846f8f Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Thu, 6 Apr 2017 22:16:17 -0400 Subject: [PATCH] abstract access to highres atime/mtime --- src/fs_base_stat.hpp | 50 +++++++++++++++++++++++++++++++++++ src/fs_base_utime.hpp | 10 ++++--- src/fs_base_utime_generic.hpp | 20 +++++++++++--- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/src/fs_base_stat.hpp b/src/fs_base_stat.hpp index e67d5267..69632739 100644 --- a/src/fs_base_stat.hpp +++ b/src/fs_base_stat.hpp @@ -62,6 +62,56 @@ namespace fs { return ::fstat(fd,&st); } + + static + inline + timespec * + stat_atime(struct stat &st) + { +#if __APPLE__ + return &st.st_atimespec; +#else + return &st.st_atim; +#endif + } + + static + inline + const + timespec * + stat_atime(const struct stat &st) + { +#if __APPLE__ + return &st.st_atimespec; +#else + return &st.st_atim; +#endif + } + + static + inline + timespec * + stat_mtime(struct stat &st) + { +#if __APPLE__ + return &st.st_mtimespec; +#else + return &st.st_mtim; +#endif + } + + static + inline + const + timespec * + stat_mtime(const struct stat &st) + { +#if __APPLE__ + return &st.st_mtimespec; +#else + return &st.st_mtim; +#endif + } } #endif diff --git a/src/fs_base_utime.hpp b/src/fs_base_utime.hpp index ae1b1afe..2def8d5c 100644 --- a/src/fs_base_utime.hpp +++ b/src/fs_base_utime.hpp @@ -25,6 +25,8 @@ # include "fs_base_utime_generic.hpp" #endif +#include "fs_base_stat.hpp" + namespace fs { static @@ -35,8 +37,8 @@ namespace fs { struct timespec times[2]; - times[0] = st.st_atim; - times[1] = st.st_mtim; + times[0] = *fs::stat_atime(st); + times[1] = *fs::stat_mtime(st); return fs::utime(AT_FDCWD,path,times,0); } @@ -49,8 +51,8 @@ namespace fs { struct timespec times[2]; - times[0] = st.st_atim; - times[1] = st.st_mtim; + times[0] = *fs::stat_atime(st); + times[1] = *fs::stat_mtime(st); return fs::utime(fd,times); } diff --git a/src/fs_base_utime_generic.hpp b/src/fs_base_utime_generic.hpp index eeea26ad..0734d8a1 100644 --- a/src/fs_base_utime_generic.hpp +++ b/src/fs_base_utime_generic.hpp @@ -25,6 +25,8 @@ #include #include +#include "fs_base_stat.hpp" + #ifndef UTIME_NOW # define UTIME_NOW ((1l << 30) - 1l) #endif @@ -123,6 +125,8 @@ _set_utime_omit_to_current_value(const int dirfd, { int rv; struct stat st; + timespec *atime; + timespec *mtime; if(!_any_timespec_is_utime_omit(ts)) return 0; @@ -131,10 +135,13 @@ _set_utime_omit_to_current_value(const int dirfd, if(rv == -1) return -1; + atime = fs::stat_atime(st); + mtime = fs::stat_mtime(st); + if(ts[0].tv_nsec == UTIME_OMIT) - TIMESPEC_TO_TIMEVAL(&tv[0],&st.st_atim); + TIMESPEC_TO_TIMEVAL(&tv[0],atime); if(ts[1].tv_nsec == UTIME_OMIT) - TIMESPEC_TO_TIMEVAL(&tv[1],&st.st_mtim); + TIMESPEC_TO_TIMEVAL(&tv[1],mtime); return 0; } @@ -148,6 +155,8 @@ _set_utime_omit_to_current_value(const int fd, { int rv; struct stat st; + timespec *atime; + timespec *mtime; if(!_any_timespec_is_utime_omit(ts)) return 0; @@ -156,10 +165,13 @@ _set_utime_omit_to_current_value(const int fd, if(rv == -1) return -1; + atime = fs::stat_atime(st); + mtime = fs::stat_mtime(st); + if(ts[0].tv_nsec == UTIME_OMIT) - TIMESPEC_TO_TIMEVAL(&tv[0],&st.st_atim); + TIMESPEC_TO_TIMEVAL(&tv[0],atime; if(ts[1].tv_nsec == UTIME_OMIT) - TIMESPEC_TO_TIMEVAL(&tv[1],&st.st_mtim); + TIMESPEC_TO_TIMEVAL(&tv[1],mtime); return 0; }