aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-03-12 19:49:22 +0000
committerRon Yorston <rmy@pobox.com>2014-03-12 19:49:22 +0000
commit005da0c098e47daabd31ddead579a93430c3c6f9 (patch)
treee18adcc2b696cbaa7fd54a6fc2ea097e4ae12774
parent216cef35d0e3364b6bc32e361e626f68b3fb48d8 (diff)
downloadbusybox-w32-005da0c098e47daabd31ddead579a93430c3c6f9.tar.gz
busybox-w32-005da0c098e47daabd31ddead579a93430c3c6f9.tar.bz2
busybox-w32-005da0c098e47daabd31ddead579a93430c3c6f9.zip
Remove unused mingw_utime; update utimes to handle read-only files
-rw-r--r--include/mingw.h4
-rw-r--r--win32/mingw.c61
2 files changed, 20 insertions, 45 deletions
diff --git a/include/mingw.h b/include/mingw.h
index c405c4a97..775987b62 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -1,4 +1,3 @@
1#include <sys/utime.h>
2 1
3#define NOIMPL(name,...) static inline int name(__VA_ARGS__) { errno = ENOSYS; return -1; } 2#define NOIMPL(name,...) static inline int name(__VA_ARGS__) { errno = ENOSYS; return -1; }
4#define IMPL(name,ret,retval,...) static inline ret name(__VA_ARGS__) { return retval; } 3#define IMPL(name,ret,retval,...) static inline ret name(__VA_ARGS__) { return retval; }
@@ -388,11 +387,8 @@ int mingw_access(const char *name, int mode);
388/* 387/*
389 * utime.h 388 * utime.h
390 */ 389 */
391int mingw_utime(const char *file_name, const struct utimbuf *times);
392int utimes(const char *file_name, const struct timeval times[2]); 390int utimes(const char *file_name, const struct timeval times[2]);
393 391
394#define utime mingw_utime
395
396/* 392/*
397 * MinGW specific 393 * MinGW specific
398 */ 394 */
diff --git a/win32/mingw.c b/win32/mingw.c
index d5a4d6297..6bff1af1c 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -359,41 +359,6 @@ int mingw_fstat(int fd, struct mingw_stat *buf)
359 return -1; 359 return -1;
360} 360}
361 361
362static inline void time_t_to_filetime(time_t t, FILETIME *ft)
363{
364 long long winTime = t * 10000000LL + 116444736000000000LL;
365 ft->dwLowDateTime = winTime;
366 ft->dwHighDateTime = winTime >> 32;
367}
368
369int mingw_utime (const char *file_name, const struct utimbuf *times)
370{
371 FILETIME mft, aft;
372 int fh, rc;
373
374 /* must have write permission */
375 if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0)
376 return -1;
377
378 if (times) {
379 time_t_to_filetime(times->modtime, &mft);
380 time_t_to_filetime(times->actime, &aft);
381 }
382 else {
383 SYSTEMTIME st;
384 GetSystemTime(&st);
385 SystemTimeToFileTime(&st, &aft);
386 mft = aft;
387 }
388 if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) {
389 errno = EINVAL;
390 rc = -1;
391 } else
392 rc = 0;
393 close(fh);
394 return rc;
395}
396
397static inline void timeval_to_filetime(const struct timeval tv, FILETIME *ft) 362static inline void timeval_to_filetime(const struct timeval tv, FILETIME *ft)
398{ 363{
399 long long winTime = ((tv.tv_sec * 1000000LL) + tv.tv_usec) * 10LL + 116444736000000000LL; 364 long long winTime = ((tv.tv_sec * 1000000LL) + tv.tv_usec) * 10LL + 116444736000000000LL;
@@ -407,18 +372,25 @@ int utimes(const char *file_name, const struct timeval times[2])
407 int fh, rc; 372 int fh, rc;
408 373
409 /* must have write permission */ 374 /* must have write permission */
410 if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) 375 DWORD attrs = GetFileAttributes(file_name);
411 return -1; 376 if (attrs != INVALID_FILE_ATTRIBUTES &&
377 (attrs & FILE_ATTRIBUTE_READONLY)) {
378 /* ignore errors here; open() will report them */
379 SetFileAttributes(file_name, attrs & ~FILE_ATTRIBUTE_READONLY);
380 }
381
382 if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) {
383 rc = -1;
384 goto revert_attrs;
385 }
412 386
413 if (times) { 387 if (times) {
414 timeval_to_filetime(times[0], &aft); 388 timeval_to_filetime(times[0], &aft);
415 timeval_to_filetime(times[1], &mft); 389 timeval_to_filetime(times[1], &mft);
416 } 390 }
417 else { 391 else {
418 SYSTEMTIME st; 392 GetSystemTimeAsFileTime(&mft);
419 GetSystemTime(&st); 393 aft = mft;
420 SystemTimeToFileTime(&st, &aft);
421 mft = aft;
422 } 394 }
423 if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) { 395 if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) {
424 errno = EINVAL; 396 errno = EINVAL;
@@ -426,6 +398,13 @@ int utimes(const char *file_name, const struct timeval times[2])
426 } else 398 } else
427 rc = 0; 399 rc = 0;
428 close(fh); 400 close(fh);
401
402revert_attrs:
403 if (attrs != INVALID_FILE_ATTRIBUTES &&
404 (attrs & FILE_ATTRIBUTE_READONLY)) {
405 /* ignore errors again */
406 SetFileAttributes(file_name, attrs);
407 }
429 return rc; 408 return rc;
430} 409}
431 410