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