Skip to content

Commit cc5b1b5

Browse files
kbleesdscho
authored andcommitted
Win32: implement nanosecond-precision file times
We no longer use any of MSVCRT's stat-functions, so there's no need to stick to a CRT-compatible 'struct stat' either. Define and use our own POSIX-2013-compatible 'struct stat' with nanosecond- precision file times. Note: Due to performance issues when using git variants with different file time resolutions, this patch does *not* yet enable nanosecond precision in the Makefile (use 'make USE_NSEC=1'). Signed-off-by: Karsten Blees <[email protected]>
1 parent 837bced commit cc5b1b5

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

compat/mingw.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,9 @@ int mingw_lstat(const char *file_name, struct stat *buf)
715715
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
716716
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
717717
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
718-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
719-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
720-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
718+
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
719+
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
720+
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
721721
return 0;
722722
}
723723
error:
@@ -762,9 +762,9 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
762762
buf->st_nlink = 1;
763763
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
764764
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
765-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
766-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
767-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
765+
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
766+
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
767+
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
768768
return 0;
769769
}
770770

compat/mingw.h

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -328,24 +328,49 @@ static inline long long filetime_to_hnsec(const FILETIME *ft)
328328
return winTime - 116444736000000000LL;
329329
}
330330

331-
static inline time_t filetime_to_time_t(const FILETIME *ft)
332-
{
333-
return (time_t)(filetime_to_hnsec(ft) / 10000000);
334-
}
335-
336331
/*
337-
* Use mingw specific stat()/lstat()/fstat() implementations on Windows.
332+
* Use mingw specific stat()/lstat()/fstat() implementations on Windows,
333+
* including our own struct stat with 64 bit st_size and nanosecond-precision
334+
* file times.
338335
*/
339336
#ifndef __MINGW64_VERSION_MAJOR
340337
#define off_t off64_t
341338
#define lseek _lseeki64
339+
struct timespec {
340+
time_t tv_sec;
341+
long tv_nsec;
342+
};
342343
#endif
343344

344-
/* use struct stat with 64 bit st_size */
345+
static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts)
346+
{
347+
long long hnsec = filetime_to_hnsec(ft);
348+
ts->tv_sec = (time_t)(hnsec / 10000000);
349+
ts->tv_nsec = (hnsec % 10000000) * 100;
350+
}
351+
352+
struct mingw_stat {
353+
_dev_t st_dev;
354+
_ino_t st_ino;
355+
_mode_t st_mode;
356+
short st_nlink;
357+
short st_uid;
358+
short st_gid;
359+
_dev_t st_rdev;
360+
off64_t st_size;
361+
struct timespec st_atim;
362+
struct timespec st_mtim;
363+
struct timespec st_ctim;
364+
};
365+
366+
#define st_atime st_atim.tv_sec
367+
#define st_mtime st_mtim.tv_sec
368+
#define st_ctime st_ctim.tv_sec
369+
345370
#ifdef stat
346371
#undef stat
347372
#endif
348-
#define stat _stati64
373+
#define stat mingw_stat
349374
int mingw_lstat(const char *file_name, struct stat *buf);
350375
int mingw_stat(const char *file_name, struct stat *buf);
351376
int mingw_fstat(int fd, struct stat *buf);
@@ -358,13 +383,6 @@ int mingw_fstat(int fd, struct stat *buf);
358383
#endif
359384
extern int (*lstat)(const char *file_name, struct stat *buf);
360385

361-
#ifndef _stati64
362-
# define _stati64(x,y) mingw_stat(x,y)
363-
#elif defined (_USE_32BIT_TIME_T)
364-
# define _stat32i64(x,y) mingw_stat(x,y)
365-
#else
366-
# define _stat64(x,y) mingw_stat(x,y)
367-
#endif
368386

369387
int mingw_utime(const char *file_name, const struct utimbuf *times);
370388
#define utime mingw_utime

compat/win32/fscache.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ struct fsentry {
3838
struct {
3939
/* More stat members (only used for file entries). */
4040
off64_t st_size;
41-
time_t st_atime;
42-
time_t st_mtime;
43-
time_t st_ctime;
41+
struct timespec st_atim;
42+
struct timespec st_mtim;
43+
struct timespec st_ctim;
4444
};
4545
};
4646
};
@@ -151,9 +151,9 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
151151
fdata->dwReserved0);
152152
fse->st_size = S_ISLNK(fse->st_mode) ? MAX_LONG_PATH :
153153
fdata->nFileSizeLow | (((off_t) fdata->nFileSizeHigh) << 32);
154-
fse->st_atime = filetime_to_time_t(&(fdata->ftLastAccessTime));
155-
fse->st_mtime = filetime_to_time_t(&(fdata->ftLastWriteTime));
156-
fse->st_ctime = filetime_to_time_t(&(fdata->ftCreationTime));
154+
filetime_to_timespec(&(fdata->ftLastAccessTime), &(fse->st_atim));
155+
filetime_to_timespec(&(fdata->ftLastWriteTime), &(fse->st_mtim));
156+
filetime_to_timespec(&(fdata->ftCreationTime), &(fse->st_ctim));
157157

158158
return fse;
159159
}
@@ -432,9 +432,9 @@ int fscache_lstat(const char *filename, struct stat *st)
432432
st->st_nlink = 1;
433433
st->st_mode = fse->st_mode;
434434
st->st_size = fse->st_size;
435-
st->st_atime = fse->st_atime;
436-
st->st_mtime = fse->st_mtime;
437-
st->st_ctime = fse->st_ctime;
435+
st->st_atim = fse->st_atim;
436+
st->st_mtim = fse->st_mtim;
437+
st->st_ctim = fse->st_ctim;
438438

439439
/* don't forget to release fsentry */
440440
fsentry_release(fse);

config.mak.uname

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ ifeq ($(uname_S),Windows)
354354
NO_SVN_TESTS = YesPlease
355355
RUNTIME_PREFIX = YesPlease
356356
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
357-
NO_NSEC = YesPlease
358357
USE_WIN32_MMAP = YesPlease
359358
# USE_NED_ALLOCATOR = YesPlease
360359
UNRELIABLE_FSTAT = UnfortunatelyYes
@@ -504,7 +503,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
504503
NO_SVN_TESTS = YesPlease
505504
RUNTIME_PREFIX = YesPlease
506505
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
507-
NO_NSEC = YesPlease
508506
USE_WIN32_MMAP = YesPlease
509507
USE_NED_ALLOCATOR = YesPlease
510508
UNRELIABLE_FSTAT = UnfortunatelyYes

0 commit comments

Comments
 (0)