tool_filetime.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_filetime.h"
  25. #include "tool_cfgable.h"
  26. #include "tool_msgs.h"
  27. #include "curlx.h"
  28. #ifdef HAVE_UTIME_H
  29. # include <utime.h>
  30. #elif defined(HAVE_SYS_UTIME_H)
  31. # include <sys/utime.h>
  32. #endif
  33. /* Returns 0 on success, non-zero on file problems */
  34. int getfiletime(const char *filename, struct GlobalConfig *global,
  35. curl_off_t *stamp)
  36. {
  37. int rc = 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)
  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");
  56. else {
  57. *stamp = (converted - CURL_OFF_T_C(116444736000000000)) / 10000000;
  58. rc = 0;
  59. }
  60. }
  61. else {
  62. warnf(global, "Failed to get filetime: "
  63. "GetFileTime failed: GetLastError %u",
  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",
  71. (unsigned int)GetLastError());
  72. }
  73. #else
  74. struct_stat statbuf;
  75. if(-1 != stat(filename, &statbuf)) {
  76. *stamp = (curl_off_t)statbuf.st_mtime;
  77. rc = 0;
  78. }
  79. else
  80. warnf(global, "Failed to get filetime: %s", strerror(errno));
  81. #endif
  82. return rc;
  83. }
  84. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || defined(_WIN32)
  85. void setfiletime(curl_off_t filetime, const char *filename,
  86. struct GlobalConfig *global)
  87. {
  88. if(filetime >= 0) {
  89. /* Windows utime() may attempt to adjust the unix GMT file time by a daylight
  90. saving time offset and since it's GMT that is bad behavior. When we have
  91. access to a 64-bit type we can bypass utime and set the times directly. */
  92. #if defined(_WIN32)
  93. HANDLE hfile;
  94. TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
  95. /* 910670515199 is the maximum unix filetime that can be used as a
  96. Windows FILETIME without overflow: 30827-12-31T23:59:59. */
  97. if(filetime > CURL_OFF_T_C(910670515199)) {
  98. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  99. " on outfile: overflow", filetime);
  100. curlx_unicodefree(tchar_filename);
  101. return;
  102. }
  103. hfile = CreateFile(tchar_filename, FILE_WRITE_ATTRIBUTES,
  104. (FILE_SHARE_READ | FILE_SHARE_WRITE |
  105. FILE_SHARE_DELETE),
  106. NULL, OPEN_EXISTING, 0, NULL);
  107. curlx_unicodefree(tchar_filename);
  108. if(hfile != INVALID_HANDLE_VALUE) {
  109. curl_off_t converted = ((curl_off_t)filetime * 10000000) +
  110. CURL_OFF_T_C(116444736000000000);
  111. FILETIME ft;
  112. ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
  113. ft.dwHighDateTime = (DWORD)(converted >> 32);
  114. if(!SetFileTime(hfile, NULL, &ft, &ft)) {
  115. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  116. " on outfile: SetFileTime failed: GetLastError %u",
  117. filetime, (unsigned int)GetLastError());
  118. }
  119. CloseHandle(hfile);
  120. }
  121. else {
  122. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  123. " on outfile: CreateFile failed: GetLastError %u",
  124. filetime, (unsigned int)GetLastError());
  125. }
  126. #elif defined(HAVE_UTIMES)
  127. struct timeval times[2];
  128. times[0].tv_sec = times[1].tv_sec = (time_t)filetime;
  129. times[0].tv_usec = times[1].tv_usec = 0;
  130. if(utimes(filename, times)) {
  131. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  132. " on '%s': %s", filetime, filename, strerror(errno));
  133. }
  134. #elif defined(HAVE_UTIME)
  135. struct utimbuf times;
  136. times.actime = (time_t)filetime;
  137. times.modtime = (time_t)filetime;
  138. if(utime(filename, &times)) {
  139. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  140. " on '%s': %s", filetime, filename, strerror(errno));
  141. }
  142. #endif
  143. }
  144. }
  145. #endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
  146. defined(_WIN32) */