Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/src/tool_filetime.c
2649 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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
28
#ifdef HAVE_UTIME_H
29
# include <utime.h>
30
#elif defined(HAVE_SYS_UTIME_H)
31
# include <sys/utime.h>
32
#endif
33
34
/* Returns 0 on success, non-zero on file problems */
35
int getfiletime(const char *filename, curl_off_t *stamp)
36
{
37
int rc = 1;
38
39
/* Windows stat() may attempt to adjust the Unix GMT file time by a daylight
40
saving time offset and since it is 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) && !defined(CURL_WINDOWS_UWP)
43
HANDLE hfile;
44
TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar(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 < 116444736000000000)
58
warnf("Failed to get filetime: underflow");
59
else {
60
*stamp = (converted - 116444736000000000) / 10000000;
61
rc = 0;
62
}
63
}
64
else {
65
warnf("Failed to get filetime: GetFileTime failed: GetLastError 0x%08lx",
66
GetLastError());
67
}
68
CloseHandle(hfile);
69
}
70
else if(GetLastError() != ERROR_FILE_NOT_FOUND) {
71
warnf("Failed to get filetime: CreateFile failed: GetLastError 0x%08lx",
72
GetLastError());
73
}
74
#else
75
struct_stat statbuf;
76
if(curlx_stat(filename, &statbuf) != -1) {
77
*stamp = (curl_off_t)statbuf.st_mtime;
78
rc = 0;
79
}
80
else {
81
char errbuf[STRERROR_LEN];
82
warnf("Failed to get filetime: %s",
83
curlx_strerror(errno, errbuf, sizeof(errbuf)));
84
}
85
#endif
86
return rc;
87
}
88
89
#if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || defined(_WIN32)
90
void setfiletime(curl_off_t filetime, const char *filename)
91
{
92
/* Windows utime() may attempt to adjust the Unix GMT file time by a daylight
93
saving time offset and since it is 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) && !defined(CURL_WINDOWS_UWP)
96
HANDLE hfile;
97
TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar(filename);
98
99
/* 910670515199 is the maximum Unix filetime that can be used as a Windows
100
FILETIME without overflow: 30827-12-31T23:59:59. */
101
if(filetime > 910670515199) {
102
filetime = 910670515199;
103
warnf("Capping set filetime to max to avoid overflow");
104
}
105
else if(filetime < -6857222400) {
106
/* dates before 14 september 1752 (pre-Gregorian calendar) are not
107
accurate */
108
filetime = -6857222400;
109
warnf("Capping set filetime to minimum to avoid overflow");
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
116444736000000000;
120
FILETIME ft;
121
ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
122
ft.dwHighDateTime = (DWORD)(converted >> 32);
123
if(!SetFileTime(hfile, NULL, &ft, &ft)) {
124
warnf("Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
125
" on outfile: SetFileTime failed: GetLastError 0x%08lx",
126
filetime, GetLastError());
127
}
128
CloseHandle(hfile);
129
}
130
else {
131
warnf("Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
132
" on outfile: CreateFile failed: GetLastError 0x%08lx",
133
filetime, GetLastError());
134
}
135
136
#elif defined(HAVE_UTIMES)
137
struct timeval times[2];
138
times[0].tv_sec = times[1].tv_sec = (time_t)filetime;
139
times[0].tv_usec = times[1].tv_usec = 0;
140
if(utimes(filename, times)) {
141
char errbuf[STRERROR_LEN];
142
warnf("Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
143
" on '%s': %s", filetime, filename,
144
curlx_strerror(errno, errbuf, sizeof(errbuf)));
145
}
146
147
#elif defined(HAVE_UTIME)
148
struct utimbuf times;
149
times.actime = (time_t)filetime;
150
times.modtime = (time_t)filetime;
151
if(utime(filename, &times)) {
152
char errbuf[STRERROR_LEN];
153
warnf("Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
154
" on '%s': %s", filetime, filename,
155
curlx_strerror(errno, errbuf, sizeof(errbuf)));
156
}
157
#endif
158
}
159
#endif /* HAVE_UTIME || HAVE_UTIMES || _WIN32 */
160
161