aboutsummaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-03-04 08:10:24 +0000
committerRon Yorston <rmy@pobox.com>2019-03-04 08:10:24 +0000
commit7c9be982c7758c477cd2735ed3784f3eee67a78e (patch)
treec4c0928b9db5a5e6d05d25f04d8ef281869aa16b /win32
parentb7e469eac680e5e1fe998198dbd2dbad8782eb34 (diff)
downloadbusybox-w32-7c9be982c7758c477cd2735ed3784f3eee67a78e.tar.gz
busybox-w32-7c9be982c7758c477cd2735ed3784f3eee67a78e.tar.bz2
busybox-w32-7c9be982c7758c477cd2735ed3784f3eee67a78e.zip
win32: simplify utimes(2) implementation
SetFileTime only needs FILE_WRITE_ATTRIBUTES access, not full read/write access. Therefore it isn't necessary to change the permissions of read-only files. The flag FILE_FLAG_BACKUP_SEMANTICS is required to access directories but does no harm if used on a file. As a result there's no need to get file attributes.
Diffstat (limited to 'win32')
-rw-r--r--win32/mingw.c37
1 files changed, 6 insertions, 31 deletions
diff --git a/win32/mingw.c b/win32/mingw.c
index f84cb76d3..ea439a66d 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -678,31 +678,13 @@ int utimes(const char *file_name, const struct timeval tims[2])
678{ 678{
679 FILETIME mft, aft; 679 FILETIME mft, aft;
680 HANDLE fh; 680 HANDLE fh;
681 DWORD flags, attrs; 681 int rc = 0;
682 int rc;
683 682
684 flags = FILE_ATTRIBUTE_NORMAL; 683 fh = CreateFile(file_name, FILE_WRITE_ATTRIBUTES, 0,
685 684 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
686 /* must have write permission */
687 attrs = GetFileAttributes(file_name);
688 if ( attrs != INVALID_FILE_ATTRIBUTES ) {
689 if ( attrs & FILE_ATTRIBUTE_READONLY ) {
690 /* ignore errors here; open() will report them */
691 SetFileAttributes(file_name, attrs & ~FILE_ATTRIBUTE_READONLY);
692 }
693
694 if ( attrs & FILE_ATTRIBUTE_DIRECTORY ) {
695 flags = FILE_FLAG_BACKUP_SEMANTICS;
696 }
697 }
698
699 fh = CreateFile(file_name, GENERIC_READ|GENERIC_WRITE,
700 FILE_SHARE_READ|FILE_SHARE_WRITE,
701 NULL, OPEN_EXISTING, flags, NULL);
702 if ( fh == INVALID_HANDLE_VALUE ) { 685 if ( fh == INVALID_HANDLE_VALUE ) {
703 errno = err_win_to_posix(GetLastError()); 686 errno = err_win_to_posix(GetLastError());
704 rc = -1; 687 return -1;
705 goto revert_attrs;
706 } 688 }
707 689
708 if (tims) { 690 if (tims) {
@@ -713,19 +695,12 @@ int utimes(const char *file_name, const struct timeval tims[2])
713 GetSystemTimeAsFileTime(&mft); 695 GetSystemTimeAsFileTime(&mft);
714 aft = mft; 696 aft = mft;
715 } 697 }
698
716 if (!SetFileTime(fh, NULL, &aft, &mft)) { 699 if (!SetFileTime(fh, NULL, &aft, &mft)) {
717 errno = EINVAL; 700 errno = EINVAL;
718 rc = -1; 701 rc = -1;
719 } else
720 rc = 0;
721 CloseHandle(fh);
722
723revert_attrs:
724 if (attrs != INVALID_FILE_ATTRIBUTES &&
725 (attrs & FILE_ATTRIBUTE_READONLY)) {
726 /* ignore errors again */
727 SetFileAttributes(file_name, attrs);
728 } 702 }
703 CloseHandle(fh);
729 return rc; 704 return rc;
730} 705}
731 706