| /* A part of the Windows CE C Extra Library (celibc) |
| Copyright 2007-2015 PC GO Ld. |
| |
| This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| */ |
| |
| #include "stat.h" |
| #include <shared.h> |
| #include <stdlib.h> |
| #include <windows.h> |
| #include <errno.h> |
| #include <unistd.h> |
| #include <fcntl.h> |
| |
| #if _WCE >= 3 |
| void GetCurrentFT(FILETIME *); |
| #endif |
| |
| static void __timespec_to_filetime(const struct timespec *ts, FILETIME *ft) { |
| //if(ts->tv_nsec) { |
| ULARGE_INTEGER li; |
| li.QuadPart = ts->tv_nsec / 100; |
| li.QuadPart += ts->tv_sec * 10000000ULL; |
| li.QuadPart += 116444736000000000ULL; |
| ft->dwLowDateTime = li.LowPart; |
| ft->dwHighDateTime = li.HighPart; |
| //} else *ft = __time_t_to_filetime(ts->tv_sec); |
| } |
| |
| |
| /* |
| Change the access time of FILE to TSP[0] and |
| the modification time of FILE to TSP[1]. |
| */ |
| |
| int futimens(int fd, const struct timespec *tsp) { |
| if(!tsp) { |
| errno = EACCES; |
| return -1; |
| } |
| |
| FILETIME ft[2], *atime = NULL, *mtime = NULL; |
| if(tsp[0].tv_nsec != UTIME_OMIT) { |
| atime = ft; |
| if(tsp[0].tv_nsec == UTIME_NOW) { |
| #if _WCE >= 3 |
| GetCurrentFT(atime); |
| #else |
| SYSTEMTIME st; |
| GetSystemTime(&st); |
| if(!SystemTimeToFileTime(&st, atime)) return -1; |
| #endif |
| } else __timespec_to_filetime(tsp, atime); |
| } |
| if(tsp[1].tv_nsec != UTIME_OMIT) { |
| mtime = ft + 1; |
| if(tsp[1].tv_nsec == UTIME_NOW) { |
| #if _WCE >= 3 |
| GetCurrentFT(mtime); |
| #else |
| SYSTEMTIME st; |
| GetSystemTime(&st); |
| if(!SystemTimeToFileTime(&st, mtime)) return -1; |
| #endif |
| } else __timespec_to_filetime(tsp + 1, mtime); |
| } |
| |
| return SetFileTime((void *)fd, NULL, atime, mtime) ? 0 : -1; |
| } |
| |
| int utimensat(int dirfd, const char *file, const struct timespec *tsp, int flags) { |
| if(!file) { |
| errno = EINVAL; |
| return -1; |
| } |
| if(!tsp) { |
| errno = EACCES; |
| return -1; |
| } |
| if(!*file) { |
| errno = ENOENT; |
| return -1; |
| } |
| |
| /* |
| size_t len = strlen(file) + 1; |
| wchar_t *wfile = (wchar_t *)malloc(len * sizeof(wchar_t)); |
| mbstowcs(wfile, file, len); |
| HANDLE fh = CreateFileW(wfile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| free(wfile); |
| */ |
| int fd = open(file, O_WRONLY); |
| if(fd == -1) return -1; |
| int r = futimens(fd, tsp); |
| if(close(fd) < 0) return -1; |
| return r; |
| } |