diff --git a/cache.h b/cache.h index 95ce0a9f7b29b2..a04a4d35765d15 100644 --- a/cache.h +++ b/cache.h @@ -1023,12 +1023,16 @@ struct checkout { extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath); struct cache_def { - char path[PATH_MAX + 1]; - int len; + struct strbuf path; int flags; int track_flags; int prefix_len_stat_func; }; +#define CACHE_DEF_INIT { STRBUF_INIT, 0, 0, 0 } +static inline void cache_def_clear(struct cache_def *cache) +{ + strbuf_release(&cache->path); +} extern int has_symlink_leading_path(const char *name, int len); extern int threaded_has_symlink_leading_path(struct cache_def *, const char *, int); diff --git a/compat/mingw.c b/compat/mingw.c index 4fa10aa5ca32b1..e4a8671dfb145c 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -560,7 +560,7 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf) static int do_stat_internal(int follow, const char *file_name, struct stat *buf) { int namelen; - char alt_name[PATH_MAX]; + char alt_name[MAX_LONG_PATH]; if (!do_lstat(follow, file_name, buf)) return 0; @@ -576,7 +576,7 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf) return -1; while (namelen && file_name[namelen-1] == '/') --namelen; - if (!namelen || namelen >= PATH_MAX) + if (!namelen || namelen >= MAX_LONG_PATH) return -1; memcpy(alt_name, file_name, namelen); diff --git a/dir.c b/dir.c index 98bb50fbabb69d..c927114fddd021 100644 --- a/dir.c +++ b/dir.c @@ -531,8 +531,7 @@ int add_excludes_from_file_to_list(const char *fname, buf = xrealloc(buf, size+1); buf[size++] = '\n'; } - } - else { + } else { size = xsize_t(st.st_size); if (size == 0) { close(fd); @@ -766,17 +765,19 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) group = &dir->exclude_list_group[EXC_DIRS]; - /* Pop the exclude lists from the EXCL_DIRS exclude_list_group + /* + * Pop the exclude lists from the EXCL_DIRS exclude_list_group * which originate from directories not in the prefix of the - * path being checked. */ + * path being checked. + */ while ((stk = dir->exclude_stack) != NULL) { if (stk->baselen <= baselen && - !strncmp(dir->basebuf, base, stk->baselen)) + !strncmp(dir->basebuf.buf, base, stk->baselen)) break; el = &group->el[dir->exclude_stack->exclude_ix]; dir->exclude_stack = stk->prev; dir->exclude = NULL; - free((char *)el->src); /* see strdup() below */ + free((char *)el->src); /* see strbuf_detach() below */ clear_exclude_list(el); free(stk); group->nr--; @@ -786,8 +787,17 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) if (dir->exclude) return; + /* + * Lazy initialization. All call sites currently just + * memset(dir, 0, sizeof(*dir)) before use. Changing all of + * them seems lots of work for little benefit. + */ + if (!dir->basebuf.buf) + strbuf_init(&dir->basebuf, PATH_MAX); + /* Read from the parent directories and push them down. */ current = stk ? stk->baselen : -1; + strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current); while (current < baselen) { struct exclude_stack *stk = xcalloc(1, sizeof(*stk)); const char *cp; @@ -795,8 +805,7 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) if (current < 0) { cp = base; current = 0; - } - else { + } else { cp = strchr(base + current + 1, '/'); if (!cp) die("oops in prep_exclude"); @@ -806,48 +815,47 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) stk->baselen = cp - base; stk->exclude_ix = group->nr; el = add_exclude_list(dir, EXC_DIRS, NULL); - memcpy(dir->basebuf + current, base + current, - stk->baselen - current); + strbuf_add(&dir->basebuf, base + current, stk->baselen - current); + assert(stk->baselen == dir->basebuf.len); /* Abort if the directory is excluded */ if (stk->baselen) { int dt = DT_DIR; - dir->basebuf[stk->baselen - 1] = 0; + dir->basebuf.buf[stk->baselen - 1] = 0; dir->exclude = last_exclude_matching_from_lists(dir, - dir->basebuf, stk->baselen - 1, - dir->basebuf + current, &dt); - dir->basebuf[stk->baselen - 1] = '/'; + dir->basebuf.buf, stk->baselen - 1, + dir->basebuf.buf + current, &dt); + dir->basebuf.buf[stk->baselen - 1] = '/'; if (dir->exclude && dir->exclude->flags & EXC_FLAG_NEGATIVE) dir->exclude = NULL; if (dir->exclude) { - dir->basebuf[stk->baselen] = 0; dir->exclude_stack = stk; return; } } - /* Try to read per-directory file unless path is too long */ - if (dir->exclude_per_dir && - stk->baselen + strlen(dir->exclude_per_dir) < PATH_MAX) { - strcpy(dir->basebuf + stk->baselen, - dir->exclude_per_dir); + /* Try to read per-directory file */ + if (dir->exclude_per_dir) { /* * dir->basebuf gets reused by the traversal, but we * need fname to remain unchanged to ensure the src * member of each struct exclude correctly * back-references its source file. Other invocations * of add_exclude_list provide stable strings, so we - * strdup() and free() here in the caller. + * strbuf_detach() and free() here in the caller. */ - el->src = strdup(dir->basebuf); - add_excludes_from_file_to_list(dir->basebuf, - dir->basebuf, stk->baselen, el, 1); + struct strbuf sb = STRBUF_INIT; + strbuf_addbuf(&sb, &dir->basebuf); + strbuf_addstr(&sb, dir->exclude_per_dir); + el->src = strbuf_detach(&sb, NULL); + add_excludes_from_file_to_list(el->src, el->src, + stk->baselen, el, 1); } dir->exclude_stack = stk; current = stk->baselen; } - dir->basebuf[baselen] = '\0'; + strbuf_setlen(&dir->basebuf, baselen); } /* @@ -1648,4 +1656,5 @@ void clear_directory(struct dir_struct *dir) free(stk); stk = prev; } + strbuf_release(&dir->basebuf); } diff --git a/dir.h b/dir.h index 55e53456afab4c..6c45e9d4b9a905 100644 --- a/dir.h +++ b/dir.h @@ -15,6 +15,27 @@ struct dir_entry { #define EXC_FLAG_MUSTBEDIR 8 #define EXC_FLAG_NEGATIVE 16 +struct exclude { + /* + * This allows callers of last_exclude_matching() etc. + * to determine the origin of the matching pattern. + */ + struct exclude_list *el; + + const char *pattern; + int patternlen; + int nowildcardlen; + const char *base; + int baselen; + int flags; + + /* + * Counting starts from 1 for line numbers in ignore files, + * and from -1 decrementing for patterns from CLI args. + */ + int srcpos; +}; + /* * Each excludes file will be parsed into a fresh exclude_list which * is appended to the relevant exclude_list_group (either EXC_DIRS or @@ -32,26 +53,7 @@ struct exclude_list { /* origin of list, e.g. path to filename, or descriptive string */ const char *src; - struct exclude { - /* - * This allows callers of last_exclude_matching() etc. - * to determine the origin of the matching pattern. - */ - struct exclude_list *el; - - const char *pattern; - int patternlen; - int nowildcardlen; - const char *base; - int baselen; - int flags; - - /* - * Counting starts from 1 for line numbers in ignore files, - * and from -1 decrementing for patterns from CLI args. - */ - int srcpos; - } **excludes; + struct exclude **excludes; }; /* @@ -117,7 +119,7 @@ struct dir_struct { */ struct exclude_stack *exclude_stack; struct exclude *exclude; - char basebuf[PATH_MAX]; + struct strbuf basebuf; }; /* diff --git a/preload-index.c b/preload-index.c index 8253778c85d7c7..c7970d7cb95587 100644 --- a/preload-index.c +++ b/preload-index.c @@ -37,9 +37,8 @@ static void *preload_thread(void *_data) struct thread_data *p = _data; struct index_state *index = p->index; struct cache_entry **cep = index->cache + p->offset; - struct cache_def cache; + struct cache_def cache = CACHE_DEF_INIT; - memset(&cache, 0, sizeof(cache)); nr = p->nr; if (nr + p->offset > index->cache_nr) nr = index->cache_nr - p->offset; @@ -64,6 +63,7 @@ static void *preload_thread(void *_data) continue; ce_mark_uptodate(ce); } while (--nr > 0); + cache_def_clear(&cache); return NULL; } diff --git a/symlinks.c b/symlinks.c index c2b41a85013eec..5261e8cf499006 100644 --- a/symlinks.c +++ b/symlinks.c @@ -35,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 @@ -73,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 || @@ -93,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 @@ -121,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; @@ -158,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 @@ -175,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); @@ -273,21 +272,18 @@ static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name 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; } @@ -297,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) { @@ -317,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)