]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'jc/fake-lstat'
authorJunio C Hamano <gitster@pobox.com>
Wed, 27 Dec 2023 22:52:24 +0000 (14:52 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Dec 2023 22:52:24 +0000 (14:52 -0800)
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()

read-cache-ll.h
read-cache.c
statinfo.c
statinfo.h

index 9a1a7edc5a22cf0b0d91f7edd03225ea19607156..2a50a784f0ec6ae4da94fed41345e625c5bbce1c 100644 (file)
@@ -436,6 +436,14 @@ int match_stat_data_racy(const struct index_state *istate,
 
 void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
 
+/*
+ * Fill members of st by members of sd enough to convince match_stat()
+ * to consider that they match.  It should be usable as a replacement
+ * for lstat() for a tracked path that is known to be up-to-date via
+ * some out-of-line means (like fsmonitor).
+ */
+int fake_lstat(const struct cache_entry *ce, struct stat *st);
+
 #define REFRESH_REALLY                   (1 << 0) /* ignore_valid */
 #define REFRESH_UNMERGED                 (1 << 1) /* allow unmerged */
 #define REFRESH_QUIET                    (1 << 2) /* be quiet about it */
index 080bd39713bc1782434b7212954e6bc590fa4d6d..eb750e2e49a370e727ab22548d2bbe2536a0a40f 100644 (file)
@@ -197,6 +197,33 @@ void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, st
        }
 }
 
+static unsigned int st_mode_from_ce(const struct cache_entry *ce)
+{
+       extern int trust_executable_bit, has_symlinks;
+
+       switch (ce->ce_mode & S_IFMT) {
+       case S_IFLNK:
+               return has_symlinks ? S_IFLNK : (S_IFREG | 0644);
+       case S_IFREG:
+               return (ce->ce_mode & (trust_executable_bit ? 0755 : 0644)) | S_IFREG;
+       case S_IFGITLINK:
+               return S_IFDIR | 0755;
+       case S_IFDIR:
+               return ce->ce_mode;
+       default:
+               BUG("unsupported ce_mode: %o", ce->ce_mode);
+       }
+}
+
+int fake_lstat(const struct cache_entry *ce, struct stat *st)
+{
+       fake_lstat_data(&ce->ce_stat_data, st);
+       st->st_mode = st_mode_from_ce(ce);
+
+       /* always succeed as lstat() replacement */
+       return 0;
+}
+
 static int ce_compare_data(struct index_state *istate,
                           const struct cache_entry *ce,
                           struct stat *st)
index 9367ca099cd114d56d7b2103b0e56fa4933f5781..3c6bc049c15d44c33c064fecbbd2236571b2dadb 100644 (file)
@@ -31,6 +31,33 @@ void fill_stat_data(struct stat_data *sd, struct stat *st)
        sd->sd_size = munge_st_size(st->st_size);
 }
 
+static void set_times(struct stat *st, const struct stat_data *sd)
+{
+       st->st_ctime = sd->sd_ctime.sec;
+       st->st_mtime = sd->sd_mtime.sec;
+#ifdef NO_NSEC
+       ; /* nothing */
+#else
+#ifdef USE_ST_TIMESPEC
+       st->st_ctimespec.tv_nsec = sd->sd_ctime.nsec;
+       st->st_mtimespec.tv_nsec = sd->sd_mtime.nsec;
+#else
+       st->st_ctim.tv_nsec = sd->sd_ctime.nsec;
+       st->st_mtim.tv_nsec = sd->sd_mtime.nsec;
+#endif
+#endif
+}
+
+void fake_lstat_data(const struct stat_data *sd, struct stat *st)
+{
+       set_times(st, sd);
+       st->st_dev = sd->sd_dev;
+       st->st_ino = sd->sd_ino;
+       st->st_uid = sd->sd_uid;
+       st->st_gid = sd->sd_gid;
+       st->st_size = sd->sd_size;
+}
+
 int match_stat_data(const struct stat_data *sd, struct stat *st)
 {
        int changed = 0;
index 700f502ac0b3cb938c2f376653dc1e91ac4e6d24..5b21a30f90bfd2e1fed39fed0d61b9abb360af33 100644 (file)
@@ -46,6 +46,14 @@ struct stat_validity {
  */
 void fill_stat_data(struct stat_data *sd, struct stat *st);
 
+/*
+ * The inverse of the above.  When we know the cache_entry that
+ * contains sd is up-to-date, but still need to pretend we called
+ * lstat() to learn that fact, this function fills "st" enough to
+ * fool ie_match_stat().
+ */
+void fake_lstat_data(const struct stat_data *sd, struct stat *st);
+
 /*
  * Return 0 if st is consistent with a file not having been changed
  * since sd was filled.  If there are differences, return a