]>
Commit | Line | Data |
---|---|---|
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 |
7 | static 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 | ||
148bc06b KB |
35 | static struct cache_def { |
36 | char path[PATH_MAX + 1]; | |
37 | int len; | |
38 | int flags; | |
39 | int track_flags; | |
40 | int prefix_len_stat_func; | |
41 | } cache; | |
42 | ||
60b458b7 | 43 | static inline void reset_lstat_cache(void) |
c40641b7 | 44 | { |
92604b46 KB |
45 | cache.path[0] = '\0'; |
46 | cache.len = 0; | |
47 | cache.flags = 0; | |
60b458b7 KB |
48 | /* |
49 | * The track_flags and prefix_len_stat_func members is only | |
50 | * set by the safeguard rule inside lstat_cache() | |
51 | */ | |
c40641b7 LT |
52 | } |
53 | ||
92604b46 | 54 | #define FL_DIR (1 << 0) |
09c93066 KB |
55 | #define FL_NOENT (1 << 1) |
56 | #define FL_SYMLINK (1 << 2) | |
57 | #define FL_LSTATERR (1 << 3) | |
58 | #define FL_ERR (1 << 4) | |
bad4a54f | 59 | #define FL_FULLPATH (1 << 5) |
92604b46 KB |
60 | |
61 | /* | |
62 | * Check if name 'name' of length 'len' has a symlink leading | |
09c93066 | 63 | * component, or if the directory exists and is real, or not. |
92604b46 KB |
64 | * |
65 | * To speed up the check, some information is allowed to be cached. | |
bad4a54f KB |
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. | |
92604b46 KB |
72 | */ |
73 | static int lstat_cache(int len, const char *name, | |
bad4a54f | 74 | int track_flags, int prefix_len_stat_func) |
c40641b7 | 75 | { |
aeabab5c | 76 | int match_len, last_slash, last_slash_dir, previous_slash; |
bad4a54f | 77 | int match_flags, ret_flags, save_flags, max_len, ret; |
c40641b7 | 78 | struct stat st; |
f859c846 | 79 | |
bad4a54f KB |
80 | if (cache.track_flags != track_flags || |
81 | cache.prefix_len_stat_func != prefix_len_stat_func) { | |
09c93066 | 82 | /* |
60b458b7 KB |
83 | * As a safeguard rule we clear the cache if the |
84 | * values of track_flags and/or prefix_len_stat_func | |
85 | * does not match with the last supplied values. | |
09c93066 | 86 | */ |
60b458b7 KB |
87 | reset_lstat_cache(); |
88 | cache.track_flags = track_flags; | |
89 | cache.prefix_len_stat_func = prefix_len_stat_func; | |
09c93066 KB |
90 | match_len = last_slash = 0; |
91 | } else { | |
92 | /* | |
93 | * Check to see if we have a match from the cache for | |
94 | * the 2 "excluding" path types. | |
95 | */ | |
aeabab5c | 96 | match_len = last_slash = |
148bc06b KB |
97 | longest_path_match(name, len, cache.path, cache.len, |
98 | &previous_slash); | |
09c93066 KB |
99 | match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK); |
100 | if (match_flags && match_len == cache.len) | |
101 | return match_flags; | |
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 | */ | |
110 | match_flags = track_flags & FL_DIR; | |
111 | if (match_flags && len == match_len) | |
112 | return match_flags; | |
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 | */ | |
119 | ret_flags = FL_DIR; | |
120 | last_slash_dir = last_slash; | |
121 | max_len = len < PATH_MAX ? len : PATH_MAX; | |
122 | while (match_len < max_len) { | |
123 | do { | |
124 | cache.path[match_len] = name[match_len]; | |
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; | |
130 | cache.path[last_slash] = '\0'; | |
f859c846 | 131 | |
bad4a54f KB |
132 | if (last_slash <= prefix_len_stat_func) |
133 | ret = stat(cache.path, &st); | |
134 | else | |
135 | ret = lstat(cache.path, &st); | |
136 | ||
137 | if (ret) { | |
92604b46 | 138 | ret_flags = FL_LSTATERR; |
09c93066 KB |
139 | if (errno == ENOENT) |
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 KB |
144 | } else if (S_ISLNK(st.st_mode)) { |
145 | ret_flags = FL_SYMLINK; | |
146 | } else { | |
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 | */ |
09c93066 | 157 | save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK); |
bad4a54f | 158 | if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) { |
92604b46 KB |
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 KB |
174 | */ |
175 | cache.path[last_slash_dir] = '\0'; | |
176 | cache.len = last_slash_dir; | |
177 | cache.flags = FL_DIR; | |
178 | } else { | |
60b458b7 | 179 | reset_lstat_cache(); |
92604b46 KB |
180 | } |
181 | return ret_flags; | |
182 | } | |
183 | ||
aeabab5c KB |
184 | /* |
185 | * Invalidate the given 'name' from the cache, if 'name' matches | |
186 | * completely with the cache. | |
187 | */ | |
188 | void invalidate_lstat_cache(int len, const char *name) | |
189 | { | |
190 | int match_len, previous_slash; | |
191 | ||
148bc06b KB |
192 | match_len = longest_path_match(name, len, cache.path, cache.len, |
193 | &previous_slash); | |
aeabab5c KB |
194 | if (len == match_len) { |
195 | if ((cache.track_flags & FL_DIR) && previous_slash > 0) { | |
196 | cache.path[previous_slash] = '\0'; | |
197 | cache.len = previous_slash; | |
198 | cache.flags = FL_DIR; | |
199 | } else | |
60b458b7 | 200 | reset_lstat_cache(); |
aeabab5c KB |
201 | } |
202 | } | |
203 | ||
bda6eb0d KB |
204 | /* |
205 | * Completely clear the contents of the cache | |
206 | */ | |
207 | void clear_lstat_cache(void) | |
208 | { | |
60b458b7 | 209 | reset_lstat_cache(); |
bda6eb0d KB |
210 | } |
211 | ||
bad4a54f KB |
212 | #define USE_ONLY_LSTAT 0 |
213 | ||
92604b46 KB |
214 | /* |
215 | * Return non-zero if path 'name' has a leading symlink component | |
216 | */ | |
217 | int has_symlink_leading_path(int len, const char *name) | |
218 | { | |
219 | return lstat_cache(len, name, | |
bad4a54f | 220 | FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & |
92604b46 | 221 | FL_SYMLINK; |
f859c846 | 222 | } |
09c93066 KB |
223 | |
224 | /* | |
225 | * Return non-zero if path 'name' has a leading symlink component or | |
226 | * if some leading path component does not exists. | |
227 | */ | |
228 | int has_symlink_or_noent_leading_path(int len, const char *name) | |
229 | { | |
230 | return lstat_cache(len, name, | |
bad4a54f | 231 | FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) & |
09c93066 KB |
232 | (FL_SYMLINK|FL_NOENT); |
233 | } | |
bad4a54f KB |
234 | |
235 | /* | |
236 | * Return non-zero if all path components of 'name' exists as a | |
237 | * directory. If prefix_len > 0, we will test with the stat() | |
238 | * function instead of the lstat() function for a prefix length of | |
239 | * 'prefix_len', thus we then allow for symlinks in the prefix part as | |
240 | * long as those points to real existing directories. | |
241 | */ | |
242 | int has_dirs_only_path(int len, const char *name, int prefix_len) | |
243 | { | |
244 | return lstat_cache(len, name, | |
245 | FL_DIR|FL_FULLPATH, prefix_len) & | |
246 | FL_DIR; | |
247 | } |