git
/
symlinks.c
350 строк · 10.0 Кб
1#include "git-compat-util.h"2#include "gettext.h"3#include "setup.h"4#include "symlinks.h"5
6static int threaded_check_leading_path(struct cache_def *cache, const char *name,7int len, int warn_on_lstat_err);8static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len);9
10/*
11* Returns the length (on a path component basis) of the longest
12* common prefix match of 'name_a' and 'name_b'.
13*/
14static int longest_path_match(const char *name_a, int len_a,15const char *name_b, int len_b,16int *previous_slash)17{
18int max_len, match_len = 0, match_len_prev = 0, i = 0;19
20max_len = len_a < len_b ? len_a : len_b;21while (i < max_len && name_a[i] == name_b[i]) {22if (name_a[i] == '/') {23match_len_prev = match_len;24match_len = i;25}26i++;27}28/*29* Is 'name_b' a substring of 'name_a', the other way around,
30* or is 'name_a' and 'name_b' the exact same string?
31*/
32if (i >= max_len && ((len_a > len_b && name_a[len_b] == '/') ||33(len_a < len_b && name_b[len_a] == '/') ||34(len_a == len_b))) {35match_len_prev = match_len;36match_len = i;37}38*previous_slash = match_len_prev;39return match_len;40}
41
42static struct cache_def default_cache = CACHE_DEF_INIT;43
44static inline void reset_lstat_cache(struct cache_def *cache)45{
46strbuf_reset(&cache->path);47cache->flags = 0;48/*49* The track_flags and prefix_len_stat_func members is only
50* set by the safeguard rule inside lstat_cache()
51*/
52}
53
54#define FL_DIR (1 << 0)55#define FL_NOENT (1 << 1)56#define FL_SYMLINK (1 << 2)57#define FL_LSTATERR (1 << 3)58#define FL_ERR (1 << 4)59#define FL_FULLPATH (1 << 5)60
61/*
62* Check if name 'name' of length 'len' has a symlink leading
63* component, or if the directory exists and is real, or not.
64*
65* To speed up the check, some information is allowed to be cached.
66* This can be indicated by the 'track_flags' argument, which also can
67* be used to indicate that we should check the full path.
68*
69* The 'prefix_len_stat_func' parameter can be used to set the length
70* of the prefix, where the cache should use the stat() function
71* instead of the lstat() function to test each path component.
72*/
73static int lstat_cache_matchlen(struct cache_def *cache,74const char *name, int len,75int *ret_flags, int track_flags,76int prefix_len_stat_func)77{
78int match_len, last_slash, last_slash_dir, previous_slash;79int save_flags, ret, saved_errno = 0;80struct stat st;81
82if (cache->track_flags != track_flags ||83cache->prefix_len_stat_func != prefix_len_stat_func) {84/*85* As a safeguard rule we clear the cache if the
86* values of track_flags and/or prefix_len_stat_func
87* does not match with the last supplied values.
88*/
89reset_lstat_cache(cache);90cache->track_flags = track_flags;91cache->prefix_len_stat_func = prefix_len_stat_func;92match_len = last_slash = 0;93} else {94/*95* Check to see if we have a match from the cache for
96* the 2 "excluding" path types.
97*/
98match_len = last_slash =99longest_path_match(name, len, cache->path.buf,100cache->path.len, &previous_slash);101*ret_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);102
103if (!(track_flags & FL_FULLPATH) && match_len == len)104match_len = last_slash = previous_slash;105
106if (*ret_flags && match_len == cache->path.len)107return match_len;108/*109* If we now have match_len > 0, we would know that
110* the matched part will always be a directory.
111*
112* Also, if we are tracking directories and 'name' is
113* a substring of the cache on a path component basis,
114* we can return immediately.
115*/
116*ret_flags = track_flags & FL_DIR;117if (*ret_flags && len == match_len)118return match_len;119}120
121/*122* Okay, no match from the cache so far, so now we have to
123* check the rest of the path components.
124*/
125*ret_flags = FL_DIR;126last_slash_dir = last_slash;127if (len > cache->path.len)128strbuf_grow(&cache->path, len - cache->path.len);129while (match_len < len) {130do {131cache->path.buf[match_len] = name[match_len];132match_len++;133} while (match_len < len && name[match_len] != '/');134if (match_len >= len && !(track_flags & FL_FULLPATH))135break;136last_slash = match_len;137cache->path.buf[last_slash] = '\0';138
139if (last_slash <= prefix_len_stat_func)140ret = stat(cache->path.buf, &st);141else142ret = lstat(cache->path.buf, &st);143
144if (ret) {145*ret_flags = FL_LSTATERR;146saved_errno = errno;147if (errno == ENOENT)148*ret_flags |= FL_NOENT;149} else if (S_ISDIR(st.st_mode)) {150last_slash_dir = last_slash;151continue;152} else if (S_ISLNK(st.st_mode)) {153*ret_flags = FL_SYMLINK;154} else {155*ret_flags = FL_ERR;156}157break;158}159
160/*161* At the end update the cache. Note that max 3 different
162* path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
163* for the moment!
164*/
165save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);166if (save_flags && last_slash > 0) {167cache->path.buf[last_slash] = '\0';168cache->path.len = last_slash;169cache->flags = save_flags;170} else if ((track_flags & FL_DIR) && last_slash_dir > 0) {171/*172* We have a separate test for the directory case,
173* since it could be that we have found a symlink or a
174* non-existing directory and the track_flags says
175* that we cannot cache this fact, so the cache would
176* then have been left empty in this case.
177*
178* But if we are allowed to track real directories, we
179* can still cache the path components before the last
180* one (the found symlink or non-existing component).
181*/
182cache->path.buf[last_slash_dir] = '\0';183cache->path.len = last_slash_dir;184cache->flags = FL_DIR;185} else {186reset_lstat_cache(cache);187}188if (saved_errno)189errno = saved_errno;190return match_len;191}
192
193static int lstat_cache(struct cache_def *cache, const char *name, int len,194int track_flags, int prefix_len_stat_func)195{
196int flags;197(void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,198prefix_len_stat_func);199return flags;200}
201
202#define USE_ONLY_LSTAT 0203
204/*
205* Return non-zero if path 'name' has a leading symlink component
206*/
207int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name, int len)208{
209return lstat_cache(cache, name, len, FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & FL_SYMLINK;210}
211
212int has_symlink_leading_path(const char *name, int len)213{
214return threaded_has_symlink_leading_path(&default_cache, name, len);215}
216
217int check_leading_path(const char *name, int len, int warn_on_lstat_err)218{
219return threaded_check_leading_path(&default_cache, name, len,220warn_on_lstat_err);221}
222
223/*
224* Return zero if some leading path component of 'name' does not exist.
225*
226* Return -1 if leading path exists and is a directory.
227*
228* Return the length of a leading component if it either exists but it's not a
229* directory, or if we were unable to lstat() it. If warn_on_lstat_err is true,
230* also emit a warning for this error.
231*/
232static int threaded_check_leading_path(struct cache_def *cache, const char *name,233int len, int warn_on_lstat_err)234{
235int flags;236int match_len = lstat_cache_matchlen(cache, name, len, &flags,237FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);238int saved_errno = errno;239
240if (flags & FL_NOENT)241return 0;242else if (flags & FL_DIR)243return -1;244if (warn_on_lstat_err && (flags & FL_LSTATERR)) {245char *path = xmemdupz(name, match_len);246errno = saved_errno;247warning_errno(_("failed to lstat '%s'"), path);248free(path);249}250return match_len;251}
252
253int has_dirs_only_path(const char *name, int len, int prefix_len)254{
255return threaded_has_dirs_only_path(&default_cache, name, len, prefix_len);256}
257
258/*
259* Return non-zero if all path components of 'name' exists as a
260* directory. If prefix_len > 0, we will test with the stat()
261* function instead of the lstat() function for a prefix length of
262* 'prefix_len', thus we then allow for symlinks in the prefix part as
263* long as those points to real existing directories.
264*/
265static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name, int len, int prefix_len)266{
267/*268* Note: this function is used by the checkout machinery, which also
269* takes care to properly reset the cache when it performs an operation
270* that would leave the cache outdated. If this function starts caching
271* anything else besides FL_DIR, remember to also invalidate the cache
272* when creating or deleting paths that might be in the cache.
273*/
274return lstat_cache(cache, name, len,275FL_DIR|FL_FULLPATH, prefix_len) &276FL_DIR;277}
278
279static struct strbuf removal = STRBUF_INIT;280
281static void do_remove_scheduled_dirs(int new_len)282{
283while (removal.len > new_len) {284removal.buf[removal.len] = '\0';285if ((startup_info->original_cwd &&286!strcmp(removal.buf, startup_info->original_cwd)) ||287rmdir(removal.buf))288break;289do {290removal.len--;291} while (removal.len > new_len &&292removal.buf[removal.len] != '/');293}294removal.len = new_len;295}
296
297void schedule_dir_for_removal(const char *name, int len)298{
299int match_len, last_slash, i, previous_slash;300
301if (startup_info->original_cwd &&302!strcmp(name, startup_info->original_cwd))303return; /* Do not remove the current working directory */304
305match_len = last_slash = i =306longest_path_match(name, len, removal.buf, removal.len,307&previous_slash);308/* Find last slash inside 'name' */309while (i < len) {310if (name[i] == '/')311last_slash = i;312i++;313}314
315/*316* If we are about to go down the directory tree, we check if
317* we must first go upwards the tree, such that we then can
318* remove possible empty directories as we go upwards.
319*/
320if (match_len < last_slash && match_len < removal.len)321do_remove_scheduled_dirs(match_len);322/*323* If we go deeper down the directory tree, we only need to
324* save the new path components as we go down.
325*/
326if (match_len < last_slash)327strbuf_add(&removal, &name[match_len], last_slash - match_len);328}
329
330void remove_scheduled_dirs(void)331{
332do_remove_scheduled_dirs(0);333}
334
335void invalidate_lstat_cache(void)336{
337reset_lstat_cache(&default_cache);338}
339
340#undef rmdir341int lstat_cache_aware_rmdir(const char *path)342{
343/* Any change in this function must be made also in `mingw_rmdir()` */344int ret = rmdir(path);345
346if (!ret)347invalidate_lstat_cache();348
349return ret;350}
351