Skip to content

Commit 0990e7a

Browse files
committed
Merge branch 'kb/lstat-cache'
* kb/lstat-cache: lstat_cache(): introduce clear_lstat_cache() function lstat_cache(): introduce invalidate_lstat_cache() function lstat_cache(): introduce has_dirs_only_path() function lstat_cache(): introduce has_symlink_or_noent_leading_path() function lstat_cache(): more cache effective symlink/directory detection
2 parents 9847a52 + bda6eb0 commit 0990e7a

File tree

4 files changed

+238
-67
lines changed

4 files changed

+238
-67
lines changed

cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ struct checkout {
721721

722722
extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
723723
extern int has_symlink_leading_path(int len, const char *name);
724+
extern int has_symlink_or_noent_leading_path(int len, const char *name);
725+
extern int has_dirs_only_path(int len, const char *name, int prefix_len);
726+
extern void invalidate_lstat_cache(int len, const char *name);
727+
extern void clear_lstat_cache(void);
724728

725729
extern struct alternate_object_database {
726730
struct alternate_object_database *next;

entry.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,25 @@ static void create_directories(const char *path, const struct checkout *state)
99
const char *slash = path;
1010

1111
while ((slash = strchr(slash+1, '/')) != NULL) {
12-
struct stat st;
13-
int stat_status;
14-
1512
len = slash - path;
1613
memcpy(buf, path, len);
1714
buf[len] = 0;
1815

19-
if (len <= state->base_dir_len)
20-
/*
21-
* checkout-index --prefix=<dir>; <dir> is
22-
* allowed to be a symlink to an existing
23-
* directory.
24-
*/
25-
stat_status = stat(buf, &st);
26-
else
27-
/*
28-
* if there currently is a symlink, we would
29-
* want to replace it with a real directory.
30-
*/
31-
stat_status = lstat(buf, &st);
32-
33-
if (!stat_status && S_ISDIR(st.st_mode))
16+
/*
17+
* For 'checkout-index --prefix=<dir>', <dir> is
18+
* allowed to be a symlink to an existing directory,
19+
* and we set 'state->base_dir_len' below, such that
20+
* we test the path components of the prefix with the
21+
* stat() function instead of the lstat() function.
22+
*/
23+
if (has_dirs_only_path(len, buf, state->base_dir_len))
3424
continue; /* ok, it is already a directory. */
3525

3626
/*
37-
* We know stat_status == 0 means something exists
38-
* there and this mkdir would fail, but that is an
39-
* error codepath; we do not care, as we unlink and
40-
* mkdir again in such a case.
27+
* If this mkdir() would fail, it could be that there
28+
* is already a symlink or something else exists
29+
* there, therefore we then try to unlink it and try
30+
* one more time to create the directory.
4131
*/
4232
if (mkdir(buf, 0777)) {
4333
if (errno == EEXIST && state->force &&

symlinks.c

Lines changed: 220 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,241 @@
11
#include "cache.h"
22

3-
struct pathname {
3+
static struct cache_def {
4+
char path[PATH_MAX + 1];
45
int len;
5-
char path[PATH_MAX];
6-
};
6+
int flags;
7+
int track_flags;
8+
int prefix_len_stat_func;
9+
} cache;
710

8-
/* Return matching pathname prefix length, or zero if not matching */
9-
static inline int match_pathname(int len, const char *name, struct pathname *match)
11+
/*
12+
* Returns the length (on a path component basis) of the longest
13+
* common prefix match of 'name' and the cached path string.
14+
*/
15+
static inline int longest_match_lstat_cache(int len, const char *name,
16+
int *previous_slash)
1017
{
11-
int match_len = match->len;
12-
return (len > match_len &&
13-
name[match_len] == '/' &&
14-
!memcmp(name, match->path, match_len)) ? match_len : 0;
18+
int max_len, match_len = 0, match_len_prev = 0, i = 0;
19+
20+
max_len = len < cache.len ? len : cache.len;
21+
while (i < max_len && name[i] == cache.path[i]) {
22+
if (name[i] == '/') {
23+
match_len_prev = match_len;
24+
match_len = i;
25+
}
26+
i++;
27+
}
28+
/* Is the cached path string a substring of 'name'? */
29+
if (i == cache.len && cache.len < len && name[cache.len] == '/') {
30+
match_len_prev = match_len;
31+
match_len = cache.len;
32+
/* Is 'name' a substring of the cached path string? */
33+
} else if ((i == len && len < cache.len && cache.path[len] == '/') ||
34+
(i == len && len == cache.len)) {
35+
match_len_prev = match_len;
36+
match_len = len;
37+
}
38+
*previous_slash = match_len_prev;
39+
return match_len;
1540
}
1641

17-
static inline void set_pathname(int len, const char *name, struct pathname *match)
42+
static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func)
1843
{
19-
if (len < PATH_MAX) {
20-
match->len = len;
21-
memcpy(match->path, name, len);
22-
match->path[len] = 0;
23-
}
44+
cache.path[0] = '\0';
45+
cache.len = 0;
46+
cache.flags = 0;
47+
cache.track_flags = track_flags;
48+
cache.prefix_len_stat_func = prefix_len_stat_func;
2449
}
2550

26-
int has_symlink_leading_path(int len, const char *name)
51+
#define FL_DIR (1 << 0)
52+
#define FL_NOENT (1 << 1)
53+
#define FL_SYMLINK (1 << 2)
54+
#define FL_LSTATERR (1 << 3)
55+
#define FL_ERR (1 << 4)
56+
#define FL_FULLPATH (1 << 5)
57+
58+
/*
59+
* Check if name 'name' of length 'len' has a symlink leading
60+
* component, or if the directory exists and is real, or not.
61+
*
62+
* To speed up the check, some information is allowed to be cached.
63+
* This can be indicated by the 'track_flags' argument, which also can
64+
* be used to indicate that we should check the full path.
65+
*
66+
* The 'prefix_len_stat_func' parameter can be used to set the length
67+
* of the prefix, where the cache should use the stat() function
68+
* instead of the lstat() function to test each path component.
69+
*/
70+
static int lstat_cache(int len, const char *name,
71+
int track_flags, int prefix_len_stat_func)
2772
{
28-
static struct pathname link, nonlink;
29-
char path[PATH_MAX];
73+
int match_len, last_slash, last_slash_dir, previous_slash;
74+
int match_flags, ret_flags, save_flags, max_len, ret;
3075
struct stat st;
31-
char *sp;
32-
int known_dir;
3376

34-
/*
35-
* See if the last known symlink cache matches.
36-
*/
37-
if (match_pathname(len, name, &link))
38-
return 1;
77+
if (cache.track_flags != track_flags ||
78+
cache.prefix_len_stat_func != prefix_len_stat_func) {
79+
/*
80+
* As a safeguard we clear the cache if the values of
81+
* track_flags and/or prefix_len_stat_func does not
82+
* match with the last supplied values.
83+
*/
84+
reset_lstat_cache(track_flags, prefix_len_stat_func);
85+
match_len = last_slash = 0;
86+
} else {
87+
/*
88+
* Check to see if we have a match from the cache for
89+
* the 2 "excluding" path types.
90+
*/
91+
match_len = last_slash =
92+
longest_match_lstat_cache(len, name, &previous_slash);
93+
match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK);
94+
if (match_flags && match_len == cache.len)
95+
return match_flags;
96+
/*
97+
* If we now have match_len > 0, we would know that
98+
* the matched part will always be a directory.
99+
*
100+
* Also, if we are tracking directories and 'name' is
101+
* a substring of the cache on a path component basis,
102+
* we can return immediately.
103+
*/
104+
match_flags = track_flags & FL_DIR;
105+
if (match_flags && len == match_len)
106+
return match_flags;
107+
}
39108

40109
/*
41-
* Get rid of the last known directory part
110+
* Okay, no match from the cache so far, so now we have to
111+
* check the rest of the path components.
42112
*/
43-
known_dir = match_pathname(len, name, &nonlink);
44-
45-
while ((sp = strchr(name + known_dir + 1, '/')) != NULL) {
46-
int thislen = sp - name ;
47-
memcpy(path, name, thislen);
48-
path[thislen] = 0;
49-
50-
if (lstat(path, &st))
51-
return 0;
52-
if (S_ISDIR(st.st_mode)) {
53-
set_pathname(thislen, path, &nonlink);
54-
known_dir = thislen;
113+
ret_flags = FL_DIR;
114+
last_slash_dir = last_slash;
115+
max_len = len < PATH_MAX ? len : PATH_MAX;
116+
while (match_len < max_len) {
117+
do {
118+
cache.path[match_len] = name[match_len];
119+
match_len++;
120+
} while (match_len < max_len && name[match_len] != '/');
121+
if (match_len >= max_len && !(track_flags & FL_FULLPATH))
122+
break;
123+
last_slash = match_len;
124+
cache.path[last_slash] = '\0';
125+
126+
if (last_slash <= prefix_len_stat_func)
127+
ret = stat(cache.path, &st);
128+
else
129+
ret = lstat(cache.path, &st);
130+
131+
if (ret) {
132+
ret_flags = FL_LSTATERR;
133+
if (errno == ENOENT)
134+
ret_flags |= FL_NOENT;
135+
} else if (S_ISDIR(st.st_mode)) {
136+
last_slash_dir = last_slash;
55137
continue;
56-
}
57-
if (S_ISLNK(st.st_mode)) {
58-
set_pathname(thislen, path, &link);
59-
return 1;
138+
} else if (S_ISLNK(st.st_mode)) {
139+
ret_flags = FL_SYMLINK;
140+
} else {
141+
ret_flags = FL_ERR;
60142
}
61143
break;
62144
}
63-
return 0;
145+
146+
/*
147+
* At the end update the cache. Note that max 3 different
148+
* path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
149+
* for the moment!
150+
*/
151+
save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
152+
if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
153+
cache.path[last_slash] = '\0';
154+
cache.len = last_slash;
155+
cache.flags = save_flags;
156+
} else if (track_flags & FL_DIR &&
157+
last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
158+
/*
159+
* We have a separate test for the directory case,
160+
* since it could be that we have found a symlink or a
161+
* non-existing directory and the track_flags says
162+
* that we cannot cache this fact, so the cache would
163+
* then have been left empty in this case.
164+
*
165+
* But if we are allowed to track real directories, we
166+
* can still cache the path components before the last
167+
* one (the found symlink or non-existing component).
168+
*/
169+
cache.path[last_slash_dir] = '\0';
170+
cache.len = last_slash_dir;
171+
cache.flags = FL_DIR;
172+
} else {
173+
reset_lstat_cache(track_flags, prefix_len_stat_func);
174+
}
175+
return ret_flags;
176+
}
177+
178+
/*
179+
* Invalidate the given 'name' from the cache, if 'name' matches
180+
* completely with the cache.
181+
*/
182+
void invalidate_lstat_cache(int len, const char *name)
183+
{
184+
int match_len, previous_slash;
185+
186+
match_len = longest_match_lstat_cache(len, name, &previous_slash);
187+
if (len == match_len) {
188+
if ((cache.track_flags & FL_DIR) && previous_slash > 0) {
189+
cache.path[previous_slash] = '\0';
190+
cache.len = previous_slash;
191+
cache.flags = FL_DIR;
192+
} else
193+
reset_lstat_cache(cache.track_flags,
194+
cache.prefix_len_stat_func);
195+
}
196+
}
197+
198+
/*
199+
* Completely clear the contents of the cache
200+
*/
201+
void clear_lstat_cache(void)
202+
{
203+
reset_lstat_cache(0, 0);
204+
}
205+
206+
#define USE_ONLY_LSTAT 0
207+
208+
/*
209+
* Return non-zero if path 'name' has a leading symlink component
210+
*/
211+
int has_symlink_leading_path(int len, const char *name)
212+
{
213+
return lstat_cache(len, name,
214+
FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) &
215+
FL_SYMLINK;
216+
}
217+
218+
/*
219+
* Return non-zero if path 'name' has a leading symlink component or
220+
* if some leading path component does not exists.
221+
*/
222+
int has_symlink_or_noent_leading_path(int len, const char *name)
223+
{
224+
return lstat_cache(len, name,
225+
FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
226+
(FL_SYMLINK|FL_NOENT);
227+
}
228+
229+
/*
230+
* Return non-zero if all path components of 'name' exists as a
231+
* directory. If prefix_len > 0, we will test with the stat()
232+
* function instead of the lstat() function for a prefix length of
233+
* 'prefix_len', thus we then allow for symlinks in the prefix part as
234+
* long as those points to real existing directories.
235+
*/
236+
int has_dirs_only_path(int len, const char *name, int prefix_len)
237+
{
238+
return lstat_cache(len, name,
239+
FL_DIR|FL_FULLPATH, prefix_len) &
240+
FL_DIR;
64241
}

unpack-trees.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static void unlink_entry(struct cache_entry *ce)
6161
char *cp, *prev;
6262
char *name = ce->name;
6363

64-
if (has_symlink_leading_path(ce_namelen(ce), ce->name))
64+
if (has_symlink_or_noent_leading_path(ce_namelen(ce), ce->name))
6565
return;
6666
if (unlink(name))
6767
return;
@@ -580,7 +580,7 @@ static int verify_absent(struct cache_entry *ce, const char *action,
580580
if (o->index_only || o->reset || !o->update)
581581
return 0;
582582

583-
if (has_symlink_leading_path(ce_namelen(ce), ce->name))
583+
if (has_symlink_or_noent_leading_path(ce_namelen(ce), ce->name))
584584
return 0;
585585

586586
if (!lstat(ce->name, &st)) {

0 commit comments

Comments
 (0)