Skip to content

Commit c33fa87

Browse files
committed
cache: add fake_lstat()
At times, we may already know that a path represented by a cache_entry ce has no changes via some out-of-line means, like fsmonitor, and yet need the control to go through a codepath that requires us to have "struct stat" obtained by lstat() on the path, for various purposes (e.g. "ie_match_stat()" wants cached stat-info is still current wrt "struct stat", "diff" wants to know st_mode). The callers of lstat() on a tracked file, when its cache_entry knows it is up-to-date, can instead call this helper to pretend that it called lstat() by faking the "struct stat" information. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43c8a30 commit c33fa87

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

read-cache-ll.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,14 @@ int match_stat_data_racy(const struct index_state *istate,
436436

437437
void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
438438

439+
/*
440+
* Fill members of st by members of sd enough to convince match_stat()
441+
* to consider that they match. It should be usable as a replacement
442+
* for lstat() for a tracked path that is known to be up-to-date via
443+
* some out-of-line means (like fsmonitor).
444+
*/
445+
int fake_lstat(const struct cache_entry *ce, struct stat *st);
446+
439447
#define REFRESH_REALLY (1 << 0) /* ignore_valid */
440448
#define REFRESH_UNMERGED (1 << 1) /* allow unmerged */
441449
#define REFRESH_QUIET (1 << 2) /* be quiet about it */

read-cache.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,33 @@ void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, st
197197
}
198198
}
199199

200+
static unsigned int st_mode_from_ce(const struct cache_entry *ce)
201+
{
202+
extern int trust_executable_bit, has_symlinks;
203+
204+
switch (ce->ce_mode & S_IFMT) {
205+
case S_IFLNK:
206+
return has_symlinks ? S_IFLNK : (S_IFREG | 0644);
207+
case S_IFREG:
208+
return (ce->ce_mode & (trust_executable_bit ? 0755 : 0644)) | S_IFREG;
209+
case S_IFGITLINK:
210+
return S_IFDIR | 0755;
211+
case S_IFDIR:
212+
return ce->ce_mode;
213+
default:
214+
BUG("unsupported ce_mode: %o", ce->ce_mode);
215+
}
216+
}
217+
218+
int fake_lstat(const struct cache_entry *ce, struct stat *st)
219+
{
220+
fake_lstat_data(&ce->ce_stat_data, st);
221+
st->st_mode = st_mode_from_ce(ce);
222+
223+
/* always succeed as lstat() replacement */
224+
return 0;
225+
}
226+
200227
static int ce_compare_data(struct index_state *istate,
201228
const struct cache_entry *ce,
202229
struct stat *st)

statinfo.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ void fill_stat_data(struct stat_data *sd, struct stat *st)
1515
sd->sd_size = st->st_size;
1616
}
1717

18+
static void set_times(struct stat *st, const struct stat_data *sd)
19+
{
20+
st->st_ctime = sd->sd_ctime.sec;
21+
st->st_mtime = sd->sd_mtime.sec;
22+
#ifdef NO_NSEC
23+
; /* nothing */
24+
#else
25+
#ifdef USE_ST_TIMESPEC
26+
st->st_ctimespec.tv_nsec = sd->sd_ctime.nsec;
27+
st->st_mtimespec.tv_nsec = sd->sd_mtime.nsec;
28+
#else
29+
st->st_ctim.tv_nsec = sd->sd_ctime.nsec;
30+
st->st_mtim.tv_nsec = sd->sd_mtime.nsec;
31+
#endif
32+
#endif
33+
}
34+
35+
void fake_lstat_data(const struct stat_data *sd, struct stat *st)
36+
{
37+
set_times(st, sd);
38+
st->st_dev = sd->sd_dev;
39+
st->st_ino = sd->sd_ino;
40+
st->st_uid = sd->sd_uid;
41+
st->st_gid = sd->sd_gid;
42+
st->st_size = sd->sd_size;
43+
}
44+
1845
int match_stat_data(const struct stat_data *sd, struct stat *st)
1946
{
2047
int changed = 0;

statinfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ struct stat_validity {
4646
*/
4747
void fill_stat_data(struct stat_data *sd, struct stat *st);
4848

49+
/*
50+
* The inverse of the above. When we know the cache_entry that
51+
* contains sd is up-to-date, but still need to pretend we called
52+
* lstat() to learn that fact, this function fills "st" enough to
53+
* fool ie_match_stat().
54+
*/
55+
void fake_lstat_data(const struct stat_data *sd, struct stat *st);
56+
4957
/*
5058
* Return 0 if st is consistent with a file not having been changed
5159
* since sd was filled. If there are differences, return a

0 commit comments

Comments
 (0)