Extract the per-directory hook checking logic into a new
virHookCheckDir() helper function, separating it from the
input validation done in virHookCheck().

This prepares for supporting multiple hook directories by
making virHookCheckDir() reusable per directory.

No functional change.

Signed-off-by: Marc-André Lureau <[email protected]>
---
 src/util/virhook.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/src/util/virhook.c b/src/util/virhook.c
index d012bb182576..9cbe55faa03e 100644
--- a/src/util/virhook.c
+++ b/src/util/virhook.c
@@ -129,17 +129,17 @@ VIR_ENUM_IMPL(virHookBhyveOp,
 static int virHooksFound = -1;
 
 /**
- * virHookCheck:
+ * virHookCheckDir:
+ * @hook_dir: the directory to check for hooks
  * @driver: the driver name "daemon", "qemu", "lxc"...
  *
- * Check is there is an installed hook for the given driver, if this
- * is the case register it. Then subsequent calls to virHookCall
- * will call the hook if found.
+ * Check if there is an installed hook for the given driver in the
+ * specified directory.
  *
  * Returns 1 if found, 0 if not found, and -1 in case of error
  */
 static int
-virHookCheck(int no, const char *driver)
+virHookCheckDir(const char *hook_dir, const char *driver)
 {
     int ret;
     g_autoptr(DIR) dir = NULL;
@@ -147,13 +147,7 @@ virHookCheck(int no, const char *driver)
     g_autofree char *path = NULL;
     g_autofree char *dir_path = NULL;
 
-    if (driver == NULL) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Invalid hook name for #%1$d"), no);
-        return -1;
-    }
-
-    path = g_build_filename(LIBVIRT_HOOK_DIR, driver, NULL);
+    path = g_build_filename(hook_dir, driver, NULL);
 
     if (!virFileExists(path)) {
         VIR_DEBUG("No hook script %s", path);
@@ -196,6 +190,27 @@ virHookCheck(int no, const char *driver)
     return ret;
 }
 
+/**
+ * virHookCheck:
+ * @no: the driver number
+ * @driver: the driver name "daemon", "qemu", "lxc"...
+ *
+ * Check if there is an installed hook for the given driver.
+ *
+ * Returns 1 if found, 0 if not found, and -1 in case of error
+ */
+static int
+virHookCheck(int no, const char *driver)
+{
+    if (driver == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Invalid hook name for #%1$d"), no);
+        return -1;
+    }
+
+    return virHookCheckDir(LIBVIRT_HOOK_DIR, driver);
+}
+
 /*
  * virHookInitialize:
  *

-- 
2.56.0.rc0.29.g47ce80527c56

Reply via email to