]> git.ipfire.org Git - thirdparty/git.git/blame - symlinks.c
treewide: be explicit about dependence on strbuf.h
[thirdparty/git.git] / symlinks.c
CommitLineData
f859c846 1#include "cache.h"
f394e093 2#include "gettext.h"
e38da487 3#include "setup.h"
f859c846 4
fab78a0c
MT
5static int threaded_check_leading_path(struct cache_def *cache, const char *name,
6 int len, int warn_on_lstat_err);
72f3196a
JH
7static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len);
8
92604b46
KB
9/*
10 * Returns the length (on a path component basis) of the longest
148bc06b 11 * common prefix match of 'name_a' and 'name_b'.
92604b46 12 */
148bc06b
KB
13static int longest_path_match(const char *name_a, int len_a,
14 const char *name_b, int len_b,
15 int *previous_slash)
c40641b7 16{
aeabab5c 17 int max_len, match_len = 0, match_len_prev = 0, i = 0;
92604b46 18
148bc06b
KB
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] == '/') {
aeabab5c 22 match_len_prev = match_len;
92604b46 23 match_len = i;
aeabab5c 24 }
92604b46
KB
25 i++;
26 }
60b458b7 27 /*
148bc06b
KB
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?
60b458b7 30 */
148bc06b
KB
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))) {
aeabab5c 34 match_len_prev = match_len;
60b458b7 35 match_len = i;
aeabab5c
KB
36 }
37 *previous_slash = match_len_prev;
92604b46 38 return match_len;
c40641b7
LT
39}
40
e7c73053 41static struct cache_def default_cache = CACHE_DEF_INIT;
148bc06b 42
867f72bf 43static inline void reset_lstat_cache(struct cache_def *cache)
c40641b7 44{
e7c73053 45 strbuf_reset(&cache->path);
867f72bf 46 cache->flags = 0;
60b458b7
KB
47 /*
48 * The track_flags and prefix_len_stat_func members is only
49 * set by the safeguard rule inside lstat_cache()
50 */
c40641b7
LT
51}
52
92604b46 53#define FL_DIR (1 << 0)
09c93066
KB
54#define FL_NOENT (1 << 1)
55#define FL_SYMLINK (1 << 2)
56#define FL_LSTATERR (1 << 3)
57#define FL_ERR (1 << 4)
bad4a54f 58#define FL_FULLPATH (1 << 5)
92604b46
KB
59
60/*
61 * Check if name 'name' of length 'len' has a symlink leading
09c93066 62 * component, or if the directory exists and is real, or not.
92604b46
KB
63 *
64 * To speed up the check, some information is allowed to be cached.
bad4a54f
KB
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.
92604b46 71 */
4856ff2a
CB
72static 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)
c40641b7 76{
aeabab5c 77 int match_len, last_slash, last_slash_dir, previous_slash;
fab78a0c 78 int save_flags, ret, saved_errno = 0;
c40641b7 79 struct stat st;
f859c846 80
867f72bf
LT
81 if (cache->track_flags != track_flags ||
82 cache->prefix_len_stat_func != prefix_len_stat_func) {
09c93066 83 /*
60b458b7
KB
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.
09c93066 87 */
867f72bf
LT
88 reset_lstat_cache(cache);
89 cache->track_flags = track_flags;
90 cache->prefix_len_stat_func = prefix_len_stat_func;
09c93066
KB
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 */
aeabab5c 97 match_len = last_slash =
e7c73053
KB
98 longest_path_match(name, len, cache->path.buf,
99 cache->path.len, &previous_slash);
4856ff2a 100 *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
77716755
KB
101
102 if (!(track_flags & FL_FULLPATH) && match_len == len)
103 match_len = last_slash = previous_slash;
104
e7c73053 105 if (*ret_flags && match_len == cache->path.len)
4856ff2a 106 return match_len;
09c93066
KB
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 */
4856ff2a
CB
115 *ret_flags = track_flags & FL_DIR;
116 if (*ret_flags && len == match_len)
117 return match_len;
09c93066 118 }
c40641b7 119
92604b46
KB
120 /*
121 * Okay, no match from the cache so far, so now we have to
122 * check the rest of the path components.
123 */
4856ff2a 124 *ret_flags = FL_DIR;
92604b46 125 last_slash_dir = last_slash;
e7c73053
KB
126 if (len > cache->path.len)
127 strbuf_grow(&cache->path, len - cache->path.len);
128 while (match_len < len) {
92604b46 129 do {
e7c73053 130 cache->path.buf[match_len] = name[match_len];
92604b46 131 match_len++;
e7c73053
KB
132 } while (match_len < len && name[match_len] != '/');
133 if (match_len >= len && !(track_flags & FL_FULLPATH))
92604b46
KB
134 break;
135 last_slash = match_len;
e7c73053 136 cache->path.buf[last_slash] = '\0';
f859c846 137
bad4a54f 138 if (last_slash <= prefix_len_stat_func)
e7c73053 139 ret = stat(cache->path.buf, &st);
bad4a54f 140 else
e7c73053 141 ret = lstat(cache->path.buf, &st);
bad4a54f
KB
142
143 if (ret) {
4856ff2a 144 *ret_flags = FL_LSTATERR;
fab78a0c 145 saved_errno = errno;
09c93066 146 if (errno == ENOENT)
4856ff2a 147 *ret_flags |= FL_NOENT;
92604b46
KB
148 } else if (S_ISDIR(st.st_mode)) {
149 last_slash_dir = last_slash;
c40641b7 150 continue;
92604b46 151 } else if (S_ISLNK(st.st_mode)) {
4856ff2a 152 *ret_flags = FL_SYMLINK;
92604b46 153 } else {
4856ff2a 154 *ret_flags = FL_ERR;
f859c846 155 }
c40641b7 156 break;
f859c846 157 }
92604b46
KB
158
159 /*
09c93066
KB
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!
92604b46 163 */
4856ff2a 164 save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
e7c73053
KB
165 if (save_flags && last_slash > 0) {
166 cache->path.buf[last_slash] = '\0';
167 cache->path.len = last_slash;
867f72bf 168 cache->flags = save_flags;
e7c73053 169 } else if ((track_flags & FL_DIR) && last_slash_dir > 0) {
92604b46
KB
170 /*
171 * We have a separate test for the directory case,
09c93066
KB
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.
92604b46
KB
176 *
177 * But if we are allowed to track real directories, we
178 * can still cache the path components before the last
09c93066 179 * one (the found symlink or non-existing component).
92604b46 180 */
e7c73053
KB
181 cache->path.buf[last_slash_dir] = '\0';
182 cache->path.len = last_slash_dir;
867f72bf 183 cache->flags = FL_DIR;
92604b46 184 } else {
867f72bf 185 reset_lstat_cache(cache);
92604b46 186 }
fab78a0c
MT
187 if (saved_errno)
188 errno = saved_errno;
4856ff2a
CB
189 return match_len;
190}
191
192static 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;
92604b46
KB
199}
200
bad4a54f
KB
201#define USE_ONLY_LSTAT 0
202
b9fd2846
LT
203/*
204 * Return non-zero if path 'name' has a leading symlink component
205 */
206int 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
57199892 211int has_symlink_leading_path(const char *name, int len)
92604b46 212{
b9fd2846 213 return threaded_has_symlink_leading_path(&default_cache, name, len);
f859c846 214}
09c93066 215
fab78a0c 216int check_leading_path(const char *name, int len, int warn_on_lstat_err)
09c93066 217{
fab78a0c
MT
218 return threaded_check_leading_path(&default_cache, name, len,
219 warn_on_lstat_err);
15438d5a
JH
220}
221
222/*
462b4e8d 223 * Return zero if some leading path component of 'name' does not exist.
15438d5a
JH
224 *
225 * Return -1 if leading path exists and is a directory.
226 *
462b4e8d 227 * Return the length of a leading component if it either exists but it's not a
fab78a0c
MT
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.
15438d5a 230 */
fab78a0c
MT
231static int threaded_check_leading_path(struct cache_def *cache, const char *name,
232 int len, int warn_on_lstat_err)
15438d5a 233{
f66caaf9
CB
234 int flags;
235 int match_len = lstat_cache_matchlen(cache, name, len, &flags,
236 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
fab78a0c
MT
237 int saved_errno = errno;
238
1d718a51 239 if (flags & FL_NOENT)
f66caaf9
CB
240 return 0;
241 else if (flags & FL_DIR)
242 return -1;
fab78a0c
MT
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;
09c93066 250}
bad4a54f 251
57199892 252int has_dirs_only_path(const char *name, int len, int prefix_len)
bad4a54f 253{
15438d5a
JH
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 */
72f3196a 264static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len)
15438d5a 265{
684dd4c2
MT
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 */
867f72bf 273 return lstat_cache(cache, name, len,
bad4a54f
KB
274 FL_DIR|FL_FULLPATH, prefix_len) &
275 FL_DIR;
276}
78478927 277
e7c73053 278static struct strbuf removal = STRBUF_INIT;
78478927
KB
279
280static void do_remove_scheduled_dirs(int new_len)
281{
282 while (removal.len > new_len) {
e7c73053 283 removal.buf[removal.len] = '\0';
00fcce28
EN
284 if ((startup_info->original_cwd &&
285 !strcmp(removal.buf, startup_info->original_cwd)) ||
286 rmdir(removal.buf))
78478927
KB
287 break;
288 do {
289 removal.len--;
290 } while (removal.len > new_len &&
e7c73053 291 removal.buf[removal.len] != '/');
78478927
KB
292 }
293 removal.len = new_len;
78478927
KB
294}
295
296void schedule_dir_for_removal(const char *name, int len)
297{
298 int match_len, last_slash, i, previous_slash;
299
00fcce28
EN
300 if (startup_info->original_cwd &&
301 !strcmp(name, startup_info->original_cwd))
302 return; /* Do not remove the current working directory */
303
78478927 304 match_len = last_slash = i =
e7c73053 305 longest_path_match(name, len, removal.buf, removal.len,
78478927
KB
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 */
e7c73053
KB
325 if (match_len < last_slash)
326 strbuf_add(&removal, &name[match_len], last_slash - match_len);
78478927
KB
327}
328
329void remove_scheduled_dirs(void)
330{
331 do_remove_scheduled_dirs(0);
78478927 332}
684dd4c2
MT
333
334void invalidate_lstat_cache(void)
335{
336 reset_lstat_cache(&default_cache);
337}
338
339#undef rmdir
340int 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}