blob: b29e340c2da43e9ac35adcda05322b1e068017ca [file] [log] [blame]
Elijah Newrencb2a5132023-04-22 20:17:09 +00001#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00002#include "gettext.h"
Elijah Newrene38da482023-03-21 06:26:05 +00003#include "setup.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +00004#include "symlinks.h"
Junio C Hamanof859c842007-05-11 22:11:07 -07005
Matheus Tavaresfab78a02021-03-18 15:43:47 -03006static int threaded_check_leading_path(struct cache_def *cache, const char *name,
7 int len, int warn_on_lstat_err);
Junio C Hamano72f31962012-09-15 22:38:28 -07008static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len);
9
Kjetil Barvik148bc062009-02-09 21:54:05 +010010/*
11 * Returns the length (on a path component basis) of the longest
12 * common prefix match of 'name_a' and 'name_b'.
13 */
14static int longest_path_match(const char *name_a, int len_a,
15 const char *name_b, int len_b,
16 int *previous_slash)
17{
18 int max_len, match_len = 0, match_len_prev = 0, i = 0;
19
20 max_len = len_a < len_b ? len_a : len_b;
21 while (i < max_len && name_a[i] == name_b[i]) {
22 if (name_a[i] == '/') {
23 match_len_prev = match_len;
24 match_len = i;
25 }
26 i++;
27 }
28 /*
29 * Is 'name_b' a substring of 'name_a', the other way around,
30 * or is 'name_a' and 'name_b' the exact same string?
31 */
32 if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||
33 (len_a < len_b && name_b[len_a] == '/') ||
34 (len_a == len_b))) {
35 match_len_prev = match_len;
36 match_len = i;
37 }
38 *previous_slash = match_len_prev;
39 return match_len;
40}
41
Karsten Bleese7c73052014-07-05 00:41:46 +020042static struct cache_def default_cache = CACHE_DEF_INIT;
Junio C Hamanof859c842007-05-11 22:11:07 -070043
Linus Torvalds867f72b2009-07-09 13:23:59 -070044static inline void reset_lstat_cache(struct cache_def *cache)
Linus Torvaldsc40641b2008-05-09 09:21:07 -070045{
Karsten Bleese7c73052014-07-05 00:41:46 +020046 strbuf_reset(&cache->path);
Linus Torvalds867f72b2009-07-09 13:23:59 -070047 cache->flags = 0;
Kjetil Barvik60b458b2009-02-09 21:54:04 +010048 /*
49 * The track_flags and prefix_len_stat_func members is only
50 * set by the safeguard rule inside lstat_cache()
51 */
Kjetil Barvik92604b42009-01-18 16:14:50 +010052}
53
54#define FL_DIR (1 << 0)
Kjetil Barvik09c93062009-01-18 16:14:51 +010055#define FL_NOENT (1 << 1)
56#define FL_SYMLINK (1 << 2)
57#define FL_LSTATERR (1 << 3)
58#define FL_ERR (1 << 4)
Kjetil Barvikbad4a542009-01-18 16:14:52 +010059#define FL_FULLPATH (1 << 5)
Kjetil Barvik92604b42009-01-18 16:14:50 +010060
61/*
62 * Check if name 'name' of length 'len' has a symlink leading
Kjetil Barvik09c93062009-01-18 16:14:51 +010063 * component, or if the directory exists and is real, or not.
Kjetil Barvik92604b42009-01-18 16:14:50 +010064 *
65 * To speed up the check, some information is allowed to be cached.
Kjetil Barvikbad4a542009-01-18 16:14:52 +010066 * This can be indicated by the 'track_flags' argument, which also can
67 * be used to indicate that we should check the full path.
68 *
69 * The 'prefix_len_stat_func' parameter can be used to set the length
70 * of the prefix, where the cache should use the stat() function
71 * instead of the lstat() function to test each path component.
Kjetil Barvik92604b42009-01-18 16:14:50 +010072 */
Clemens Buchacher4856ff22010-10-09 15:52:59 +020073static int lstat_cache_matchlen(struct cache_def *cache,
74 const char *name, int len,
75 int *ret_flags, int track_flags,
76 int prefix_len_stat_func)
Kjetil Barvik92604b42009-01-18 16:14:50 +010077{
Kjetil Barvikaeabab52009-01-18 16:14:53 +010078 int match_len, last_slash, last_slash_dir, previous_slash;
Matheus Tavaresfab78a02021-03-18 15:43:47 -030079 int save_flags, ret, saved_errno = 0;
Linus Torvaldsc40641b2008-05-09 09:21:07 -070080 struct stat st;
Junio C Hamanof859c842007-05-11 22:11:07 -070081
Linus Torvalds867f72b2009-07-09 13:23:59 -070082 if (cache->track_flags != track_flags ||
83 cache->prefix_len_stat_func != prefix_len_stat_func) {
Kjetil Barvik09c93062009-01-18 16:14:51 +010084 /*
Kjetil Barvik60b458b2009-02-09 21:54:04 +010085 * As a safeguard rule we clear the cache if the
86 * values of track_flags and/or prefix_len_stat_func
87 * does not match with the last supplied values.
Kjetil Barvik09c93062009-01-18 16:14:51 +010088 */
Linus Torvalds867f72b2009-07-09 13:23:59 -070089 reset_lstat_cache(cache);
90 cache->track_flags = track_flags;
91 cache->prefix_len_stat_func = prefix_len_stat_func;
Kjetil Barvik09c93062009-01-18 16:14:51 +010092 match_len = last_slash = 0;
93 } else {
94 /*
95 * Check to see if we have a match from the cache for
96 * the 2 "excluding" path types.
97 */
Kjetil Barvikaeabab52009-01-18 16:14:53 +010098 match_len = last_slash =
Karsten Bleese7c73052014-07-05 00:41:46 +020099 longest_path_match(name, len, cache->path.buf,
100 cache->path.len, &previous_slash);
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200101 *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
Kjetil Barvik77716752009-06-14 15:08:28 +0200102
103 if (!(track_flags & FL_FULLPATH) && match_len == len)
104 match_len = last_slash = previous_slash;
105
Karsten Bleese7c73052014-07-05 00:41:46 +0200106 if (*ret_flags && match_len == cache->path.len)
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200107 return match_len;
Kjetil Barvik09c93062009-01-18 16:14:51 +0100108 /*
109 * If we now have match_len > 0, we would know that
110 * the matched part will always be a directory.
111 *
112 * Also, if we are tracking directories and 'name' is
113 * a substring of the cache on a path component basis,
114 * we can return immediately.
115 */
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200116 *ret_flags = track_flags & FL_DIR;
117 if (*ret_flags && len == match_len)
118 return match_len;
Kjetil Barvik09c93062009-01-18 16:14:51 +0100119 }
Linus Torvaldsc40641b2008-05-09 09:21:07 -0700120
121 /*
Kjetil Barvik92604b42009-01-18 16:14:50 +0100122 * Okay, no match from the cache so far, so now we have to
123 * check the rest of the path components.
Linus Torvaldsc40641b2008-05-09 09:21:07 -0700124 */
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200125 *ret_flags = FL_DIR;
Kjetil Barvik92604b42009-01-18 16:14:50 +0100126 last_slash_dir = last_slash;
Karsten Bleese7c73052014-07-05 00:41:46 +0200127 if (len > cache->path.len)
128 strbuf_grow(&cache->path, len - cache->path.len);
129 while (match_len < len) {
Kjetil Barvik92604b42009-01-18 16:14:50 +0100130 do {
Karsten Bleese7c73052014-07-05 00:41:46 +0200131 cache->path.buf[match_len] = name[match_len];
Kjetil Barvik92604b42009-01-18 16:14:50 +0100132 match_len++;
Karsten Bleese7c73052014-07-05 00:41:46 +0200133 } while (match_len < len && name[match_len] != '/');
134 if (match_len >= len && !(track_flags & FL_FULLPATH))
Kjetil Barvik92604b42009-01-18 16:14:50 +0100135 break;
136 last_slash = match_len;
Karsten Bleese7c73052014-07-05 00:41:46 +0200137 cache->path.buf[last_slash] = '\0';
Linus Torvaldsc40641b2008-05-09 09:21:07 -0700138
Kjetil Barvikbad4a542009-01-18 16:14:52 +0100139 if (last_slash <= prefix_len_stat_func)
Karsten Bleese7c73052014-07-05 00:41:46 +0200140 ret = stat(cache->path.buf, &st);
Kjetil Barvikbad4a542009-01-18 16:14:52 +0100141 else
Karsten Bleese7c73052014-07-05 00:41:46 +0200142 ret = lstat(cache->path.buf, &st);
Kjetil Barvikbad4a542009-01-18 16:14:52 +0100143
144 if (ret) {
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200145 *ret_flags = FL_LSTATERR;
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300146 saved_errno = errno;
Kjetil Barvik09c93062009-01-18 16:14:51 +0100147 if (errno == ENOENT)
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200148 *ret_flags |= FL_NOENT;
Kjetil Barvik92604b42009-01-18 16:14:50 +0100149 } else if (S_ISDIR(st.st_mode)) {
150 last_slash_dir = last_slash;
Linus Torvaldsc40641b2008-05-09 09:21:07 -0700151 continue;
Kjetil Barvik92604b42009-01-18 16:14:50 +0100152 } else if (S_ISLNK(st.st_mode)) {
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200153 *ret_flags = FL_SYMLINK;
Kjetil Barvik92604b42009-01-18 16:14:50 +0100154 } else {
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200155 *ret_flags = FL_ERR;
Junio C Hamanof859c842007-05-11 22:11:07 -0700156 }
Linus Torvaldsc40641b2008-05-09 09:21:07 -0700157 break;
Junio C Hamanof859c842007-05-11 22:11:07 -0700158 }
Kjetil Barvik92604b42009-01-18 16:14:50 +0100159
160 /*
Kjetil Barvik09c93062009-01-18 16:14:51 +0100161 * At the end update the cache. Note that max 3 different
162 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
163 * for the moment!
Kjetil Barvik92604b42009-01-18 16:14:50 +0100164 */
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200165 save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
Karsten Bleese7c73052014-07-05 00:41:46 +0200166 if (save_flags && last_slash > 0) {
167 cache->path.buf[last_slash] = '\0';
168 cache->path.len = last_slash;
Linus Torvalds867f72b2009-07-09 13:23:59 -0700169 cache->flags = save_flags;
Karsten Bleese7c73052014-07-05 00:41:46 +0200170 } else if ((track_flags & FL_DIR) && last_slash_dir > 0) {
Kjetil Barvik92604b42009-01-18 16:14:50 +0100171 /*
172 * We have a separate test for the directory case,
Kjetil Barvik09c93062009-01-18 16:14:51 +0100173 * since it could be that we have found a symlink or a
174 * non-existing directory and the track_flags says
175 * that we cannot cache this fact, so the cache would
176 * then have been left empty in this case.
Kjetil Barvik92604b42009-01-18 16:14:50 +0100177 *
178 * But if we are allowed to track real directories, we
179 * can still cache the path components before the last
Kjetil Barvik09c93062009-01-18 16:14:51 +0100180 * one (the found symlink or non-existing component).
Kjetil Barvik92604b42009-01-18 16:14:50 +0100181 */
Karsten Bleese7c73052014-07-05 00:41:46 +0200182 cache->path.buf[last_slash_dir] = '\0';
183 cache->path.len = last_slash_dir;
Linus Torvalds867f72b2009-07-09 13:23:59 -0700184 cache->flags = FL_DIR;
Kjetil Barvik92604b42009-01-18 16:14:50 +0100185 } else {
Linus Torvalds867f72b2009-07-09 13:23:59 -0700186 reset_lstat_cache(cache);
Kjetil Barvik92604b42009-01-18 16:14:50 +0100187 }
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300188 if (saved_errno)
189 errno = saved_errno;
Clemens Buchacher4856ff22010-10-09 15:52:59 +0200190 return match_len;
191}
192
193static int lstat_cache(struct cache_def *cache, const char *name, int len,
194 int track_flags, int prefix_len_stat_func)
195{
196 int flags;
197 (void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,
198 prefix_len_stat_func);
199 return flags;
Kjetil Barvik92604b42009-01-18 16:14:50 +0100200}
201
Kjetil Barvikbad4a542009-01-18 16:14:52 +0100202#define USE_ONLY_LSTAT 0
203
Kjetil Barvik92604b42009-01-18 16:14:50 +0100204/*
205 * Return non-zero if path 'name' has a leading symlink component
206 */
Linus Torvaldsb9fd2842009-07-09 13:35:31 -0700207int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len)
208{
209 return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK;
210}
211
Kjetil Barvik57199892009-02-09 21:54:06 +0100212int has_symlink_leading_path(const char *name, int len)
Kjetil Barvik92604b42009-01-18 16:14:50 +0100213{
Linus Torvaldsb9fd2842009-07-09 13:35:31 -0700214 return threaded_has_symlink_leading_path(&default_cache, name, len);
Junio C Hamanof859c842007-05-11 22:11:07 -0700215}
Kjetil Barvik09c93062009-01-18 16:14:51 +0100216
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300217int check_leading_path(const char *name, int len, int warn_on_lstat_err)
Kjetil Barvik09c93062009-01-18 16:14:51 +0100218{
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300219 return threaded_check_leading_path(&default_cache, name, len,
220 warn_on_lstat_err);
Jared Hance15438d52012-03-02 21:31:15 -0500221}
222
223/*
Matheus Tavares462b4e82021-03-18 15:43:46 -0300224 * Return zero if some leading path component of 'name' does not exist.
Jared Hance15438d52012-03-02 21:31:15 -0500225 *
226 * Return -1 if leading path exists and is a directory.
227 *
Matheus Tavares462b4e82021-03-18 15:43:46 -0300228 * Return the length of a leading component if it either exists but it's not a
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300229 * directory, or if we were unable to lstat() it. If warn_on_lstat_err is true,
230 * also emit a warning for this error.
Jared Hance15438d52012-03-02 21:31:15 -0500231 */
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300232static int threaded_check_leading_path(struct cache_def *cache, const char *name,
233 int len, int warn_on_lstat_err)
Jared Hance15438d52012-03-02 21:31:15 -0500234{
Clemens Buchacherf66caaf2010-10-09 15:53:00 +0200235 int flags;
236 int match_len = lstat_cache_matchlen(cache, name, len, &flags,
237 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300238 int saved_errno = errno;
239
Clemens Buchacher1d718a52011-02-20 13:13:43 +0100240 if (flags & FL_NOENT)
Clemens Buchacherf66caaf2010-10-09 15:53:00 +0200241 return 0;
242 else if (flags & FL_DIR)
243 return -1;
Matheus Tavaresfab78a02021-03-18 15:43:47 -0300244 if (warn_on_lstat_err && (flags & FL_LSTATERR)) {
245 char *path = xmemdupz(name, match_len);
246 errno = saved_errno;
247 warning_errno(_("failed to lstat '%s'"), path);
248 free(path);
249 }
250 return match_len;
Kjetil Barvik09c93062009-01-18 16:14:51 +0100251}
Kjetil Barvikbad4a542009-01-18 16:14:52 +0100252
Kjetil Barvik57199892009-02-09 21:54:06 +0100253int has_dirs_only_path(const char *name, int len, int prefix_len)
Kjetil Barvikbad4a542009-01-18 16:14:52 +0100254{
Jared Hance15438d52012-03-02 21:31:15 -0500255 return threaded_has_dirs_only_path(&default_cache, name, len, prefix_len);
256}
257
258/*
259 * Return non-zero if all path components of 'name' exists as a
260 * directory. If prefix_len > 0, we will test with the stat()
261 * function instead of the lstat() function for a prefix length of
262 * 'prefix_len', thus we then allow for symlinks in the prefix part as
263 * long as those points to real existing directories.
264 */
Junio C Hamano72f31962012-09-15 22:38:28 -0700265static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len)
Jared Hance15438d52012-03-02 21:31:15 -0500266{
Matheus Tavares684dd4c2020-12-10 10:27:55 -0300267 /*
268 * Note: this function is used by the checkout machinery, which also
269 * takes care to properly reset the cache when it performs an operation
270 * that would leave the cache outdated. If this function starts caching
271 * anything else besides FL_DIR, remember to also invalidate the cache
272 * when creating or deleting paths that might be in the cache.
273 */
Linus Torvalds867f72b2009-07-09 13:23:59 -0700274 return lstat_cache(cache, name, len,
Kjetil Barvikbad4a542009-01-18 16:14:52 +0100275 FL_DIR|FL_FULLPATH, prefix_len) &
276 FL_DIR;
277}
Kjetil Barvik78478922009-02-09 21:54:07 +0100278
Karsten Bleese7c73052014-07-05 00:41:46 +0200279static struct strbuf removal = STRBUF_INIT;
Kjetil Barvik78478922009-02-09 21:54:07 +0100280
281static void do_remove_scheduled_dirs(int new_len)
282{
283 while (removal.len > new_len) {
Karsten Bleese7c73052014-07-05 00:41:46 +0200284 removal.buf[removal.len] = '\0';
Elijah Newren00fcce22021-12-09 05:08:29 +0000285 if ((startup_info->original_cwd &&
286 !strcmp(removal.buf, startup_info->original_cwd)) ||
287 rmdir(removal.buf))
Kjetil Barvik78478922009-02-09 21:54:07 +0100288 break;
289 do {
290 removal.len--;
291 } while (removal.len > new_len &&
Karsten Bleese7c73052014-07-05 00:41:46 +0200292 removal.buf[removal.len] != '/');
Kjetil Barvik78478922009-02-09 21:54:07 +0100293 }
294 removal.len = new_len;
Kjetil Barvik78478922009-02-09 21:54:07 +0100295}
296
297void schedule_dir_for_removal(const char *name, int len)
298{
299 int match_len, last_slash, i, previous_slash;
300
Elijah Newren00fcce22021-12-09 05:08:29 +0000301 if (startup_info->original_cwd &&
302 !strcmp(name, startup_info->original_cwd))
303 return; /* Do not remove the current working directory */
304
Kjetil Barvik78478922009-02-09 21:54:07 +0100305 match_len = last_slash = i =
Karsten Bleese7c73052014-07-05 00:41:46 +0200306 longest_path_match(name, len, removal.buf, removal.len,
Kjetil Barvik78478922009-02-09 21:54:07 +0100307 &previous_slash);
308 /* Find last slash inside 'name' */
309 while (i < len) {
310 if (name[i] == '/')
311 last_slash = i;
312 i++;
313 }
314
315 /*
316 * If we are about to go down the directory tree, we check if
317 * we must first go upwards the tree, such that we then can
318 * remove possible empty directories as we go upwards.
319 */
320 if (match_len < last_slash && match_len < removal.len)
321 do_remove_scheduled_dirs(match_len);
322 /*
323 * If we go deeper down the directory tree, we only need to
324 * save the new path components as we go down.
325 */
Karsten Bleese7c73052014-07-05 00:41:46 +0200326 if (match_len < last_slash)
327 strbuf_add(&removal, &name[match_len], last_slash - match_len);
Kjetil Barvik78478922009-02-09 21:54:07 +0100328}
329
330void remove_scheduled_dirs(void)
331{
332 do_remove_scheduled_dirs(0);
Kjetil Barvik78478922009-02-09 21:54:07 +0100333}
Matheus Tavares684dd4c2020-12-10 10:27:55 -0300334
335void invalidate_lstat_cache(void)
336{
337 reset_lstat_cache(&default_cache);
338}
339
340#undef rmdir
341int lstat_cache_aware_rmdir(const char *path)
342{
343 /* Any change in this function must be made also in `mingw_rmdir()` */
344 int ret = rmdir(path);
345
346 if (!ret)
347 invalidate_lstat_cache();
348
349 return ret;
350}