1#include "cache.h"
23
static struct cache_def {
4char path[PATH_MAX + 1];
5int len;
6int flags;
7int track_flags;
8int prefix_len_stat_func;
9} cache;
1011
/*
12* Returns the length (on a path component basis) of the longest
13* common prefix match of 'name' and the cached path string.
14*/
15static inline int longest_match_lstat_cache(int len, const char *name,
16int *previous_slash)
17{
18int max_len, match_len = 0, match_len_prev = 0, i = 0;
1920
max_len = len < cache.len ? len : cache.len;
21while (i < max_len && name[i] == cache.path[i]) {
22if (name[i] == '/') {
23match_len_prev = match_len;
24match_len = i;
25}
26i++;
27}
28/* Is the cached path string a substring of 'name'? */
29if (i == cache.len && cache.len < len && name[cache.len] == '/') {
30match_len_prev = match_len;
31match_len = cache.len;
32/* Is 'name' a substring of the cached path string? */
33} else if ((i == len && len < cache.len && cache.path[len] == '/') ||
34(i == len && len == cache.len)) {
35match_len_prev = match_len;
36match_len = len;
37}
38*previous_slash = match_len_prev;
39return match_len;
40}
4142
static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func)
43{
44cache.path[0] = '\0';
45cache.len = 0;
46cache.flags = 0;
47cache.track_flags = track_flags;
48cache.prefix_len_stat_func = prefix_len_stat_func;
49}
5051
#define FL_DIR (1 << 0)
52#define FL_NOENT (1 << 1)
53#define FL_SYMLINK (1 << 2)
54#define FL_LSTATERR (1 << 3)
55#define FL_ERR (1 << 4)
56#define FL_FULLPATH (1 << 5)
5758
/*
59* Check if name 'name' of length 'len' has a symlink leading
60* component, or if the directory exists and is real, or not.
61*
62* To speed up the check, some information is allowed to be cached.
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.
69*/
70static int lstat_cache(int len, const char *name,
71int track_flags, int prefix_len_stat_func)
72{
73int match_len, last_slash, last_slash_dir, previous_slash;
74int match_flags, ret_flags, save_flags, max_len, ret;
75struct stat st;
7677
if (cache.track_flags != track_flags ||
78cache.prefix_len_stat_func != prefix_len_stat_func) {
79/*
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.
83*/
84reset_lstat_cache(track_flags, prefix_len_stat_func);
85match_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*/
91match_len = last_slash =
92longest_match_lstat_cache(len, name, &previous_slash);
93match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK);
94if (match_flags && match_len == cache.len)
95return 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*/
104match_flags = track_flags & FL_DIR;
105if (match_flags && len == match_len)
106return match_flags;
107}
108109
/*
110* Okay, no match from the cache so far, so now we have to
111* check the rest of the path components.
112*/
113ret_flags = FL_DIR;
114last_slash_dir = last_slash;
115max_len = len < PATH_MAX ? len : PATH_MAX;
116while (match_len < max_len) {
117do {
118cache.path[match_len] = name[match_len];
119match_len++;
120} while (match_len < max_len && name[match_len] != '/');
121if (match_len >= max_len && !(track_flags & FL_FULLPATH))
122break;
123last_slash = match_len;
124cache.path[last_slash] = '\0';
125126
if (last_slash <= prefix_len_stat_func)
127ret = stat(cache.path, &st);
128else
129ret = lstat(cache.path, &st);
130131
if (ret) {
132ret_flags = FL_LSTATERR;
133if (errno == ENOENT)
134ret_flags |= FL_NOENT;
135} else if (S_ISDIR(st.st_mode)) {
136last_slash_dir = last_slash;
137continue;
138} else if (S_ISLNK(st.st_mode)) {
139ret_flags = FL_SYMLINK;
140} else {
141ret_flags = FL_ERR;
142}
143break;
144}
145146
/*
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!
150*/
151save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
152if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
153cache.path[last_slash] = '\0';
154cache.len = last_slash;
155cache.flags = save_flags;
156} else if (track_flags & FL_DIR &&
157last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
158/*
159* We have a separate test for the directory case,
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.
164*
165* But if we are allowed to track real directories, we
166* can still cache the path components before the last
167* one (the found symlink or non-existing component).
168*/
169cache.path[last_slash_dir] = '\0';
170cache.len = last_slash_dir;
171cache.flags = FL_DIR;
172} else {
173reset_lstat_cache(track_flags, prefix_len_stat_func);
174}
175return ret_flags;
176}
177178
/*
179* Invalidate the given 'name' from the cache, if 'name' matches
180* completely with the cache.
181*/
182void invalidate_lstat_cache(int len, const char *name)
183{
184int match_len, previous_slash;
185186
match_len = longest_match_lstat_cache(len, name, &previous_slash);
187if (len == match_len) {
188if ((cache.track_flags & FL_DIR) && previous_slash > 0) {
189cache.path[previous_slash] = '\0';
190cache.len = previous_slash;
191cache.flags = FL_DIR;
192} else
193reset_lstat_cache(cache.track_flags,
194cache.prefix_len_stat_func);
195}
196}
197198
/*
199* Completely clear the contents of the cache
200*/
201void clear_lstat_cache(void)
202{
203reset_lstat_cache(0, 0);
204}
205206
#define USE_ONLY_LSTAT 0
207208
/*
209* Return non-zero if path 'name' has a leading symlink component
210*/
211int has_symlink_leading_path(int len, const char *name)
212{
213return lstat_cache(len, name,
214FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) &
215FL_SYMLINK;
216}
217218
/*
219* Return non-zero if path 'name' has a leading symlink component or
220* if some leading path component does not exists.
221*/
222int has_symlink_or_noent_leading_path(int len, const char *name)
223{
224return lstat_cache(len, name,
225FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
226(FL_SYMLINK|FL_NOENT);
227}
228229
/*
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*/
236int has_dirs_only_path(int len, const char *name, int prefix_len)
237{
238return lstat_cache(len, name,
239FL_DIR|FL_FULLPATH, prefix_len) &
240FL_DIR;
241}