]>
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 | */ | |
15 | static inline int longest_match_lstat_cache(int len, const char *name) | |
c40641b7 | 16 | { |
92604b46 KB |
17 | int max_len, match_len = 0, i = 0; |
18 | ||
19 | max_len = len < cache.len ? len : cache.len; | |
20 | while (i < max_len && name[i] == cache.path[i]) { | |
21 | if (name[i] == '/') | |
22 | match_len = i; | |
23 | i++; | |
24 | } | |
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)) | |
31 | match_len = len; | |
32 | return match_len; | |
c40641b7 LT |
33 | } |
34 | ||
bad4a54f | 35 | static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func) |
c40641b7 | 36 | { |
92604b46 KB |
37 | cache.path[0] = '\0'; |
38 | cache.len = 0; | |
39 | cache.flags = 0; | |
09c93066 | 40 | cache.track_flags = track_flags; |
bad4a54f | 41 | cache.prefix_len_stat_func = prefix_len_stat_func; |
c40641b7 LT |
42 | } |
43 | ||
92604b46 | 44 | #define FL_DIR (1 << 0) |
09c93066 KB |
45 | #define FL_NOENT (1 << 1) |
46 | #define FL_SYMLINK (1 << 2) | |
47 | #define FL_LSTATERR (1 << 3) | |
48 | #define FL_ERR (1 << 4) | |
bad4a54f | 49 | #define FL_FULLPATH (1 << 5) |
92604b46 KB |
50 | |
51 | /* | |
52 | * Check if name 'name' of length 'len' has a symlink leading | |
09c93066 | 53 | * component, or if the directory exists and is real, or not. |
92604b46 KB |
54 | * |
55 | * To speed up the check, some information is allowed to be cached. | |
bad4a54f KB |
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. | |
58 | * | |
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. | |
92604b46 KB |
62 | */ |
63 | static int lstat_cache(int len, const char *name, | |
bad4a54f | 64 | int track_flags, int prefix_len_stat_func) |
c40641b7 | 65 | { |
92604b46 | 66 | int match_len, last_slash, last_slash_dir; |
bad4a54f | 67 | int match_flags, ret_flags, save_flags, max_len, ret; |
c40641b7 | 68 | struct stat st; |
f859c846 | 69 | |
bad4a54f KB |
70 | if (cache.track_flags != track_flags || |
71 | cache.prefix_len_stat_func != prefix_len_stat_func) { | |
09c93066 | 72 | /* |
bad4a54f KB |
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. | |
09c93066 | 76 | */ |
bad4a54f | 77 | reset_lstat_cache(track_flags, prefix_len_stat_func); |
09c93066 KB |
78 | match_len = last_slash = 0; |
79 | } else { | |
80 | /* | |
81 | * Check to see if we have a match from the cache for | |
82 | * the 2 "excluding" path types. | |
83 | */ | |
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) | |
87 | return match_flags; | |
88 | /* | |
89 | * If we now have match_len > 0, we would know that | |
90 | * the matched part will always be a directory. | |
91 | * | |
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. | |
95 | */ | |
96 | match_flags = track_flags & FL_DIR; | |
97 | if (match_flags && len == match_len) | |
98 | return match_flags; | |
99 | } | |
c40641b7 | 100 | |
92604b46 KB |
101 | /* |
102 | * Okay, no match from the cache so far, so now we have to | |
103 | * check the rest of the path components. | |
104 | */ | |
105 | ret_flags = FL_DIR; | |
106 | last_slash_dir = last_slash; | |
107 | max_len = len < PATH_MAX ? len : PATH_MAX; | |
108 | while (match_len < max_len) { | |
109 | do { | |
110 | cache.path[match_len] = name[match_len]; | |
111 | match_len++; | |
112 | } while (match_len < max_len && name[match_len] != '/'); | |
bad4a54f | 113 | if (match_len >= max_len && !(track_flags & FL_FULLPATH)) |
92604b46 KB |
114 | break; |
115 | last_slash = match_len; | |
116 | cache.path[last_slash] = '\0'; | |
f859c846 | 117 | |
bad4a54f KB |
118 | if (last_slash <= prefix_len_stat_func) |
119 | ret = stat(cache.path, &st); | |
120 | else | |
121 | ret = lstat(cache.path, &st); | |
122 | ||
123 | if (ret) { | |
92604b46 | 124 | ret_flags = FL_LSTATERR; |
09c93066 KB |
125 | if (errno == ENOENT) |
126 | ret_flags |= FL_NOENT; | |
92604b46 KB |
127 | } else if (S_ISDIR(st.st_mode)) { |
128 | last_slash_dir = last_slash; | |
c40641b7 | 129 | continue; |
92604b46 KB |
130 | } else if (S_ISLNK(st.st_mode)) { |
131 | ret_flags = FL_SYMLINK; | |
132 | } else { | |
133 | ret_flags = FL_ERR; | |
f859c846 | 134 | } |
c40641b7 | 135 | break; |
f859c846 | 136 | } |
92604b46 KB |
137 | |
138 | /* | |
09c93066 KB |
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 | |
141 | * for the moment! | |
92604b46 | 142 | */ |
09c93066 | 143 | save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK); |
bad4a54f | 144 | if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) { |
92604b46 KB |
145 | cache.path[last_slash] = '\0'; |
146 | cache.len = last_slash; | |
147 | cache.flags = save_flags; | |
148 | } else if (track_flags & FL_DIR && | |
bad4a54f | 149 | last_slash_dir > 0 && last_slash_dir <= PATH_MAX) { |
92604b46 KB |
150 | /* |
151 | * We have a separate test for the directory case, | |
09c93066 KB |
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. | |
92604b46 KB |
156 | * |
157 | * But if we are allowed to track real directories, we | |
158 | * can still cache the path components before the last | |
09c93066 | 159 | * one (the found symlink or non-existing component). |
92604b46 KB |
160 | */ |
161 | cache.path[last_slash_dir] = '\0'; | |
162 | cache.len = last_slash_dir; | |
163 | cache.flags = FL_DIR; | |
164 | } else { | |
bad4a54f | 165 | reset_lstat_cache(track_flags, prefix_len_stat_func); |
92604b46 KB |
166 | } |
167 | return ret_flags; | |
168 | } | |
169 | ||
bad4a54f KB |
170 | #define USE_ONLY_LSTAT 0 |
171 | ||
92604b46 KB |
172 | /* |
173 | * Return non-zero if path 'name' has a leading symlink component | |
174 | */ | |
175 | int has_symlink_leading_path(int len, const char *name) | |
176 | { | |
177 | return lstat_cache(len, name, | |
bad4a54f | 178 | FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) & |
92604b46 | 179 | FL_SYMLINK; |
f859c846 | 180 | } |
09c93066 KB |
181 | |
182 | /* | |
183 | * Return non-zero if path 'name' has a leading symlink component or | |
184 | * if some leading path component does not exists. | |
185 | */ | |
186 | int has_symlink_or_noent_leading_path(int len, const char *name) | |
187 | { | |
188 | return lstat_cache(len, name, | |
bad4a54f | 189 | FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) & |
09c93066 KB |
190 | (FL_SYMLINK|FL_NOENT); |
191 | } | |
bad4a54f KB |
192 | |
193 | /* | |
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. | |
199 | */ | |
200 | int has_dirs_only_path(int len, const char *name, int prefix_len) | |
201 | { | |
202 | return lstat_cache(len, name, | |
203 | FL_DIR|FL_FULLPATH, prefix_len) & | |
204 | FL_DIR; | |
205 | } |