1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker * _ _ ____ _
3*6236dae4SAndroid Build Coastguard Worker * Project ___| | | | _ \| |
4*6236dae4SAndroid Build Coastguard Worker * / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker * | (__| |_| | _ <| |___
6*6236dae4SAndroid Build Coastguard Worker * \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker *
8*6236dae4SAndroid Build Coastguard Worker * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker *
10*6236dae4SAndroid Build Coastguard Worker * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker *
14*6236dae4SAndroid Build Coastguard Worker * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker *
18*6236dae4SAndroid Build Coastguard Worker * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker *
21*6236dae4SAndroid Build Coastguard Worker * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker *
23*6236dae4SAndroid Build Coastguard Worker ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker #include "tool_filetime.h"
25*6236dae4SAndroid Build Coastguard Worker #include "tool_cfgable.h"
26*6236dae4SAndroid Build Coastguard Worker #include "tool_msgs.h"
27*6236dae4SAndroid Build Coastguard Worker #include "curlx.h"
28*6236dae4SAndroid Build Coastguard Worker
29*6236dae4SAndroid Build Coastguard Worker #ifdef HAVE_UTIME_H
30*6236dae4SAndroid Build Coastguard Worker # include <utime.h>
31*6236dae4SAndroid Build Coastguard Worker #elif defined(HAVE_SYS_UTIME_H)
32*6236dae4SAndroid Build Coastguard Worker # include <sys/utime.h>
33*6236dae4SAndroid Build Coastguard Worker #endif
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard Worker /* Returns 0 on success, non-zero on file problems */
getfiletime(const char * filename,struct GlobalConfig * global,curl_off_t * stamp)36*6236dae4SAndroid Build Coastguard Worker int getfiletime(const char *filename, struct GlobalConfig *global,
37*6236dae4SAndroid Build Coastguard Worker curl_off_t *stamp)
38*6236dae4SAndroid Build Coastguard Worker {
39*6236dae4SAndroid Build Coastguard Worker int rc = 1;
40*6236dae4SAndroid Build Coastguard Worker
41*6236dae4SAndroid Build Coastguard Worker /* Windows stat() may attempt to adjust the Unix GMT file time by a daylight
42*6236dae4SAndroid Build Coastguard Worker saving time offset and since it is GMT that is bad behavior. When we have
43*6236dae4SAndroid Build Coastguard Worker access to a 64-bit type we can bypass stat and get the times directly. */
44*6236dae4SAndroid Build Coastguard Worker #if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
45*6236dae4SAndroid Build Coastguard Worker HANDLE hfile;
46*6236dae4SAndroid Build Coastguard Worker TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
47*6236dae4SAndroid Build Coastguard Worker
48*6236dae4SAndroid Build Coastguard Worker hfile = CreateFile(tchar_filename, FILE_READ_ATTRIBUTES,
49*6236dae4SAndroid Build Coastguard Worker (FILE_SHARE_READ | FILE_SHARE_WRITE |
50*6236dae4SAndroid Build Coastguard Worker FILE_SHARE_DELETE),
51*6236dae4SAndroid Build Coastguard Worker NULL, OPEN_EXISTING, 0, NULL);
52*6236dae4SAndroid Build Coastguard Worker curlx_unicodefree(tchar_filename);
53*6236dae4SAndroid Build Coastguard Worker if(hfile != INVALID_HANDLE_VALUE) {
54*6236dae4SAndroid Build Coastguard Worker FILETIME ft;
55*6236dae4SAndroid Build Coastguard Worker if(GetFileTime(hfile, NULL, NULL, &ft)) {
56*6236dae4SAndroid Build Coastguard Worker curl_off_t converted = (curl_off_t)ft.dwLowDateTime
57*6236dae4SAndroid Build Coastguard Worker | ((curl_off_t)ft.dwHighDateTime) << 32;
58*6236dae4SAndroid Build Coastguard Worker
59*6236dae4SAndroid Build Coastguard Worker if(converted < CURL_OFF_T_C(116444736000000000))
60*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to get filetime: underflow");
61*6236dae4SAndroid Build Coastguard Worker else {
62*6236dae4SAndroid Build Coastguard Worker *stamp = (converted - CURL_OFF_T_C(116444736000000000)) / 10000000;
63*6236dae4SAndroid Build Coastguard Worker rc = 0;
64*6236dae4SAndroid Build Coastguard Worker }
65*6236dae4SAndroid Build Coastguard Worker }
66*6236dae4SAndroid Build Coastguard Worker else {
67*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to get filetime: "
68*6236dae4SAndroid Build Coastguard Worker "GetFileTime failed: GetLastError %u",
69*6236dae4SAndroid Build Coastguard Worker (unsigned int)GetLastError());
70*6236dae4SAndroid Build Coastguard Worker }
71*6236dae4SAndroid Build Coastguard Worker CloseHandle(hfile);
72*6236dae4SAndroid Build Coastguard Worker }
73*6236dae4SAndroid Build Coastguard Worker else if(GetLastError() != ERROR_FILE_NOT_FOUND) {
74*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to get filetime: "
75*6236dae4SAndroid Build Coastguard Worker "CreateFile failed: GetLastError %u",
76*6236dae4SAndroid Build Coastguard Worker (unsigned int)GetLastError());
77*6236dae4SAndroid Build Coastguard Worker }
78*6236dae4SAndroid Build Coastguard Worker #else
79*6236dae4SAndroid Build Coastguard Worker struct_stat statbuf;
80*6236dae4SAndroid Build Coastguard Worker if(-1 != stat(filename, &statbuf)) {
81*6236dae4SAndroid Build Coastguard Worker *stamp = (curl_off_t)statbuf.st_mtime;
82*6236dae4SAndroid Build Coastguard Worker rc = 0;
83*6236dae4SAndroid Build Coastguard Worker }
84*6236dae4SAndroid Build Coastguard Worker else
85*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to get filetime: %s", strerror(errno));
86*6236dae4SAndroid Build Coastguard Worker #endif
87*6236dae4SAndroid Build Coastguard Worker return rc;
88*6236dae4SAndroid Build Coastguard Worker }
89*6236dae4SAndroid Build Coastguard Worker
90*6236dae4SAndroid Build Coastguard Worker #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || defined(_WIN32)
setfiletime(curl_off_t filetime,const char * filename,struct GlobalConfig * global)91*6236dae4SAndroid Build Coastguard Worker void setfiletime(curl_off_t filetime, const char *filename,
92*6236dae4SAndroid Build Coastguard Worker struct GlobalConfig *global)
93*6236dae4SAndroid Build Coastguard Worker {
94*6236dae4SAndroid Build Coastguard Worker if(filetime >= 0) {
95*6236dae4SAndroid Build Coastguard Worker /* Windows utime() may attempt to adjust the Unix GMT file time by a daylight
96*6236dae4SAndroid Build Coastguard Worker saving time offset and since it is GMT that is bad behavior. When we have
97*6236dae4SAndroid Build Coastguard Worker access to a 64-bit type we can bypass utime and set the times directly. */
98*6236dae4SAndroid Build Coastguard Worker #if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
99*6236dae4SAndroid Build Coastguard Worker HANDLE hfile;
100*6236dae4SAndroid Build Coastguard Worker TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
101*6236dae4SAndroid Build Coastguard Worker
102*6236dae4SAndroid Build Coastguard Worker /* 910670515199 is the maximum Unix filetime that can be used as a
103*6236dae4SAndroid Build Coastguard Worker Windows FILETIME without overflow: 30827-12-31T23:59:59. */
104*6236dae4SAndroid Build Coastguard Worker if(filetime > CURL_OFF_T_C(910670515199)) {
105*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
106*6236dae4SAndroid Build Coastguard Worker " on outfile: overflow", filetime);
107*6236dae4SAndroid Build Coastguard Worker curlx_unicodefree(tchar_filename);
108*6236dae4SAndroid Build Coastguard Worker return;
109*6236dae4SAndroid Build Coastguard Worker }
110*6236dae4SAndroid Build Coastguard Worker
111*6236dae4SAndroid Build Coastguard Worker hfile = CreateFile(tchar_filename, FILE_WRITE_ATTRIBUTES,
112*6236dae4SAndroid Build Coastguard Worker (FILE_SHARE_READ | FILE_SHARE_WRITE |
113*6236dae4SAndroid Build Coastguard Worker FILE_SHARE_DELETE),
114*6236dae4SAndroid Build Coastguard Worker NULL, OPEN_EXISTING, 0, NULL);
115*6236dae4SAndroid Build Coastguard Worker curlx_unicodefree(tchar_filename);
116*6236dae4SAndroid Build Coastguard Worker if(hfile != INVALID_HANDLE_VALUE) {
117*6236dae4SAndroid Build Coastguard Worker curl_off_t converted = ((curl_off_t)filetime * 10000000) +
118*6236dae4SAndroid Build Coastguard Worker CURL_OFF_T_C(116444736000000000);
119*6236dae4SAndroid Build Coastguard Worker FILETIME ft;
120*6236dae4SAndroid Build Coastguard Worker ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
121*6236dae4SAndroid Build Coastguard Worker ft.dwHighDateTime = (DWORD)(converted >> 32);
122*6236dae4SAndroid Build Coastguard Worker if(!SetFileTime(hfile, NULL, &ft, &ft)) {
123*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
124*6236dae4SAndroid Build Coastguard Worker " on outfile: SetFileTime failed: GetLastError %u",
125*6236dae4SAndroid Build Coastguard Worker filetime, (unsigned int)GetLastError());
126*6236dae4SAndroid Build Coastguard Worker }
127*6236dae4SAndroid Build Coastguard Worker CloseHandle(hfile);
128*6236dae4SAndroid Build Coastguard Worker }
129*6236dae4SAndroid Build Coastguard Worker else {
130*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
131*6236dae4SAndroid Build Coastguard Worker " on outfile: CreateFile failed: GetLastError %u",
132*6236dae4SAndroid Build Coastguard Worker filetime, (unsigned int)GetLastError());
133*6236dae4SAndroid Build Coastguard Worker }
134*6236dae4SAndroid Build Coastguard Worker
135*6236dae4SAndroid Build Coastguard Worker #elif defined(HAVE_UTIMES)
136*6236dae4SAndroid Build Coastguard Worker struct timeval times[2];
137*6236dae4SAndroid Build Coastguard Worker times[0].tv_sec = times[1].tv_sec = (time_t)filetime;
138*6236dae4SAndroid Build Coastguard Worker times[0].tv_usec = times[1].tv_usec = 0;
139*6236dae4SAndroid Build Coastguard Worker if(utimes(filename, times)) {
140*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
141*6236dae4SAndroid Build Coastguard Worker " on '%s': %s", filetime, filename, strerror(errno));
142*6236dae4SAndroid Build Coastguard Worker }
143*6236dae4SAndroid Build Coastguard Worker
144*6236dae4SAndroid Build Coastguard Worker #elif defined(HAVE_UTIME)
145*6236dae4SAndroid Build Coastguard Worker struct utimbuf times;
146*6236dae4SAndroid Build Coastguard Worker times.actime = (time_t)filetime;
147*6236dae4SAndroid Build Coastguard Worker times.modtime = (time_t)filetime;
148*6236dae4SAndroid Build Coastguard Worker if(utime(filename, ×)) {
149*6236dae4SAndroid Build Coastguard Worker warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
150*6236dae4SAndroid Build Coastguard Worker " on '%s': %s", filetime, filename, strerror(errno));
151*6236dae4SAndroid Build Coastguard Worker }
152*6236dae4SAndroid Build Coastguard Worker #endif
153*6236dae4SAndroid Build Coastguard Worker }
154*6236dae4SAndroid Build Coastguard Worker }
155*6236dae4SAndroid Build Coastguard Worker #endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
156*6236dae4SAndroid Build Coastguard Worker defined(_WIN32) */
157