tool_filetime.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "tool_filetime.h"
  23. #include "tool_cfgable.h"
  24. #include "tool_msgs.h"
  25. #include "curlx.h"
  26. #ifdef HAVE_UTIME_H
  27. # include <utime.h>
  28. #elif defined(HAVE_SYS_UTIME_H)
  29. # include <sys/utime.h>
  30. #endif
  31. #if defined(__GNUC__) && defined(__MINGW32__)
  32. /* GCC 10 on mingw has issues with this, disable */
  33. #pragma GCC diagnostic ignored "-Wformat"
  34. #endif
  35. curl_off_t getfiletime(const char *filename, struct GlobalConfig *global)
  36. {
  37. curl_off_t result = -1;
  38. /* Windows stat() may attempt to adjust the unix GMT file time by a daylight
  39. saving time offset and since it's GMT that is bad behavior. When we have
  40. access to a 64-bit type we can bypass stat and get the times directly. */
  41. #if defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)
  42. HANDLE hfile;
  43. TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
  44. hfile = CreateFile(tchar_filename, FILE_READ_ATTRIBUTES,
  45. (FILE_SHARE_READ | FILE_SHARE_WRITE |
  46. FILE_SHARE_DELETE),
  47. NULL, OPEN_EXISTING, 0, NULL);
  48. curlx_unicodefree(tchar_filename);
  49. if(hfile != INVALID_HANDLE_VALUE) {
  50. FILETIME ft;
  51. if(GetFileTime(hfile, NULL, NULL, &ft)) {
  52. curl_off_t converted = (curl_off_t)ft.dwLowDateTime
  53. | ((curl_off_t)ft.dwHighDateTime) << 32;
  54. if(converted < CURL_OFF_T_C(116444736000000000)) {
  55. warnf(global, "Failed to get filetime: underflow\n");
  56. }
  57. else {
  58. result = (converted - CURL_OFF_T_C(116444736000000000)) / 10000000;
  59. }
  60. }
  61. else {
  62. warnf(global, "Failed to get filetime: "
  63. "GetFileTime failed: GetLastError %u\n",
  64. (unsigned int)GetLastError());
  65. }
  66. CloseHandle(hfile);
  67. }
  68. else if(GetLastError() != ERROR_FILE_NOT_FOUND) {
  69. warnf(global, "Failed to get filetime: "
  70. "CreateFile failed: GetLastError %u\n",
  71. (unsigned int)GetLastError());
  72. }
  73. #else
  74. struct_stat statbuf;
  75. if(-1 != stat(filename, &statbuf)) {
  76. result = (curl_off_t)statbuf.st_mtime;
  77. }
  78. else if(errno != ENOENT) {
  79. warnf(global, "Failed to get filetime: %s\n", strerror(errno));
  80. }
  81. #endif
  82. return result;
  83. }
  84. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
  85. (defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8))
  86. void setfiletime(curl_off_t filetime, const char *filename,
  87. struct GlobalConfig *global)
  88. {
  89. if(filetime >= 0) {
  90. /* Windows utime() may attempt to adjust the unix GMT file time by a daylight
  91. saving time offset and since it's GMT that is bad behavior. When we have
  92. access to a 64-bit type we can bypass utime and set the times directly. */
  93. #if defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)
  94. HANDLE hfile;
  95. TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
  96. /* 910670515199 is the maximum unix filetime that can be used as a
  97. Windows FILETIME without overflow: 30827-12-31T23:59:59. */
  98. if(filetime > CURL_OFF_T_C(910670515199)) {
  99. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  100. " on outfile: overflow\n", filetime);
  101. curlx_unicodefree(tchar_filename);
  102. return;
  103. }
  104. hfile = CreateFile(tchar_filename, FILE_WRITE_ATTRIBUTES,
  105. (FILE_SHARE_READ | FILE_SHARE_WRITE |
  106. FILE_SHARE_DELETE),
  107. NULL, OPEN_EXISTING, 0, NULL);
  108. curlx_unicodefree(tchar_filename);
  109. if(hfile != INVALID_HANDLE_VALUE) {
  110. curl_off_t converted = ((curl_off_t)filetime * 10000000) +
  111. CURL_OFF_T_C(116444736000000000);
  112. FILETIME ft;
  113. ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
  114. ft.dwHighDateTime = (DWORD)(converted >> 32);
  115. if(!SetFileTime(hfile, NULL, &ft, &ft)) {
  116. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  117. " on outfile: SetFileTime failed: GetLastError %u\n",
  118. filetime, (unsigned int)GetLastError());
  119. }
  120. CloseHandle(hfile);
  121. }
  122. else {
  123. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  124. " on outfile: CreateFile failed: GetLastError %u\n",
  125. filetime, (unsigned int)GetLastError());
  126. }
  127. #elif defined(HAVE_UTIMES)
  128. struct timeval times[2];
  129. times[0].tv_sec = times[1].tv_sec = (time_t)filetime;
  130. times[0].tv_usec = times[1].tv_usec = 0;
  131. if(utimes(filename, times)) {
  132. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  133. " on '%s': %s\n", filetime, filename, strerror(errno));
  134. }
  135. #elif defined(HAVE_UTIME)
  136. struct utimbuf times;
  137. times.actime = (time_t)filetime;
  138. times.modtime = (time_t)filetime;
  139. if(utime(filename, &times)) {
  140. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  141. " on '%s': %s\n", filetime, filename, strerror(errno));
  142. }
  143. #endif
  144. }
  145. }
  146. #endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
  147. (defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)) */