From fb46f0422517cba6d566131054117738e826424c Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 15 Aug 2021 09:17:25 +0100 Subject: win32: implement utimes(2) using utimensat(2) Saves 176 bytes. --- win32/mingw.c | 56 ++++++++++++++++++-------------------------------------- 1 file changed, 18 insertions(+), 38 deletions(-) (limited to 'win32') diff --git a/win32/mingw.c b/win32/mingw.c index 604e378c8..e3424ff07 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -740,14 +740,6 @@ int mingw_fstat(int fd, struct mingw_stat *buf) return -1; } -static inline void timeval_to_filetime(const struct timeval tv, FILETIME *ft) -{ - long long winTime = tv.tv_sec * 10000000LL + tv.tv_usec * 10LL + - 116444736000000000LL; - ft->dwLowDateTime = winTime; - ft->dwHighDateTime = winTime >> 32; -} - static inline void timespec_to_filetime(const struct timespec tv, FILETIME *ft) { long long winTime = tv.tv_sec * 10000000LL + tv.tv_nsec / 100LL + @@ -756,36 +748,6 @@ static inline void timespec_to_filetime(const struct timespec tv, FILETIME *ft) ft->dwHighDateTime = winTime >> 32; } -int utimes(const char *file_name, const struct timeval tims[2]) -{ - FILETIME mft, aft; - HANDLE fh; - int rc = 0; - - fh = CreateFile(file_name, FILE_WRITE_ATTRIBUTES, 0, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - if ( fh == INVALID_HANDLE_VALUE ) { - errno = err_win_to_posix(); - return -1; - } - - if (tims) { - timeval_to_filetime(tims[0], &aft); - timeval_to_filetime(tims[1], &mft); - } - else { - GetSystemTimeAsFileTime(&mft); - aft = mft; - } - - if (!SetFileTime(fh, NULL, &aft, &mft)) { - errno = err_win_to_posix(); - rc = -1; - } - CloseHandle(fh); - return rc; -} - static int hutimens(HANDLE fh, const struct timespec times[2]) { FILETIME now, aft, mft; @@ -858,6 +820,24 @@ int utimensat(int fd, const char *path, const struct timespec times[2], return rc; } +int utimes(const char *file_name, const struct timeval tv[2]) +{ + struct timespec ts[2]; + + if (tv) { + if (tv[0].tv_usec < 0 || tv[0].tv_usec >= 1000000 || + tv[1].tv_usec < 0 || tv[1].tv_usec >= 1000000) { + errno = EINVAL; + return -1; + } + ts[0].tv_sec = tv[0].tv_sec; + ts[0].tv_nsec = tv[0].tv_usec * 1000; + ts[1].tv_sec = tv[1].tv_sec; + ts[1].tv_nsec = tv[1].tv_usec * 1000; + } + return utimensat(AT_FDCWD, file_name, tv ? ts : NULL, 0); +} + unsigned int sleep (unsigned int seconds) { Sleep(seconds*1000); -- cgit v1.2.3-55-g6feb