symlinks.con commit GIT 1.6.2.3 (3bd1bb3)
   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                                            int *previous_slash)
  17{
  18        int max_len, match_len = 0, match_len_prev = 0, i = 0;
  19
  20        max_len = len < cache.len ? len : cache.len;
  21        while (i < max_len && name[i] == cache.path[i]) {
  22                if (name[i] == '/') {
  23                        match_len_prev = match_len;
  24                        match_len = i;
  25                }
  26                i++;
  27        }
  28        /* Is the cached path string a substring of 'name'? */
  29        if (i == cache.len && cache.len < len && name[cache.len] == '/') {
  30                match_len_prev = match_len;
  31                match_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)) {
  35                match_len_prev = match_len;
  36                match_len = len;
  37        }
  38        *previous_slash = match_len_prev;
  39        return match_len;
  40}
  41
  42static inline void reset_lstat_cache(int track_flags, int prefix_len_stat_func)
  43{
  44        cache.path[0] = '\0';
  45        cache.len = 0;
  46        cache.flags = 0;
  47        cache.track_flags = track_flags;
  48        cache.prefix_len_stat_func = prefix_len_stat_func;
  49}
  50
  51#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)
  57
  58/*
  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,
  71                       int track_flags, int prefix_len_stat_func)
  72{
  73        int match_len, last_slash, last_slash_dir, previous_slash;
  74        int match_flags, ret_flags, save_flags, max_len, ret;
  75        struct stat st;
  76
  77        if (cache.track_flags != track_flags ||
  78            cache.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                 */
  84                reset_lstat_cache(track_flags, prefix_len_stat_func);
  85                match_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                 */
  91                match_len = last_slash =
  92                        longest_match_lstat_cache(len, name, &previous_slash);
  93                match_flags = cache.flags & track_flags & (FL_NOENT|FL_SYMLINK);
  94                if (match_flags && match_len == cache.len)
  95                        return 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                 */
 104                match_flags = track_flags & FL_DIR;
 105                if (match_flags && len == match_len)
 106                        return match_flags;
 107        }
 108
 109        /*
 110         * Okay, no match from the cache so far, so now we have to
 111         * check the rest of the path components.
 112         */
 113        ret_flags = FL_DIR;
 114        last_slash_dir = last_slash;
 115        max_len = len < PATH_MAX ? len : PATH_MAX;
 116        while (match_len < max_len) {
 117                do {
 118                        cache.path[match_len] = name[match_len];
 119                        match_len++;
 120                } while (match_len < max_len && name[match_len] != '/');
 121                if (match_len >= max_len && !(track_flags & FL_FULLPATH))
 122                        break;
 123                last_slash = match_len;
 124                cache.path[last_slash] = '\0';
 125
 126                if (last_slash <= prefix_len_stat_func)
 127                        ret = stat(cache.path, &st);
 128                else
 129                        ret = lstat(cache.path, &st);
 130
 131                if (ret) {
 132                        ret_flags = FL_LSTATERR;
 133                        if (errno == ENOENT)
 134                                ret_flags |= FL_NOENT;
 135                } else if (S_ISDIR(st.st_mode)) {
 136                        last_slash_dir = last_slash;
 137                        continue;
 138                } else if (S_ISLNK(st.st_mode)) {
 139                        ret_flags = FL_SYMLINK;
 140                } else {
 141                        ret_flags = FL_ERR;
 142                }
 143                break;
 144        }
 145
 146        /*
 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         */
 151        save_flags = ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
 152        if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
 153                cache.path[last_slash] = '\0';
 154                cache.len = last_slash;
 155                cache.flags = save_flags;
 156        } else if (track_flags & FL_DIR &&
 157                   last_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                 */
 169                cache.path[last_slash_dir] = '\0';
 170                cache.len = last_slash_dir;
 171                cache.flags = FL_DIR;
 172        } else {
 173                reset_lstat_cache(track_flags, prefix_len_stat_func);
 174        }
 175        return ret_flags;
 176}
 177
 178/*
 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{
 184        int match_len, previous_slash;
 185
 186        match_len = longest_match_lstat_cache(len, name, &previous_slash);
 187        if (len == match_len) {
 188                if ((cache.track_flags & FL_DIR) && previous_slash > 0) {
 189                        cache.path[previous_slash] = '\0';
 190                        cache.len = previous_slash;
 191                        cache.flags = FL_DIR;
 192                } else
 193                        reset_lstat_cache(cache.track_flags,
 194                                          cache.prefix_len_stat_func);
 195        }
 196}
 197
 198/*
 199 * Completely clear the contents of the cache
 200 */
 201void clear_lstat_cache(void)
 202{
 203        reset_lstat_cache(0, 0);
 204}
 205
 206#define USE_ONLY_LSTAT  0
 207
 208/*
 209 * Return non-zero if path 'name' has a leading symlink component
 210 */
 211int has_symlink_leading_path(int len, const char *name)
 212{
 213        return lstat_cache(len, name,
 214                           FL_SYMLINK|FL_DIR, USE_ONLY_LSTAT) &
 215                FL_SYMLINK;
 216}
 217
 218/*
 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{
 224        return lstat_cache(len, name,
 225                           FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT) &
 226                (FL_SYMLINK|FL_NOENT);
 227}
 228
 229/*
 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{
 238        return lstat_cache(len, name,
 239                           FL_DIR|FL_FULLPATH, prefix_len) &
 240                FL_DIR;
 241}