backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virusbmock.c
blobf35ad407adfad48113293ef5e4843b25a010f580
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <dirent.h>
24 #include <dlfcn.h>
25 #include <fcntl.h>
27 #include "viralloc.h"
28 #include "virfile.h"
29 #include "virstring.h"
30 #include "virusb.h"
32 #define USB_SYSFS "/sys/bus/usb"
33 #define FAKE_USB_SYSFS "virusbtestdata/sys_bus_usb"
35 static int (*realopen)(const char *pathname, int flags, ...);
36 static DIR *(*realopendir)(const char *name);
38 static void init_syms(void)
40 if (realopen)
41 return;
43 realopen = dlsym(RTLD_NEXT, "open");
44 realopendir = dlsym(RTLD_NEXT, "opendir");
45 if (!realopen || !realopendir) {
46 fprintf(stderr, "Error getting symbols");
47 abort();
51 static char *get_fake_path(const char *real_path)
53 const char *p = NULL;
54 char *path = NULL;
56 if ((p = STRSKIP(real_path, USB_SYSFS)) &&
57 virAsprintfQuiet(&path, "%s/%s/%s", abs_srcdir, FAKE_USB_SYSFS, p) < 0)
58 goto error;
59 else if (!p && VIR_STRDUP_QUIET(path, real_path) < 0)
60 goto error;
62 return path;
64 error:
65 errno = ENOMEM;
66 return NULL;
69 DIR *opendir(const char *name)
71 char *path;
72 DIR* ret;
74 init_syms();
76 path = get_fake_path(name);
78 ret = realopendir(path);
79 VIR_FREE(path);
80 return ret;
83 int open(const char *pathname, int flags, ...)
85 char *path;
86 int ret;
87 va_list ap;
88 mode_t mode = 0;
90 init_syms();
92 path = get_fake_path(pathname);
93 if (!path)
94 return -1;
96 /* The mode argument is mandatory when O_CREAT is set in flags,
97 * otherwise the argument is ignored.
99 if (flags & O_CREAT) {
100 va_start(ap, flags);
101 mode = (mode_t) va_arg(ap, int);
102 va_end(ap);
105 ret = realopen(path, flags, mode);
107 VIR_FREE(path);
108 return ret;