1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
8 static int threaded_check_leading_path(struct cache_def
*cache
, const char *name
,
9 int len
, int warn_on_lstat_err
);
10 static int threaded_has_dirs_only_path(struct cache_def
*cache
, const char *name
, int len
, int prefix_len
);
13 * Returns the length (on a path component basis) of the longest
14 * common prefix match of 'name_a' and 'name_b'.
16 static int longest_path_match(const char *name_a
, int len_a
,
17 const char *name_b
, int len_b
,
20 int max_len
, match_len
= 0, match_len_prev
= 0, i
= 0;
22 max_len
= len_a
< len_b
? len_a
: len_b
;
23 while (i
< max_len
&& name_a
[i
] == name_b
[i
]) {
24 if (name_a
[i
] == '/') {
25 match_len_prev
= match_len
;
31 * Is 'name_b' a substring of 'name_a', the other way around,
32 * or is 'name_a' and 'name_b' the exact same string?
34 if (i
>= max_len
&& ((len_a
> len_b
&& name_a
[len_b
] == '/') ||
35 (len_a
< len_b
&& name_b
[len_a
] == '/') ||
37 match_len_prev
= match_len
;
40 *previous_slash
= match_len_prev
;
44 static struct cache_def default_cache
= CACHE_DEF_INIT
;
46 static inline void reset_lstat_cache(struct cache_def
*cache
)
48 strbuf_reset(&cache
->path
);
51 * The track_flags and prefix_len_stat_func members is only
52 * set by the safeguard rule inside lstat_cache()
56 #define FL_DIR (1 << 0)
57 #define FL_NOENT (1 << 1)
58 #define FL_SYMLINK (1 << 2)
59 #define FL_LSTATERR (1 << 3)
60 #define FL_ERR (1 << 4)
61 #define FL_FULLPATH (1 << 5)
64 * Check if name 'name' of length 'len' has a symlink leading
65 * component, or if the directory exists and is real, or not.
67 * To speed up the check, some information is allowed to be cached.
68 * This can be indicated by the 'track_flags' argument, which also can
69 * be used to indicate that we should check the full path.
71 * The 'prefix_len_stat_func' parameter can be used to set the length
72 * of the prefix, where the cache should use the stat() function
73 * instead of the lstat() function to test each path component.
75 static int lstat_cache_matchlen(struct cache_def
*cache
,
76 const char *name
, int len
,
77 int *ret_flags
, int track_flags
,
78 int prefix_len_stat_func
)
80 int match_len
, last_slash
, last_slash_dir
, previous_slash
;
81 int save_flags
, ret
, saved_errno
= 0;
84 if (cache
->track_flags
!= track_flags
||
85 cache
->prefix_len_stat_func
!= prefix_len_stat_func
) {
87 * As a safeguard rule we clear the cache if the
88 * values of track_flags and/or prefix_len_stat_func
89 * does not match with the last supplied values.
91 reset_lstat_cache(cache
);
92 cache
->track_flags
= track_flags
;
93 cache
->prefix_len_stat_func
= prefix_len_stat_func
;
94 match_len
= last_slash
= 0;
97 * Check to see if we have a match from the cache for
98 * the 2 "excluding" path types.
100 match_len
= last_slash
=
101 longest_path_match(name
, len
, cache
->path
.buf
,
102 cache
->path
.len
, &previous_slash
);
103 *ret_flags
= cache
->flags
& track_flags
& (FL_NOENT
|FL_SYMLINK
);
105 if (!(track_flags
& FL_FULLPATH
) && match_len
== len
)
106 match_len
= last_slash
= previous_slash
;
108 if (*ret_flags
&& match_len
== cache
->path
.len
)
111 * If we now have match_len > 0, we would know that
112 * the matched part will always be a directory.
114 * Also, if we are tracking directories and 'name' is
115 * a substring of the cache on a path component basis,
116 * we can return immediately.
118 *ret_flags
= track_flags
& FL_DIR
;
119 if (*ret_flags
&& len
== match_len
)
124 * Okay, no match from the cache so far, so now we have to
125 * check the rest of the path components.
128 last_slash_dir
= last_slash
;
129 if (len
> cache
->path
.len
)
130 strbuf_grow(&cache
->path
, len
- cache
->path
.len
);
131 while (match_len
< len
) {
133 cache
->path
.buf
[match_len
] = name
[match_len
];
135 } while (match_len
< len
&& name
[match_len
] != '/');
136 if (match_len
>= len
&& !(track_flags
& FL_FULLPATH
))
138 last_slash
= match_len
;
139 cache
->path
.buf
[last_slash
] = '\0';
141 if (last_slash
<= prefix_len_stat_func
)
142 ret
= stat(cache
->path
.buf
, &st
);
144 ret
= lstat(cache
->path
.buf
, &st
);
147 *ret_flags
= FL_LSTATERR
;
150 *ret_flags
|= FL_NOENT
;
151 } else if (S_ISDIR(st
.st_mode
)) {
152 last_slash_dir
= last_slash
;
154 } else if (S_ISLNK(st
.st_mode
)) {
155 *ret_flags
= FL_SYMLINK
;
163 * At the end update the cache. Note that max 3 different
164 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
167 save_flags
= *ret_flags
& track_flags
& (FL_NOENT
|FL_SYMLINK
);
168 if (save_flags
&& last_slash
> 0) {
169 cache
->path
.buf
[last_slash
] = '\0';
170 cache
->path
.len
= last_slash
;
171 cache
->flags
= save_flags
;
172 } else if ((track_flags
& FL_DIR
) && last_slash_dir
> 0) {
174 * We have a separate test for the directory case,
175 * since it could be that we have found a symlink or a
176 * non-existing directory and the track_flags says
177 * that we cannot cache this fact, so the cache would
178 * then have been left empty in this case.
180 * But if we are allowed to track real directories, we
181 * can still cache the path components before the last
182 * one (the found symlink or non-existing component).
184 cache
->path
.buf
[last_slash_dir
] = '\0';
185 cache
->path
.len
= last_slash_dir
;
186 cache
->flags
= FL_DIR
;
188 reset_lstat_cache(cache
);
195 static int lstat_cache(struct cache_def
*cache
, const char *name
, int len
,
196 int track_flags
, int prefix_len_stat_func
)
199 (void)lstat_cache_matchlen(cache
, name
, len
, &flags
, track_flags
,
200 prefix_len_stat_func
);
204 #define USE_ONLY_LSTAT 0
207 * Return non-zero if path 'name' has a leading symlink component
209 int threaded_has_symlink_leading_path(struct cache_def
*cache
, const char *name
, int len
)
211 return lstat_cache(cache
, name
, len
, FL_SYMLINK
|FL_DIR
, USE_ONLY_LSTAT
) & FL_SYMLINK
;
214 int has_symlink_leading_path(const char *name
, int len
)
216 return threaded_has_symlink_leading_path(&default_cache
, name
, len
);
219 int check_leading_path(const char *name
, int len
, int warn_on_lstat_err
)
221 return threaded_check_leading_path(&default_cache
, name
, len
,
226 * Return zero if some leading path component of 'name' does not exist.
228 * Return -1 if leading path exists and is a directory.
230 * Return the length of a leading component if it either exists but it's not a
231 * directory, or if we were unable to lstat() it. If warn_on_lstat_err is true,
232 * also emit a warning for this error.
234 static int threaded_check_leading_path(struct cache_def
*cache
, const char *name
,
235 int len
, int warn_on_lstat_err
)
238 int match_len
= lstat_cache_matchlen(cache
, name
, len
, &flags
,
239 FL_SYMLINK
|FL_NOENT
|FL_DIR
, USE_ONLY_LSTAT
);
240 int saved_errno
= errno
;
242 if (flags
& FL_NOENT
)
244 else if (flags
& FL_DIR
)
246 if (warn_on_lstat_err
&& (flags
& FL_LSTATERR
)) {
247 char *path
= xmemdupz(name
, match_len
);
249 warning_errno(_("failed to lstat '%s'"), path
);
255 int has_dirs_only_path(const char *name
, int len
, int prefix_len
)
257 return threaded_has_dirs_only_path(&default_cache
, name
, len
, prefix_len
);
261 * Return non-zero if all path components of 'name' exists as a
262 * directory. If prefix_len > 0, we will test with the stat()
263 * function instead of the lstat() function for a prefix length of
264 * 'prefix_len', thus we then allow for symlinks in the prefix part as
265 * long as those points to real existing directories.
267 static int threaded_has_dirs_only_path(struct cache_def
*cache
, const char *name
, int len
, int prefix_len
)
270 * Note: this function is used by the checkout machinery, which also
271 * takes care to properly reset the cache when it performs an operation
272 * that would leave the cache outdated. If this function starts caching
273 * anything else besides FL_DIR, remember to also invalidate the cache
274 * when creating or deleting paths that might be in the cache.
276 return lstat_cache(cache
, name
, len
,
277 FL_DIR
|FL_FULLPATH
, prefix_len
) &
281 static struct strbuf removal
= STRBUF_INIT
;
283 static void do_remove_scheduled_dirs(int new_len
)
285 while (removal
.len
> new_len
) {
286 removal
.buf
[removal
.len
] = '\0';
287 if ((startup_info
->original_cwd
&&
288 !strcmp(removal
.buf
, startup_info
->original_cwd
)) ||
293 } while (removal
.len
> new_len
&&
294 removal
.buf
[removal
.len
] != '/');
296 removal
.len
= new_len
;
299 void schedule_dir_for_removal(const char *name
, int len
)
301 int match_len
, last_slash
, i
, previous_slash
;
303 if (startup_info
->original_cwd
&&
304 !strcmp(name
, startup_info
->original_cwd
))
305 return; /* Do not remove the current working directory */
307 match_len
= last_slash
= i
=
308 longest_path_match(name
, len
, removal
.buf
, removal
.len
,
310 /* Find last slash inside 'name' */
318 * If we are about to go down the directory tree, we check if
319 * we must first go upwards the tree, such that we then can
320 * remove possible empty directories as we go upwards.
322 if (match_len
< last_slash
&& match_len
< removal
.len
)
323 do_remove_scheduled_dirs(match_len
);
325 * If we go deeper down the directory tree, we only need to
326 * save the new path components as we go down.
328 if (match_len
< last_slash
)
329 strbuf_add(&removal
, &name
[match_len
], last_slash
- match_len
);
332 void remove_scheduled_dirs(void)
334 do_remove_scheduled_dirs(0);
337 void invalidate_lstat_cache(void)
339 reset_lstat_cache(&default_cache
);
343 int lstat_cache_aware_rmdir(const char *path
)
345 /* Any change in this function must be made also in `mingw_rmdir()` */
346 int ret
= rmdir(path
);
349 invalidate_lstat_cache();