🌐 AI搜索 & 代理 主页
blob: 5261e8cf499006c1d84fc42a3e96e4dee7f09ba1 [file] [log] [blame]
Junio C Hamanof859c842007-05-12 05:11:071#include "cache.h"
2
Junio C Hamano72f31962012-09-16 05:38:283static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len);
4static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len);
5
Kjetil Barvik148bc062009-02-09 20:54:056/*
7 * Returns the length (on a path component basis) of the longest
8 * common prefix match of 'name_a' and 'name_b'.
9 */
10static int longest_path_match(const char *name_a, int len_a,
11 const char *name_b, int len_b,
12 int *previous_slash)
13{
14 int max_len, match_len = 0, match_len_prev = 0, i = 0;
15
16 max_len = len_a < len_b ? len_a : len_b;
17 while (i < max_len && name_a[i] == name_b[i]) {
18 if (name_a[i] == '/') {
19 match_len_prev = match_len;
20 match_len = i;
21 }
22 i++;
23 }
24 /*
25 * Is 'name_b' a substring of 'name_a', the other way around,
26 * or is 'name_a' and 'name_b' the exact same string?
27 */
28 if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||
29 (len_a < len_b && name_b[len_a] == '/') ||
30 (len_a == len_b))) {
31 match_len_prev = match_len;
32 match_len = i;
33 }
34 *previous_slash = match_len_prev;
35 return match_len;
36}
37
Karsten Bleese7c73052014-07-04 22:41:4638static struct cache_def default_cache = CACHE_DEF_INIT;
Junio C Hamanof859c842007-05-12 05:11:0739
Linus Torvalds867f72b2009-07-09 20:23:5940static inline void reset_lstat_cache(struct cache_def *cache)
Linus Torvaldsc40641b2008-05-09 16:21:0741{
Karsten Bleese7c73052014-07-04 22:41:4642 strbuf_reset(&cache->path);
Linus Torvalds867f72b2009-07-09 20:23:5943 cache->flags = 0;
Kjetil Barvik60b458b2009-02-09 20:54:0444 /*
45 * The track_flags and prefix_len_stat_func members is only
46 * set by the safeguard rule inside lstat_cache()
47 */
Kjetil Barvik92604b42009-01-18 15:14:5048}
49
50#define FL_DIR (1 << 0)
Kjetil Barvik09c93062009-01-18 15:14:5151#define FL_NOENT (1 << 1)
52#define FL_SYMLINK (1 << 2)
53#define FL_LSTATERR (1 << 3)
54#define FL_ERR (1 << 4)
Kjetil Barvikbad4a542009-01-18 15:14:5255#define FL_FULLPATH (1 << 5)
Kjetil Barvik92604b42009-01-18 15:14:5056
57/*
58 * Check if name 'name' of length 'len' has a symlink leading
Kjetil Barvik09c93062009-01-18 15:14:5159 * component, or if the directory exists and is real, or not.
Kjetil Barvik92604b42009-01-18 15:14:5060 *
61 * To speed up the check, some information is allowed to be cached.
Kjetil Barvikbad4a542009-01-18 15:14:5262 * This can be indicated by the 'track_flags' argument, which also can
63 * be used to indicate that we should check the full path.
64 *
65 * The 'prefix_len_stat_func' parameter can be used to set the length
66 * of the prefix, where the cache should use the stat() function
67 * instead of the lstat() function to test each path component.
Kjetil Barvik92604b42009-01-18 15:14:5068 */
Clemens Buchacher4856ff22010-10-09 13:52:5969static int lstat_cache_matchlen(struct cache_def *cache,
70 const char *name, int len,
71 int *ret_flags, int track_flags,
72 int prefix_len_stat_func)
Kjetil Barvik92604b42009-01-18 15:14:5073{
Kjetil Barvikaeabab52009-01-18 15:14:5374 int match_len, last_slash, last_slash_dir, previous_slash;
Karsten Bleese7c73052014-07-04 22:41:4675 int save_flags, ret;
Linus Torvaldsc40641b2008-05-09 16:21:0776 struct stat st;
Junio C Hamanof859c842007-05-12 05:11:0777
Linus Torvalds867f72b2009-07-09 20:23:5978 if (cache->track_flags != track_flags ||
79 cache->prefix_len_stat_func != prefix_len_stat_func) {
Kjetil Barvik09c93062009-01-18 15:14:5180 /*
Kjetil Barvik60b458b2009-02-09 20:54:0481 * As a safeguard rule we clear the cache if the
82 * values of track_flags and/or prefix_len_stat_func
83 * does not match with the last supplied values.
Kjetil Barvik09c93062009-01-18 15:14:5184 */
Linus Torvalds867f72b2009-07-09 20:23:5985 reset_lstat_cache(cache);
86 cache->track_flags = track_flags;
87 cache->prefix_len_stat_func = prefix_len_stat_func;
Kjetil Barvik09c93062009-01-18 15:14:5188 match_len = last_slash = 0;
89 } else {
90 /*
91 * Check to see if we have a match from the cache for
92 * the 2 "excluding" path types.
93 */
Kjetil Barvikaeabab52009-01-18 15:14:5394 match_len = last_slash =
Karsten Bleese7c73052014-07-04 22:41:4695 longest_path_match(name, len, cache->path.buf,
96 cache->path.len, &previous_slash);
Clemens Buchacher4856ff22010-10-09 13:52:5997 *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
Kjetil Barvik77716752009-06-14 13:08:2898
99 if (!(track_flags & FL_FULLPATH) && match_len == len)
100 match_len = last_slash = previous_slash;
101
Karsten Bleese7c73052014-07-04 22:41:46102 if (*ret_flags && match_len == cache->path.len)
Clemens Buchacher4856ff22010-10-09 13:52:59103 return match_len;
Kjetil Barvik09c93062009-01-18 15:14:51104 /*
105 * If we now have match_len > 0, we would know that
106 * the matched part will always be a directory.
107 *
108 * Also, if we are tracking directories and 'name' is
109 * a substring of the cache on a path component basis,
110 * we can return immediately.
111 */
Clemens Buchacher4856ff22010-10-09 13:52:59112 *ret_flags = track_flags & FL_DIR;
113 if (*ret_flags && len == match_len)
114 return match_len;
Kjetil Barvik09c93062009-01-18 15:14:51115 }
Linus Torvaldsc40641b2008-05-09 16:21:07116
117 /*
Kjetil Barvik92604b42009-01-18 15:14:50118 * Okay, no match from the cache so far, so now we have to
119 * check the rest of the path components.
Linus Torvaldsc40641b2008-05-09 16:21:07120 */
Clemens Buchacher4856ff22010-10-09 13:52:59121 *ret_flags = FL_DIR;
Kjetil Barvik92604b42009-01-18 15:14:50122 last_slash_dir = last_slash;
Karsten Bleese7c73052014-07-04 22:41:46123 if (len > cache->path.len)
124 strbuf_grow(&cache->path, len - cache->path.len);
125 while (match_len < len) {
Kjetil Barvik92604b42009-01-18 15:14:50126 do {
Karsten Bleese7c73052014-07-04 22:41:46127 cache->path.buf[match_len] = name[match_len];
Kjetil Barvik92604b42009-01-18 15:14:50128 match_len++;
Karsten Bleese7c73052014-07-04 22:41:46129 } while (match_len < len && name[match_len] != '/');
130 if (match_len >= len && !(track_flags & FL_FULLPATH))
Kjetil Barvik92604b42009-01-18 15:14:50131 break;
132 last_slash = match_len;
Karsten Bleese7c73052014-07-04 22:41:46133 cache->path.buf[last_slash] = '\0';
Linus Torvaldsc40641b2008-05-09 16:21:07134
Kjetil Barvikbad4a542009-01-18 15:14:52135 if (last_slash <= prefix_len_stat_func)
Karsten Bleese7c73052014-07-04 22:41:46136 ret = stat(cache->path.buf, &st);
Kjetil Barvikbad4a542009-01-18 15:14:52137 else
Karsten Bleese7c73052014-07-04 22:41:46138 ret = lstat(cache->path.buf, &st);
Kjetil Barvikbad4a542009-01-18 15:14:52139
140 if (ret) {
Clemens Buchacher4856ff22010-10-09 13:52:59141 *ret_flags = FL_LSTATERR;
Kjetil Barvik09c93062009-01-18 15:14:51142 if (errno == ENOENT)
Clemens Buchacher4856ff22010-10-09 13:52:59143 *ret_flags |= FL_NOENT;
Kjetil Barvik92604b42009-01-18 15:14:50144 } else if (S_ISDIR(st.st_mode)) {
145 last_slash_dir = last_slash;
Linus Torvaldsc40641b2008-05-09 16:21:07146 continue;
Kjetil Barvik92604b42009-01-18 15:14:50147 } else if (S_ISLNK(st.st_mode)) {
Clemens Buchacher4856ff22010-10-09 13:52:59148 *ret_flags = FL_SYMLINK;
Kjetil Barvik92604b42009-01-18 15:14:50149 } else {
Clemens Buchacher4856ff22010-10-09 13:52:59150 *ret_flags = FL_ERR;
Junio C Hamanof859c842007-05-12 05:11:07151 }
Linus Torvaldsc40641b2008-05-09 16:21:07152 break;
Junio C Hamanof859c842007-05-12 05:11:07153 }
Kjetil Barvik92604b42009-01-18 15:14:50154
155 /*
Kjetil Barvik09c93062009-01-18 15:14:51156 * At the end update the cache. Note that max 3 different
157 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
158 * for the moment!
Kjetil Barvik92604b42009-01-18 15:14:50159 */
Clemens Buchacher4856ff22010-10-09 13:52:59160 save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
Karsten Bleese7c73052014-07-04 22:41:46161 if (save_flags && last_slash > 0) {
162 cache->path.buf[last_slash] = '\0';
163 cache->path.len = last_slash;
Linus Torvalds867f72b2009-07-09 20:23:59164 cache->flags = save_flags;
Karsten Bleese7c73052014-07-04 22:41:46165 } else if ((track_flags & FL_DIR) && last_slash_dir > 0) {
Kjetil Barvik92604b42009-01-18 15:14:50166 /*
167 * We have a separate test for the directory case,
Kjetil Barvik09c93062009-01-18 15:14:51168 * since it could be that we have found a symlink or a
169 * non-existing directory and the track_flags says
170 * that we cannot cache this fact, so the cache would
171 * then have been left empty in this case.
Kjetil Barvik92604b42009-01-18 15:14:50172 *
173 * But if we are allowed to track real directories, we
174 * can still cache the path components before the last
Kjetil Barvik09c93062009-01-18 15:14:51175 * one (the found symlink or non-existing component).
Kjetil Barvik92604b42009-01-18 15:14:50176 */
Karsten Bleese7c73052014-07-04 22:41:46177 cache->path.buf[last_slash_dir] = '\0';
178 cache->path.len = last_slash_dir;
Linus Torvalds867f72b2009-07-09 20:23:59179 cache->flags = FL_DIR;
Kjetil Barvik92604b42009-01-18 15:14:50180 } else {
Linus Torvalds867f72b2009-07-09 20:23:59181 reset_lstat_cache(cache);
Kjetil Barvik92604b42009-01-18 15:14:50182 }
Clemens Buchacher4856ff22010-10-09 13:52:59183 return match_len;
184}
185
186static int lstat_cache(struct cache_def *cache, const char *name, int len,
187 int track_flags, int prefix_len_stat_func)
188{
189 int flags;
190 (void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,
191 prefix_len_stat_func);
192 return flags;
Kjetil Barvik92604b42009-01-18 15:14:50193}
194
Kjetil Barvikbad4a542009-01-18 15:14:52195#define USE_ONLY_LSTAT 0
196
Kjetil Barvik92604b42009-01-18 15:14:50197/*
198 * Return non-zero if path 'name' has a leading symlink component
199 */
Linus Torvaldsb9fd2842009-07-09 20:35:31200int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len)
201{
202 return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK;
203}
204
205/*
206 * Return non-zero if path 'name' has a leading symlink component
207 */
Kjetil Barvik57199892009-02-09 20:54:06208int has_symlink_leading_path(const char *name, int len)
Kjetil Barvik92604b42009-01-18 15:14:50209{
Linus Torvaldsb9fd2842009-07-09 20:35:31210 return threaded_has_symlink_leading_path(&default_cache, name, len);
Junio C Hamanof859c842007-05-12 05:11:07211}
Kjetil Barvik09c93062009-01-18 15:14:51212
213/*
Clemens Buchacherf66caaf2010-10-09 13:53:00214 * Return zero if path 'name' has a leading symlink component or
Kjetil Barvik09c93062009-01-18 15:14:51215 * if some leading path component does not exists.
Clemens Buchacherf66caaf2010-10-09 13:53:00216 *
217 * Return -1 if leading path exists and is a directory.
218 *
219 * Return path length if leading path exists and is neither a
220 * directory nor a symlink.
Kjetil Barvik09c93062009-01-18 15:14:51221 */
Clemens Buchacherf66caaf2010-10-09 13:53:00222int check_leading_path(const char *name, int len)
Kjetil Barvik09c93062009-01-18 15:14:51223{
Jared Hance15438d52012-03-03 02:31:15224 return threaded_check_leading_path(&default_cache, name, len);
225}
226
227/*
228 * Return zero if path 'name' has a leading symlink component or
229 * if some leading path component does not exists.
230 *
231 * Return -1 if leading path exists and is a directory.
232 *
233 * Return path length if leading path exists and is neither a
234 * directory nor a symlink.
235 */
Junio C Hamano72f31962012-09-16 05:38:28236static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len)
Jared Hance15438d52012-03-03 02:31:15237{
Clemens Buchacherf66caaf2010-10-09 13:53:00238 int flags;
239 int match_len = lstat_cache_matchlen(cache, name, len, &flags,
240 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
Clemens Buchacher1d718a52011-02-20 12:13:43241 if (flags & FL_NOENT)
Clemens Buchacherf66caaf2010-10-09 13:53:00242 return 0;
243 else if (flags & FL_DIR)
244 return -1;
245 else
246 return match_len;
Kjetil Barvik09c93062009-01-18 15:14:51247}
Kjetil Barvikbad4a542009-01-18 15:14:52248
249/*
250 * Return non-zero if all path components of 'name' exists as a
251 * directory. If prefix_len > 0, we will test with the stat()
252 * function instead of the lstat() function for a prefix length of
253 * 'prefix_len', thus we then allow for symlinks in the prefix part as
254 * long as those points to real existing directories.
255 */
Kjetil Barvik57199892009-02-09 20:54:06256int has_dirs_only_path(const char *name, int len, int prefix_len)
Kjetil Barvikbad4a542009-01-18 15:14:52257{
Jared Hance15438d52012-03-03 02:31:15258 return threaded_has_dirs_only_path(&default_cache, name, len, prefix_len);
259}
260
261/*
262 * Return non-zero if all path components of 'name' exists as a
263 * directory. If prefix_len > 0, we will test with the stat()
264 * function instead of the lstat() function for a prefix length of
265 * 'prefix_len', thus we then allow for symlinks in the prefix part as
266 * long as those points to real existing directories.
267 */
Junio C Hamano72f31962012-09-16 05:38:28268static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len)
Jared Hance15438d52012-03-03 02:31:15269{
Linus Torvalds867f72b2009-07-09 20:23:59270 return lstat_cache(cache, name, len,
Kjetil Barvikbad4a542009-01-18 15:14:52271 FL_DIR|FL_FULLPATH, prefix_len) &
272 FL_DIR;
273}
Kjetil Barvik78478922009-02-09 20:54:07274
Karsten Bleese7c73052014-07-04 22:41:46275static struct strbuf removal = STRBUF_INIT;
Kjetil Barvik78478922009-02-09 20:54:07276
277static void do_remove_scheduled_dirs(int new_len)
278{
279 while (removal.len > new_len) {
Karsten Bleese7c73052014-07-04 22:41:46280 removal.buf[removal.len] = '\0';
281 if (rmdir(removal.buf))
Kjetil Barvik78478922009-02-09 20:54:07282 break;
283 do {
284 removal.len--;
285 } while (removal.len > new_len &&
Karsten Bleese7c73052014-07-04 22:41:46286 removal.buf[removal.len] != '/');
Kjetil Barvik78478922009-02-09 20:54:07287 }
288 removal.len = new_len;
Kjetil Barvik78478922009-02-09 20:54:07289}
290
291void schedule_dir_for_removal(const char *name, int len)
292{
293 int match_len, last_slash, i, previous_slash;
294
295 match_len = last_slash = i =
Karsten Bleese7c73052014-07-04 22:41:46296 longest_path_match(name, len, removal.buf, removal.len,
Kjetil Barvik78478922009-02-09 20:54:07297 &previous_slash);
298 /* Find last slash inside 'name' */
299 while (i < len) {
300 if (name[i] == '/')
301 last_slash = i;
302 i++;
303 }
304
305 /*
306 * If we are about to go down the directory tree, we check if
307 * we must first go upwards the tree, such that we then can
308 * remove possible empty directories as we go upwards.
309 */
310 if (match_len < last_slash && match_len < removal.len)
311 do_remove_scheduled_dirs(match_len);
312 /*
313 * If we go deeper down the directory tree, we only need to
314 * save the new path components as we go down.
315 */
Karsten Bleese7c73052014-07-04 22:41:46316 if (match_len < last_slash)
317 strbuf_add(&removal, &name[match_len], last_slash - match_len);
Kjetil Barvik78478922009-02-09 20:54:07318}
319
320void remove_scheduled_dirs(void)
321{
322 do_remove_scheduled_dirs(0);
Kjetil Barvik78478922009-02-09 20:54:07323}