]> git.ipfire.org Git - thirdparty/git.git/blame - symlinks.c
Demonstrate bugs when a directory is replaced with a symlink
[thirdparty/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 */
867f72bf 67static int lstat_cache(struct cache_def *cache, const char *name, int len,
bad4a54f 68 int track_flags, int prefix_len_stat_func)
c40641b7 69{
aeabab5c 70 int match_len, last_slash, last_slash_dir, previous_slash;
bad4a54f 71 int match_flags, ret_flags, save_flags, max_len, ret;
c40641b7 72 struct stat st;
f859c846 73
867f72bf
LT
74 if (cache->track_flags != track_flags ||
75 cache->prefix_len_stat_func != prefix_len_stat_func) {
09c93066 76 /*
60b458b7
KB
77 * As a safeguard rule we clear the cache if the
78 * values of track_flags and/or prefix_len_stat_func
79 * does not match with the last supplied values.
09c93066 80 */
867f72bf
LT
81 reset_lstat_cache(cache);
82 cache->track_flags = track_flags;
83 cache->prefix_len_stat_func = prefix_len_stat_func;
09c93066
KB
84 match_len = last_slash = 0;
85 } else {
86 /*
87 * Check to see if we have a match from the cache for
88 * the 2 "excluding" path types.
89 */
aeabab5c 90 match_len = last_slash =
867f72bf 91 longest_path_match(name, len, cache->path, cache->len,
148bc06b 92 &previous_slash);
867f72bf
LT
93 match_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
94 if (match_flags && match_len == cache->len)
09c93066
KB
95 return match_flags;
96 /*
97 * If we now have match_len > 0, we would know that
98 * the matched part will always be a directory.
99 *
100 * Also, if we are tracking directories and 'name' is
101 * a substring of the cache on a path component basis,
102 * we can return immediately.
103 */
104 match_flags = track_flags & FL_DIR;
105 if (match_flags && len == match_len)
106 return match_flags;
107 }
c40641b7 108
92604b46
KB
109 /*
110 * Okay, no match from the cache so far, so now we have to
111 * check the rest of the path components.
112 */
113 ret_flags = FL_DIR;
114 last_slash_dir = last_slash;
115 max_len = len < PATH_MAX ? len : PATH_MAX;
116 while (match_len < max_len) {
117 do {
867f72bf 118 cache->path[match_len] = name[match_len];
92604b46
KB
119 match_len++;
120 } while (match_len < max_len && name[match_len] != '/');
bad4a54f 121 if (match_len >= max_len && !(track_flags & FL_FULLPATH))
92604b46
KB
122 break;
123 last_slash = match_len;
867f72bf 124 cache->path[last_slash] = '\0';
f859c846 125
bad4a54f 126 if (last_slash <= prefix_len_stat_func)
867f72bf 127 ret = stat(cache->path, &st);
bad4a54f 128 else
867f72bf 129 ret = lstat(cache->path, &st);
bad4a54f
KB
130
131 if (ret) {
92604b46 132 ret_flags = FL_LSTATERR;
09c93066
KB
133 if (errno == ENOENT)
134 ret_flags |= FL_NOENT;
92604b46
KB
135 } else if (S_ISDIR(st.st_mode)) {
136 last_slash_dir = last_slash;
c40641b7 137 continue;
92604b46
KB
138 } else if (S_ISLNK(st.st_mode)) {
139 ret_flags = FL_SYMLINK;
140 } else {
141 ret_flags = FL_ERR;
f859c846 142 }
c40641b7 143 break;
f859c846 144 }
92604b46
KB
145
146 /*
09c93066
KB
147 * At the end update the cache. Note that max 3 different
148 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
149 * for the moment!
92604b46 150 */
09c93066 151 save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
bad4a54f 152 if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
867f72bf
LT
153 cache->path[last_slash] = '\0';
154 cache->len = last_slash;
155 cache->flags = save_flags;
60b458b7 156 } else if ((track_flags & FL_DIR) &&
bad4a54f 157 last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
92604b46
KB
158 /*
159 * We have a separate test for the directory case,
09c93066
KB
160 * since it could be that we have found a symlink or a
161 * non-existing directory and the track_flags says
162 * that we cannot cache this fact, so the cache would
163 * then have been left empty in this case.
92604b46
KB
164 *
165 * But if we are allowed to track real directories, we
166 * can still cache the path components before the last
09c93066 167 * one (the found symlink or non-existing component).
92604b46 168 */
867f72bf
LT
169 cache->path[last_slash_dir] = '\0';
170 cache->len = last_slash_dir;
171 cache->flags = FL_DIR;
92604b46 172 } else {
867f72bf 173 reset_lstat_cache(cache);
92604b46
KB
174 }
175 return ret_flags;
176}
177
aeabab5c
KB
178/*
179 * Invalidate the given 'name' from the cache, if 'name' matches
180 * completely with the cache.
181 */
57199892 182void invalidate_lstat_cache(const char *name, int len)
aeabab5c
KB
183{
184 int match_len, previous_slash;
867f72bf 185 struct cache_def *cache = &default_cache; /* FIXME */
aeabab5c 186
867f72bf 187 match_len = longest_path_match(name, len, cache->path, cache->len,
148bc06b 188 &previous_slash);
aeabab5c 189 if (len == match_len) {
867f72bf
LT
190 if ((cache->track_flags & FL_DIR) && previous_slash > 0) {
191 cache->path[previous_slash] = '\0';
192 cache->len = previous_slash;
193 cache->flags = FL_DIR;
cb319c36 194 } else {
867f72bf 195 reset_lstat_cache(cache);
cb319c36 196 }
aeabab5c
KB
197 }
198}
199
bda6eb0d
KB
200/*
201 * Completely clear the contents of the cache
202 */
203void clear_lstat_cache(void)
204{
867f72bf
LT
205 struct cache_def *cache = &default_cache; /* FIXME */
206 reset_lstat_cache(cache);
bda6eb0d
KB
207}
208
bad4a54f
KB
209#define USE_ONLY_LSTAT 0
210
b9fd2846
LT
211/*
212 * Return non-zero if path 'name' has a leading symlink component
213 */
214int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len)
215{
216 return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK;
217}
218
92604b46
KB
219/*
220 * Return non-zero if path 'name' has a leading symlink component
221 */
57199892 222int has_symlink_leading_path(const char *name, int len)
92604b46 223{
b9fd2846 224 return threaded_has_symlink_leading_path(&default_cache, name, len);
f859c846 225}
09c93066
KB
226
227/*
228 * Return non-zero if path 'name' has a leading symlink component or
229 * if some leading path component does not exists.
230 */
57199892 231int has_symlink_or_noent_leading_path(const char *name, int len)
09c93066 232{
867f72bf
LT
233 struct cache_def *cache = &default_cache; /* FIXME */
234 return lstat_cache(cache, name, len,
bad4a54f 235 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
09c93066
KB
236 (FL_SYMLINK|FL_NOENT);
237}
bad4a54f
KB
238
239/*
240 * Return non-zero if all path components of 'name' exists as a
241 * directory. If prefix_len > 0, we will test with the stat()
242 * function instead of the lstat() function for a prefix length of
243 * 'prefix_len', thus we then allow for symlinks in the prefix part as
244 * long as those points to real existing directories.
245 */
57199892 246int has_dirs_only_path(const char *name, int len, int prefix_len)
bad4a54f 247{
867f72bf
LT
248 struct cache_def *cache = &default_cache; /* FIXME */
249 return lstat_cache(cache, name, len,
bad4a54f
KB
250 FL_DIR|FL_FULLPATH, prefix_len) &
251 FL_DIR;
252}
78478927
KB
253
254static struct removal_def {
255 char path[PATH_MAX];
256 int len;
257} removal;
258
259static void do_remove_scheduled_dirs(int new_len)
260{
261 while (removal.len > new_len) {
262 removal.path[removal.len] = '\0';
263 if (rmdir(removal.path))
264 break;
265 do {
266 removal.len--;
267 } while (removal.len > new_len &&
268 removal.path[removal.len] != '/');
269 }
270 removal.len = new_len;
78478927
KB
271}
272
273void schedule_dir_for_removal(const char *name, int len)
274{
275 int match_len, last_slash, i, previous_slash;
276
277 match_len = last_slash = i =
278 longest_path_match(name, len, removal.path, removal.len,
279 &previous_slash);
280 /* Find last slash inside 'name' */
281 while (i < len) {
282 if (name[i] == '/')
283 last_slash = i;
284 i++;
285 }
286
287 /*
288 * If we are about to go down the directory tree, we check if
289 * we must first go upwards the tree, such that we then can
290 * remove possible empty directories as we go upwards.
291 */
292 if (match_len < last_slash && match_len < removal.len)
293 do_remove_scheduled_dirs(match_len);
294 /*
295 * If we go deeper down the directory tree, we only need to
296 * save the new path components as we go down.
297 */
298 if (match_len < last_slash) {
299 memcpy(&removal.path[match_len], &name[match_len],
300 last_slash - match_len);
301 removal.len = last_slash;
302 }
78478927
KB
303}
304
305void remove_scheduled_dirs(void)
306{
307 do_remove_scheduled_dirs(0);
78478927 308}