notes.c: mark a private file-scope symbol as static
[git/git.git] / symlinks.c
CommitLineData
f859c846
JH
1#include "cache.h"
2
92604b46
KB
3/*
4 * Returns the length (on a path component basis) of the longest
148bc06b 5 * common prefix match of 'name_a' and 'name_b'.
92604b46 6 */
148bc06b
KB
7static int longest_path_match(const char *name_a, int len_a,
8 const char *name_b, int len_b,
9 int *previous_slash)
c40641b7 10{
aeabab5c 11 int max_len, match_len = 0, match_len_prev = 0, i = 0;
92604b46 12
148bc06b
KB
13 max_len = len_a < len_b ? len_a : len_b;
14 while (i < max_len && name_a[i] == name_b[i]) {
15 if (name_a[i] == '/') {
aeabab5c 16 match_len_prev = match_len;
92604b46 17 match_len = i;
aeabab5c 18 }
92604b46
KB
19 i++;
20 }
60b458b7 21 /*
148bc06b
KB
22 * Is 'name_b' a substring of 'name_a', the other way around,
23 * or is 'name_a' and 'name_b' the exact same string?
60b458b7 24 */
148bc06b
KB
25 if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||
26 (len_a < len_b && name_b[len_a] == '/') ||
27 (len_a == len_b))) {
aeabab5c 28 match_len_prev = match_len;
60b458b7 29 match_len = i;
aeabab5c
KB
30 }
31 *previous_slash = match_len_prev;
92604b46 32 return match_len;
c40641b7
LT
33}
34
b9fd2846 35static struct cache_def default_cache;
148bc06b 36
867f72bf 37static inline void reset_lstat_cache(struct cache_def *cache)
c40641b7 38{
867f72bf
LT
39 cache->path[0] = '\0';
40 cache->len = 0;
41 cache->flags = 0;
60b458b7
KB
42 /*
43 * The track_flags and prefix_len_stat_func members is only
44 * set by the safeguard rule inside lstat_cache()
45 */
c40641b7
LT
46}
47
92604b46 48#define FL_DIR (1 << 0)
09c93066
KB
49#define FL_NOENT (1 << 1)
50#define FL_SYMLINK (1 << 2)
51#define FL_LSTATERR (1 << 3)
52#define FL_ERR (1 << 4)
bad4a54f 53#define FL_FULLPATH (1 << 5)
92604b46
KB
54
55/*
56 * Check if name 'name' of length 'len' has a symlink leading
09c93066 57 * component, or if the directory exists and is real, or not.
92604b46
KB
58 *
59 * To speed up the check, some information is allowed to be cached.
bad4a54f
KB
60 * This can be indicated by the 'track_flags' argument, which also can
61 * be used to indicate that we should check the full path.
62 *
63 * The 'prefix_len_stat_func' parameter can be used to set the length
64 * of the prefix, where the cache should use the stat() function
65 * instead of the lstat() function to test each path component.
92604b46 66 */
4856ff2a
CB
67static int lstat_cache_matchlen(struct cache_def *cache,
68 const char *name, int len,
69 int *ret_flags, int track_flags,
70 int prefix_len_stat_func)
c40641b7 71{
aeabab5c 72 int match_len, last_slash, last_slash_dir, previous_slash;
4856ff2a 73 int save_flags, max_len, ret;
c40641b7 74 struct stat st;
f859c846 75
867f72bf
LT
76 if (cache->track_flags != track_flags ||
77 cache->prefix_len_stat_func != prefix_len_stat_func) {
09c93066 78 /*
60b458b7
KB
79 * As a safeguard rule we clear the cache if the
80 * values of track_flags and/or prefix_len_stat_func
81 * does not match with the last supplied values.
09c93066 82 */
867f72bf
LT
83 reset_lstat_cache(cache);
84 cache->track_flags = track_flags;
85 cache->prefix_len_stat_func = prefix_len_stat_func;
09c93066
KB
86 match_len = last_slash = 0;
87 } else {
88 /*
89 * Check to see if we have a match from the cache for
90 * the 2 "excluding" path types.
91 */
aeabab5c 92 match_len = last_slash =
867f72bf 93 longest_path_match(name, len, cache->path, cache->len,
148bc06b 94 &previous_slash);
4856ff2a 95 *ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
77716755
KB
96
97 if (!(track_flags & FL_FULLPATH) && match_len == len)
98 match_len = last_slash = previous_slash;
99
4856ff2a
CB
100 if (*ret_flags && match_len == cache->len)
101 return match_len;
09c93066
KB
102 /*
103 * If we now have match_len > 0, we would know that
104 * the matched part will always be a directory.
105 *
106 * Also, if we are tracking directories and 'name' is
107 * a substring of the cache on a path component basis,
108 * we can return immediately.
109 */
4856ff2a
CB
110 *ret_flags = track_flags & FL_DIR;
111 if (*ret_flags && len == match_len)
112 return match_len;
09c93066 113 }
c40641b7 114
92604b46
KB
115 /*
116 * Okay, no match from the cache so far, so now we have to
117 * check the rest of the path components.
118 */
4856ff2a 119 *ret_flags = FL_DIR;
92604b46
KB
120 last_slash_dir = last_slash;
121 max_len = len < PATH_MAX ? len : PATH_MAX;
122 while (match_len < max_len) {
123 do {
867f72bf 124 cache->path[match_len] = name[match_len];
92604b46
KB
125 match_len++;
126 } while (match_len < max_len && name[match_len] != '/');
bad4a54f 127 if (match_len >= max_len && !(track_flags & FL_FULLPATH))
92604b46
KB
128 break;
129 last_slash = match_len;
867f72bf 130 cache->path[last_slash] = '\0';
f859c846 131
bad4a54f 132 if (last_slash <= prefix_len_stat_func)
867f72bf 133 ret = stat(cache->path, &st);
bad4a54f 134 else
867f72bf 135 ret = lstat(cache->path, &st);
bad4a54f
KB
136
137 if (ret) {
4856ff2a 138 *ret_flags = FL_LSTATERR;
09c93066 139 if (errno == ENOENT)
4856ff2a 140 *ret_flags |= FL_NOENT;
92604b46
KB
141 } else if (S_ISDIR(st.st_mode)) {
142 last_slash_dir = last_slash;
c40641b7 143 continue;
92604b46 144 } else if (S_ISLNK(st.st_mode)) {
4856ff2a 145 *ret_flags = FL_SYMLINK;
92604b46 146 } else {
4856ff2a 147 *ret_flags = FL_ERR;
f859c846 148 }
c40641b7 149 break;
f859c846 150 }
92604b46
KB
151
152 /*
09c93066
KB
153 * At the end update the cache. Note that max 3 different
154 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
155 * for the moment!
92604b46 156 */
4856ff2a 157 save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
bad4a54f 158 if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
867f72bf
LT
159 cache->path[last_slash] = '\0';
160 cache->len = last_slash;
161 cache->flags = save_flags;
60b458b7 162 } else if ((track_flags & FL_DIR) &&
bad4a54f 163 last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
92604b46
KB
164 /*
165 * We have a separate test for the directory case,
09c93066
KB
166 * since it could be that we have found a symlink or a
167 * non-existing directory and the track_flags says
168 * that we cannot cache this fact, so the cache would
169 * then have been left empty in this case.
92604b46
KB
170 *
171 * But if we are allowed to track real directories, we
172 * can still cache the path components before the last
09c93066 173 * one (the found symlink or non-existing component).
92604b46 174 */
867f72bf
LT
175 cache->path[last_slash_dir] = '\0';
176 cache->len = last_slash_dir;
177 cache->flags = FL_DIR;
92604b46 178 } else {
867f72bf 179 reset_lstat_cache(cache);
92604b46 180 }
4856ff2a
CB
181 return match_len;
182}
183
184static int lstat_cache(struct cache_def *cache, const char *name, int len,
185 int track_flags, int prefix_len_stat_func)
186{
187 int flags;
188 (void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,
189 prefix_len_stat_func);
190 return flags;
92604b46
KB
191}
192
bad4a54f
KB
193#define USE_ONLY_LSTAT 0
194
b9fd2846
LT
195/*
196 * Return non-zero if path 'name' has a leading symlink component
197 */
198int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len)
199{
200 return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK;
201}
202
92604b46
KB
203/*
204 * Return non-zero if path 'name' has a leading symlink component
205 */
57199892 206int has_symlink_leading_path(const char *name, int len)
92604b46 207{
b9fd2846 208 return threaded_has_symlink_leading_path(&default_cache, name, len);
f859c846 209}
09c93066
KB
210
211/*
f66caaf9 212 * Return zero if path 'name' has a leading symlink component or
09c93066 213 * if some leading path component does not exists.
f66caaf9
CB
214 *
215 * Return -1 if leading path exists and is a directory.
216 *
217 * Return path length if leading path exists and is neither a
218 * directory nor a symlink.
09c93066 219 */
f66caaf9 220int check_leading_path(const char *name, int len)
09c93066 221{
15438d5a
JH
222 return threaded_check_leading_path(&default_cache, name, len);
223}
224
225/*
226 * Return zero if path 'name' has a leading symlink component or
227 * if some leading path component does not exists.
228 *
229 * Return -1 if leading path exists and is a directory.
230 *
231 * Return path length if leading path exists and is neither a
232 * directory nor a symlink.
233 */
234int threaded_check_leading_path(struct cache_def *cache, const char *name, int len)
235{
f66caaf9
CB
236 int flags;
237 int match_len = lstat_cache_matchlen(cache, name, len, &flags,
238 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
1d718a51 239 if (flags & FL_NOENT)
f66caaf9
CB
240 return 0;
241 else if (flags & FL_DIR)
242 return -1;
243 else
244 return match_len;
09c93066 245}
bad4a54f
KB
246
247/*
248 * Return non-zero if all path components of 'name' exists as a
249 * directory. If prefix_len > 0, we will test with the stat()
250 * function instead of the lstat() function for a prefix length of
251 * 'prefix_len', thus we then allow for symlinks in the prefix part as
252 * long as those points to real existing directories.
253 */
57199892 254int has_dirs_only_path(const char *name, int len, int prefix_len)
bad4a54f 255{
15438d5a
JH
256 return threaded_has_dirs_only_path(&default_cache, name, len, prefix_len);
257}
258
259/*
260 * Return non-zero if all path components of 'name' exists as a
261 * directory. If prefix_len > 0, we will test with the stat()
262 * function instead of the lstat() function for a prefix length of
263 * 'prefix_len', thus we then allow for symlinks in the prefix part as
264 * long as those points to real existing directories.
265 */
266int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len)
267{
867f72bf 268 return lstat_cache(cache, name, len,
bad4a54f
KB
269 FL_DIR|FL_FULLPATH, prefix_len) &
270 FL_DIR;
271}
78478927
KB
272
273static struct removal_def {
274 char path[PATH_MAX];
275 int len;
276} removal;
277
278static void do_remove_scheduled_dirs(int new_len)
279{
280 while (removal.len > new_len) {
281 removal.path[removal.len] = '\0';
282 if (rmdir(removal.path))
283 break;
284 do {
285 removal.len--;
286 } while (removal.len > new_len &&
287 removal.path[removal.len] != '/');
288 }
289 removal.len = new_len;
78478927
KB
290}
291
292void schedule_dir_for_removal(const char *name, int len)
293{
294 int match_len, last_slash, i, previous_slash;
295
296 match_len = last_slash = i =
297 longest_path_match(name, len, removal.path, removal.len,
298 &previous_slash);
299 /* Find last slash inside 'name' */
300 while (i < len) {
301 if (name[i] == '/')
302 last_slash = i;
303 i++;
304 }
305
306 /*
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.
310 */
311 if (match_len < last_slash && match_len < removal.len)
312 do_remove_scheduled_dirs(match_len);
313 /*
314 * If we go deeper down the directory tree, we only need to
315 * save the new path components as we go down.
316 */
317 if (match_len < last_slash) {
318 memcpy(&removal.path[match_len], &name[match_len],
319 last_slash - match_len);
320 removal.len = last_slash;
321 }
78478927
KB
322}
323
324void remove_scheduled_dirs(void)
325{
326 do_remove_scheduled_dirs(0);
78478927 327}