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