]> git.ipfire.org Git - thirdparty/git.git/blob - symlinks.c
setup.h: move declarations for setup.c functions from cache.h
[thirdparty/git.git] / symlinks.c
1 #include "cache.h"
2 #include "gettext.h"
3 #include "setup.h"
4
5 static int threaded_check_leading_path(struct cache_def *cache, const char *name,
6 int len, int warn_on_lstat_err);
7 static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len);
8
9 /*
10 * Returns the length (on a path component basis) of the longest
11 * common prefix match of 'name_a' and 'name_b'.
12 */
13 static int longest_path_match(const char *name_a, int len_a,
14 const char *name_b, int len_b,
15 int *previous_slash)
16 {
17 int max_len, match_len = 0, match_len_prev = 0, i = 0;
18
19 max_len = len_a < len_b ? len_a : len_b;
20 while (i < max_len && name_a[i] == name_b[i]) {
21 if (name_a[i] == '/') {
22 match_len_prev = match_len;
23 match_len = i;
24 }
25 i++;
26 }
27 /*
28 * Is 'name_b' a substring of 'name_a', the other way around,
29 * or is 'name_a' and 'name_b' the exact same string?
30 */
31 if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||
32 (len_a < len_b && name_b[len_a] == '/') ||
33 (len_a == len_b))) {
34 match_len_prev = match_len;
35 match_len = i;
36 }
37 *previous_slash = match_len_prev;
38 return match_len;
39 }
40
41 static struct cache_def default_cache = CACHE_DEF_INIT;
42
43 static inline void reset_lstat_cache(struct cache_def *cache)
44 {
45 strbuf_reset(&cache->path);
46 cache->flags = 0;
47 /*
48 * The track_flags and prefix_len_stat_func members is only
49 * set by the safeguard rule inside lstat_cache()
50 */
51 }
52
53 #define FL_DIR (1 << 0)
54 #define FL_NOENT (1 << 1)
55 #define FL_SYMLINK (1 << 2)
56 #define FL_LSTATERR (1 << 3)
57 #define FL_ERR (1 << 4)
58 #define FL_FULLPATH (1 << 5)
59
60 /*
61 * Check if name 'name' of length 'len' has a symlink leading
62 * component, or if the directory exists and is real, or not.
63 *
64 * To speed up the check, some information is allowed to be cached.
65 * This can be indicated by the 'track_flags' argument, which also can
66 * be used to indicate that we should check the full path.
67 *
68 * The 'prefix_len_stat_func' parameter can be used to set the length
69 * of the prefix, where the cache should use the stat() function
70 * instead of the lstat() function to test each path component.
71 */
72 static int lstat_cache_matchlen(struct cache_def *cache,
73 const char *name, int len,
74 int *ret_flags, int track_flags,
75 int prefix_len_stat_func)
76 {
77 int match_len, last_slash, last_slash_dir, previous_slash;
78 int save_flags, ret, saved_errno = 0;
79 struct stat st;
80
81 if (cache->track_flags != track_flags ||
82 cache->prefix_len_stat_func != prefix_len_stat_func) {
83 /*
84 * As a safeguard rule we clear the cache if the
85 * values of track_flags and/or prefix_len_stat_func
86 * does not match with the last supplied values.
87 */
88 reset_lstat_cache(cache);
89 cache->track_flags = track_flags;
90 cache->prefix_len_stat_func = prefix_len_stat_func;
91 match_len = last_slash = 0;
92 } else {
93 /*
94 * Check to see if we have a match from the cache for
95 * the 2 "excluding" path types.
96 */
97 match_len = last_slash =
98 longest_path_match(name, len, cache->path.buf,
99 cache->path.len, &previous_slash);
100 *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
101
102 if (!(track_flags & FL_FULLPATH) && match_len == len)
103 match_len = last_slash = previous_slash;
104
105 if (*ret_flags && match_len == cache->path.len)
106 return match_len;
107 /*
108 * If we now have match_len > 0, we would know that
109 * the matched part will always be a directory.
110 *
111 * Also, if we are tracking directories and 'name' is
112 * a substring of the cache on a path component basis,
113 * we can return immediately.
114 */
115 *ret_flags = track_flags & FL_DIR;
116 if (*ret_flags && len == match_len)
117 return match_len;
118 }
119
120 /*
121 * Okay, no match from the cache so far, so now we have to
122 * check the rest of the path components.
123 */
124 *ret_flags = FL_DIR;
125 last_slash_dir = last_slash;
126 if (len > cache->path.len)
127 strbuf_grow(&cache->path, len - cache->path.len);
128 while (match_len < len) {
129 do {
130 cache->path.buf[match_len] = name[match_len];
131 match_len++;
132 } while (match_len < len && name[match_len] != '/');
133 if (match_len >= len && !(track_flags & FL_FULLPATH))
134 break;
135 last_slash = match_len;
136 cache->path.buf[last_slash] = '\0';
137
138 if (last_slash <= prefix_len_stat_func)
139 ret = stat(cache->path.buf, &st);
140 else
141 ret = lstat(cache->path.buf, &st);
142
143 if (ret) {
144 *ret_flags = FL_LSTATERR;
145 saved_errno = errno;
146 if (errno == ENOENT)
147 *ret_flags |= FL_NOENT;
148 } else if (S_ISDIR(st.st_mode)) {
149 last_slash_dir = last_slash;
150 continue;
151 } else if (S_ISLNK(st.st_mode)) {
152 *ret_flags = FL_SYMLINK;
153 } else {
154 *ret_flags = FL_ERR;
155 }
156 break;
157 }
158
159 /*
160 * At the end update the cache. Note that max 3 different
161 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
162 * for the moment!
163 */
164 save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
165 if (save_flags && last_slash > 0) {
166 cache->path.buf[last_slash] = '\0';
167 cache->path.len = last_slash;
168 cache->flags = save_flags;
169 } else if ((track_flags & FL_DIR) && last_slash_dir > 0) {
170 /*
171 * We have a separate test for the directory case,
172 * since it could be that we have found a symlink or a
173 * non-existing directory and the track_flags says
174 * that we cannot cache this fact, so the cache would
175 * then have been left empty in this case.
176 *
177 * But if we are allowed to track real directories, we
178 * can still cache the path components before the last
179 * one (the found symlink or non-existing component).
180 */
181 cache->path.buf[last_slash_dir] = '\0';
182 cache->path.len = last_slash_dir;
183 cache->flags = FL_DIR;
184 } else {
185 reset_lstat_cache(cache);
186 }
187 if (saved_errno)
188 errno = saved_errno;
189 return match_len;
190 }
191
192 static int lstat_cache(struct cache_def *cache, const char *name, int len,
193 int track_flags, int prefix_len_stat_func)
194 {
195 int flags;
196 (void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,
197 prefix_len_stat_func);
198 return flags;
199 }
200
201 #define USE_ONLY_LSTAT 0
202
203 /*
204 * Return non-zero if path 'name' has a leading symlink component
205 */
206 int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len)
207 {
208 return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK;
209 }
210
211 int has_symlink_leading_path(const char *name, int len)
212 {
213 return threaded_has_symlink_leading_path(&default_cache, name, len);
214 }
215
216 int check_leading_path(const char *name, int len, int warn_on_lstat_err)
217 {
218 return threaded_check_leading_path(&default_cache, name, len,
219 warn_on_lstat_err);
220 }
221
222 /*
223 * Return zero if some leading path component of 'name' does not exist.
224 *
225 * Return -1 if leading path exists and is a directory.
226 *
227 * Return the length of a leading component if it either exists but it's not a
228 * directory, or if we were unable to lstat() it. If warn_on_lstat_err is true,
229 * also emit a warning for this error.
230 */
231 static int threaded_check_leading_path(struct cache_def *cache, const char *name,
232 int len, int warn_on_lstat_err)
233 {
234 int flags;
235 int match_len = lstat_cache_matchlen(cache, name, len, &flags,
236 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
237 int saved_errno = errno;
238
239 if (flags & FL_NOENT)
240 return 0;
241 else if (flags & FL_DIR)
242 return -1;
243 if (warn_on_lstat_err && (flags & FL_LSTATERR)) {
244 char *path = xmemdupz(name, match_len);
245 errno = saved_errno;
246 warning_errno(_("failed to lstat '%s'"), path);
247 free(path);
248 }
249 return match_len;
250 }
251
252 int has_dirs_only_path(const char *name, int len, int prefix_len)
253 {
254 return threaded_has_dirs_only_path(&default_cache, name, len, prefix_len);
255 }
256
257 /*
258 * Return non-zero if all path components of 'name' exists as a
259 * directory. If prefix_len > 0, we will test with the stat()
260 * function instead of the lstat() function for a prefix length of
261 * 'prefix_len', thus we then allow for symlinks in the prefix part as
262 * long as those points to real existing directories.
263 */
264 static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len)
265 {
266 /*
267 * Note: this function is used by the checkout machinery, which also
268 * takes care to properly reset the cache when it performs an operation
269 * that would leave the cache outdated. If this function starts caching
270 * anything else besides FL_DIR, remember to also invalidate the cache
271 * when creating or deleting paths that might be in the cache.
272 */
273 return lstat_cache(cache, name, len,
274 FL_DIR|FL_FULLPATH, prefix_len) &
275 FL_DIR;
276 }
277
278 static struct strbuf removal = STRBUF_INIT;
279
280 static void do_remove_scheduled_dirs(int new_len)
281 {
282 while (removal.len > new_len) {
283 removal.buf[removal.len] = '\0';
284 if ((startup_info->original_cwd &&
285 !strcmp(removal.buf, startup_info->original_cwd)) ||
286 rmdir(removal.buf))
287 break;
288 do {
289 removal.len--;
290 } while (removal.len > new_len &&
291 removal.buf[removal.len] != '/');
292 }
293 removal.len = new_len;
294 }
295
296 void schedule_dir_for_removal(const char *name, int len)
297 {
298 int match_len, last_slash, i, previous_slash;
299
300 if (startup_info->original_cwd &&
301 !strcmp(name, startup_info->original_cwd))
302 return; /* Do not remove the current working directory */
303
304 match_len = last_slash = i =
305 longest_path_match(name, len, removal.buf, removal.len,
306 &previous_slash);
307 /* Find last slash inside 'name' */
308 while (i < len) {
309 if (name[i] == '/')
310 last_slash = i;
311 i++;
312 }
313
314 /*
315 * If we are about to go down the directory tree, we check if
316 * we must first go upwards the tree, such that we then can
317 * remove possible empty directories as we go upwards.
318 */
319 if (match_len < last_slash && match_len < removal.len)
320 do_remove_scheduled_dirs(match_len);
321 /*
322 * If we go deeper down the directory tree, we only need to
323 * save the new path components as we go down.
324 */
325 if (match_len < last_slash)
326 strbuf_add(&removal, &name[match_len], last_slash - match_len);
327 }
328
329 void remove_scheduled_dirs(void)
330 {
331 do_remove_scheduled_dirs(0);
332 }
333
334 void invalidate_lstat_cache(void)
335 {
336 reset_lstat_cache(&default_cache);
337 }
338
339 #undef rmdir
340 int lstat_cache_aware_rmdir(const char *path)
341 {
342 /* Any change in this function must be made also in `mingw_rmdir()` */
343 int ret = rmdir(path);
344
345 if (!ret)
346 invalidate_lstat_cache();
347
348 return ret;
349 }