🌐 AI搜索 & 代理 主页
blob: 7b0a86d35776e8695423c13403c9f4fa3465017d [file] [log] [blame]
Junio C Hamanof859c842007-05-12 05:11:071#include "cache.h"
2
Kjetil Barvik148bc062009-02-09 20:54:053/*
4 * Returns the length (on a path component basis) of the longest
5 * common prefix match of 'name_a' and 'name_b'.
6 */
7static 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
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 /*
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 */
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 }
31 *previous_slash = match_len_prev;
32 return match_len;
33}
34
Linus Torvaldsb9fd2842009-07-09 20:35:3135static struct cache_def default_cache;
Junio C Hamanof859c842007-05-12 05:11:0736
Linus Torvalds867f72b2009-07-09 20:23:5937static inline void reset_lstat_cache(struct cache_def *cache)
Linus Torvaldsc40641b2008-05-09 16:21:0738{
Linus Torvalds867f72b2009-07-09 20:23:5939 cache->path[0] = '\0';
40 cache->len = 0;
41 cache->flags = 0;
Kjetil Barvik60b458b2009-02-09 20:54:0442 /*
43 * The track_flags and prefix_len_stat_func members is only
44 * set by the safeguard rule inside lstat_cache()
45 */
Kjetil Barvik92604b42009-01-18 15:14:5046}
47
48#define FL_DIR (1 << 0)
Kjetil Barvik09c93062009-01-18 15:14:5149#define FL_NOENT (1 << 1)
50#define FL_SYMLINK (1 << 2)
51#define FL_LSTATERR (1 << 3)
52#define FL_ERR (1 << 4)
Kjetil Barvikbad4a542009-01-18 15:14:5253#define FL_FULLPATH (1 << 5)
Kjetil Barvik92604b42009-01-18 15:14:5054
55/*
56 * Check if name 'name' of length 'len' has a symlink leading
Kjetil Barvik09c93062009-01-18 15:14:5157 * component, or if the directory exists and is real, or not.
Kjetil Barvik92604b42009-01-18 15:14:5058 *
59 * To speed up the check, some information is allowed to be cached.
Kjetil Barvikbad4a542009-01-18 15:14:5260 * This can be indicated by the 'track_flags' argument, which also can
61 * be used to indicate that we should check the full path.
62 *
63 * The 'prefix_len_stat_func' parameter can be used to set the length
64 * of the prefix, where the cache should use the stat() function
65 * instead of the lstat() function to test each path component.
Kjetil Barvik92604b42009-01-18 15:14:5066 */
Linus Torvalds867f72b2009-07-09 20:23:5967static int lstat_cache(struct cache_def *cache, const char *name, int len,
Kjetil Barvikbad4a542009-01-18 15:14:5268 int track_flags, int prefix_len_stat_func)
Kjetil Barvik92604b42009-01-18 15:14:5069{
Kjetil Barvikaeabab52009-01-18 15:14:5370 int match_len, last_slash, last_slash_dir, previous_slash;
Kjetil Barvikbad4a542009-01-18 15:14:5271 int match_flags, ret_flags, save_flags, max_len, ret;
Linus Torvaldsc40641b2008-05-09 16:21:0772 struct stat st;
Junio C Hamanof859c842007-05-12 05:11:0773
Linus Torvalds867f72b2009-07-09 20:23:5974 if (cache->track_flags != track_flags ||
75 cache->prefix_len_stat_func != prefix_len_stat_func) {
Kjetil Barvik09c93062009-01-18 15:14:5176 /*
Kjetil Barvik60b458b2009-02-09 20:54:0477 * As a safeguard rule we clear the cache if the
78 * values of track_flags and/or prefix_len_stat_func
79 * does not match with the last supplied values.
Kjetil Barvik09c93062009-01-18 15:14:5180 */
Linus Torvalds867f72b2009-07-09 20:23:5981 reset_lstat_cache(cache);
82 cache->track_flags = track_flags;
83 cache->prefix_len_stat_func = prefix_len_stat_func;
Kjetil Barvik09c93062009-01-18 15:14:5184 match_len = last_slash = 0;
85 } else {
86 /*
87 * Check to see if we have a match from the cache for
88 * the 2 "excluding" path types.
89 */
Kjetil Barvikaeabab52009-01-18 15:14:5390 match_len = last_slash =
Linus Torvalds867f72b2009-07-09 20:23:5991 longest_path_match(name, len, cache->path, cache->len,
Kjetil Barvik148bc062009-02-09 20:54:0592 &previous_slash);
Linus Torvalds867f72b2009-07-09 20:23:5993 match_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
Kjetil Barvik77716752009-06-14 13:08:2894
95 if (!(track_flags & FL_FULLPATH) && match_len == len)
96 match_len = last_slash = previous_slash;
97
Linus Torvalds867f72b2009-07-09 20:23:5998 if (match_flags && match_len == cache->len)
Kjetil Barvik09c93062009-01-18 15:14:5199 return match_flags;
100 /*
101 * If we now have match_len > 0, we would know that
102 * the matched part will always be a directory.
103 *
104 * Also, if we are tracking directories and 'name' is
105 * a substring of the cache on a path component basis,
106 * we can return immediately.
107 */
108 match_flags = track_flags & FL_DIR;
109 if (match_flags && len == match_len)
110 return match_flags;
111 }
Linus Torvaldsc40641b2008-05-09 16:21:07112
113 /*
Kjetil Barvik92604b42009-01-18 15:14:50114 * Okay, no match from the cache so far, so now we have to
115 * check the rest of the path components.
Linus Torvaldsc40641b2008-05-09 16:21:07116 */
Kjetil Barvik92604b42009-01-18 15:14:50117 ret_flags = FL_DIR;
118 last_slash_dir = last_slash;
119 max_len = len < PATH_MAX ? len : PATH_MAX;
120 while (match_len < max_len) {
121 do {
Linus Torvalds867f72b2009-07-09 20:23:59122 cache->path[match_len] = name[match_len];
Kjetil Barvik92604b42009-01-18 15:14:50123 match_len++;
124 } while (match_len < max_len && name[match_len] != '/');
Kjetil Barvikbad4a542009-01-18 15:14:52125 if (match_len >= max_len && !(track_flags & FL_FULLPATH))
Kjetil Barvik92604b42009-01-18 15:14:50126 break;
127 last_slash = match_len;
Linus Torvalds867f72b2009-07-09 20:23:59128 cache->path[last_slash] = '\0';
Linus Torvaldsc40641b2008-05-09 16:21:07129
Kjetil Barvikbad4a542009-01-18 15:14:52130 if (last_slash <= prefix_len_stat_func)
Linus Torvalds867f72b2009-07-09 20:23:59131 ret = stat(cache->path, &st);
Kjetil Barvikbad4a542009-01-18 15:14:52132 else
Linus Torvalds867f72b2009-07-09 20:23:59133 ret = lstat(cache->path, &st);
Kjetil Barvikbad4a542009-01-18 15:14:52134
135 if (ret) {
Kjetil Barvik92604b42009-01-18 15:14:50136 ret_flags = FL_LSTATERR;
Kjetil Barvik09c93062009-01-18 15:14:51137 if (errno == ENOENT)
138 ret_flags |= FL_NOENT;
Kjetil Barvik92604b42009-01-18 15:14:50139 } else if (S_ISDIR(st.st_mode)) {
140 last_slash_dir = last_slash;
Linus Torvaldsc40641b2008-05-09 16:21:07141 continue;
Kjetil Barvik92604b42009-01-18 15:14:50142 } else if (S_ISLNK(st.st_mode)) {
143 ret_flags = FL_SYMLINK;
144 } else {
145 ret_flags = FL_ERR;
Junio C Hamanof859c842007-05-12 05:11:07146 }
Linus Torvaldsc40641b2008-05-09 16:21:07147 break;
Junio C Hamanof859c842007-05-12 05:11:07148 }
Kjetil Barvik92604b42009-01-18 15:14:50149
150 /*
Kjetil Barvik09c93062009-01-18 15:14:51151 * At the end update the cache. Note that max 3 different
152 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
153 * for the moment!
Kjetil Barvik92604b42009-01-18 15:14:50154 */
Kjetil Barvik09c93062009-01-18 15:14:51155 save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
Kjetil Barvikbad4a542009-01-18 15:14:52156 if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
Linus Torvalds867f72b2009-07-09 20:23:59157 cache->path[last_slash] = '\0';
158 cache->len = last_slash;
159 cache->flags = save_flags;
Kjetil Barvik60b458b2009-02-09 20:54:04160 } else if ((track_flags & FL_DIR) &&
Kjetil Barvikbad4a542009-01-18 15:14:52161 last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
Kjetil Barvik92604b42009-01-18 15:14:50162 /*
163 * We have a separate test for the directory case,
Kjetil Barvik09c93062009-01-18 15:14:51164 * since it could be that we have found a symlink or a
165 * non-existing directory and the track_flags says
166 * that we cannot cache this fact, so the cache would
167 * then have been left empty in this case.
Kjetil Barvik92604b42009-01-18 15:14:50168 *
169 * But if we are allowed to track real directories, we
170 * can still cache the path components before the last
Kjetil Barvik09c93062009-01-18 15:14:51171 * one (the found symlink or non-existing component).
Kjetil Barvik92604b42009-01-18 15:14:50172 */
Linus Torvalds867f72b2009-07-09 20:23:59173 cache->path[last_slash_dir] = '\0';
174 cache->len = last_slash_dir;
175 cache->flags = FL_DIR;
Kjetil Barvik92604b42009-01-18 15:14:50176 } else {
Linus Torvalds867f72b2009-07-09 20:23:59177 reset_lstat_cache(cache);
Kjetil Barvik92604b42009-01-18 15:14:50178 }
179 return ret_flags;
180}
181
Kjetil Barvikaeabab52009-01-18 15:14:53182/*
183 * Invalidate the given 'name' from the cache, if 'name' matches
184 * completely with the cache.
185 */
Kjetil Barvik57199892009-02-09 20:54:06186void invalidate_lstat_cache(const char *name, int len)
Kjetil Barvikaeabab52009-01-18 15:14:53187{
188 int match_len, previous_slash;
Linus Torvalds867f72b2009-07-09 20:23:59189 struct cache_def *cache = &default_cache; /* FIXME */
Kjetil Barvikaeabab52009-01-18 15:14:53190
Linus Torvalds867f72b2009-07-09 20:23:59191 match_len = longest_path_match(name, len, cache->path, cache->len,
Kjetil Barvik148bc062009-02-09 20:54:05192 &previous_slash);
Kjetil Barvikaeabab52009-01-18 15:14:53193 if (len == match_len) {
Linus Torvalds867f72b2009-07-09 20:23:59194 if ((cache->track_flags & FL_DIR) && previous_slash > 0) {
195 cache->path[previous_slash] = '\0';
196 cache->len = previous_slash;
197 cache->flags = FL_DIR;
Kjetil Barvikcb319c32009-06-07 11:33:05198 } else {
Linus Torvalds867f72b2009-07-09 20:23:59199 reset_lstat_cache(cache);
Kjetil Barvikcb319c32009-06-07 11:33:05200 }
Kjetil Barvikaeabab52009-01-18 15:14:53201 }
202}
203
Kjetil Barvikbda6eb02009-01-18 15:14:54204/*
205 * Completely clear the contents of the cache
206 */
207void clear_lstat_cache(void)
208{
Linus Torvalds867f72b2009-07-09 20:23:59209 struct cache_def *cache = &default_cache; /* FIXME */
210 reset_lstat_cache(cache);
Kjetil Barvikbda6eb02009-01-18 15:14:54211}
212
Kjetil Barvikbad4a542009-01-18 15:14:52213#define USE_ONLY_LSTAT 0
214
Kjetil Barvik92604b42009-01-18 15:14:50215/*
216 * Return non-zero if path 'name' has a leading symlink component
217 */
Linus Torvaldsb9fd2842009-07-09 20:35:31218int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len)
219{
220 return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK;
221}
222
223/*
224 * Return non-zero if path 'name' has a leading symlink component
225 */
Kjetil Barvik57199892009-02-09 20:54:06226int has_symlink_leading_path(const char *name, int len)
Kjetil Barvik92604b42009-01-18 15:14:50227{
Linus Torvaldsb9fd2842009-07-09 20:35:31228 return threaded_has_symlink_leading_path(&default_cache, name, len);
Junio C Hamanof859c842007-05-12 05:11:07229}
Kjetil Barvik09c93062009-01-18 15:14:51230
231/*
232 * Return non-zero if path 'name' has a leading symlink component or
233 * if some leading path component does not exists.
234 */
Kjetil Barvik57199892009-02-09 20:54:06235int has_symlink_or_noent_leading_path(const char *name, int len)
Kjetil Barvik09c93062009-01-18 15:14:51236{
Linus Torvalds867f72b2009-07-09 20:23:59237 struct cache_def *cache = &default_cache; /* FIXME */
238 return lstat_cache(cache, name, len,
Kjetil Barvikbad4a542009-01-18 15:14:52239 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
Kjetil Barvik09c93062009-01-18 15:14:51240 (FL_SYMLINK|FL_NOENT);
241}
Kjetil Barvikbad4a542009-01-18 15:14:52242
243/*
244 * Return non-zero if all path components of 'name' exists as a
245 * directory. If prefix_len > 0, we will test with the stat()
246 * function instead of the lstat() function for a prefix length of
247 * 'prefix_len', thus we then allow for symlinks in the prefix part as
248 * long as those points to real existing directories.
249 */
Kjetil Barvik57199892009-02-09 20:54:06250int has_dirs_only_path(const char *name, int len, int prefix_len)
Kjetil Barvikbad4a542009-01-18 15:14:52251{
Linus Torvalds867f72b2009-07-09 20:23:59252 struct cache_def *cache = &default_cache; /* FIXME */
253 return lstat_cache(cache, name, len,
Kjetil Barvikbad4a542009-01-18 15:14:52254 FL_DIR|FL_FULLPATH, prefix_len) &
255 FL_DIR;
256}
Kjetil Barvik78478922009-02-09 20:54:07257
258static struct removal_def {
259 char path[PATH_MAX];
260 int len;
261} removal;
262
263static void do_remove_scheduled_dirs(int new_len)
264{
265 while (removal.len > new_len) {
266 removal.path[removal.len] = '\0';
267 if (rmdir(removal.path))
268 break;
269 do {
270 removal.len--;
271 } while (removal.len > new_len &&
272 removal.path[removal.len] != '/');
273 }
274 removal.len = new_len;
Kjetil Barvik78478922009-02-09 20:54:07275}
276
277void schedule_dir_for_removal(const char *name, int len)
278{
279 int match_len, last_slash, i, previous_slash;
280
281 match_len = last_slash = i =
282 longest_path_match(name, len, removal.path, removal.len,
283 &previous_slash);
284 /* Find last slash inside 'name' */
285 while (i < len) {
286 if (name[i] == '/')
287 last_slash = i;
288 i++;
289 }
290
291 /*
292 * If we are about to go down the directory tree, we check if
293 * we must first go upwards the tree, such that we then can
294 * remove possible empty directories as we go upwards.
295 */
296 if (match_len < last_slash && match_len < removal.len)
297 do_remove_scheduled_dirs(match_len);
298 /*
299 * If we go deeper down the directory tree, we only need to
300 * save the new path components as we go down.
301 */
302 if (match_len < last_slash) {
303 memcpy(&removal.path[match_len], &name[match_len],
304 last_slash - match_len);
305 removal.len = last_slash;
306 }
Kjetil Barvik78478922009-02-09 20:54:07307}
308
309void remove_scheduled_dirs(void)
310{
311 do_remove_scheduled_dirs(0);
Kjetil Barvik78478922009-02-09 20:54:07312}