[PATCH v4 5/8] virmock.h: Introduce VIR_MOCK_CALL_STAT

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There is some magic going on when it comes to stat() or lstat().
Basically, stat() can either be a regular function, an inline
function that calls __xstat(_STAT_VER, ...) or a macro that does
the same as the inline func. Don't ask why is that, just read the
documentation in sys/stat.h and make sure you have a bucket next
to you. Anyway, currently there will not be both stat and __xstat
symbols at the same time, as one of them gets overwritten to the
other one during compilation. But this is not true anymore once
we start chaining our mocking libraries. Therefore we need a
wrapper that calls desired function from glibc.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 tests/vircgroupmock.c | 14 +++++++-------
 tests/virmock.h       | 23 +++++++++++++++++++++++
 tests/virpcimock.c    | 19 +++++++++----------
 3 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/tests/vircgroupmock.c b/tests/vircgroupmock.c
index 10fe24d..287bd46 100644
--- a/tests/vircgroupmock.c
+++ b/tests/vircgroupmock.c
@@ -556,7 +556,7 @@ int __lxstat(int ver, const char *path, struct stat *sb)
             errno = ENOMEM;
             return -1;
         }
-        ret = real___lxstat(ver, newpath, sb);
+        ret = VIR_MOCK_CALL_LSTAT(ver, newpath, sb);
         free(newpath);
     } else if (STRPREFIX(path, fakedevicedir0)) {
         sb->st_mode = S_IFBLK;
@@ -567,7 +567,7 @@ int __lxstat(int ver, const char *path, struct stat *sb)
         sb->st_rdev = makedev(9, 0);
         return 0;
     } else {
-        ret = real___lxstat(ver, path, sb);
+        ret = VIR_MOCK_CALL_LSTAT(ver, path, sb);
     }
     return ret;
 }
@@ -587,7 +587,7 @@ int lstat(const char *path, struct stat *sb)
             errno = ENOMEM;
             return -1;
         }
-        ret = real_lstat(newpath, sb);
+        ret = VIR_MOCK_CALL_LSTAT(_STAT_VER, newpath, sb);
         free(newpath);
     } else if (STRPREFIX(path, fakedevicedir0)) {
         sb->st_mode = S_IFBLK;
@@ -598,7 +598,7 @@ int lstat(const char *path, struct stat *sb)
         sb->st_rdev = makedev(9, 0);
         return 0;
     } else {
-        ret = real_lstat(path, sb);
+        ret = VIR_MOCK_CALL_LSTAT(_STAT_VER, path, sb);
     }
     return ret;
 }
@@ -618,7 +618,7 @@ int __xstat(int ver, const char *path, struct stat *sb)
             errno = ENOMEM;
             return -1;
         }
-        ret = real___xstat(ver, newpath, sb);
+        ret = VIR_MOCK_CALL_STAT(ver, newpath, sb);
         free(newpath);
     } else if (STRPREFIX(path, fakedevicedir0)) {
         sb->st_mode = S_IFBLK;
@@ -629,7 +629,7 @@ int __xstat(int ver, const char *path, struct stat *sb)
         sb->st_rdev = makedev(9, 0);
         return 0;
     } else {
-        ret = real___xstat(ver, path, sb);
+        ret = VIR_MOCK_CALL_STAT(ver, path, sb);
     }
     return ret;
 }
@@ -669,7 +669,7 @@ int stat(const char *path, struct stat *sb)
         if (!(newpath = strdup(path)))
             return -1;
     }
-    ret = real_stat(newpath, sb);
+    ret = VIR_MOCK_CALL_STAT(_STAT_VER, newpath, sb);
     free(newpath);
     return ret;
 }
diff --git a/tests/virmock.h b/tests/virmock.h
index 62a7c8f..27c03ba 100644
--- a/tests/virmock.h
+++ b/tests/virmock.h
@@ -27,6 +27,7 @@
 # endif
 # include <stdlib.h>
 # include <stdio.h>
+# include <sys/stat.h>
 
 # include "internal.h"
 
@@ -254,6 +255,28 @@
     static void (*real_##name)(void);                                   \
     void name(void)
 
+static inline int
+callStat(int (*realStat)(const char *, struct stat *),
+         int (*realXstat)(int, const char *, struct stat *),
+         int __ver, const char *__filename, struct stat *__stat_buf)
+{
+    if (!realXstat) {
+        if (__ver == _STAT_VER) {
+            return realStat(__filename, __stat_buf);
+        } else {
+            fprintf(stderr, "Not handled __xstat(ver=%d)", __ver);
+            abort();
+        }
+    }
+
+    return realXstat(__ver, __filename, __stat_buf);
+}
+
+# define VIR_MOCK_CALL_STAT(__ver, __filename, __stat_buf)              \
+    callStat(real_stat, real___xstat, __ver, __filename, __stat_buf)
+# define VIR_MOCK_CALL_LSTAT(__ver, __filename, __stat_buf)             \
+    callStat(real_lstat, real___lxstat, __ver, __filename, __stat_buf)
+
 /*
  * The VIR_MOCK_WRAP_NNN_MMM() macros are intended for use in the
  * individual test suites. The define a stub implementation of
diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index c7786e5..f8a9fac 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -351,8 +351,7 @@ pci_device_new_from_stub(const struct pciDevice *data)
 
     /* If there is a config file for the device within virpcitestdata dir,
      * symlink it. Otherwise create a dummy config file. */
-    if ((real_stat && real_stat(configSrc, &sb) == 0) ||
-        (real___xstat && real___xstat(_STAT_VER, configSrc, &sb) == 0)) {
+    if (VIR_MOCK_CALL_STAT(_STAT_VER, configSrc, &sb) == 0) {
         /* On success, copy @configSrc into the destination (a copy,
          * rather than a symlink, is required since we write into the
          * file, and parallel VPATH builds must not stomp on the
@@ -867,10 +866,10 @@ __lxstat(int ver, const char *path, struct stat *sb)
         char *newpath;
         if (getrealpath(&newpath, path) < 0)
             return -1;
-        ret = real___lxstat(ver, newpath, sb);
+        ret = VIR_MOCK_CALL_LSTAT(ver, newpath, sb);
         VIR_FREE(newpath);
     } else {
-        ret = real___lxstat(ver, path, sb);
+        ret = VIR_MOCK_CALL_LSTAT(ver, path, sb);
     }
     return ret;
 }
@@ -886,10 +885,10 @@ lstat(const char *path, struct stat *sb)
         char *newpath;
         if (getrealpath(&newpath, path) < 0)
             return -1;
-        ret = real_lstat(newpath, sb);
+        ret = VIR_MOCK_CALL_LSTAT(_STAT_VER, newpath, sb);
         VIR_FREE(newpath);
     } else {
-        ret = real_lstat(path, sb);
+        ret = VIR_MOCK_CALL_LSTAT(_STAT_VER, path, sb);
     }
     return ret;
 }
@@ -905,10 +904,10 @@ __xstat(int ver, const char *path, struct stat *sb)
         char *newpath;
         if (getrealpath(&newpath, path) < 0)
             return -1;
-        ret = real___xstat(ver, newpath, sb);
+        ret = VIR_MOCK_CALL_STAT(ver, newpath, sb);
         VIR_FREE(newpath);
     } else {
-        ret = real___xstat(ver, path, sb);
+        ret = VIR_MOCK_CALL_STAT(ver, path, sb);
     }
     return ret;
 }
@@ -924,10 +923,10 @@ stat(const char *path, struct stat *sb)
         char *newpath;
         if (getrealpath(&newpath, path) < 0)
             return -1;
-        ret = real_stat(newpath, sb);
+        ret = VIR_MOCK_CALL_STAT(_STAT_VER, newpath, sb);
         VIR_FREE(newpath);
     } else {
-        ret = real_stat(path, sb);
+        ret = VIR_MOCK_CALL_STAT(_STAT_VER, path, sb);
     }
     return ret;
 }
-- 
2.8.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]