tool_filetime.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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)
  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");
  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",
  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",
  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", strerror(errno));
  78. }
  79. #endif
  80. return result;
  81. }
  82. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || defined(WIN32)
  83. void setfiletime(curl_off_t filetime, const char *filename,
  84. struct GlobalConfig *global)
  85. {
  86. if(filetime >= 0) {
  87. /* Windows utime() may attempt to adjust the unix GMT file time by a daylight
  88. saving time offset and since it's GMT that is bad behavior. When we have
  89. access to a 64-bit type we can bypass utime and set the times directly. */
  90. #if defined(WIN32)
  91. HANDLE hfile;
  92. TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
  93. /* 910670515199 is the maximum unix filetime that can be used as a
  94. Windows FILETIME without overflow: 30827-12-31T23:59:59. */
  95. if(filetime > CURL_OFF_T_C(910670515199)) {
  96. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  97. " on outfile: overflow", filetime);
  98. curlx_unicodefree(tchar_filename);
  99. return;
  100. }
  101. hfile = CreateFile(tchar_filename, FILE_WRITE_ATTRIBUTES,
  102. (FILE_SHARE_READ | FILE_SHARE_WRITE |
  103. FILE_SHARE_DELETE),
  104. NULL, OPEN_EXISTING, 0, NULL);
  105. curlx_unicodefree(tchar_filename);
  106. if(hfile != INVALID_HANDLE_VALUE) {
  107. curl_off_t converted = ((curl_off_t)filetime * 10000000) +
  108. CURL_OFF_T_C(116444736000000000);
  109. FILETIME ft;
  110. ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
  111. ft.dwHighDateTime = (DWORD)(converted >> 32);
  112. if(!SetFileTime(hfile, NULL, &ft, &ft)) {
  113. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  114. " on outfile: SetFileTime failed: GetLastError %u",
  115. filetime, (unsigned int)GetLastError());
  116. }
  117. CloseHandle(hfile);
  118. }
  119. else {
  120. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  121. " on outfile: CreateFile failed: GetLastError %u",
  122. filetime, (unsigned int)GetLastError());
  123. }
  124. #elif defined(HAVE_UTIMES)
  125. struct timeval times[2];
  126. times[0].tv_sec = times[1].tv_sec = (time_t)filetime;
  127. times[0].tv_usec = times[1].tv_usec = 0;
  128. if(utimes(filename, times)) {
  129. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  130. " on '%s': %s", filetime, filename, strerror(errno));
  131. }
  132. #elif defined(HAVE_UTIME)
  133. struct utimbuf times;
  134. times.actime = (time_t)filetime;
  135. times.modtime = (time_t)filetime;
  136. if(utime(filename, &times)) {
  137. warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
  138. " on '%s': %s", filetime, filename, strerror(errno));
  139. }
  140. #endif
  141. }
  142. }
  143. #endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
  144. defined(WIN32) */