@matheusbsbs / matheus-bagliano / commits / 148bc06b91
Fork 0

lstat_cache(): generalise longest_match_lstat_cache()

Rename the function to longst_path_match() and generalise it such that it can also be used by other functions. Signed-off-by: Kjetil Barvik <barvik@broadpark.no> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kjetil Barvik committed Feb 9, 2009 at 21:54 UTC 148bc06b9101042342a89454a003998fc0e1ded3
1 file changed +24 -22
symlinks.c
+24 -22
@@ -1,38 +1,30 @@
1 #include "cache.h"
2
3 -static struct cache_def {
4 - char path[PATH_MAX + 1];
5 - int len;
6 - int flags;
7 - int track_flags;
8 - int prefix_len_stat_func;
9 -} cache;
10 -
3 /*
4 * Returns the length (on a path component basis) of the longest
13 - * common prefix match of 'name' and the cached path string.
5 + * common prefix match of 'name_a' and 'name_b'.
6 */
15 -static inline int longest_match_lstat_cache(int len, const char *name,
16 - int *previous_slash)
7 +static int longest_path_match(const char *name_a, int len_a,
8 + const char *name_b, int len_b,
9 + int *previous_slash)
10 {
11 int max_len, match_len = 0, match_len_prev = 0, i = 0;
12
20 - max_len = len < cache.len ? len : cache.len;
21 - while (i < max_len && name[i] == cache.path[i]) {
22 - if (name[i] == '/') {
13 + max_len = len_a < len_b ? len_a : len_b;
14 + while (i < max_len && name_a[i] == name_b[i]) {
15 + if (name_a[i] == '/') {
16 match_len_prev = match_len;
17 match_len = i;
18 }
19 i++;
20 }
21 /*
29 - * Is the cached path string a substring of 'name', is 'name'
30 - * a substring of the cached path string, or is 'name' and the
31 - * cached path string the exact same string?
22 + * Is 'name_b' a substring of 'name_a', the other way around,
23 + * or is 'name_a' and 'name_b' the exact same string?
24 */
33 - if (i >= max_len && ((len > cache.len && name[cache.len] == '/') ||
34 - (len < cache.len && cache.path[len] == '/') ||
35 - (len == cache.len))) {
25 + if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||
26 + (len_a < len_b && name_b[len_a] == '/') ||
27 + (len_a == len_b))) {
28 match_len_prev = match_len;
29 match_len = i;
30 }
@@ -40,6 +32,14 @@ static inline int longest_match_lstat_cache(int len, const char *name,
32 return match_len;
33 }
34
35 +static struct cache_def {
36 + char path[PATH_MAX + 1];
37 + int len;
38 + int flags;
39 + int track_flags;
40 + int prefix_len_stat_func;
41 +} cache;
42 +
43 static inline void reset_lstat_cache(void)
44 {
45 cache.path[0] = '\0';
@@ -94,7 +94,8 @@ static int lstat_cache(int len, const char *name,
94 * the 2 "excluding" path types.
95 */
96 match_len = last_slash =
97 - longest_match_lstat_cache(len, name, &previous_slash);
97 + longest_path_match(name, len, cache.path, cache.len,
98 + &previous_slash);
99 match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK);
100 if (match_flags && match_len == cache.len)
101 return match_flags;
@@ -188,7 +189,8 @@ void invalidate_lstat_cache(int len, const char *name)
189 {
190 int match_len, previous_slash;
191
191 - match_len = longest_match_lstat_cache(len, name, &previous_slash);
192 + match_len = longest_path_match(name, len, cache.path, cache.len,
193 + &previous_slash);
194 if (len == match_len) {
195 if ((cache.track_flags & FL_DIR) && previous_slash > 0) {
196 cache.path[previous_slash] = '\0';