virfilewrapper.c 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/*
 * virfilewrapper.c: Wrapper for universal file access
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#include "viralloc.h"
#include "virfile.h"
#include "virfilewrapper.h"
#include "virmock.h"
#include "virstring.h"


/* Mapping for prefix overrides */
static size_t noverrides;
static const char **overrides;

/* nprefixes == noverrides, but two variables make it easier to use
 * VIR_*_ELEMENT macros */
static size_t nprefixes;
static const char **prefixes;

/* TODO: callbacks */


static int (*real_open)(const char *path, int flags, ...);
static FILE *(*real_fopen)(const char *path, const char *mode);
static int (*real_access)(const char *path, int mode);
static int (*real_stat)(const char *path, struct stat *sb);
static int (*real___xstat)(int ver, const char *path, struct stat *sb);
static int (*real_lstat)(const char *path, struct stat *sb);
static int (*real___lxstat)(int ver, const char *path, struct stat *sb);
static int (*real_mkdir)(const char *path, mode_t mode);
static DIR *(*real_opendir)(const char *path);

static void init_syms(void)
{
    if (real_fopen)
        return;

    VIR_MOCK_REAL_INIT(fopen);
    VIR_MOCK_REAL_INIT(access);
    VIR_MOCK_REAL_INIT_ALT(lstat, __lxstat);
    VIR_MOCK_REAL_INIT_ALT(stat, __xstat);
    VIR_MOCK_REAL_INIT(mkdir);
    VIR_MOCK_REAL_INIT(open);
    VIR_MOCK_REAL_INIT(opendir);
}


int
virFileWrapperAddPrefix(const char *prefix,
                     const char *override)
{
    /* Both parameters are mandatory */
    if (!prefix || !override)
        return -1;

    init_syms();

    if (VIR_APPEND_ELEMENT_QUIET(prefixes, nprefixes, prefix) < 0 ||
        VIR_APPEND_ELEMENT_QUIET(overrides, noverrides, override) < 0) {
        VIR_FREE(prefixes);
        VIR_FREE(overrides);
        return -1;
    }

    return 0;
}


void
virFileWrapperRemovePrefix(const char *prefix)
{
    size_t i = 0;

    for (i = 0; i < noverrides; i++) {
        if (STREQ(prefixes[i], prefix))
            break;
    }

    if (i == noverrides)
        return;

    VIR_DELETE_ELEMENT(overrides, i, noverrides);
    VIR_DELETE_ELEMENT(prefixes, i, nprefixes);
}

void
virFileWrapperClearPrefixes(void)
{
    nprefixes = 0;
    noverrides = 0;

    VIR_FREE(prefixes);
    VIR_FREE(overrides);
}

static char *
virFileWrapperOverridePrefix(const char *path)
{
    char *ret = NULL;
    size_t i = 0;

    for (i = 0; i < noverrides; i++) {
        const char *tmp = STRSKIP(path, prefixes[i]);

        if (!tmp)
            continue;

        if (virAsprintfQuiet(&ret, "%s%s", overrides[i], tmp) < 0)
            return NULL;

        break;
    }

    if (!ret)
        ignore_value(VIR_STRDUP_QUIET(ret, path));

    return ret;
}


#define PATH_OVERRIDE(newpath, path)                    \
    do {                                                \
        init_syms();                                    \
                                                        \
        newpath = virFileWrapperOverridePrefix(path);      \
        if (!newpath)                                   \
            abort();                                    \
    } while (0)


FILE *fopen(const char *path, const char *mode)
{
    FILE *ret = NULL;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real_fopen(newpath, mode);

    VIR_FREE(newpath);

    return ret;
}

int access(const char *path, int mode)
{
    int ret = -1;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real_access(newpath, mode);

    VIR_FREE(newpath);

    return ret;
}

R
Roman Bogorodskiy 已提交
180
#ifdef HAVE___LXSTAT
181 182 183 184 185 186 187 188 189 190 191 192 193
int __lxstat(int ver, const char *path, struct stat *sb)
{
    int ret = -1;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real___lxstat(ver, newpath, sb);

    VIR_FREE(newpath);

    return ret;
}
R
Roman Bogorodskiy 已提交
194
#endif /* HAVE___LXSTAT */
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

int lstat(const char *path, struct stat *sb)
{
    int ret = -1;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real_lstat(newpath, sb);

    VIR_FREE(newpath);

    return ret;
}

R
Roman Bogorodskiy 已提交
210
#ifdef HAVE___XSTAT
211 212 213 214 215 216 217 218 219 220 221 222 223
int __xstat(int ver, const char *path, struct stat *sb)
{
    int ret = -1;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real___xstat(ver, newpath, sb);

    VIR_FREE(newpath);

    return ret;
}
R
Roman Bogorodskiy 已提交
224
#endif /* HAVE___XSTAT */
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

int stat(const char *path, struct stat *sb)
{
    int ret = -1;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real_stat(newpath, sb);

    VIR_FREE(newpath);

    return ret;
}

int mkdir(const char *path, mode_t mode)
{
    int ret = -1;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real_mkdir(newpath, mode);

    VIR_FREE(newpath);

    return ret;
}

int open(const char *path, int flags, ...)
{
    int ret = -1;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real_open(newpath, flags);

    VIR_FREE(newpath);

    return ret;
}

DIR *opendir(const char *path)
{
    DIR *ret = NULL;
    char *newpath = NULL;

    PATH_OVERRIDE(newpath, path);

    ret = real_opendir(newpath);

    VIR_FREE(newpath);

    return ret;
}