diff options
author | Ron Yorston <rmy@pobox.com> | 2021-08-15 09:17:25 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2021-08-15 09:17:25 +0100 |
commit | fb46f0422517cba6d566131054117738e826424c (patch) | |
tree | b08c16b4db6b45b92c8351ec1a74ac188fffc191 /win32 | |
parent | 4c6c8d61bc95f5f56d91c83390f3e2631c55c37f (diff) | |
download | busybox-w32-fb46f0422517cba6d566131054117738e826424c.tar.gz busybox-w32-fb46f0422517cba6d566131054117738e826424c.tar.bz2 busybox-w32-fb46f0422517cba6d566131054117738e826424c.zip |
win32: implement utimes(2) using utimensat(2)
Saves 176 bytes.
Diffstat (limited to 'win32')
-rw-r--r-- | win32/mingw.c | 56 |
1 files changed, 18 insertions, 38 deletions
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) | |||
740 | return -1; | 740 | return -1; |
741 | } | 741 | } |
742 | 742 | ||
743 | static inline void timeval_to_filetime(const struct timeval tv, FILETIME *ft) | ||
744 | { | ||
745 | long long winTime = tv.tv_sec * 10000000LL + tv.tv_usec * 10LL + | ||
746 | 116444736000000000LL; | ||
747 | ft->dwLowDateTime = winTime; | ||
748 | ft->dwHighDateTime = winTime >> 32; | ||
749 | } | ||
750 | |||
751 | static inline void timespec_to_filetime(const struct timespec tv, FILETIME *ft) | 743 | static inline void timespec_to_filetime(const struct timespec tv, FILETIME *ft) |
752 | { | 744 | { |
753 | long long winTime = tv.tv_sec * 10000000LL + tv.tv_nsec / 100LL + | 745 | 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) | |||
756 | ft->dwHighDateTime = winTime >> 32; | 748 | ft->dwHighDateTime = winTime >> 32; |
757 | } | 749 | } |
758 | 750 | ||
759 | int utimes(const char *file_name, const struct timeval tims[2]) | ||
760 | { | ||
761 | FILETIME mft, aft; | ||
762 | HANDLE fh; | ||
763 | int rc = 0; | ||
764 | |||
765 | fh = CreateFile(file_name, FILE_WRITE_ATTRIBUTES, 0, | ||
766 | NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | ||
767 | if ( fh == INVALID_HANDLE_VALUE ) { | ||
768 | errno = err_win_to_posix(); | ||
769 | return -1; | ||
770 | } | ||
771 | |||
772 | if (tims) { | ||
773 | timeval_to_filetime(tims[0], &aft); | ||
774 | timeval_to_filetime(tims[1], &mft); | ||
775 | } | ||
776 | else { | ||
777 | GetSystemTimeAsFileTime(&mft); | ||
778 | aft = mft; | ||
779 | } | ||
780 | |||
781 | if (!SetFileTime(fh, NULL, &aft, &mft)) { | ||
782 | errno = err_win_to_posix(); | ||
783 | rc = -1; | ||
784 | } | ||
785 | CloseHandle(fh); | ||
786 | return rc; | ||
787 | } | ||
788 | |||
789 | static int hutimens(HANDLE fh, const struct timespec times[2]) | 751 | static int hutimens(HANDLE fh, const struct timespec times[2]) |
790 | { | 752 | { |
791 | FILETIME now, aft, mft; | 753 | FILETIME now, aft, mft; |
@@ -858,6 +820,24 @@ int utimensat(int fd, const char *path, const struct timespec times[2], | |||
858 | return rc; | 820 | return rc; |
859 | } | 821 | } |
860 | 822 | ||
823 | int utimes(const char *file_name, const struct timeval tv[2]) | ||
824 | { | ||
825 | struct timespec ts[2]; | ||
826 | |||
827 | if (tv) { | ||
828 | if (tv[0].tv_usec < 0 || tv[0].tv_usec >= 1000000 || | ||
829 | tv[1].tv_usec < 0 || tv[1].tv_usec >= 1000000) { | ||
830 | errno = EINVAL; | ||
831 | return -1; | ||
832 | } | ||
833 | ts[0].tv_sec = tv[0].tv_sec; | ||
834 | ts[0].tv_nsec = tv[0].tv_usec * 1000; | ||
835 | ts[1].tv_sec = tv[1].tv_sec; | ||
836 | ts[1].tv_nsec = tv[1].tv_usec * 1000; | ||
837 | } | ||
838 | return utimensat(AT_FDCWD, file_name, tv ? ts : NULL, 0); | ||
839 | } | ||
840 | |||
861 | unsigned int sleep (unsigned int seconds) | 841 | unsigned int sleep (unsigned int seconds) |
862 | { | 842 | { |
863 | Sleep(seconds*1000); | 843 | Sleep(seconds*1000); |