Skip to content

Commit 01f86eb

Browse files
committed
Merge branch 'jc/fake-lstat'
A new helper to let us pretend that we called lstat() when we know our cache_entry is up-to-date via fsmonitor. * jc/fake-lstat: cache: add fake_lstat()
2 parents db2cf6f + c33fa87 commit 01f86eb

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
@@ -31,6 +31,33 @@ void fill_stat_data(struct stat_data *sd, struct stat *st)
3131
sd->sd_size = munge_st_size(st->st_size);
3232
}
3333

34+
static void set_times(struct stat *st, const struct stat_data *sd)
35+
{
36+
st->st_ctime = sd->sd_ctime.sec;
37+
st->st_mtime = sd->sd_mtime.sec;
38+
#ifdef NO_NSEC
39+
; /* nothing */
40+
#else
41+
#ifdef USE_ST_TIMESPEC
42+
st->st_ctimespec.tv_nsec = sd->sd_ctime.nsec;
43+
st->st_mtimespec.tv_nsec = sd->sd_mtime.nsec;
44+
#else
45+
st->st_ctim.tv_nsec = sd->sd_ctime.nsec;
46+
st->st_mtim.tv_nsec = sd->sd_mtime.nsec;
47+
#endif
48+
#endif
49+
}
50+
51+
void fake_lstat_data(const struct stat_data *sd, struct stat *st)
52+
{
53+
set_times(st, sd);
54+
st->st_dev = sd->sd_dev;
55+
st->st_ino = sd->sd_ino;
56+
st->st_uid = sd->sd_uid;
57+
st->st_gid = sd->sd_gid;
58+
st->st_size = sd->sd_size;
59+
}
60+
3461
int match_stat_data(const struct stat_data *sd, struct stat *st)
3562
{
3663
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)