Merge pull request #770 from neheb/dev

util.h: Remove deprecated utime for non-Windows
This commit is contained in:
Yann Collet 2019-09-10 12:56:53 -07:00 committed by GitHub
commit 9b2b96edc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -86,7 +86,7 @@ extern "C" {
# else # else
# if defined(__linux__) || defined(__linux) # if defined(__linux__) || defined(__linux)
# ifndef _POSIX_C_SOURCE # ifndef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE 200112L /* use feature test macro */ # define _POSIX_C_SOURCE 200809L /* use feature test macro */
# endif # endif
# endif # endif
# include <unistd.h> /* declares _POSIX_VERSION */ # include <unistd.h> /* declares _POSIX_VERSION */

View File

@ -37,12 +37,17 @@ extern "C" {
#include <assert.h> #include <assert.h>
#include <sys/types.h> /* stat, utime */ #include <sys/types.h> /* stat, utime */
#include <sys/stat.h> /* stat */ #include <sys/stat.h> /* stat */
#if defined(_MSC_VER) #if defined(_WIN32)
# include <sys/utime.h> /* utime */ # include <sys/utime.h> /* utime */
# include <io.h> /* _chmod */ # include <io.h> /* _chmod */
#else #else
# include <unistd.h> /* chown, stat */ # include <unistd.h> /* chown, stat */
# if PLATFORM_POSIX_VERSION < 200809L
# include <utime.h> /* utime */ # include <utime.h> /* utime */
# else
# include <fcntl.h> /* AT_FDCWD */
# include <sys/stat.h> /* for utimensat */
# endif
#endif #endif
#include <time.h> /* time */ #include <time.h> /* time */
#include <limits.h> /* INT_MAX */ #include <limits.h> /* INT_MAX */
@ -287,14 +292,23 @@ UTIL_STATIC int UTIL_isRegFile(const char* infilename);
UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf) UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
{ {
int res = 0; int res = 0;
struct utimbuf timebuf;
if (!UTIL_isRegFile(filename)) if (!UTIL_isRegFile(filename))
return -1; return -1;
timebuf.actime = time(NULL); {
timebuf.modtime = statbuf->st_mtime; #if defined(_WIN32) || (PLATFORM_POSIX_VERSION < 200809L)
res += utime(filename, &timebuf); /* set access and modification times */ struct utimbuf timebuf;
timebuf.actime = time(NULL);
timebuf.modtime = statbuf->st_mtime;
res += utime(filename, &timebuf); /* set access and modification times */
#else
struct timespec timebuf[2] = {};
timebuf[0].tv_nsec = UTIME_NOW;
timebuf[1].tv_sec = statbuf->st_mtime;
res += utimensat(AT_FDCWD, filename, timebuf, 0); /* set access and modification times */
#endif
}
#if !defined(_WIN32) #if !defined(_WIN32)
res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */