]>
Commit | Line | Data |
---|---|---|
f859c846 JH |
1 | #include "cache.h" |
2 | ||
72f3196a JH |
3 | static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len); |
4 | static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len); | |
5 | ||
92604b46 KB |
6 | /* |
7 | * Returns the length (on a path component basis) of the longest | |
148bc06b | 8 | * common prefix match of 'name_a' and 'name_b'. |
92604b46 | 9 | */ |
148bc06b KB |
10 | static int longest_path_match(const char *name_a, int len_a, |
11 | const char *name_b, int len_b, | |
12 | int *previous_slash) | |
c40641b7 | 13 | { |
aeabab5c | 14 | int max_len, match_len = 0, match_len_prev = 0, i = 0; |
92604b46 | 15 | |
148bc06b KB |
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] == '/') { | |
aeabab5c | 19 | match_len_prev = match_len; |
92604b46 | 20 | match_len = i; |
aeabab5c | 21 | } |
92604b46 KB |
22 | i++; |
23 | } | |
60b458b7 | 24 | /* |
148bc06b KB |
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? | |
60b458b7 | 27 | */ |
148bc06b KB |
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))) { | |
aeabab5c | 31 | match_len_prev = match_len; |
60b458b7 | 32 | match_len = i; |
aeabab5c KB |
33 | } |
34 | *previous_slash = match_len_prev; | |
92604b46 | 35 | return match_len; |
c40641b7 LT |
36 | } |
37 | ||
e7c73053 | 38 | static struct cache_def default_cache = CACHE_DEF_INIT; |
148bc06b | 39 | |
867f72bf | 40 | static inline void reset_lstat_cache(struct cache_def *cache) |
c40641b7 | 41 | { |
e7c73053 | 42 | strbuf_reset(&cache->path); |
867f72bf | 43 | cache->flags = 0; |
60b458b7 KB |
44 | /* |
45 | * The track_flags and prefix_len_stat_func members is only | |
46 | * set by the safeguard rule inside lstat_cache() | |
47 | */ | |
c40641b7 LT |
48 | } |
49 | ||
92604b46 | 50 | #define FL_DIR (1 << 0) |
09c93066 KB |
51 | #define FL_NOENT (1 << 1) |
52 | #define FL_SYMLINK (1 << 2) | |
53 | #define FL_LSTATERR (1 << 3) | |
54 | #define FL_ERR (1 << 4) | |
bad4a54f | 55 | #define FL_FULLPATH (1 << 5) |
92604b46 KB |
56 | |
57 | /* | |
58 | * Check if name 'name' of length 'len' has a symlink leading | |
09c93066 | 59 | * component, or if the directory exists and is real, or not. |
92604b46 KB |
60 | * |
61 | * To speed up the check, some information is allowed to be cached. | |
bad4a54f KB |
62 | * 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. | |
92604b46 | 68 | */ |
4856ff2a CB |
69 | static 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) | |
c40641b7 | 73 | { |
aeabab5c | 74 | int match_len, last_slash, last_slash_dir, previous_slash; |
e7c73053 | 75 | int save_flags, ret; |
c40641b7 | 76 | struct stat st; |
f859c846 | 77 | |
867f72bf LT |
78 | if (cache->track_flags != track_flags || |
79 | cache->prefix_len_stat_func != prefix_len_stat_func) { | |
09c93066 | 80 | /* |
60b458b7 KB |
81 | * 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. | |
09c93066 | 84 | */ |
867f72bf LT |
85 | reset_lstat_cache(cache); |
86 | cache->track_flags = track_flags; | |
87 | cache->prefix_len_stat_func = prefix_len_stat_func; | |
09c93066 KB |
88 | 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 | */ | |
aeabab5c | 94 | match_len = last_slash = |
e7c73053 KB |
95 | longest_path_match(name, len, cache->path.buf, |
96 | cache->path.len, &previous_slash); | |
4856ff2a | 97 | *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK); |
77716755 KB |
98 | |
99 | if (!(track_flags & FL_FULLPATH) && match_len == len) | |
100 | match_len = last_slash = previous_slash; | |
101 | ||
e7c73053 | 102 | if (*ret_flags && match_len == cache->path.len) |
4856ff2a | 103 | return match_len; |
09c93066 KB |
104 | /* |
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 | */ | |
4856ff2a CB |
112 | *ret_flags = track_flags & FL_DIR; |
113 | if (*ret_flags && len == match_len) | |
114 | return match_len; | |
09c93066 | 115 | } |
c40641b7 | 116 | |
92604b46 KB |
117 | /* |
118 | * Okay, no match from the cache so far, so now we have to | |
119 | * check the rest of the path components. | |
120 | */ | |
4856ff2a | 121 | *ret_flags = FL_DIR; |
92604b46 | 122 | last_slash_dir = last_slash; |
e7c73053 KB |
123 | if (len > cache->path.len) |
124 | strbuf_grow(&cache->path, len - cache->path.len); | |
125 | while (match_len < len) { | |
92604b46 | 126 | do { |
e7c73053 | 127 | cache->path.buf[match_len] = name[match_len]; |
92604b46 | 128 | match_len++; |
e7c73053 KB |
129 | } while (match_len < len && name[match_len] != '/'); |
130 | if (match_len >= len && !(track_flags & FL_FULLPATH)) | |
92604b46 KB |
131 | break; |
132 | last_slash = match_len; | |
e7c73053 | 133 | cache->path.buf[last_slash] = '\0'; |
f859c846 | 134 | |
bad4a54f | 135 | if (last_slash <= prefix_len_stat_func) |
e7c73053 | 136 | ret = stat(cache->path.buf, &st); |
bad4a54f | 137 | else |
e7c73053 | 138 | ret = lstat(cache->path.buf, &st); |
bad4a54f KB |
139 | |
140 | if (ret) { | |
4856ff2a | 141 | *ret_flags = FL_LSTATERR; |
09c93066 | 142 | if (errno == ENOENT) |
4856ff2a | 143 | *ret_flags |= FL_NOENT; |
92604b46 KB |
144 | } else if (S_ISDIR(st.st_mode)) { |
145 | last_slash_dir = last_slash; | |
c40641b7 | 146 | continue; |
92604b46 | 147 | } else if (S_ISLNK(st.st_mode)) { |
4856ff2a | 148 | *ret_flags = FL_SYMLINK; |
92604b46 | 149 | } else { |
4856ff2a | 150 | *ret_flags = FL_ERR; |
f859c846 | 151 | } |
c40641b7 | 152 | break; |
f859c846 | 153 | } |
92604b46 KB |
154 | |
155 | /* | |
09c93066 KB |
156 | * 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! | |
92604b46 | 159 | */ |
4856ff2a | 160 | save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK); |
e7c73053 KB |
161 | if (save_flags && last_slash > 0) { |
162 | cache->path.buf[last_slash] = '\0'; | |
163 | cache->path.len = last_slash; | |
867f72bf | 164 | cache->flags = save_flags; |
e7c73053 | 165 | } else if ((track_flags & FL_DIR) && last_slash_dir > 0) { |
92604b46 KB |
166 | /* |
167 | * We have a separate test for the directory case, | |
09c93066 KB |
168 | * 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. | |
92604b46 KB |
172 | * |
173 | * But if we are allowed to track real directories, we | |
174 | * can still cache the path components before the last | |
09c93066 | 175 | * one (the found symlink or non-existing component). |
92604b46 | 176 | */ |
e7c73053 KB |
177 | cache->path.buf[last_slash_dir] = '\0'; |
178 | cache->path.len = last_slash_dir; | |
867f72bf | 179 | cache->flags = FL_DIR; |
92604b46 | 180 | } else { |
867f72bf | 181 | reset_lstat_cache(cache); |
92604b46 | 182 | } |
4856ff2a CB |
183 | return match_len; |
184 | } | |
185 | ||
186 | static 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; | |
92604b46 KB |
193 | } |
194 | ||
bad4a54f KB |
195 | #define USE_ONLY_LSTAT 0 |
196 | ||
b9fd2846 LT |
197 | /* |
198 | * Return non-zero if path 'name' has a leading symlink component | |
199 | */ | |
200 | int 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 | ||
92604b46 KB |
205 | /* |
206 | * Return non-zero if path 'name' has a leading symlink component | |
207 | */ | |
57199892 | 208 | int has_symlink_leading_path(const char *name, int len) |
92604b46 | 209 | { |
b9fd2846 | 210 | return threaded_has_symlink_leading_path(&default_cache, name, len); |
f859c846 | 211 | } |
09c93066 KB |
212 | |
213 | /* | |
f66caaf9 | 214 | * Return zero if path 'name' has a leading symlink component or |
09c93066 | 215 | * if some leading path component does not exists. |
f66caaf9 CB |
216 | * |
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. | |
09c93066 | 221 | */ |
f66caaf9 | 222 | int check_leading_path(const char *name, int len) |
09c93066 | 223 | { |
15438d5a JH |
224 | 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 | */ | |
72f3196a | 236 | static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len) |
15438d5a | 237 | { |
f66caaf9 CB |
238 | int flags; |
239 | int match_len = lstat_cache_matchlen(cache, name, len, &flags, | |
240 | FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT); | |
1d718a51 | 241 | if (flags & FL_NOENT) |
f66caaf9 CB |
242 | return 0; |
243 | else if (flags & FL_DIR) | |
244 | return -1; | |
245 | else | |
246 | return match_len; | |
09c93066 | 247 | } |
bad4a54f KB |
248 | |
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 | */ | |
57199892 | 256 | int has_dirs_only_path(const char *name, int len, int prefix_len) |
bad4a54f | 257 | { |
15438d5a JH |
258 | 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 | */ | |
72f3196a | 268 | static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len) |
15438d5a | 269 | { |
867f72bf | 270 | return lstat_cache(cache, name, len, |
bad4a54f KB |
271 | FL_DIR|FL_FULLPATH, prefix_len) & |
272 | FL_DIR; | |
273 | } | |
78478927 | 274 | |
e7c73053 | 275 | static struct strbuf removal = STRBUF_INIT; |
78478927 KB |
276 | |
277 | static void do_remove_scheduled_dirs(int new_len) | |
278 | { | |
279 | while (removal.len > new_len) { | |
e7c73053 KB |
280 | removal.buf[removal.len] = '\0'; |
281 | if (rmdir(removal.buf)) | |
78478927 KB |
282 | break; |
283 | do { | |
284 | removal.len--; | |
285 | } while (removal.len > new_len && | |
e7c73053 | 286 | removal.buf[removal.len] != '/'); |
78478927 KB |
287 | } |
288 | removal.len = new_len; | |
78478927 KB |
289 | } |
290 | ||
291 | void 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 = | |
e7c73053 | 296 | longest_path_match(name, len, removal.buf, removal.len, |
78478927 KB |
297 | &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 | */ | |
e7c73053 KB |
316 | if (match_len < last_slash) |
317 | strbuf_add(&removal, &name[match_len], last_slash - match_len); | |
78478927 KB |
318 | } |
319 | ||
320 | void remove_scheduled_dirs(void) | |
321 | { | |
322 | do_remove_scheduled_dirs(0); | |
78478927 | 323 | } |