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 <gitster@pobox.com>
Junio C Hamano committed
Sep 14, 2023 at 14:46 UTC
c33fa871a5c89091cfc89fd7b6ef504d2d48bef2
4 files changed
+70
read-cache-ll.h
+8
@@ -436,6 +436,14 @@ int match_stat_data_racy(const struct index_state *istate,
436
437
void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
438
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
+
447
#define REFRESH_REALLY (1 << 0) /* ignore_valid */
448
#define REFRESH_UNMERGED (1 << 1) /* allow unmerged */
449
#define REFRESH_QUIET (1 << 2) /* be quiet about it */
read-cache.c
+27
@@ -197,6 +197,33 @@ void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, st
197
}
198
}
199
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
+
227
static int ce_compare_data(struct index_state *istate,
228
const struct cache_entry *ce,
229
struct stat *st)
statinfo.c
+27
@@ -15,6 +15,33 @@ void fill_stat_data(struct stat_data *sd, struct stat *st)
15
sd->sd_size = st->st_size;
16
}
17
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
+
45
int match_stat_data(const struct stat_data *sd, struct stat *st)
46
{
47
int changed = 0;
statinfo.h
+8
@@ -46,6 +46,14 @@ struct stat_validity {
46
*/
47
void fill_stat_data(struct stat_data *sd, struct stat *st);
48
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
+
57
/*
58
* Return 0 if st is consistent with a file not having been changed
59
* since sd was filled. If there are differences, return a