add a stat_validity struct
It can sometimes be useful to know whether a path in the filesystem has been updated without going to the work of opening and re-reading its content. We trust the stat() information on disk already to handle index updates, and we can use the same trick here. This patch introduces a "stat_validity" struct which encapsulates the concept of checking the stat-freshness of a file. It is implemented on top of "struct stat_data" to reuse the logic about which stat entries to trust for a particular platform, but hides the complexity behind two simple functions: check and update. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
c21d39d7c7
commit
3861253224
30
read-cache.c
30
read-cache.c
@ -1950,3 +1950,33 @@ void *read_blob_data_from_index(struct index_state *istate, const char *path, un
|
||||
*size = sz;
|
||||
return data;
|
||||
}
|
||||
|
||||
void stat_validity_clear(struct stat_validity *sv)
|
||||
{
|
||||
free(sv->sd);
|
||||
sv->sd = NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void stat_validity_update(struct stat_validity *sv, int fd)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
|
||||
stat_validity_clear(sv);
|
||||
else {
|
||||
if (!sv->sd)
|
||||
sv->sd = xcalloc(1, sizeof(struct stat_data));
|
||||
fill_stat_data(sv->sd, &st);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user