4 static int threaded_check_leading_path(struct cache_def
*cache
, const char *name
, int len
);
5 static int threaded_has_dirs_only_path(struct cache_def
*cache
, const char *name
, int len
, int prefix_len
);
8 * Returns the length (on a path component basis) of the longest
9 * common prefix match of 'name_a' and 'name_b'.
11 static int longest_path_match(const char *name_a
, int len_a
,
12 const char *name_b
, int len_b
,
15 int max_len
, match_len
= 0, match_len_prev
= 0, i
= 0;
17 max_len
= len_a
< len_b
? len_a
: len_b
;
18 while (i
< max_len
&& name_a
[i
] == name_b
[i
]) {
19 if (name_a
[i
] == '/') {
20 match_len_prev
= match_len
;
26 * Is 'name_b' a substring of 'name_a', the other way around,
27 * or is 'name_a' and 'name_b' the exact same string?
29 if (i
>= max_len
&& ((len_a
> len_b
&& name_a
[len_b
] == '/') ||
30 (len_a
< len_b
&& name_b
[len_a
] == '/') ||
32 match_len_prev
= match_len
;
35 *previous_slash
= match_len_prev
;
39 static struct cache_def default_cache
;
41 static inline void reset_lstat_cache(struct cache_def
*cache
)
43 cache
->path
[0] = '\0';
47 * The track_flags and prefix_len_stat_func members is only
48 * set by the safeguard rule inside lstat_cache()
52 #define FL_DIR (1 << 0)
53 #define FL_NOENT (1 << 1)
54 #define FL_SYMLINK (1 << 2)
55 #define FL_LSTATERR (1 << 3)
56 #define FL_ERR (1 << 4)
57 #define FL_FULLPATH (1 << 5)
60 * Check if name 'name' of length 'len' has a symlink leading
61 * component, or if the directory exists and is real, or not.
63 * To speed up the check, some information is allowed to be cached.
64 * This can be indicated by the 'track_flags' argument, which also can
65 * be used to indicate that we should check the full path.
67 * The 'prefix_len_stat_func' parameter can be used to set the length
68 * of the prefix, where the cache should use the stat() function
69 * instead of the lstat() function to test each path component.
71 static int lstat_cache_matchlen(struct cache_def
*cache
,
72 const char *name
, int len
,
73 int *ret_flags
, int track_flags
,
74 int prefix_len_stat_func
)
76 int match_len
, last_slash
, last_slash_dir
, previous_slash
;
80 if (cache
->track_flags
!= track_flags
||
81 cache
->prefix_len_stat_func
!= prefix_len_stat_func
) {
83 * As a safeguard rule we clear the cache if the
84 * values of track_flags and/or prefix_len_stat_func
85 * does not match with the last supplied values.
87 reset_lstat_cache(cache
);
88 cache
->track_flags
= track_flags
;
89 cache
->prefix_len_stat_func
= prefix_len_stat_func
;
90 match_len
= last_slash
= 0;
93 * Check to see if we have a match from the cache for
94 * the 2 "excluding" path types.
96 match_len
= last_slash
=
97 longest_path_match(name
, len
, cache
->path
, cache
->len
,
99 *ret_flags
= cache
->flags
& track_flags
& (FL_NOENT
|FL_SYMLINK
);
101 if (!(track_flags
& FL_FULLPATH
) && match_len
== len
)
102 match_len
= last_slash
= previous_slash
;
104 if (*ret_flags
&& match_len
== cache
->len
)
107 * If we now have match_len > 0, we would know that
108 * the matched part will always be a directory.
110 * Also, if we are tracking directories and 'name' is
111 * a substring of the cache on a path component basis,
112 * we can return immediately.
114 *ret_flags
= track_flags
& FL_DIR
;
115 if (*ret_flags
&& len
== match_len
)
120 * Okay, no match from the cache so far, so now we have to
121 * check the rest of the path components.
124 last_slash_dir
= last_slash
;
125 while (match_len
< len
) {
127 cache
->path
[match_len
] = name
[match_len
];
129 } while (match_len
< len
&& name
[match_len
] != '/');
130 if (match_len
>= len
&& !(track_flags
& FL_FULLPATH
))
132 last_slash
= match_len
;
133 cache
->path
[last_slash
] = '\0';
135 if (last_slash
<= prefix_len_stat_func
)
136 ret
= stat(cache
->path
, &st
);
138 ret
= lstat(cache
->path
, &st
);
141 *ret_flags
= FL_LSTATERR
;
143 *ret_flags
|= FL_NOENT
;
144 } else if (S_ISDIR(st
.st_mode
)) {
145 last_slash_dir
= last_slash
;
147 } else if (S_ISLNK(st
.st_mode
)) {
148 *ret_flags
= FL_SYMLINK
;
156 * At the end update the cache. Note that max 3 different
157 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
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
;
164 cache
->flags
= save_flags
;
165 } else if ((track_flags
& FL_DIR
) &&
166 last_slash_dir
> 0 && last_slash_dir
<= PATH_MAX
) {
168 * We have a separate test for the directory case,
169 * since it could be that we have found a symlink or a
170 * non-existing directory and the track_flags says
171 * that we cannot cache this fact, so the cache would
172 * then have been left empty in this case.
174 * But if we are allowed to track real directories, we
175 * can still cache the path components before the last
176 * one (the found symlink or non-existing component).
178 cache
->path
[last_slash_dir
] = '\0';
179 cache
->len
= last_slash_dir
;
180 cache
->flags
= FL_DIR
;
182 reset_lstat_cache(cache
);
187 static int lstat_cache(struct cache_def
*cache
, const char *name
, int len
,
188 int track_flags
, int prefix_len_stat_func
)
191 (void)lstat_cache_matchlen(cache
, name
, len
, &flags
, track_flags
,
192 prefix_len_stat_func
);
196 #define USE_ONLY_LSTAT 0
199 * Return non-zero if path 'name' has a leading symlink component
201 int threaded_has_symlink_leading_path(struct cache_def
*cache
, const char *name
, int len
)
203 return lstat_cache(cache
, name
, len
, FL_SYMLINK
|FL_DIR
, USE_ONLY_LSTAT
) & FL_SYMLINK
;
207 * Return non-zero if path 'name' has a leading symlink component
209 int has_symlink_leading_path(const char *name
, int len
)
211 return threaded_has_symlink_leading_path(&default_cache
, name
, len
);
215 * Return zero if path 'name' has a leading symlink component or
216 * if some leading path component does not exists.
218 * Return -1 if leading path exists and is a directory.
220 * Return path length if leading path exists and is neither a
221 * directory nor a symlink.
223 int check_leading_path(const char *name
, int len
)
225 return threaded_check_leading_path(&default_cache
, name
, len
);
229 * Return zero if path 'name' has a leading symlink component or
230 * if some leading path component does not exists.
232 * Return -1 if leading path exists and is a directory.
234 * Return path length if leading path exists and is neither a
235 * directory nor a symlink.
237 static int threaded_check_leading_path(struct cache_def
*cache
, const char *name
, int len
)
240 int match_len
= lstat_cache_matchlen(cache
, name
, len
, &flags
,
241 FL_SYMLINK
|FL_NOENT
|FL_DIR
, USE_ONLY_LSTAT
);
242 if (flags
& FL_NOENT
)
244 else if (flags
& FL_DIR
)
251 * Return non-zero if all path components of 'name' exists as a
252 * directory. If prefix_len > 0, we will test with the stat()
253 * function instead of the lstat() function for a prefix length of
254 * 'prefix_len', thus we then allow for symlinks in the prefix part as
255 * long as those points to real existing directories.
257 int has_dirs_only_path(const char *name
, int len
, int prefix_len
)
259 return threaded_has_dirs_only_path(&default_cache
, name
, len
, prefix_len
);
263 * Return non-zero if all path components of 'name' exists as a
264 * directory. If prefix_len > 0, we will test with the stat()
265 * function instead of the lstat() function for a prefix length of
266 * 'prefix_len', thus we then allow for symlinks in the prefix part as
267 * long as those points to real existing directories.
269 static int threaded_has_dirs_only_path(struct cache_def
*cache
, const char *name
, int len
, int prefix_len
)
271 return lstat_cache(cache
, name
, len
,
272 FL_DIR
|FL_FULLPATH
, prefix_len
) &
276 struct strbuf removal
= STRBUF_INIT
;
278 static void do_remove_scheduled_dirs(int new_len
)
280 while (removal
.len
> new_len
) {
281 removal
.buf
[removal
.len
] = '\0';
282 if (rmdir(removal
.buf
))
286 } while (removal
.len
> new_len
&&
287 removal
.buf
[removal
.len
] != '/');
289 removal
.len
= new_len
;
292 void schedule_dir_for_removal(const char *name
, int len
)
294 int match_len
, last_slash
, i
, previous_slash
;
296 match_len
= last_slash
= i
=
297 longest_path_match(name
, len
, removal
.buf
, removal
.len
,
299 /* Find last slash inside 'name' */
307 * If we are about to go down the directory tree, we check if
308 * we must first go upwards the tree, such that we then can
309 * remove possible empty directories as we go upwards.
311 if (match_len
< last_slash
&& match_len
< removal
.len
)
312 do_remove_scheduled_dirs(match_len
);
314 * If we go deeper down the directory tree, we only need to
315 * save the new path components as we go down.
317 if (match_len
< last_slash
)
318 strbuf_add(&removal
, &name
[match_len
], last_slash
- match_len
);
321 void remove_scheduled_dirs(void)
323 do_remove_scheduled_dirs(0);