|
1 | 1 | #include "cache.h"
|
2 | 2 |
|
3 |
| -struct pathname { |
| 3 | +static struct cache_def { |
| 4 | + char path[PATH_MAX + 1]; |
4 | 5 | int len;
|
5 |
| - char path[PATH_MAX]; |
6 |
| -}; |
| 6 | + int flags; |
| 7 | + int track_flags; |
| 8 | + int prefix_len_stat_func; |
| 9 | +} cache; |
7 | 10 |
|
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) |
10 | 17 | {
|
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; |
15 | 40 | }
|
16 | 41 |
|
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) |
18 | 43 | {
|
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; |
24 | 49 | }
|
25 | 50 |
|
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) |
27 | 72 | {
|
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; |
30 | 75 | struct stat st;
|
31 |
| - char *sp; |
32 |
| - int known_dir; |
33 | 76 |
|
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 | + } |
39 | 108 |
|
40 | 109 | /*
|
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. |
42 | 112 | */
|
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; |
55 | 137 | 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; |
60 | 142 | }
|
61 | 143 | break;
|
62 | 144 | }
|
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; |
64 | 241 | }
|
0 commit comments