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