symlinks: remove PATH_MAX limitation

'git checkout' fails if a directory is longer than PATH_MAX, because the lstat_cache in symlinks.c checks if the leading directory exists using PATH_MAX-bounded string operations. Remove the limitation by using strbuf instead. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jul 5, 2014 at 00:41 UTC e7c730530065fb2f366ecbcb1e71d7f7346b6a25
3 files changed +36 -39
cache.h
+6 -2
@@ -1068,12 +1068,16 @@ struct checkout {
1068 extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
1069
1070 struct cache_def {
1071 - char path[PATH_MAX + 1];
1072 - int len;
1071 + struct strbuf path;
1072 int flags;
1073 int track_flags;
1074 int prefix_len_stat_func;
1075 };
1076 +#define CACHE_DEF_INIT { STRBUF_INIT, 0, 0, 0 }
1077 +static inline void cache_def_free(struct cache_def *cache)
1078 +{
1079 + strbuf_release(&cache->path);
1080 +}
1081
1082 extern int has_symlink_leading_path(const char *name, int len);
1083 extern int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);
preload-index.c
+2 -2
@@ -37,9 +37,8 @@ static void *preload_thread(void *_data)
37 struct thread_data *p = _data;
38 struct index_state *index = p->index;
39 struct cache_entry **cep = index->cache + p->offset;
40 - struct cache_def cache;
40 + struct cache_def cache = CACHE_DEF_INIT;
41
42 - memset(&cache, 0, sizeof(cache));
42 nr = p->nr;
43 if (nr + p->offset > index->cache_nr)
44 nr = index->cache_nr - p->offset;
@@ -64,6 +63,7 @@ static void *preload_thread(void *_data)
63 continue;
64 ce_mark_uptodate(ce);
65 } while (--nr > 0);
66 + cache_def_free(&cache);
67 return NULL;
68 }
69
symlinks.c
+28 -35
@@ -35,12 +35,11 @@ static int longest_path_match(const char *name_a, int len_a,
35 return match_len;
36 }
37
38 -static struct cache_def default_cache;
38 +static struct cache_def default_cache = CACHE_DEF_INIT;
39
40 static inline void reset_lstat_cache(struct cache_def *cache)
41 {
42 - cache->path[0] = '\0';
43 - cache->len = 0;
42 + strbuf_reset(&cache->path);
43 cache->flags = 0;
44 /*
45 * The track_flags and prefix_len_stat_func members is only
@@ -73,7 +72,7 @@ static int lstat_cache_matchlen(struct cache_def *cache,
72 int prefix_len_stat_func)
73 {
74 int match_len, last_slash, last_slash_dir, previous_slash;
76 - int save_flags, max_len, ret;
75 + int save_flags, ret;
76 struct stat st;
77
78 if (cache->track_flags != track_flags ||
@@ -93,14 +92,14 @@ static int lstat_cache_matchlen(struct cache_def *cache,
92 * the 2 "excluding" path types.
93 */
94 match_len = last_slash =
96 - longest_path_match(name, len, cache->path, cache->len,
97 - &previous_slash);
95 + longest_path_match(name, len, cache->path.buf,
96 + cache->path.len, &previous_slash);
97 *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
98
99 if (!(track_flags & FL_FULLPATH) && match_len == len)
100 match_len = last_slash = previous_slash;
101
103 - if (*ret_flags && match_len == cache->len)
102 + if (*ret_flags && match_len == cache->path.len)
103 return match_len;
104 /*
105 * If we now have match_len > 0, we would know that
@@ -121,21 +120,22 @@ static int lstat_cache_matchlen(struct cache_def *cache,
120 */
121 *ret_flags = FL_DIR;
122 last_slash_dir = last_slash;
124 - max_len = len < PATH_MAX ? len : PATH_MAX;
125 - while (match_len < max_len) {
123 + if (len > cache->path.len)
124 + strbuf_grow(&cache->path, len - cache->path.len);
125 + while (match_len < len) {
126 do {
127 - cache->path[match_len] = name[match_len];
127 + cache->path.buf[match_len] = name[match_len];
128 match_len++;
129 - } while (match_len < max_len && name[match_len] != '/');
130 - if (match_len >= max_len && !(track_flags & FL_FULLPATH))
129 + } while (match_len < len && name[match_len] != '/');
130 + if (match_len >= len && !(track_flags & FL_FULLPATH))
131 break;
132 last_slash = match_len;
133 - cache->path[last_slash] = '\0';
133 + cache->path.buf[last_slash] = '\0';
134
135 if (last_slash <= prefix_len_stat_func)
136 - ret = stat(cache->path, &st);
136 + ret = stat(cache->path.buf, &st);
137 else
138 - ret = lstat(cache->path, &st);
138 + ret = lstat(cache->path.buf, &st);
139
140 if (ret) {
141 *ret_flags = FL_LSTATERR;
@@ -158,12 +158,11 @@ static int lstat_cache_matchlen(struct cache_def *cache,
158 * for the moment!
159 */
160 save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
161 - if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
162 - cache->path[last_slash] = '\0';
163 - cache->len = last_slash;
161 + if (save_flags && last_slash > 0) {
162 + cache->path.buf[last_slash] = '\0';
163 + cache->path.len = last_slash;
164 cache->flags = save_flags;
165 - } else if ((track_flags & FL_DIR) &&
166 - last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
165 + } else if ((track_flags & FL_DIR) && last_slash_dir > 0) {
166 /*
167 * We have a separate test for the directory case,
168 * 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,
174 * can still cache the path components before the last
175 * one (the found symlink or non-existing component).
176 */
178 - cache->path[last_slash_dir] = '\0';
179 - cache->len = last_slash_dir;
177 + cache->path.buf[last_slash_dir] = '\0';
178 + cache->path.len = last_slash_dir;
179 cache->flags = FL_DIR;
180 } else {
181 reset_lstat_cache(cache);
@@ -273,21 +272,18 @@ static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name
272 FL_DIR;
273 }
274
276 -static struct removal_def {
277 - char path[PATH_MAX];
278 - int len;
279 -} removal;
275 +static struct strbuf removal = STRBUF_INIT;
276
277 static void do_remove_scheduled_dirs(int new_len)
278 {
279 while (removal.len > new_len) {
284 - removal.path[removal.len] = '\0';
285 - if (rmdir(removal.path))
280 + removal.buf[removal.len] = '\0';
281 + if (rmdir(removal.buf))
282 break;
283 do {
284 removal.len--;
285 } while (removal.len > new_len &&
290 - removal.path[removal.len] != '/');
286 + removal.buf[removal.len] != '/');
287 }
288 removal.len = new_len;
289 }
@@ -297,7 +293,7 @@ void schedule_dir_for_removal(const char *name, int len)
293 int match_len, last_slash, i, previous_slash;
294
295 match_len = last_slash = i =
300 - longest_path_match(name, len, removal.path, removal.len,
296 + longest_path_match(name, len, removal.buf, removal.len,
297 &previous_slash);
298 /* Find last slash inside 'name' */
299 while (i < len) {
@@ -317,11 +313,8 @@ void schedule_dir_for_removal(const char *name, int len)
313 * If we go deeper down the directory tree, we only need to
314 * save the new path components as we go down.
315 */
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 - }
316 + if (match_len < last_slash)
317 + strbuf_add(&removal, &name[match_len], last_slash - match_len);
318 }
319
320 void remove_scheduled_dirs(void)