Junio C Hamano | f859c84 | 2007-05-11 22:11:07 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
Junio C Hamano | 72f3196 | 2012-09-15 22:38:28 -0700 | [diff] [blame] | 3 | static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len); |
| 4 | static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len); |
| 5 | |
Kjetil Barvik | 148bc06 | 2009-02-09 21:54:05 +0100 | [diff] [blame] | 6 | /* |
| 7 | * Returns the length (on a path component basis) of the longest |
| 8 | * common prefix match of 'name_a' and 'name_b'. |
| 9 | */ |
| 10 | static int longest_path_match(const char *name_a, int len_a, |
| 11 | const char *name_b, int len_b, |
| 12 | int *previous_slash) |
| 13 | { |
| 14 | int max_len, match_len = 0, match_len_prev = 0, i = 0; |
| 15 | |
| 16 | max_len = len_a < len_b ? len_a : len_b; |
| 17 | while (i < max_len && name_a[i] == name_b[i]) { |
| 18 | if (name_a[i] == '/') { |
| 19 | match_len_prev = match_len; |
| 20 | match_len = i; |
| 21 | } |
| 22 | i++; |
| 23 | } |
| 24 | /* |
| 25 | * Is 'name_b' a substring of 'name_a', the other way around, |
| 26 | * or is 'name_a' and 'name_b' the exact same string? |
| 27 | */ |
| 28 | if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') || |
| 29 | (len_a < len_b && name_b[len_a] == '/') || |
| 30 | (len_a == len_b))) { |
| 31 | match_len_prev = match_len; |
| 32 | match_len = i; |
| 33 | } |
| 34 | *previous_slash = match_len_prev; |
| 35 | return match_len; |
| 36 | } |
| 37 | |
Linus Torvalds | b9fd284 | 2009-07-09 13:35:31 -0700 | [diff] [blame] | 38 | static struct cache_def default_cache; |
Junio C Hamano | f859c84 | 2007-05-11 22:11:07 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 40 | static inline void reset_lstat_cache(struct cache_def *cache) |
Linus Torvalds | c40641b | 2008-05-09 09:21:07 -0700 | [diff] [blame] | 41 | { |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 42 | cache->path[0] = '\0'; |
| 43 | cache->len = 0; |
| 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; |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 76 | int save_flags, max_len, ret; |
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 = |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 96 | longest_path_match(name, len, cache->path, cache->len, |
Kjetil Barvik | 148bc06 | 2009-02-09 21:54:05 +0100 | [diff] [blame] | 97 | &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 | |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 103 | if (*ret_flags && match_len == cache->len) |
| 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; |
| 124 | max_len = len < PATH_MAX ? len : PATH_MAX; |
| 125 | while (match_len < max_len) { |
| 126 | do { |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 127 | cache->path[match_len] = name[match_len]; |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 128 | match_len++; |
| 129 | } while (match_len < max_len && name[match_len] != '/'); |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 130 | if (match_len >= max_len && !(track_flags & FL_FULLPATH)) |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 131 | break; |
| 132 | last_slash = match_len; |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 133 | cache->path[last_slash] = '\0'; |
Linus Torvalds | c40641b | 2008-05-09 09:21:07 -0700 | [diff] [blame] | 134 | |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 135 | if (last_slash <= prefix_len_stat_func) |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 136 | ret = stat(cache->path, &st); |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 137 | else |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 138 | ret = lstat(cache->path, &st); |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 139 | |
| 140 | if (ret) { |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 141 | *ret_flags = FL_LSTATERR; |
Kjetil Barvik | 09c9306 | 2009-01-18 16:14:51 +0100 | [diff] [blame] | 142 | if (errno == ENOENT) |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 143 | *ret_flags |= FL_NOENT; |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 144 | } else if (S_ISDIR(st.st_mode)) { |
| 145 | last_slash_dir = last_slash; |
Linus Torvalds | c40641b | 2008-05-09 09:21:07 -0700 | [diff] [blame] | 146 | continue; |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 147 | } else if (S_ISLNK(st.st_mode)) { |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 148 | *ret_flags = FL_SYMLINK; |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 149 | } else { |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 150 | *ret_flags = FL_ERR; |
Junio C Hamano | f859c84 | 2007-05-11 22:11:07 -0700 | [diff] [blame] | 151 | } |
Linus Torvalds | c40641b | 2008-05-09 09:21:07 -0700 | [diff] [blame] | 152 | break; |
Junio C Hamano | f859c84 | 2007-05-11 22:11:07 -0700 | [diff] [blame] | 153 | } |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 154 | |
| 155 | /* |
Kjetil Barvik | 09c9306 | 2009-01-18 16:14:51 +0100 | [diff] [blame] | 156 | * At the end update the cache. Note that max 3 different |
| 157 | * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached |
| 158 | * for the moment! |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 159 | */ |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 160 | save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK); |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 161 | if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) { |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 162 | cache->path[last_slash] = '\0'; |
| 163 | cache->len = last_slash; |
| 164 | cache->flags = save_flags; |
Kjetil Barvik | 60b458b | 2009-02-09 21:54:04 +0100 | [diff] [blame] | 165 | } else if ((track_flags & FL_DIR) && |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 166 | last_slash_dir > 0 && last_slash_dir <= PATH_MAX) { |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 167 | /* |
| 168 | * We have a separate test for the directory case, |
Kjetil Barvik | 09c9306 | 2009-01-18 16:14:51 +0100 | [diff] [blame] | 169 | * since it could be that we have found a symlink or a |
| 170 | * non-existing directory and the track_flags says |
| 171 | * that we cannot cache this fact, so the cache would |
| 172 | * then have been left empty in this case. |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 173 | * |
| 174 | * But if we are allowed to track real directories, we |
| 175 | * can still cache the path components before the last |
Kjetil Barvik | 09c9306 | 2009-01-18 16:14:51 +0100 | [diff] [blame] | 176 | * one (the found symlink or non-existing component). |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 177 | */ |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 178 | cache->path[last_slash_dir] = '\0'; |
| 179 | cache->len = last_slash_dir; |
| 180 | cache->flags = FL_DIR; |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 181 | } else { |
Linus Torvalds | 867f72b | 2009-07-09 13:23:59 -0700 | [diff] [blame] | 182 | reset_lstat_cache(cache); |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 183 | } |
Clemens Buchacher | 4856ff2 | 2010-10-09 15:52:59 +0200 | [diff] [blame] | 184 | return match_len; |
| 185 | } |
| 186 | |
| 187 | static int lstat_cache(struct cache_def *cache, const char *name, int len, |
| 188 | int track_flags, int prefix_len_stat_func) |
| 189 | { |
| 190 | int flags; |
| 191 | (void)lstat_cache_matchlen(cache, name, len, &flags, track_flags, |
| 192 | prefix_len_stat_func); |
| 193 | return flags; |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 196 | #define USE_ONLY_LSTAT 0 |
| 197 | |
Kjetil Barvik | 92604b4 | 2009-01-18 16:14:50 +0100 | [diff] [blame] | 198 | /* |
| 199 | * Return non-zero if path 'name' has a leading symlink component |
| 200 | */ |
Linus Torvalds | b9fd284 | 2009-07-09 13:35:31 -0700 | [diff] [blame] | 201 | int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len) |
| 202 | { |
| 203 | return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Return non-zero if path 'name' has a leading symlink component |
| 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 | |
| 214 | /* |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 215 | * Return zero if path 'name' has a leading symlink component or |
Kjetil Barvik | 09c9306 | 2009-01-18 16:14:51 +0100 | [diff] [blame] | 216 | * if some leading path component does not exists. |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 217 | * |
| 218 | * Return -1 if leading path exists and is a directory. |
| 219 | * |
| 220 | * Return path length if leading path exists and is neither a |
| 221 | * directory nor a symlink. |
Kjetil Barvik | 09c9306 | 2009-01-18 16:14:51 +0100 | [diff] [blame] | 222 | */ |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 223 | int check_leading_path(const char *name, int len) |
Kjetil Barvik | 09c9306 | 2009-01-18 16:14:51 +0100 | [diff] [blame] | 224 | { |
Jared Hance | 15438d5 | 2012-03-02 21:31:15 -0500 | [diff] [blame] | 225 | return threaded_check_leading_path(&default_cache, name, len); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Return zero if path 'name' has a leading symlink component or |
| 230 | * if some leading path component does not exists. |
| 231 | * |
| 232 | * Return -1 if leading path exists and is a directory. |
| 233 | * |
| 234 | * Return path length if leading path exists and is neither a |
| 235 | * directory nor a symlink. |
| 236 | */ |
Junio C Hamano | 72f3196 | 2012-09-15 22:38:28 -0700 | [diff] [blame] | 237 | static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len) |
Jared Hance | 15438d5 | 2012-03-02 21:31:15 -0500 | [diff] [blame] | 238 | { |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 239 | int flags; |
| 240 | int match_len = lstat_cache_matchlen(cache, name, len, &flags, |
| 241 | FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT); |
Clemens Buchacher | 1d718a5 | 2011-02-20 13:13:43 +0100 | [diff] [blame] | 242 | if (flags & FL_NOENT) |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | else if (flags & FL_DIR) |
| 245 | return -1; |
| 246 | else |
| 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 | |
| 250 | /* |
| 251 | * Return non-zero if all path components of 'name' exists as a |
| 252 | * directory. If prefix_len > 0, we will test with the stat() |
| 253 | * function instead of the lstat() function for a prefix length of |
| 254 | * 'prefix_len', thus we then allow for symlinks in the prefix part as |
| 255 | * long as those points to real existing directories. |
| 256 | */ |
Kjetil Barvik | 5719989 | 2009-02-09 21:54:06 +0100 | [diff] [blame] | 257 | 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] | 258 | { |
Jared Hance | 15438d5 | 2012-03-02 21:31:15 -0500 | [diff] [blame] | 259 | return threaded_has_dirs_only_path(&default_cache, name, len, prefix_len); |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Return non-zero if all path components of 'name' exists as a |
| 264 | * directory. If prefix_len > 0, we will test with the stat() |
| 265 | * function instead of the lstat() function for a prefix length of |
| 266 | * 'prefix_len', thus we then allow for symlinks in the prefix part as |
| 267 | * long as those points to real existing directories. |
| 268 | */ |
Junio C Hamano | 72f3196 | 2012-09-15 22:38:28 -0700 | [diff] [blame] | 269 | 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] | 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 | |
| 276 | static struct removal_def { |
| 277 | char path[PATH_MAX]; |
| 278 | int len; |
| 279 | } removal; |
| 280 | |
| 281 | static void do_remove_scheduled_dirs(int new_len) |
| 282 | { |
| 283 | while (removal.len > new_len) { |
| 284 | removal.path[removal.len] = '\0'; |
| 285 | if (rmdir(removal.path)) |
| 286 | break; |
| 287 | do { |
| 288 | removal.len--; |
| 289 | } while (removal.len > new_len && |
| 290 | removal.path[removal.len] != '/'); |
| 291 | } |
| 292 | removal.len = new_len; |
Kjetil Barvik | 7847892 | 2009-02-09 21:54:07 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void schedule_dir_for_removal(const char *name, int len) |
| 296 | { |
| 297 | int match_len, last_slash, i, previous_slash; |
| 298 | |
| 299 | match_len = last_slash = i = |
| 300 | longest_path_match(name, len, removal.path, removal.len, |
| 301 | &previous_slash); |
| 302 | /* Find last slash inside 'name' */ |
| 303 | while (i < len) { |
| 304 | if (name[i] == '/') |
| 305 | last_slash = i; |
| 306 | i++; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * If we are about to go down the directory tree, we check if |
| 311 | * we must first go upwards the tree, such that we then can |
| 312 | * remove possible empty directories as we go upwards. |
| 313 | */ |
| 314 | if (match_len < last_slash && match_len < removal.len) |
| 315 | do_remove_scheduled_dirs(match_len); |
| 316 | /* |
| 317 | * If we go deeper down the directory tree, we only need to |
| 318 | * save the new path components as we go down. |
| 319 | */ |
| 320 | if (match_len < last_slash) { |
| 321 | memcpy(&removal.path[match_len], &name[match_len], |
| 322 | last_slash - match_len); |
| 323 | removal.len = last_slash; |
| 324 | } |
Kjetil Barvik | 7847892 | 2009-02-09 21:54:07 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void remove_scheduled_dirs(void) |
| 328 | { |
| 329 | do_remove_scheduled_dirs(0); |
Kjetil Barvik | 7847892 | 2009-02-09 21:54:07 +0100 | [diff] [blame] | 330 | } |