testsuite/path: defer and inline get_rootpath() In a handful of use-cases, we don't need to trap the respective libc function. Although at that point we have already fetched the rootfs environment variable. Defer it until it's needed and in the process inline the function. As result we have a) less duplication in the wrappers and b) the rootpath handling is no longer split across multiple functions. Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> Link: https://github.com/kmod-project/kmod/pull/355 Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
diff --git a/testsuite/path.c b/testsuite/path.c index e6a9ef3..25548f4 100644 --- a/testsuite/path.c +++ b/testsuite/path.c
@@ -26,9 +26,6 @@ #include "testsuite.h" -static const char *rootpath; -static size_t rootpathlen; - static inline bool need_trap(const char *path) { /* @@ -45,6 +42,8 @@ static const char *trap_path(const char *path, char buf[PATH_MAX * 2]) { + static const char *rootpath; + static size_t rootpathlen; size_t len; if (!need_trap(path)) @@ -52,6 +51,17 @@ len = strlen(path); + if (rootpath == NULL) { + rootpath = getenv(S_TC_ROOTFS); + if (rootpath == NULL) { + ERR("TRAP: missing export %s?\n", S_TC_ROOTFS); + errno = ENOENT; + return NULL; + } + + rootpathlen = strlen(rootpath); + } + if (len + rootpathlen > PATH_MAX * 2) { errno = ENAMETOOLONG; return NULL; @@ -62,23 +72,6 @@ return buf; } -static bool get_rootpath(const char *f) -{ - if (rootpath != NULL) - return true; - - rootpath = getenv(S_TC_ROOTFS); - if (rootpath == NULL) { - ERR("TRAP %s(): missing export %s?\n", f, S_TC_ROOTFS); - errno = ENOENT; - return false; - } - - rootpathlen = strlen(rootpath); - - return true; -} - static void *get_libc_func(const char *f) { void *fp; @@ -101,8 +94,6 @@ char buf[PATH_MAX * 2]; \ static rettype (*_fn)(const char *); \ \ - if (!get_rootpath(__func__)) \ - return failret; \ p = trap_path(path, buf); \ if (p == NULL) \ return failret; \ @@ -120,8 +111,6 @@ char buf[PATH_MAX * 2]; \ static rettype (*_fn)(const char *, arg2t arg2); \ \ - if (!get_rootpath(__func__)) \ - return failret; \ p = trap_path(path, buf); \ if (p == NULL) \ return failret; \ @@ -139,8 +128,6 @@ char buf[PATH_MAX * 2]; \ static int (*_fn)(const char *path, int flags, ...); \ \ - if (!get_rootpath(__func__)) \ - return -1; \ p = trap_path(path, buf); \ if (p == NULL) \ return -1; \ @@ -168,8 +155,6 @@ char buf[PATH_MAX * 2]; \ static int (*_fn)(int ver, const char *path, struct stat##suffix *); \ \ - if (!get_rootpath(__func__)) \ - return -1; \ p = trap_path(path, buf); \ if (p == NULL) \ return -1; \