Skip to content

Commit 2d7370c

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 dec5a51 + a1e23fb commit 2d7370c

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
@@ -732,9 +732,9 @@ int mingw_lstat(const char *file_name, struct stat *buf)
732732
buf->st_size = S_ISLNK(buf->st_mode) ? MAX_LONG_PATH :
733733
fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
734734
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
735-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
736-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
737-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
735+
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
736+
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
737+
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
738738
return 0;
739739
}
740740
error:
@@ -779,9 +779,9 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
779779
buf->st_nlink = 1;
780780
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes, 0);
781781
buf->st_size = fdata.nFileSizeLow | (((off_t) fdata.nFileSizeHigh) << 32);
782-
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
783-
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
784-
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
782+
filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
783+
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
784+
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
785785
return 0;
786786
}
787787

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

826839
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
@@ -339,24 +339,49 @@ static inline long long filetime_to_hnsec(const FILETIME *ft)
339339
return winTime - 116444736000000000LL;
340340
}
341341

342-
static inline time_t filetime_to_time_t(const FILETIME *ft)
343-
{
344-
return (time_t)(filetime_to_hnsec(ft) / 10000000);
345-
}
346-
347342
/*
348-
* Use mingw specific stat()/lstat()/fstat() implementations on Windows.
343+
* Use mingw specific stat()/lstat()/fstat() implementations on Windows,
344+
* including our own struct stat with 64 bit st_size and nanosecond-precision
345+
* file times.
349346
*/
350347
#ifndef __MINGW64_VERSION_MAJOR
351348
#define off_t off64_t
352349
#define lseek _lseeki64
350+
struct timespec {
351+
time_t tv_sec;
352+
long tv_nsec;
353+
};
353354
#endif
354355

355-
/* use struct stat with 64 bit st_size */
356+
static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts)
357+
{
358+
long long hnsec = filetime_to_hnsec(ft);
359+
ts->tv_sec = (time_t)(hnsec / 10000000);
360+
ts->tv_nsec = (hnsec % 10000000) * 100;
361+
}
362+
363+
struct mingw_stat {
364+
_dev_t st_dev;
365+
_ino_t st_ino;
366+
_mode_t st_mode;
367+
short st_nlink;
368+
short st_uid;
369+
short st_gid;
370+
_dev_t st_rdev;
371+
off64_t st_size;
372+
struct timespec st_atim;
373+
struct timespec st_mtim;
374+
struct timespec st_ctim;
375+
};
376+
377+
#define st_atime st_atim.tv_sec
378+
#define st_mtime st_mtim.tv_sec
379+
#define st_ctime st_ctim.tv_sec
380+
356381
#ifdef stat
357382
#undef stat
358383
#endif
359-
#define stat _stati64
384+
#define stat mingw_stat
360385
int mingw_lstat(const char *file_name, struct stat *buf);
361386
int mingw_stat(const char *file_name, struct stat *buf);
362387
int mingw_fstat(int fd, struct stat *buf);
@@ -369,13 +394,6 @@ int mingw_fstat(int fd, struct stat *buf);
369394
#endif
370395
extern int (*lstat)(const char *file_name, struct stat *buf);
371396

372-
#ifndef _stati64
373-
# define _stati64(x,y) mingw_stat(x,y)
374-
#elif defined (_USE_32BIT_TIME_T)
375-
# define _stat32i64(x,y) mingw_stat(x,y)
376-
#else
377-
# define _stat64(x,y) mingw_stat(x,y)
378-
#endif
379397

380398
int mingw_utime(const char *file_name, const struct utimbuf *times);
381399
#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
@@ -360,7 +360,6 @@ ifeq ($(uname_S),Windows)
360360
NO_SVN_TESTS = YesPlease
361361
RUNTIME_PREFIX = YesPlease
362362
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
363-
NO_NSEC = YesPlease
364363
USE_WIN32_MMAP = YesPlease
365364
# USE_NED_ALLOCATOR = YesPlease
366365
UNRELIABLE_FSTAT = UnfortunatelyYes
@@ -510,7 +509,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
510509
NO_SVN_TESTS = YesPlease
511510
RUNTIME_PREFIX = YesPlease
512511
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
513-
NO_NSEC = YesPlease
514512
USE_WIN32_MMAP = YesPlease
515513
USE_NED_ALLOCATOR = YesPlease
516514
UNRELIABLE_FSTAT = UnfortunatelyYes

0 commit comments

Comments
 (0)