Skip to content

Commit f3a39bb

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 f38b953 + 9d1e90e commit f3a39bb

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
@@ -734,9 +734,9 @@ int mingw_lstat(const char *file_name, struct stat *buf)
734734
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
735735
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
736736
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
737-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
738-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
739-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
737+
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
738+
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
739+
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
740740
return 0;
741741
}
742742
error:
@@ -781,9 +781,9 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
781781
buf->st_nlink = 1;
782782
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
783783
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
784-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
785-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
786-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
784+
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
785+
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
786+
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
787787
return 0;
788788
}
789789

@@ -811,18 +811,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
811811
int mingw_fstat(int fd, struct stat *buf)
812812
{
813813
HANDLE fh = (HANDLE)_get_osfhandle(fd);
814-
if (fh == INVALID_HANDLE_VALUE) {
814+
DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
815+
816+
switch (type) {
817+
case FILE_TYPE_DISK:
818+
return get_file_info_by_handle(fh, buf);
819+
820+
case FILE_TYPE_CHAR:
821+
case FILE_TYPE_PIPE:
822+
/* initialize stat fields */
823+
memset(buf, 0, sizeof(*buf));
824+
buf->st_nlink = 1;
825+
826+
if (type == FILE_TYPE_CHAR) {
827+
buf->st_mode = _S_IFCHR;
828+
} else {
829+
buf->st_mode = _S_IFIFO;
830+
if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
831+
buf->st_size = avail;
832+
}
833+
return 0;
834+
835+
default:
815836
errno = EBADF;
816837
return -1;
817838
}
818-
/* direct non-file handles to MS's fstat() */
819-
if (GetFileType(fh) != FILE_TYPE_DISK)
820-
return _fstati64(fd, buf);
821-
822-
if (!get_file_info_by_handle(fh, buf))
823-
return 0;
824-
errno = EBADF;
825-
return -1;
826839
}
827840

828841
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
@@ -332,24 +332,49 @@ static inline long long filetime_to_hnsec(const FILETIME *ft)
332332
return winTime - 116444736000000000LL;
333333
}
334334

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

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

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

373391
int mingw_utime(const char *file_name, const struct utimbuf *times);
374392
#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
@@ -356,7 +356,6 @@ ifeq ($(uname_S),Windows)
356356
NO_SVN_TESTS = YesPlease
357357
RUNTIME_PREFIX = YesPlease
358358
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
359-
NO_NSEC = YesPlease
360359
USE_WIN32_MMAP = YesPlease
361360
# USE_NED_ALLOCATOR = YesPlease
362361
UNRELIABLE_FSTAT = UnfortunatelyYes
@@ -506,7 +505,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
506505
NO_SVN_TESTS = YesPlease
507506
RUNTIME_PREFIX = YesPlease
508507
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
509-
NO_NSEC = YesPlease
510508
USE_WIN32_MMAP = YesPlease
511509
USE_NED_ALLOCATOR = YesPlease
512510
UNRELIABLE_FSTAT = UnfortunatelyYes

0 commit comments

Comments
 (0)