| /* A part of the Native C Library for Windows NT |
| 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 <sys/stat.h> |
| #include <sys/time.h> |
| #include <windows.h> |
| #include <nt.h> |
| #include <unistd.h> |
| #include <fcntl.h> |
| #include <errno.h> |
| |
| //#include <stdio.h> |
| |
| static void __timespec_to_hurdred_nanoseconds_since_1601(const struct timespec *ts, LARGE_INTEGER *li) { |
| li->QuadPart = ts->tv_nsec / 100; |
| li->QuadPart += ts->tv_sec * 10000000ULL; |
| li->QuadPart += 116444736000000000ULL; |
| } |
| |
| int futimens(int fd, const struct timespec *tsp) { |
| if(!tsp) { |
| errno = EACCES; |
| return -1; |
| } |
| if(fd == STDOUT_FILENO || fd == STDERR_FILENO) { |
| int real_fd = (fd == STDOUT_FILENO ? _get_stdout_fd : _get_stderr_fd)(); |
| switch(real_fd) { |
| case TTY_FD_INVALID: |
| errno = EBADF; |
| return -1; |
| case TTY_FD_DEFAULT: |
| errno = EINVAL; |
| return -1; |
| } |
| fd = real_fd; |
| } |
| |
| |
| IO_STATUS_BLOCK io_status; |
| FILE_BASIC_INFORMATION fbi; |
| /* |
| long int status = NtQueryInformationFile((void *)fd, &io_status, &fbi, sizeof fbi, FileBasicInformation); |
| printf("nativelibc debug: futimens: status = 0x%lx\n", status); |
| if(status < 0) { |
| __set_errno_from_ntstatus(status); |
| return -1; |
| } |
| |
| fbi.ChangeTime.QuadPart = 0; |
| fbi.FileAttributes = 0; |
| */ |
| memset(&fbi, 0, sizeof fbi); |
| |
| if(tsp[0].tv_nsec != UTIME_OMIT) { |
| if(tsp[0].tv_nsec == UTIME_NOW) { |
| long int status = NtQuerySystemTime(&fbi.LastAccessTime); |
| if(status < 0) { |
| __set_errno_from_ntstatus(status); |
| return -1; |
| } |
| } else __timespec_to_hurdred_nanoseconds_since_1601(tsp, &fbi.LastAccessTime); |
| } |
| if(tsp[1].tv_nsec != UTIME_OMIT) { |
| if(tsp[1].tv_nsec == UTIME_NOW) { |
| if(tsp[0].tv_nsec == UTIME_NOW) { |
| fbi.LastWriteTime = fbi.LastAccessTime; |
| } else { |
| long int status = NtQuerySystemTime(&fbi.LastWriteTime); |
| if(status < 0) { |
| __set_errno_from_ntstatus(status); |
| return -1; |
| } |
| } |
| } else __timespec_to_hurdred_nanoseconds_since_1601(tsp + 1, &fbi.LastWriteTime); |
| } |
| |
| long int status = NtSetInformationFile((void *)fd, &io_status, &fbi, sizeof fbi, FileBasicInformation); |
| //printf("nativelibc debug: futimens: status = 0x%lx\n", status); |
| return __set_errno_from_ntstatus(status) ? -1 : 0; |
| } |
| |
| int utimensat(int dirfd, const char *file, const struct timespec *tsp, int flags) { |
| if(dirfd == AT_FDCWD && !file) { |
| errno = EFAULT; |
| return -1; |
| } |
| if(!tsp) { |
| errno = EACCES; |
| return -1; |
| } |
| if(!*file) { |
| errno = ENOENT; |
| return -1; |
| } |
| |
| if(*file != '/') { |
| if(dirfd == AT_FDCWD) errno = EBADF; |
| else errno = ENOSYS; |
| return -1; |
| } |
| |
| // XXX: Cannot open directories |
| int open_flags = O_WRONLY; |
| if(flags & AT_SYMLINK_NOFOLLOW) open_flags |= O_NOFOLLOW; |
| int fd = open(file, open_flags); |
| if(fd == -1) return -1; |
| |
| int r = futimens(fd, tsp); |
| int e = errno; |
| if(close(fd) < 0) return -1; |
| errno = e; |
| return r; |
| } |