3 static struct cache_def
{
10 * Returns the length (on a path component basis) of the longest
11 * common prefix match of 'name' and the cached path string.
13 static inline int longest_match_lstat_cache(int len
, const char *name
)
15 int max_len
, match_len
= 0, i
= 0;
17 max_len
= len
< cache
.len
? len
: cache
.len
;
18 while (i
< max_len
&& name
[i
] == cache
.path
[i
]) {
23 /* Is the cached path string a substring of 'name'? */
24 if (i
== cache
.len
&& cache
.len
< len
&& name
[cache
.len
] == '/')
25 match_len
= cache
.len
;
26 /* Is 'name' a substring of the cached path string? */
27 else if ((i
== len
&& len
< cache
.len
&& cache
.path
[len
] == '/') ||
28 (i
== len
&& len
== cache
.len
))
33 static inline void reset_lstat_cache(void)
40 #define FL_DIR (1 << 0)
41 #define FL_SYMLINK (1 << 1)
42 #define FL_LSTATERR (1 << 2)
43 #define FL_ERR (1 << 3)
46 * Check if name 'name' of length 'len' has a symlink leading
47 * component, or if the directory exists and is real.
49 * To speed up the check, some information is allowed to be cached.
50 * This can be indicated by the 'track_flags' argument.
52 static int lstat_cache(int len
, const char *name
,
55 int match_len
, last_slash
, last_slash_dir
;
56 int match_flags
, ret_flags
, save_flags
, max_len
;
60 * Check to see if we have a match from the cache for the
63 match_len
= last_slash
= longest_match_lstat_cache(len
, name
);
64 match_flags
= cache
.flags
& track_flags
& FL_SYMLINK
;
65 if (match_flags
&& match_len
== cache
.len
)
68 * If we now have match_len > 0, we would know that the
69 * matched part will always be a directory.
71 * Also, if we are tracking directories and 'name' is a
72 * substring of the cache on a path component basis, we can
75 match_flags
= track_flags
& FL_DIR
;
76 if (match_flags
&& len
== match_len
)
80 * Okay, no match from the cache so far, so now we have to
81 * check the rest of the path components.
84 last_slash_dir
= last_slash
;
85 max_len
= len
< PATH_MAX
? len
: PATH_MAX
;
86 while (match_len
< max_len
) {
88 cache
.path
[match_len
] = name
[match_len
];
90 } while (match_len
< max_len
&& name
[match_len
] != '/');
91 if (match_len
>= max_len
)
93 last_slash
= match_len
;
94 cache
.path
[last_slash
] = '\0';
96 if (lstat(cache
.path
, &st
)) {
97 ret_flags
= FL_LSTATERR
;
98 } else if (S_ISDIR(st
.st_mode
)) {
99 last_slash_dir
= last_slash
;
101 } else if (S_ISLNK(st
.st_mode
)) {
102 ret_flags
= FL_SYMLINK
;
110 * At the end update the cache. Note that max 2 different
111 * path types, FL_SYMLINK and FL_DIR, can be cached for the
114 save_flags
= ret_flags
& track_flags
& FL_SYMLINK
;
115 if (save_flags
&& last_slash
> 0 && last_slash
< PATH_MAX
) {
116 cache
.path
[last_slash
] = '\0';
117 cache
.len
= last_slash
;
118 cache
.flags
= save_flags
;
119 } else if (track_flags
& FL_DIR
&&
120 last_slash_dir
> 0 && last_slash_dir
< PATH_MAX
) {
122 * We have a separate test for the directory case,
123 * since it could be that we have found a symlink and
124 * the track_flags says that we cannot cache this
125 * fact, so the cache would then have been left empty
128 * But if we are allowed to track real directories, we
129 * can still cache the path components before the last
130 * one (the found symlink component).
132 cache
.path
[last_slash_dir
] = '\0';
133 cache
.len
= last_slash_dir
;
134 cache
.flags
= FL_DIR
;
142 * Return non-zero if path 'name' has a leading symlink component
144 int has_symlink_leading_path(int len
, const char *name
)
146 return lstat_cache(len
, name
,