Code

lstat_cache(): generalise longest_match_lstat_cache()
[git.git] / symlinks.c
index f262b7c44b387f4c60901124c30585b3c059fa73..4596aee9dc32c7664a1c0933138a9319dd4ae868 100644 (file)
@@ -1,51 +1,54 @@
 #include "cache.h"
 
-static struct cache_def {
-       char path[PATH_MAX + 1];
-       int len;
-       int flags;
-       int track_flags;
-       int prefix_len_stat_func;
-} cache;
-
 /*
  * Returns the length (on a path component basis) of the longest
- * common prefix match of 'name' and the cached path string.
+ * common prefix match of 'name_a' and 'name_b'.
  */
-static inline int longest_match_lstat_cache(int len, const char *name,
-                                           int *previous_slash)
+static int longest_path_match(const char *name_a, int len_a,
+                             const char *name_b, int len_b,
+                             int *previous_slash)
 {
        int max_len, match_len = 0, match_len_prev = 0, i = 0;
 
-       max_len = len < cache.len ? len : cache.len;
-       while (i < max_len && name[i] == cache.path[i]) {
-               if (name[i] == '/') {
+       max_len = len_a < len_b ? len_a : len_b;
+       while (i < max_len && name_a[i] == name_b[i]) {
+               if (name_a[i] == '/') {
                        match_len_prev = match_len;
                        match_len = i;
                }
                i++;
        }
-       /* Is the cached path string a substring of 'name'? */
-       if (i == cache.len && cache.len < len && name[cache.len] == '/') {
-               match_len_prev = match_len;
-               match_len = cache.len;
-       /* Is 'name' a substring of the cached path string? */
-       } else if ((i == len && len < cache.len && cache.path[len] == '/') ||
-                  (i == len && len == cache.len)) {
+       /*
+        * Is 'name_b' a substring of 'name_a', the other way around,
+        * or is 'name_a' and 'name_b' the exact same string?
+        */
+       if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||
+                            (len_a < len_b && name_b[len_a] == '/') ||
+                            (len_a == len_b))) {
                match_len_prev = match_len;
-               match_len = len;
+               match_len = i;
        }
        *previous_slash = match_len_prev;
        return match_len;
 }
 
-static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func)
+static struct cache_def {
+       char path[PATH_MAX + 1];
+       int len;
+       int flags;
+       int track_flags;
+       int prefix_len_stat_func;
+} cache;
+
+static inline void reset_lstat_cache(void)
 {
        cache.path[0] = '\0';
        cache.len = 0;
        cache.flags = 0;
-       cache.track_flags = track_flags;
-       cache.prefix_len_stat_func = prefix_len_stat_func;
+       /*
+        * The track_flags and prefix_len_stat_func members is only
+        * set by the safeguard rule inside lstat_cache()
+        */
 }
 
 #define FL_DIR      (1 << 0)
@@ -77,11 +80,13 @@ static int lstat_cache(int len, const char *name,
        if (cache.track_flags != track_flags ||
            cache.prefix_len_stat_func != prefix_len_stat_func) {
                /*
-                * As a safeguard we clear the cache if the values of
-                * track_flags and/or prefix_len_stat_func does not
-                * match with the last supplied values.
+                * As a safeguard rule we clear the cache if the
+                * values of track_flags and/or prefix_len_stat_func
+                * does not match with the last supplied values.
                 */
-               reset_lstat_cache(track_flags, prefix_len_stat_func);
+               reset_lstat_cache();
+               cache.track_flags = track_flags;
+               cache.prefix_len_stat_func = prefix_len_stat_func;
                match_len = last_slash = 0;
        } else {
                /*
@@ -89,7 +94,8 @@ static int lstat_cache(int len, const char *name,
                 * the 2 "excluding" path types.
                 */
                match_len = last_slash =
-                       longest_match_lstat_cache(len, name, &previous_slash);
+                       longest_path_match(name, len, cache.path, cache.len,
+                                          &previous_slash);
                match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK);
                if (match_flags && match_len == cache.len)
                        return match_flags;
@@ -153,7 +159,7 @@ static int lstat_cache(int len, const char *name,
                cache.path[last_slash] = '\0';
                cache.len = last_slash;
                cache.flags = save_flags;
-       } else if (track_flags & FL_DIR &&
+       } else if ((track_flags & FL_DIR) &&
                   last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
                /*
                 * We have a separate test for the directory case,
@@ -170,7 +176,7 @@ static int lstat_cache(int len, const char *name,
                cache.len = last_slash_dir;
                cache.flags = FL_DIR;
        } else {
-               reset_lstat_cache(track_flags, prefix_len_stat_func);
+               reset_lstat_cache();
        }
        return ret_flags;
 }
@@ -183,15 +189,15 @@ void invalidate_lstat_cache(int len, const char *name)
 {
        int match_len, previous_slash;
 
-       match_len = longest_match_lstat_cache(len, name, &previous_slash);
+       match_len = longest_path_match(name, len, cache.path, cache.len,
+                                      &previous_slash);
        if (len == match_len) {
                if ((cache.track_flags & FL_DIR) && previous_slash > 0) {
                        cache.path[previous_slash] = '\0';
                        cache.len = previous_slash;
                        cache.flags = FL_DIR;
                } else
-                       reset_lstat_cache(cache.track_flags,
-                                         cache.prefix_len_stat_func);
+                       reset_lstat_cache();
        }
 }
 
@@ -200,7 +206,7 @@ void invalidate_lstat_cache(int len, const char *name)
  */
 void clear_lstat_cache(void)
 {
-       reset_lstat_cache(0, 0);
+       reset_lstat_cache();
 }
 
 #define USE_ONLY_LSTAT  0