Skip to content

Commit 7843a0b

Browse files
committed
Merge pull request #443 from kblees/kb/nanosecond-file-times-v2.5.3
nanosecond file times for v2.5.3
2 parents 5a3bec5 + a61683e commit 7843a0b

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

compat/mingw.c

Lines changed: 28 additions & 15 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

@@ -792,18 +792,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
792792
int mingw_fstat(int fd, struct stat *buf)
793793
{
794794
HANDLE fh = (HANDLE)_get_osfhandle(fd);
795-
if (fh == INVALID_HANDLE_VALUE) {
795+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
796+
797+
switch (type) {
798+
case FILE_TYPE_DISK:
799+
return get_file_info_by_handle(fh, buf);
800+
801+
case FILE_TYPE_CHAR:
802+
case FILE_TYPE_PIPE:
803+
/* initialize stat fields */
804+
memset(buf, 0, sizeof(*buf));
805+
buf->st_nlink = 1;
806+
807+
if (type == FILE_TYPE_CHAR) {
808+
buf->st_mode = _S_IFCHR;
809+
} else {
810+
buf->st_mode = _S_IFIFO;
811+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
812+
buf->st_size = avail;
813+
}
814+
return 0;
815+
816+
default:
796817
errno = EBADF;
797818
return -1;
798819
}
799-
/* direct non-file handles to MS's fstat() */
800-
if (GetFileType(fh) != FILE_TYPE_DISK)
801-
return _fstati64(fd, buf);
802-
803-
if (!get_file_info_by_handle(fh, buf))
804-
return 0;
805-
errno = EBADF;
806-
return -1;
807820
}
808821

809822
static inline void time_t_to_filetime(time_t t, FILETIME *ft)

compat/mingw.h

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

334-
static inline time_t filetime_to_time_t(const FILETIME *ft)
335-
{
336-
return (time_t)(filetime_to_hnsec(ft) / 10000000);
337-
}
338-
339334
/*
340-
* Use mingw specific stat()/lstat()/fstat() implementations on Windows.
335+
* Use mingw specific stat()/lstat()/fstat() implementations on Windows,
336+
* including our own struct stat with 64 bit st_size and nanosecond-precision
337+
* file times.
341338
*/
342339
#ifndef __MINGW64_VERSION_MAJOR
343340
#define off_t off64_t
344341
#define lseek _lseeki64
342+
struct timespec {
343+
time_t tv_sec;
344+
long tv_nsec;
345+
};
345346
#endif
346347

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

364-
#ifndef _stati64
365-
# define _stati64(x,y) mingw_stat(x,y)
366-
#elif defined (_USE_32BIT_TIME_T)
367-
# define _stat32i64(x,y) mingw_stat(x,y)
368-
#else
369-
# define _stat64(x,y) mingw_stat(x,y)
370-
#endif
371389

372390
int mingw_utime(const char *file_name, const struct utimbuf *times);
373391
#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)