918e24ad9e76454a34ba0d958630558d55b56517
   1#include "cache.h"
   2
   3static struct cache_def {
   4        char path[PATH_MAX + 1];
   5        int len;
   6        int flags;
   7        int track_flags;
   8        int prefix_len_stat_func;
   9} cache;
  10
  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 */
  15static inline int longest_match_lstat_cache(int len, const char *name)
  16{
  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;
  33}
  34
  35static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func)
  36{
  37        cache.path[0] = '\0';
  38        cache.len = 0;
  39        cache.flags = 0;
  40        cache.track_flags = track_flags;
  41        cache.prefix_len_stat_func = prefix_len_stat_func;
  42}
  43
  44#define FL_DIR      (1 << 0)
  45#define FL_NOENT    (1 << 1)
  46#define FL_SYMLINK  (1 << 2)
  47#define FL_LSTATERR (1 << 3)
  48#define FL_ERR      (1 << 4)
  49#define FL_FULLPATH (1 << 5)
  50
  51/*
  52 * Check if name 'name' of length 'len' has a symlink leading
  53 * component, or if the directory exists and is real, or not.
  54 *
  55 * To speed up the check, some information is allowed to be cached.
  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.
  62 */
  63static int lstat_cache(int len, const char *name,
  64                       int track_flags, int prefix_len_stat_func)
  65{
  66        int match_len, last_slash, last_slash_dir;
  67        int match_flags, ret_flags, save_flags, max_len, ret;
  68        struct stat st;
  69
  70        if (cache.track_flags != track_flags ||
  71            cache.prefix_len_stat_func != prefix_len_stat_func) {
  72                /*
  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.
  76                 */
  77                reset_lstat_cache(track_flags, prefix_len_stat_func);
  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        }
 100
 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] != '/');
 113                if (match_len >= max_len && !(track_flags & FL_FULLPATH))
 114                        break;
 115                last_slash = match_len;
 116                cache.path[last_slash] = '\0';
 117
 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) {
 124                        ret_flags = FL_LSTATERR;
 125                        if (errno == ENOENT)
 126                                ret_flags |= FL_NOENT;
 127                } else if (S_ISDIR(st.st_mode)) {
 128                        last_slash_dir = last_slash;
 129                        continue;
 130                } else if (S_ISLNK(st.st_mode)) {
 131                        ret_flags = FL_SYMLINK;
 132                } else {
 133                        ret_flags = FL_ERR;
 134                }
 135                break;
 136        }
 137
 138        /*
 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!
 142         */
 143        save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
 144        if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
 145                cache.path[last_slash] = '\0';
 146                cache.len = last_slash;
 147                cache.flags = save_flags;
 148        } else if (track_flags & FL_DIR &&
 149                   last_slash_dir > 0 && last_slash_dir <= PATH_MAX) {
 150                /*
 151                 * We have a separate test for the directory case,
 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.
 156                 *
 157                 * But if we are allowed to track real directories, we
 158                 * can still cache the path components before the last
 159                 * one (the found symlink or non-existing component).
 160                 */
 161                cache.path[last_slash_dir] = '\0';
 162                cache.len = last_slash_dir;
 163                cache.flags = FL_DIR;
 164        } else {
 165                reset_lstat_cache(track_flags, prefix_len_stat_func);
 166        }
 167        return ret_flags;
 168}
 169
 170#define USE_ONLY_LSTAT  0
 171
 172/*
 173 * Return non-zero if path 'name' has a leading symlink component
 174 */
 175int has_symlink_leading_path(int len, const char *name)
 176{
 177        return lstat_cache(len, name,
 178                           FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) &
 179                FL_SYMLINK;
 180}
 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 */
 186int has_symlink_or_noent_leading_path(int len, const char *name)
 187{
 188        return lstat_cache(len, name,
 189                           FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
 190                (FL_SYMLINK|FL_NOENT);
 191}
 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 */
 200int 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}