The stat_validity code was originally written to avoid
re-reading the packed-refs file when it has not changed. It
makes sure that the file continues to match S_ISREG() when
we check it.

However, we can use the same concept on a directory to see
whether it has been modified. Even though we still have to
touch the filesystem to do the stat, this can provide a
speedup even over opendir/readdir/closedir, especially on
high-latency filesystems like NFS.

This patch adds a "mode" field to stat_validity, which lets
us check that the mode has stayed the same (rather than
explicitly checking that it is a regular file).

As a bonus cleanup, we can stop allocating the embedded
"stat_data" on the heap. Prior to this patch, we needed to
represent the case where the file did not exist, and we used
"sv->sd == NULL" for that. Now we can simply check that
"sv->mode" is 0.

Signed-off-by: Jeff King <p...@peff.net>
---
 cache.h      |  3 ++-
 read-cache.c | 16 ++++++----------
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/cache.h b/cache.h
index c97d807..2941e7e 100644
--- a/cache.h
+++ b/cache.h
@@ -1660,7 +1660,8 @@ int sane_execvp(const char *file, char *const argv[]);
  * for the index.
  */
 struct stat_validity {
-       struct stat_data *sd;
+       struct stat_data sd;
+       unsigned mode;
 };
 
 void stat_validity_clear(struct stat_validity *sv);
diff --git a/read-cache.c b/read-cache.c
index 36ff89f..115b000 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2279,8 +2279,7 @@ void *read_blob_data_from_index(struct index_state 
*istate, const char *path, un
 
 void stat_validity_clear(struct stat_validity *sv)
 {
-       free(sv->sd);
-       sv->sd = NULL;
+       memset(sv, 0, sizeof(*sv));
 }
 
 int stat_validity_check(struct stat_validity *sv, const char *path)
@@ -2288,21 +2287,18 @@ int stat_validity_check(struct stat_validity *sv, const 
char *path)
        struct stat st;
 
        if (stat(path, &st) < 0)
-               return sv->sd == NULL;
-       if (!sv->sd)
-               return 0;
-       return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);
+               return sv->mode == 0;
+       return sv->mode == st.st_mode && !match_stat_data(&sv->sd, &st);
 }
 
 void stat_validity_update(struct stat_validity *sv, int fd)
 {
        struct stat st;
 
-       if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
+       if (fstat(fd, &st) < 0)
                stat_validity_clear(sv);
        else {
-               if (!sv->sd)
-                       sv->sd = xcalloc(1, sizeof(struct stat_data));
-               fill_stat_data(sv->sd, &st);
+               sv->mode = st.st_mode;
+               fill_stat_data(&sv->sd, &st);
        }
 }
-- 
2.4.1.538.g69ac333

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to