Skip to content

Commit d88b8f4

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 63a627f + cc5b1b5 commit d88b8f4

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
@@ -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)