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