2018-02-05 15:57:39 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-01-26 10:52:03 -05:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2018-02-05 15:57:39 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2018-02-05 15:57:39 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
#include "tool_filetime.h"
|
2021-01-26 10:52:03 -05:00
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_msgs.h"
|
2020-07-23 15:28:14 -04:00
|
|
|
#include "curlx.h"
|
|
|
|
|
2018-02-05 15:57:39 -05:00
|
|
|
#ifdef HAVE_UTIME_H
|
|
|
|
# include <utime.h>
|
|
|
|
#elif defined(HAVE_SYS_UTIME_H)
|
|
|
|
# include <sys/utime.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-15 04:14:10 -04:00
|
|
|
#if defined(__GNUC__) && defined(__MINGW32__)
|
|
|
|
/* GCC 10 on mingw has issues with this, disable */
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat"
|
|
|
|
#endif
|
|
|
|
|
2021-01-26 10:52:03 -05:00
|
|
|
curl_off_t getfiletime(const char *filename, struct GlobalConfig *global)
|
2018-02-05 15:57:39 -05:00
|
|
|
{
|
|
|
|
curl_off_t result = -1;
|
|
|
|
|
|
|
|
/* Windows stat() may attempt to adjust the unix GMT file time by a daylight
|
|
|
|
saving time offset and since it's GMT that is bad behavior. When we have
|
|
|
|
access to a 64-bit type we can bypass stat and get the times directly. */
|
|
|
|
#if defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)
|
|
|
|
HANDLE hfile;
|
2020-07-23 15:28:14 -04:00
|
|
|
TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
|
2018-02-05 15:57:39 -05:00
|
|
|
|
2020-07-23 15:28:14 -04:00
|
|
|
hfile = CreateFile(tchar_filename, FILE_READ_ATTRIBUTES,
|
2018-02-05 15:57:39 -05:00
|
|
|
(FILE_SHARE_READ | FILE_SHARE_WRITE |
|
|
|
|
FILE_SHARE_DELETE),
|
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
2020-07-23 15:28:14 -04:00
|
|
|
curlx_unicodefree(tchar_filename);
|
2018-02-05 15:57:39 -05:00
|
|
|
if(hfile != INVALID_HANDLE_VALUE) {
|
|
|
|
FILETIME ft;
|
|
|
|
if(GetFileTime(hfile, NULL, NULL, &ft)) {
|
|
|
|
curl_off_t converted = (curl_off_t)ft.dwLowDateTime
|
|
|
|
| ((curl_off_t)ft.dwHighDateTime) << 32;
|
|
|
|
|
|
|
|
if(converted < CURL_OFF_T_C(116444736000000000)) {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to get filetime: underflow\n");
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (converted - CURL_OFF_T_C(116444736000000000)) / 10000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to get filetime: "
|
|
|
|
"GetFileTime failed: GetLastError %u\n",
|
|
|
|
(unsigned int)GetLastError());
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
CloseHandle(hfile);
|
|
|
|
}
|
|
|
|
else if(GetLastError() != ERROR_FILE_NOT_FOUND) {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to get filetime: "
|
|
|
|
"CreateFile failed: GetLastError %u\n",
|
|
|
|
(unsigned int)GetLastError());
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct_stat statbuf;
|
|
|
|
if(-1 != stat(filename, &statbuf)) {
|
|
|
|
result = (curl_off_t)statbuf.st_mtime;
|
|
|
|
}
|
|
|
|
else if(errno != ENOENT) {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to get filetime: %s\n", strerror(errno));
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:52:03 -05:00
|
|
|
#if defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
|
|
|
|
(defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8))
|
2018-02-05 15:57:39 -05:00
|
|
|
void setfiletime(curl_off_t filetime, const char *filename,
|
2021-01-26 10:52:03 -05:00
|
|
|
struct GlobalConfig *global)
|
2018-02-05 15:57:39 -05:00
|
|
|
{
|
|
|
|
if(filetime >= 0) {
|
|
|
|
/* Windows utime() may attempt to adjust the unix GMT file time by a daylight
|
|
|
|
saving time offset and since it's GMT that is bad behavior. When we have
|
|
|
|
access to a 64-bit type we can bypass utime and set the times directly. */
|
|
|
|
#if defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)
|
|
|
|
HANDLE hfile;
|
2020-07-23 15:28:14 -04:00
|
|
|
TCHAR *tchar_filename = curlx_convert_UTF8_to_tchar((char *)filename);
|
2018-02-05 15:57:39 -05:00
|
|
|
|
|
|
|
/* 910670515199 is the maximum unix filetime that can be used as a
|
|
|
|
Windows FILETIME without overflow: 30827-12-31T23:59:59. */
|
|
|
|
if(filetime > CURL_OFF_T_C(910670515199)) {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
|
|
|
|
" on outfile: overflow\n", filetime);
|
2020-07-23 15:28:14 -04:00
|
|
|
curlx_unicodefree(tchar_filename);
|
2018-02-05 15:57:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:28:14 -04:00
|
|
|
hfile = CreateFile(tchar_filename, FILE_WRITE_ATTRIBUTES,
|
2021-01-26 10:52:03 -05:00
|
|
|
(FILE_SHARE_READ | FILE_SHARE_WRITE |
|
|
|
|
FILE_SHARE_DELETE),
|
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
2020-07-23 15:28:14 -04:00
|
|
|
curlx_unicodefree(tchar_filename);
|
2018-02-05 15:57:39 -05:00
|
|
|
if(hfile != INVALID_HANDLE_VALUE) {
|
|
|
|
curl_off_t converted = ((curl_off_t)filetime * 10000000) +
|
2021-01-26 10:52:03 -05:00
|
|
|
CURL_OFF_T_C(116444736000000000);
|
2018-02-05 15:57:39 -05:00
|
|
|
FILETIME ft;
|
|
|
|
ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
|
|
|
|
ft.dwHighDateTime = (DWORD)(converted >> 32);
|
|
|
|
if(!SetFileTime(hfile, NULL, &ft, &ft)) {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
|
|
|
|
" on outfile: SetFileTime failed: GetLastError %u\n",
|
|
|
|
filetime, (unsigned int)GetLastError());
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
CloseHandle(hfile);
|
|
|
|
}
|
|
|
|
else {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
|
|
|
|
" on outfile: CreateFile failed: GetLastError %u\n",
|
|
|
|
filetime, (unsigned int)GetLastError());
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_UTIMES)
|
|
|
|
struct timeval times[2];
|
|
|
|
times[0].tv_sec = times[1].tv_sec = (time_t)filetime;
|
|
|
|
times[0].tv_usec = times[1].tv_usec = 0;
|
|
|
|
if(utimes(filename, times)) {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
|
2021-01-26 11:02:39 -05:00
|
|
|
" on '%s': %s\n", filetime, filename, strerror(errno));
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_UTIME)
|
|
|
|
struct utimbuf times;
|
|
|
|
times.actime = (time_t)filetime;
|
|
|
|
times.modtime = (time_t)filetime;
|
|
|
|
if(utime(filename, ×)) {
|
2021-01-26 10:52:03 -05:00
|
|
|
warnf(global, "Failed to set filetime %" CURL_FORMAT_CURL_OFF_T
|
2021-01-26 11:02:39 -05:00
|
|
|
" on '%s': %s\n", filetime, filename, strerror(errno));
|
2018-02-05 15:57:39 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(HAVE_UTIME) || defined(HAVE_UTIMES) || \
|
|
|
|
(defined(WIN32) && (SIZEOF_CURL_OFF_T >= 8)) */
|