tool_filetime.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. curl_off_t getfiletime(const char *filename, struct GlobalConfig *global)
  34. {
  35. curl_off_t result = -1;
  36. /* Windows stat() may attempt to adjust the unix GMT file time by a daylight
  37. saving time offset and since it's GMT that is bad behavior. When we have
  38. access to a 64-bit type we can bypass stat and get the times directly. */
  39. #if defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)
  40. HANDLE hfile;
  41. TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
  42. hfile = CreateFile(tchar_filename, FILE_READ_ATTRIBUTES,
  43. (FILE_SHARE_READ | FILE_SHARE_WRITE |
  44. FILE_SHARE_DELETE),
  45. NULL, OPEN_EXISTING, 0, NULL);
  46. curlx_unicodefree(tchar_filename);
  47. if(hfile != INVALID_HANDLE_VALUE) {
  48. FILETIME ft;
  49. if(GetFileTime(hfile, NULL, NULL, &ft)) {
  50. curl_off_t converted = (curl_off_t)ft.dwLowDateTime
  51. | ((curl_off_t)ft.dwHighDateTime) << 32;
  52. if(converted < CURL_OFF_T_C(116444736000000000)) {
  53. warnf(global, "Failed to get filetime: underflow\n");
  54. }
  55. else {
  56. result = (converted - CURL_OFF_T_C(116444736000000000)) / 10000000;
  57. }
  58. }
  59. else {
  60. warnf(global, "Failed to get filetime: "
  61. "GetFileTime failed: GetLastError %u\n",
  62. (unsigned int)GetLastError());
  63. }
  64. CloseHandle(hfile);
  65. }
  66. else if(GetLastError() != ERROR_FILE_NOT_FOUND) {
  67. warnf(global, "Failed to get filetime: "
  68. "CreateFile failed: GetLastError %u\n",
  69. (unsigned int)GetLastError());
  70. }
  71. #else
  72. struct_stat statbuf;
  73. if(-1 != stat(filename, &statbuf)) {
  74. result = (curl_off_t)statbuf.st_mtime;
  75. }
  76. else if(errno != ENOENT) {
  77. warnf(global, "Failed to get filetime: %s\n", strerror(errno));
  78. }
  79. #endif
  80. return result;
  81. }
  82. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
  83. (defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8))
  84. void setfiletime(curl_off_t filetime, const char *filename,
  85. struct GlobalConfig *global)
  86. {
  87. if(filetime >= 0) {
  88. /* Windows utime() may attempt to adjust the unix GMT file time by a daylight
  89. saving time offset and since it's GMT that is bad behavior. When we have
  90. access to a 64-bit type we can bypass utime and set the times directly. */
  91. #if defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)
  92. HANDLE hfile;
  93. TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
  94. /* 910670515199 is the maximum unix filetime that can be used as a
  95. Windows FILETIME without overflow: 30827-12-31T23:59:59. */
  96. if(filetime > CURL_OFF_T_C(910670515199)) {
  97. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  98. " on outfile: overflow\n", filetime);
  99. curlx_unicodefree(tchar_filename);
  100. return;
  101. }
  102. hfile = CreateFile(tchar_filename, FILE_WRITE_ATTRIBUTES,
  103. (FILE_SHARE_READ | FILE_SHARE_WRITE |
  104. FILE_SHARE_DELETE),
  105. NULL, OPEN_EXISTING, 0, NULL);
  106. curlx_unicodefree(tchar_filename);
  107. if(hfile != INVALID_HANDLE_VALUE) {
  108. curl_off_t converted = ((curl_off_t)filetime * 10000000) +
  109. CURL_OFF_T_C(116444736000000000);
  110. FILETIME ft;
  111. ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
  112. ft.dwHighDateTime = (DWORD)(converted >> 32);
  113. if(!SetFileTime(hfile, NULL, &ft, &ft)) {
  114. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  115. " on outfile: SetFileTime failed: GetLastError %u\n",
  116. filetime, (unsigned int)GetLastError());
  117. }
  118. CloseHandle(hfile);
  119. }
  120. else {
  121. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  122. " on outfile: CreateFile failed: GetLastError %u\n",
  123. filetime, (unsigned int)GetLastError());
  124. }
  125. #elif defined(HAVE_UTIMES)
  126. struct timeval times[2];
  127. times[0].tv_sec = times[1].tv_sec = (time_t)filetime;
  128. times[0].tv_usec = times[1].tv_usec = 0;
  129. if(utimes(filename, times)) {
  130. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  131. " on '%s': %s\n", filetime, filename, strerror(errno));
  132. }
  133. #elif defined(HAVE_UTIME)
  134. struct utimbuf times;
  135. times.actime = (time_t)filetime;
  136. times.modtime = (time_t)filetime;
  137. if(utime(filename, &times)) {
  138. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  139. " on '%s': %s\n", filetime, filename, strerror(errno));
  140. }
  141. #endif
  142. }
  143. }
  144. #endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
  145. (defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)) */