3 static struct cache_def {
4 char path[PATH_MAX + 1];
8 int prefix_len_stat_func;
12 * Returns the length (on a path component basis) of the longest
13 * common prefix match of 'name' and the cached path string.
15 static inline int longest_match_lstat_cache(int len, const char *name)
17 int max_len, match_len = 0, i = 0;
19 max_len = len < cache.len ? len : cache.len;
20 while (i < max_len && name[i] == cache.path[i]) {
25 /* Is the cached path string a substring of 'name'? */
26 if (i == cache.len && cache.len < len && name[cache.len] == '/')
27 match_len = cache.len;
28 /* Is 'name' a substring of the cached path string? */
29 else if ((i == len && len < cache.len && cache.path[len] == '/') ||
30 (i == len && len == cache.len))
35 static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func)
40 cache.track_flags = track_flags;
41 cache.prefix_len_stat_func = prefix_len_stat_func;
44 #define FL_DIR (1 << 0)
45 #define FL_NOENT (1 << 1)
46 #define FL_SYMLINK (1 << 2)
47 #define FL_LSTATERR (1 << 3)
48 #define FL_ERR (1 << 4)
49 #define FL_FULLPATH (1 << 5)
52 * Check if name 'name' of length 'len' has a symlink leading
53 * component, or if the directory exists and is real, or not.
55 * To speed up the check, some information is allowed to be cached.
56 * This can be indicated by the 'track_flags' argument, which also can
57 * be used to indicate that we should check the full path.
59 * The 'prefix_len_stat_func' parameter can be used to set the length
60 * of the prefix, where the cache should use the stat() function
61 * instead of the lstat() function to test each path component.
63 static int lstat_cache(int len, const char *name,
64 int track_flags, int prefix_len_stat_func)
66 int match_len, last_slash, last_slash_dir;
67 int match_flags, ret_flags, save_flags, max_len, ret;
70 if (cache.track_flags != track_flags ||
71 cache.prefix_len_stat_func != prefix_len_stat_func) {
73 * As a safeguard we clear the cache if the values of
74 * track_flags and/or prefix_len_stat_func does not
75 * match with the last supplied values.
77 reset_lstat_cache(track_flags, prefix_len_stat_func);
78 match_len = last_slash = 0;
81 * Check to see if we have a match from the cache for
82 * the 2 "excluding" path types.
84 match_len = last_slash = longest_match_lstat_cache(len, name);
85 match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK);
86 if (match_flags && match_len == cache.len)
89 * If we now have match_len > 0, we would know that
90 * the matched part will always be a directory.
92 * Also, if we are tracking directories and 'name' is
93 * a substring of the cache on a path component basis,
94 * we can return immediately.
96 match_flags = track_flags & FL_DIR;
97 if (match_flags && len == match_len)
102 * Okay, no match from the cache so far, so now we have to
103 * check the rest of the path components.
106 last_slash_dir = last_slash;
107 max_len = len < PATH_MAX ? len : PATH_MAX;
108 while (match_len < max_len) {
110 cache.path[match_len] = name[match_len];
112 } while (match_len < max_len && name[match_len] != '/');
113 if (match_len >= max_len && !(track_flags & FL_FULLPATH))
115 last_slash = match_len;
116 cache.path[last_slash] = '\0';
118 if (last_slash <= prefix_len_stat_func)
119 ret = stat(cache.path, &st);
121 ret = lstat(cache.path, &st);
124 ret_flags = FL_LSTATERR;
126 ret_flags |= FL_NOENT;
127 } else if (S_ISDIR(st.st_mode)) {
128 last_slash_dir = last_slash;
130 } else if (S_ISLNK(st.st_mode)) {
131 ret_flags = FL_SYMLINK;
139 * At the end update the cache. Note that max 3 different
140 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
143 save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
144 if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
145 cache.path[last_slash] = '\0';
146 cache.len = last_slash;
147 cache.flags = save_flags;
148 } else if (track_flags & FL_DIR &&
149 last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
151 * We have a separate test for the directory case,
152 * since it could be that we have found a symlink or a
153 * non-existing directory and the track_flags says
154 * that we cannot cache this fact, so the cache would
155 * then have been left empty in this case.
157 * But if we are allowed to track real directories, we
158 * can still cache the path components before the last
159 * one (the found symlink or non-existing component).
161 cache.path[last_slash_dir] = '\0';
162 cache.len = last_slash_dir;
163 cache.flags = FL_DIR;
165 reset_lstat_cache(track_flags, prefix_len_stat_func);
170 #define USE_ONLY_LSTAT 0
173 * Return non-zero if path 'name' has a leading symlink component
175 int has_symlink_leading_path(int len, const char *name)
177 return lstat_cache(len, name,
178 FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) &
183 * Return non-zero if path 'name' has a leading symlink component or
184 * if some leading path component does not exists.
186 int has_symlink_or_noent_leading_path(int len, const char *name)
188 return lstat_cache(len, name,
189 FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
190 (FL_SYMLINK|FL_NOENT);
194 * Return non-zero if all path components of 'name' exists as a
195 * directory. If prefix_len > 0, we will test with the stat()
196 * function instead of the lstat() function for a prefix length of
197 * 'prefix_len', thus we then allow for symlinks in the prefix part as
198 * long as those points to real existing directories.
200 int has_dirs_only_path(int len, const char *name, int prefix_len)
202 return lstat_cache(len, name,
203 FL_DIR|FL_FULLPATH, prefix_len) &