]>
Commit | Line | Data |
---|---|---|
f859c846 JH |
1 | #include "cache.h" |
2 | ||
92604b46 | 3 | static struct cache_def { |
bad4a54f | 4 | char path[PATH_MAX + 1]; |
92604b46 KB |
5 | int len; |
6 | int flags; | |
09c93066 | 7 | int track_flags; |
bad4a54f | 8 | int prefix_len_stat_func; |
92604b46 | 9 | } cache; |
c40641b7 | 10 | |
92604b46 KB |
11 | /* |
12 | * Returns the length (on a path component basis) of the longest | |
13 | * common prefix match of 'name' and the cached path string. | |
14 | */ | |
aeabab5c KB |
15 | static inline int longest_match_lstat_cache(int len, const char *name, |
16 | int *previous_slash) | |
c40641b7 | 17 | { |
aeabab5c | 18 | int max_len, match_len = 0, match_len_prev = 0, i = 0; |
92604b46 KB |
19 | |
20 | max_len = len < cache.len ? len : cache.len; | |
21 | while (i < max_len && name[i] == cache.path[i]) { | |
aeabab5c KB |
22 | if (name[i] == '/') { |
23 | match_len_prev = match_len; | |
92604b46 | 24 | match_len = i; |
aeabab5c | 25 | } |
92604b46 KB |
26 | i++; |
27 | } | |
28 | /* Is the cached path string a substring of 'name'? */ | |
aeabab5c KB |
29 | if (i == cache.len && cache.len < len && name[cache.len] == '/') { |
30 | match_len_prev = match_len; | |
92604b46 KB |
31 | match_len = cache.len; |
32 | /* Is 'name' a substring of the cached path string? */ | |
aeabab5c KB |
33 | } else if ((i == len && len < cache.len && cache.path[len] == '/') || |
34 | (i == len && len == cache.len)) { | |
35 | match_len_prev = match_len; | |
92604b46 | 36 | match_len = len; |
aeabab5c KB |
37 | } |
38 | *previous_slash = match_len_prev; | |
92604b46 | 39 | return match_len; |
c40641b7 LT |
40 | } |
41 | ||
bad4a54f | 42 | static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func) |
c40641b7 | 43 | { |
92604b46 KB |
44 | cache.path[0] = '\0'; |
45 | cache.len = 0; | |
46 | cache.flags = 0; | |
09c93066 | 47 | cache.track_flags = track_flags; |
bad4a54f | 48 | cache.prefix_len_stat_func = prefix_len_stat_func; |
c40641b7 LT |
49 | } |
50 | ||
92604b46 | 51 | #define FL_DIR (1 << 0) |
09c93066 KB |
52 | #define FL_NOENT (1 << 1) |
53 | #define FL_SYMLINK (1 << 2) | |
54 | #define FL_LSTATERR (1 << 3) | |
55 | #define FL_ERR (1 << 4) | |
bad4a54f | 56 | #define FL_FULLPATH (1 << 5) |
92604b46 KB |
57 | |
58 | /* | |
59 | * Check if name 'name' of length 'len' has a symlink leading | |
09c93066 | 60 | * component, or if the directory exists and is real, or not. |
92604b46 KB |
61 | * |
62 | * To speed up the check, some information is allowed to be cached. | |
bad4a54f KB |
63 | * This can be indicated by the 'track_flags' argument, which also can |
64 | * be used to indicate that we should check the full path. | |
65 | * | |
66 | * The 'prefix_len_stat_func' parameter can be used to set the length | |
67 | * of the prefix, where the cache should use the stat() function | |
68 | * instead of the lstat() function to test each path component. | |
92604b46 KB |
69 | */ |
70 | static int lstat_cache(int len, const char *name, | |
bad4a54f | 71 | int track_flags, int prefix_len_stat_func) |
c40641b7 | 72 | { |
aeabab5c | 73 | int match_len, last_slash, last_slash_dir, previous_slash; |
bad4a54f | 74 | int match_flags, ret_flags, save_flags, max_len, ret; |
c40641b7 | 75 | struct stat st; |
f859c846 | 76 | |
bad4a54f KB |
77 | if (cache.track_flags != track_flags || |
78 | cache.prefix_len_stat_func != prefix_len_stat_func) { | |
09c93066 | 79 | /* |
bad4a54f KB |
80 | * As a safeguard we clear the cache if the values of |
81 | * track_flags and/or prefix_len_stat_func does not | |
82 | * match with the last supplied values. | |
09c93066 | 83 | */ |
bad4a54f | 84 | reset_lstat_cache(track_flags, prefix_len_stat_func); |
09c93066 KB |
85 | match_len = last_slash = 0; |
86 | } else { | |
87 | /* | |
88 | * Check to see if we have a match from the cache for | |
89 | * the 2 "excluding" path types. | |
90 | */ | |
aeabab5c KB |
91 | match_len = last_slash = |
92 | longest_match_lstat_cache(len, name, &previous_slash); | |
09c93066 KB |
93 | match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK); |
94 | if (match_flags && match_len == cache.len) | |
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 { | |
118 | cache.path[match_len] = name[match_len]; | |
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; | |
124 | cache.path[last_slash] = '\0'; | |
f859c846 | 125 | |
bad4a54f KB |
126 | if (last_slash <= prefix_len_stat_func) |
127 | ret = stat(cache.path, &st); | |
128 | else | |
129 | ret = lstat(cache.path, &st); | |
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) { |
92604b46 KB |
153 | cache.path[last_slash] = '\0'; |
154 | cache.len = last_slash; | |
155 | cache.flags = save_flags; | |
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 KB |
168 | */ |
169 | cache.path[last_slash_dir] = '\0'; | |
170 | cache.len = last_slash_dir; | |
171 | cache.flags = FL_DIR; | |
172 | } else { | |
bad4a54f | 173 | reset_lstat_cache(track_flags, prefix_len_stat_func); |
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 | */ | |
182 | void invalidate_lstat_cache(int len, const char *name) | |
183 | { | |
184 | int match_len, previous_slash; | |
185 | ||
186 | match_len = longest_match_lstat_cache(len, name, &previous_slash); | |
187 | if (len == match_len) { | |
188 | if ((cache.track_flags & FL_DIR) && previous_slash > 0) { | |
189 | cache.path[previous_slash] = '\0'; | |
190 | cache.len = previous_slash; | |
191 | cache.flags = FL_DIR; | |
192 | } else | |
193 | reset_lstat_cache(cache.track_flags, | |
194 | cache.prefix_len_stat_func); | |
195 | } | |
196 | } | |
197 | ||
bda6eb0d KB |
198 | /* |
199 | * Completely clear the contents of the cache | |
200 | */ | |
201 | void clear_lstat_cache(void) | |
202 | { | |
203 | reset_lstat_cache(0, 0); | |
204 | } | |
205 | ||
bad4a54f KB |
206 | #define USE_ONLY_LSTAT 0 |
207 | ||
92604b46 KB |
208 | /* |
209 | * Return non-zero if path 'name' has a leading symlink component | |
210 | */ | |
211 | int has_symlink_leading_path(int len, const char *name) | |
212 | { | |
213 | return lstat_cache(len, name, | |
bad4a54f | 214 | FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & |
92604b46 | 215 | FL_SYMLINK; |
f859c846 | 216 | } |
09c93066 KB |
217 | |
218 | /* | |
219 | * Return non-zero if path 'name' has a leading symlink component or | |
220 | * if some leading path component does not exists. | |
221 | */ | |
222 | int has_symlink_or_noent_leading_path(int len, const char *name) | |
223 | { | |
224 | return lstat_cache(len, name, | |
bad4a54f | 225 | FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) & |
09c93066 KB |
226 | (FL_SYMLINK|FL_NOENT); |
227 | } | |
bad4a54f KB |
228 | |
229 | /* | |
230 | * Return non-zero if all path components of 'name' exists as a | |
231 | * directory. If prefix_len > 0, we will test with the stat() | |
232 | * function instead of the lstat() function for a prefix length of | |
233 | * 'prefix_len', thus we then allow for symlinks in the prefix part as | |
234 | * long as those points to real existing directories. | |
235 | */ | |
236 | int has_dirs_only_path(int len, const char *name, int prefix_len) | |
237 | { | |
238 | return lstat_cache(len, name, | |
239 | FL_DIR|FL_FULLPATH, prefix_len) & | |
240 | FL_DIR; | |
241 | } |