5 static int threaded_check_leading_path(struct cache_def
*cache
, const char *name
,
6 int len
, int warn_on_lstat_err
);
7 static int threaded_has_dirs_only_path(struct cache_def
*cache
, const char *name
, int len
, int prefix_len
);
10 * Returns the length (on a path component basis) of the longest
11 * common prefix match of 'name_a' and 'name_b'.
13 static int longest_path_match(const char *name_a
, int len_a
,
14 const char *name_b
, int len_b
,
17 int max_len
, match_len
= 0, match_len_prev
= 0, i
= 0;
19 max_len
= len_a
< len_b
? len_a
: len_b
;
20 while (i
< max_len
&& name_a
[i
] == name_b
[i
]) {
21 if (name_a
[i
] == '/') {
22 match_len_prev
= match_len
;
28 * Is 'name_b' a substring of 'name_a', the other way around,
29 * or is 'name_a' and 'name_b' the exact same string?
31 if (i
>= max_len
&& ((len_a
> len_b
&& name_a
[len_b
] == '/') ||
32 (len_a
< len_b
&& name_b
[len_a
] == '/') ||
34 match_len_prev
= match_len
;
37 *previous_slash
= match_len_prev
;
41 static struct cache_def default_cache
= CACHE_DEF_INIT
;
43 static inline void reset_lstat_cache(struct cache_def
*cache
)
45 strbuf_reset(&cache
->path
);
48 * The track_flags and prefix_len_stat_func members is only
49 * set by the safeguard rule inside lstat_cache()
53 #define FL_DIR (1 << 0)
54 #define FL_NOENT (1 << 1)
55 #define FL_SYMLINK (1 << 2)
56 #define FL_LSTATERR (1 << 3)
57 #define FL_ERR (1 << 4)
58 #define FL_FULLPATH (1 << 5)
61 * Check if name 'name' of length 'len' has a symlink leading
62 * component, or if the directory exists and is real, or not.
64 * To speed up the check, some information is allowed to be cached.
65 * This can be indicated by the 'track_flags' argument, which also can
66 * be used to indicate that we should check the full path.
68 * The 'prefix_len_stat_func' parameter can be used to set the length
69 * of the prefix, where the cache should use the stat() function
70 * instead of the lstat() function to test each path component.
72 static int lstat_cache_matchlen(struct cache_def
*cache
,
73 const char *name
, int len
,
74 int *ret_flags
, int track_flags
,
75 int prefix_len_stat_func
)
77 int match_len
, last_slash
, last_slash_dir
, previous_slash
;
78 int save_flags
, ret
, saved_errno
= 0;
81 if (cache
->track_flags
!= track_flags
||
82 cache
->prefix_len_stat_func
!= prefix_len_stat_func
) {
84 * As a safeguard rule we clear the cache if the
85 * values of track_flags and/or prefix_len_stat_func
86 * does not match with the last supplied values.
88 reset_lstat_cache(cache
);
89 cache
->track_flags
= track_flags
;
90 cache
->prefix_len_stat_func
= prefix_len_stat_func
;
91 match_len
= last_slash
= 0;
94 * Check to see if we have a match from the cache for
95 * the 2 "excluding" path types.
97 match_len
= last_slash
=
98 longest_path_match(name
, len
, cache
->path
.buf
,
99 cache
->path
.len
, &previous_slash
);
100 *ret_flags
= cache
->flags
& track_flags
& (FL_NOENT
|FL_SYMLINK
);
102 if (!(track_flags
& FL_FULLPATH
) && match_len
== len
)
103 match_len
= last_slash
= previous_slash
;
105 if (*ret_flags
&& match_len
== cache
->path
.len
)
108 * If we now have match_len > 0, we would know that
109 * the matched part will always be a directory.
111 * Also, if we are tracking directories and 'name' is
112 * a substring of the cache on a path component basis,
113 * we can return immediately.
115 *ret_flags
= track_flags
& FL_DIR
;
116 if (*ret_flags
&& len
== match_len
)
121 * Okay, no match from the cache so far, so now we have to
122 * check the rest of the path components.
125 last_slash_dir
= last_slash
;
126 if (len
> cache
->path
.len
)
127 strbuf_grow(&cache
->path
, len
- cache
->path
.len
);
128 while (match_len
< len
) {
130 cache
->path
.buf
[match_len
] = name
[match_len
];
132 } while (match_len
< len
&& name
[match_len
] != '/');
133 if (match_len
>= len
&& !(track_flags
& FL_FULLPATH
))
135 last_slash
= match_len
;
136 cache
->path
.buf
[last_slash
] = '\0';
138 if (last_slash
<= prefix_len_stat_func
)
139 ret
= stat(cache
->path
.buf
, &st
);
141 ret
= lstat(cache
->path
.buf
, &st
);
144 *ret_flags
= FL_LSTATERR
;
147 *ret_flags
|= FL_NOENT
;
148 } else if (S_ISDIR(st
.st_mode
)) {
149 last_slash_dir
= last_slash
;
151 } else if (S_ISLNK(st
.st_mode
)) {
152 *ret_flags
= FL_SYMLINK
;
160 * At the end update the cache. Note that max 3 different
161 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
164 save_flags
= *ret_flags
& track_flags
& (FL_NOENT
|FL_SYMLINK
);
165 if (save_flags
&& last_slash
> 0) {
166 cache
->path
.buf
[last_slash
] = '\0';
167 cache
->path
.len
= last_slash
;
168 cache
->flags
= save_flags
;
169 } else if ((track_flags
& FL_DIR
) && last_slash_dir
> 0) {
171 * We have a separate test for the directory case,
172 * since it could be that we have found a symlink or a
173 * non-existing directory and the track_flags says
174 * that we cannot cache this fact, so the cache would
175 * then have been left empty in this case.
177 * But if we are allowed to track real directories, we
178 * can still cache the path components before the last
179 * one (the found symlink or non-existing component).
181 cache
->path
.buf
[last_slash_dir
] = '\0';
182 cache
->path
.len
= last_slash_dir
;
183 cache
->flags
= FL_DIR
;
185 reset_lstat_cache(cache
);
192 static int lstat_cache(struct cache_def
*cache
, const char *name
, int len
,
193 int track_flags
, int prefix_len_stat_func
)
196 (void)lstat_cache_matchlen(cache
, name
, len
, &flags
, track_flags
,
197 prefix_len_stat_func
);
201 #define USE_ONLY_LSTAT 0
204 * Return non-zero if path 'name' has a leading symlink component
206 int threaded_has_symlink_leading_path(struct cache_def
*cache
, const char *name
, int len
)
208 return lstat_cache(cache
, name
, len
, FL_SYMLINK
|FL_DIR
, USE_ONLY_LSTAT
) & FL_SYMLINK
;
211 int has_symlink_leading_path(const char *name
, int len
)
213 return threaded_has_symlink_leading_path(&default_cache
, name
, len
);
216 int check_leading_path(const char *name
, int len
, int warn_on_lstat_err
)
218 return threaded_check_leading_path(&default_cache
, name
, len
,
223 * Return zero if some leading path component of 'name' does not exist.
225 * Return -1 if leading path exists and is a directory.
227 * Return the length of a leading component if it either exists but it's not a
228 * directory, or if we were unable to lstat() it. If warn_on_lstat_err is true,
229 * also emit a warning for this error.
231 static int threaded_check_leading_path(struct cache_def
*cache
, const char *name
,
232 int len
, int warn_on_lstat_err
)
235 int match_len
= lstat_cache_matchlen(cache
, name
, len
, &flags
,
236 FL_SYMLINK
|FL_NOENT
|FL_DIR
, USE_ONLY_LSTAT
);
237 int saved_errno
= errno
;
239 if (flags
& FL_NOENT
)
241 else if (flags
& FL_DIR
)
243 if (warn_on_lstat_err
&& (flags
& FL_LSTATERR
)) {
244 char *path
= xmemdupz(name
, match_len
);
246 warning_errno(_("failed to lstat '%s'"), path
);
252 int has_dirs_only_path(const char *name
, int len
, int prefix_len
)
254 return threaded_has_dirs_only_path(&default_cache
, name
, len
, prefix_len
);
258 * Return non-zero if all path components of 'name' exists as a
259 * directory. If prefix_len > 0, we will test with the stat()
260 * function instead of the lstat() function for a prefix length of
261 * 'prefix_len', thus we then allow for symlinks in the prefix part as
262 * long as those points to real existing directories.
264 static int threaded_has_dirs_only_path(struct cache_def
*cache
, const char *name
, int len
, int prefix_len
)
267 * Note: this function is used by the checkout machinery, which also
268 * takes care to properly reset the cache when it performs an operation
269 * that would leave the cache outdated. If this function starts caching
270 * anything else besides FL_DIR, remember to also invalidate the cache
271 * when creating or deleting paths that might be in the cache.
273 return lstat_cache(cache
, name
, len
,
274 FL_DIR
|FL_FULLPATH
, prefix_len
) &
278 static struct strbuf removal
= STRBUF_INIT
;
280 static void do_remove_scheduled_dirs(int new_len
)
282 while (removal
.len
> new_len
) {
283 removal
.buf
[removal
.len
] = '\0';
284 if ((startup_info
->original_cwd
&&
285 !strcmp(removal
.buf
, startup_info
->original_cwd
)) ||
290 } while (removal
.len
> new_len
&&
291 removal
.buf
[removal
.len
] != '/');
293 removal
.len
= new_len
;
296 void schedule_dir_for_removal(const char *name
, int len
)
298 int match_len
, last_slash
, i
, previous_slash
;
300 if (startup_info
->original_cwd
&&
301 !strcmp(name
, startup_info
->original_cwd
))
302 return; /* Do not remove the current working directory */
304 match_len
= last_slash
= i
=
305 longest_path_match(name
, len
, removal
.buf
, removal
.len
,
307 /* Find last slash inside 'name' */
315 * If we are about to go down the directory tree, we check if
316 * we must first go upwards the tree, such that we then can
317 * remove possible empty directories as we go upwards.
319 if (match_len
< last_slash
&& match_len
< removal
.len
)
320 do_remove_scheduled_dirs(match_len
);
322 * If we go deeper down the directory tree, we only need to
323 * save the new path components as we go down.
325 if (match_len
< last_slash
)
326 strbuf_add(&removal
, &name
[match_len
], last_slash
- match_len
);
329 void remove_scheduled_dirs(void)
331 do_remove_scheduled_dirs(0);
334 void invalidate_lstat_cache(void)
336 reset_lstat_cache(&default_cache
);
340 int lstat_cache_aware_rmdir(const char *path
)
342 /* Any change in this function must be made also in `mingw_rmdir()` */
343 int ret
= rmdir(path
);
346 invalidate_lstat_cache();