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