diff options
Diffstat (limited to 'symlinks.c')
-rw-r--r-- | symlinks.c | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/symlinks.c b/symlinks.c index 034943bda0..5261e8cf49 100644 --- a/symlinks.c +++ b/symlinks.c @@ -1,5 +1,8 @@ #include "cache.h" +static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len); +static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len); + /* * Returns the length (on a path component basis) of the longest * common prefix match of 'name_a' and 'name_b'. @@ -32,12 +35,11 @@ static int longest_path_match(const char *name_a, int len_a, return match_len; } -static struct cache_def default_cache; +static struct cache_def default_cache = CACHE_DEF_INIT; static inline void reset_lstat_cache(struct cache_def *cache) { - cache->path[0] = '\0'; - cache->len = 0; + strbuf_reset(&cache->path); cache->flags = 0; /* * The track_flags and prefix_len_stat_func members is only @@ -70,7 +72,7 @@ static int lstat_cache_matchlen(struct cache_def *cache, int prefix_len_stat_func) { int match_len, last_slash, last_slash_dir, previous_slash; - int save_flags, max_len, ret; + int save_flags, ret; struct stat st; if (cache->track_flags != track_flags || @@ -90,14 +92,14 @@ static int lstat_cache_matchlen(struct cache_def *cache, * the 2 "excluding" path types. */ match_len = last_slash = - longest_path_match(name, len, cache->path, cache->len, - &previous_slash); + longest_path_match(name, len, cache->path.buf, + cache->path.len, &previous_slash); *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK); if (!(track_flags & FL_FULLPATH) && match_len == len) match_len = last_slash = previous_slash; - if (*ret_flags && match_len == cache->len) + if (*ret_flags && match_len == cache->path.len) return match_len; /* * If we now have match_len > 0, we would know that @@ -118,21 +120,22 @@ static int lstat_cache_matchlen(struct cache_def *cache, */ *ret_flags = FL_DIR; last_slash_dir = last_slash; - max_len = len < PATH_MAX ? len : PATH_MAX; - while (match_len < max_len) { + if (len > cache->path.len) + strbuf_grow(&cache->path, len - cache->path.len); + while (match_len < len) { do { - cache->path[match_len] = name[match_len]; + cache->path.buf[match_len] = name[match_len]; match_len++; - } while (match_len < max_len && name[match_len] != '/'); - if (match_len >= max_len && !(track_flags & FL_FULLPATH)) + } while (match_len < len && name[match_len] != '/'); + if (match_len >= len && !(track_flags & FL_FULLPATH)) break; last_slash = match_len; - cache->path[last_slash] = '\0'; + cache->path.buf[last_slash] = '\0'; if (last_slash <= prefix_len_stat_func) - ret = stat(cache->path, &st); + ret = stat(cache->path.buf, &st); else - ret = lstat(cache->path, &st); + ret = lstat(cache->path.buf, &st); if (ret) { *ret_flags = FL_LSTATERR; @@ -155,12 +158,11 @@ static int lstat_cache_matchlen(struct cache_def *cache, * for the moment! */ save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK); - if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) { - cache->path[last_slash] = '\0'; - cache->len = last_slash; + if (save_flags && last_slash > 0) { + cache->path.buf[last_slash] = '\0'; + cache->path.len = last_slash; cache->flags = save_flags; - } else if ((track_flags & FL_DIR) && - last_slash_dir > 0 && last_slash_dir <= PATH_MAX) { + } else if ((track_flags & FL_DIR) && last_slash_dir > 0) { /* * We have a separate test for the directory case, * since it could be that we have found a symlink or a @@ -172,8 +174,8 @@ static int lstat_cache_matchlen(struct cache_def *cache, * can still cache the path components before the last * one (the found symlink or non-existing component). */ - cache->path[last_slash_dir] = '\0'; - cache->len = last_slash_dir; + cache->path.buf[last_slash_dir] = '\0'; + cache->path.len = last_slash_dir; cache->flags = FL_DIR; } else { reset_lstat_cache(cache); @@ -219,7 +221,20 @@ int has_symlink_leading_path(const char *name, int len) */ int check_leading_path(const char *name, int len) { - struct cache_def *cache = &default_cache; /* FIXME */ + return threaded_check_leading_path(&default_cache, name, len); +} + +/* + * Return zero if path 'name' has a leading symlink component or + * if some leading path component does not exists. + * + * Return -1 if leading path exists and is a directory. + * + * Return path length if leading path exists and is neither a + * directory nor a symlink. + */ +static int threaded_check_leading_path(struct cache_def *cache, const char *name, int len) +{ int flags; int match_len = lstat_cache_matchlen(cache, name, len, &flags, FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT); @@ -240,27 +255,35 @@ int check_leading_path(const char *name, int len) */ int has_dirs_only_path(const char *name, int len, int prefix_len) { - struct cache_def *cache = &default_cache; /* FIXME */ + return threaded_has_dirs_only_path(&default_cache, name, len, prefix_len); +} + +/* + * Return non-zero if all path components of 'name' exists as a + * directory. If prefix_len > 0, we will test with the stat() + * function instead of the lstat() function for a prefix length of + * 'prefix_len', thus we then allow for symlinks in the prefix part as + * long as those points to real existing directories. + */ +static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len) +{ return lstat_cache(cache, name, len, FL_DIR|FL_FULLPATH, prefix_len) & FL_DIR; } -static struct removal_def { - char path[PATH_MAX]; - int len; -} removal; +static struct strbuf removal = STRBUF_INIT; static void do_remove_scheduled_dirs(int new_len) { while (removal.len > new_len) { - removal.path[removal.len] = '\0'; - if (rmdir(removal.path)) + removal.buf[removal.len] = '\0'; + if (rmdir(removal.buf)) break; do { removal.len--; } while (removal.len > new_len && - removal.path[removal.len] != '/'); + removal.buf[removal.len] != '/'); } removal.len = new_len; } @@ -270,7 +293,7 @@ void schedule_dir_for_removal(const char *name, int len) int match_len, last_slash, i, previous_slash; match_len = last_slash = i = - longest_path_match(name, len, removal.path, removal.len, + longest_path_match(name, len, removal.buf, removal.len, &previous_slash); /* Find last slash inside 'name' */ while (i < len) { @@ -290,11 +313,8 @@ void schedule_dir_for_removal(const char *name, int len) * If we go deeper down the directory tree, we only need to * save the new path components as we go down. */ - if (match_len < last_slash) { - memcpy(&removal.path[match_len], &name[match_len], - last_slash - match_len); - removal.len = last_slash; - } + if (match_len < last_slash) + strbuf_add(&removal, &name[match_len], last_slash - match_len); } void remove_scheduled_dirs(void) |