From da42856b7970e9221216c88926b626f8efda4f51 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Tue, 6 Apr 2010 17:23:16 +0200 Subject: win32: add utimes and fix utime(file,NULL) --- win32/mingw.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'win32') diff --git a/win32/mingw.c b/win32/mingw.c index eeb96db83..c87ce2a47 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -310,6 +310,47 @@ int mingw_utime (const char *file_name, const struct utimbuf *times) time_t_to_filetime(times->modtime, &mft); time_t_to_filetime(times->actime, &aft); } + else { + SYSTEMTIME st; + GetSystemTime(&st); + SystemTimeToFileTime(&st, &aft); + mft = aft; + } + if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) { + errno = EINVAL; + rc = -1; + } else + rc = 0; + close(fh); + return rc; +} + +static inline void timeval_to_filetime(const struct timeval tv, FILETIME *ft) +{ + long long winTime = ((tv.tv_sec * 1000000LL) + tv.tv_usec) * 10LL + 116444736000000000LL; + ft->dwLowDateTime = winTime; + ft->dwHighDateTime = winTime >> 32; +} + +int utimes(const char *file_name, const struct timeval times[2]) +{ + FILETIME mft, aft; + int fh, rc; + + /* must have write permission */ + if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) + return -1; + + if (times) { + timeval_to_filetime(times[0], &aft); + timeval_to_filetime(times[1], &mft); + } + else { + SYSTEMTIME st; + GetSystemTime(&st); + SystemTimeToFileTime(&st, &aft); + mft = aft; + } if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) { errno = EINVAL; rc = -1; -- cgit v1.2.3-55-g6feb