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