Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f4e7c8

Browse files
committed
Merge pull request libgit2#3638 from ethomson/nsec
USE_NSECS fixes
2 parents 0d9a749 + 3d6a42d commit 9f4e7c8

File tree

6 files changed

+51
-20
lines changed

6 files changed

+51
-20
lines changed

CMakeLists.txt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,21 @@ IF (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
8686
OPTION( USE_OPENSSL "Link with and use openssl library" ON )
8787
ENDIF()
8888

89-
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_atim "sys/types.h;sys/stat.h"
90-
HAVE_STRUCT_STAT_ST_ATIM LANGUAGE C)
91-
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_atimespec "sys/types.h;sys/stat.h"
92-
HAVE_STRUCT_STAT_ST_ATIMESPEC LANGUAGE C)
93-
94-
IF (HAVE_STRUCT_STAT_ST_ATIM)
89+
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtim "sys/types.h;sys/stat.h"
90+
HAVE_STRUCT_STAT_ST_MTIM LANGUAGE C)
91+
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtimespec "sys/types.h;sys/stat.h"
92+
HAVE_STRUCT_STAT_ST_MTIMESPEC LANGUAGE C)
93+
CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtime_nsec sys/stat.h
94+
HAVE_STRUCT_STAT_MTIME_NSEC LANGUAGE C)
95+
96+
IF (HAVE_STRUCT_STAT_ST_MTIM)
9597
CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec sys/stat.h
9698
HAVE_STRUCT_STAT_NSEC LANGUAGE C)
97-
ELSEIF (HAVE_STRUCT_STAT_ST_ATIMESPEC)
99+
ELSEIF (HAVE_STRUCT_STAT_ST_MTIMESPEC)
98100
CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec sys/stat.h
99101
HAVE_STRUCT_STAT_NSEC LANGUAGE C)
102+
ELSE ()
103+
SET( HAVE_STRUCT_STAT_NSEC ON )
100104
ENDIF()
101105

102106
IF (HAVE_STRUCT_STAT_NSEC OR WIN32)
@@ -539,8 +543,12 @@ IF (USE_NSEC)
539543
ADD_DEFINITIONS(-DGIT_USE_NSEC)
540544
ENDIF()
541545

542-
IF (HAVE_STRUCT_STAT_ST_ATIMESPEC)
543-
ADD_DEFINITIONS(-DGIT_USE_STAT_ATIMESPEC)
546+
IF (HAVE_STRUCT_STAT_ST_MTIM)
547+
ADD_DEFINITIONS(-DGIT_USE_STAT_MTIM)
548+
ELSEIF (HAVE_STRUCT_STAT_ST_MTIMESPEC)
549+
ADD_DEFINITIONS(-DGIT_USE_STAT_MTIMESPEC)
550+
ELSEIF (HAVE_STRUCT_STAT_ST_MTIME_NSEC)
551+
ADD_DEFINITIONS(-DGIT_USE_STAT_MTIME_NSEC)
544552
ENDIF()
545553

546554
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)

src/fileops.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,6 @@ int git_futils_filestamp_check(
10341034
git_futils_filestamp *stamp, const char *path)
10351035
{
10361036
struct stat st;
1037-
const struct timespec *statmtime = &st.st_mtim;
10381037

10391038
/* if the stamp is NULL, then always reload */
10401039
if (stamp == NULL)
@@ -1043,17 +1042,17 @@ int git_futils_filestamp_check(
10431042
if (p_stat(path, &st) < 0)
10441043
return GIT_ENOTFOUND;
10451044

1046-
if (stamp->mtime.tv_sec == statmtime->tv_sec &&
1045+
if (stamp->mtime.tv_sec == st.st_mtime &&
10471046
#if defined(GIT_USE_NSEC)
1048-
stamp->mtime.tv_nsec == statmtime->tv_nsec &&
1047+
stamp->mtime.tv_nsec == st.st_mtime_nsec &&
10491048
#endif
10501049
stamp->size == (git_off_t)st.st_size &&
10511050
stamp->ino == (unsigned int)st.st_ino)
10521051
return 0;
10531052

1054-
stamp->mtime.tv_sec = statmtime->tv_sec;
1053+
stamp->mtime.tv_sec = st.st_mtime;
10551054
#if defined(GIT_USE_NSEC)
1056-
stamp->mtime.tv_nsec = statmtime->tv_nsec;
1055+
stamp->mtime.tv_nsec = st.st_mtime_nsec;
10571056
#endif
10581057
stamp->size = (git_off_t)st.st_size;
10591058
stamp->ino = (unsigned int)st.st_ino;
@@ -1076,11 +1075,11 @@ void git_futils_filestamp_set(
10761075
void git_futils_filestamp_set_from_stat(
10771076
git_futils_filestamp *stamp, struct stat *st)
10781077
{
1079-
const struct timespec *statmtime = &st->st_mtim;
1080-
10811078
if (st) {
1082-
stamp->mtime = *statmtime;
1083-
#if !defined(GIT_USE_NSEC)
1079+
stamp->mtime.tv_sec = st->st_mtime;
1080+
#if defined(GIT_USE_NSEC)
1081+
stamp->mtime.tv_nsec = st->st_mtime_nsec;
1082+
#else
10841083
stamp->mtime.tv_nsec = 0;
10851084
#endif
10861085
stamp->size = (git_off_t)st->st_size;

src/index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,8 @@ void git_index_entry__init_from_stat(
829829
entry->ctime.seconds = (int32_t)st->st_ctime;
830830
entry->mtime.seconds = (int32_t)st->st_mtime;
831831
#if defined(GIT_USE_NSEC)
832-
entry->mtime.nanoseconds = st->st_mtim.tv_nsec;
833-
entry->ctime.nanoseconds = st->st_ctim.tv_nsec;
832+
entry->mtime.nanoseconds = st->st_mtime_nsec;
833+
entry->ctime.nanoseconds = st->st_ctime_nsec;
834834
#endif
835835
entry->dev = st->st_rdev;
836836
entry->ino = st->st_ino;

src/unix/posix.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ typedef int GIT_SOCKET;
2121
#define p_lstat(p,b) lstat(p,b)
2222
#define p_stat(p,b) stat(p, b)
2323

24+
#if defined(GIT_USE_STAT_MTIMESPEC)
25+
# define st_atime_nsec st_atimespec.tv_nsec
26+
# define st_mtime_nsec st_mtimespec.tv_nsec
27+
# define st_ctime_nsec st_ctimespec.tv_nsec
28+
#elif defined(GIT_USE_STAT_MTIM)
29+
# define st_atime_nsec st_atim.tv_nsec
30+
# define st_mtime_nsec st_mtim.tv_nsec
31+
# define st_ctime_nsec st_ctim.tv_nsec
32+
#elif !defined(GIT_USE_STAT_MTIME_NSEC) && defined(GIT_USE_NEC)
33+
# error GIT_USE_NSEC defined but unknown struct stat nanosecond type
34+
#endif
35+
2436
#define p_utimes(f, t) utimes(f, t)
2537

2638
#define p_readlink(a, b, c) readlink(a, b, c)

src/win32/win32-compat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ struct p_stat {
4242
#define st_atime st_atim.tv_sec
4343
#define st_mtime st_mtim.tv_sec
4444
#define st_ctime st_ctim.tv_sec
45+
#define st_atime_nsec st_atim.tv_nsec
46+
#define st_mtime_nsec st_mtim.tv_nsec
47+
#define st_ctime_nsec st_ctim.tv_nsec
4548
};
4649

4750
#define stat p_stat

tests/index/nsec.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,17 @@ void test_index_nsec__staging_maintains_other_nanos(void)
6161
cl_assert_equal_b(true, has_nsecs());
6262

6363
cl_assert((entry = git_index_get_bypath(repo_index, "a.txt", 0)));
64+
65+
/* if we are writing nanoseconds to the index, expect them to be
66+
* nonzero. if we are *not*, expect that we truncated the entry.
67+
*/
68+
#ifdef GIT_USE_NSEC
69+
cl_assert(entry->ctime.nanoseconds != 0);
70+
cl_assert(entry->mtime.nanoseconds != 0);
71+
#else
6472
cl_assert_equal_i(0, entry->ctime.nanoseconds);
6573
cl_assert_equal_i(0, entry->mtime.nanoseconds);
74+
#endif
6675
}
6776

6877
void test_index_nsec__status_doesnt_clear_nsecs(void)

0 commit comments

Comments
 (0)