render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 7ff748b22516848e8432d56ffa25a20452569648 Mon Sep 17 00:00:00 2001
Message-Id: <7ff748b22516848e8432d56ffa25a20452569648@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Tue, 2 Jul 2019 15:13:24 +0200
Subject: [PATCH] util: vircgroup: introduce virCgroup(Get|Set)ValueRaw
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If we need to get a path of specific file and we need to check its
existence before we use it then we can reuse that path to get/set
values instead of calling the existing get/set value functions which
would be building the path again.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit 3f741f9ace878e8aa9f537992b877a1e059d53a9)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1658890

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <176473826dad107b8880a33355b59dcefcb75e63.1562073117.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c     | 74 +++++++++++++++++++++++++---------------
 src/util/vircgrouppriv.h |  6 ++++
 2 files changed, 52 insertions(+), 28 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 3c99934b25..769e23a523 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -460,28 +460,22 @@ virCgroupGetBlockDevString(const char *path)
 
 
 int
-virCgroupSetValueStr(virCgroupPtr group,
-                     int controller,
-                     const char *key,
+virCgroupSetValueRaw(const char *path,
                      const char *value)
 {
-    VIR_AUTOFREE(char *) keypath = NULL;
-    char *tmp = NULL;
+    char *tmp;
 
-    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
-        return -1;
-
-    VIR_DEBUG("Set value '%s' to '%s'", keypath, value);
-    if (virFileWriteStr(keypath, value, 0) < 0) {
+    VIR_DEBUG("Set value '%s' to '%s'", path, value);
+    if (virFileWriteStr(path, value, 0) < 0) {
         if (errno == EINVAL &&
-            (tmp = strrchr(keypath, '/'))) {
+            (tmp = strrchr(path, '/'))) {
             virReportSystemError(errno,
                                  _("Invalid value '%s' for '%s'"),
                                  value, tmp + 1);
             return -1;
         }
         virReportSystemError(errno,
-                             _("Unable to write to '%s'"), keypath);
+                             _("Unable to write to '%s'"), path);
         return -1;
     }
 
@@ -489,6 +483,45 @@ virCgroupSetValueStr(virCgroupPtr group,
 }
 
 
+int
+virCgroupGetValueRaw(const char *path,
+                     char **value)
+{
+    int rc;
+
+    *value = NULL;
+
+    VIR_DEBUG("Get value %s", path);
+
+    if ((rc = virFileReadAll(path, 1024*1024, value)) < 0) {
+        virReportSystemError(errno,
+                             _("Unable to read from '%s'"), path);
+        return -1;
+    }
+
+    /* Terminated with '\n' has sometimes harmful effects to the caller */
+    if (rc > 0 && (*value)[rc - 1] == '\n')
+        (*value)[rc - 1] = '\0';
+
+    return 0;
+}
+
+
+int
+virCgroupSetValueStr(virCgroupPtr group,
+                     int controller,
+                     const char *key,
+                     const char *value)
+{
+    VIR_AUTOFREE(char *) keypath = NULL;
+
+    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
+        return -1;
+
+    return virCgroupSetValueRaw(keypath, value);
+}
+
+
 int
 virCgroupGetValueStr(virCgroupPtr group,
                      int controller,
@@ -496,26 +529,11 @@ virCgroupGetValueStr(virCgroupPtr group,
                      char **value)
 {
     VIR_AUTOFREE(char *) keypath = NULL;
-    int rc;
-
-    *value = NULL;
 
     if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
         return -1;
 
-    VIR_DEBUG("Get value %s", keypath);
-
-    if ((rc = virFileReadAll(keypath, 1024*1024, value)) < 0) {
-        virReportSystemError(errno,
-                             _("Unable to read from '%s'"), keypath);
-        return -1;
-    }
-
-    /* Terminated with '\n' has sometimes harmful effects to the caller */
-    if (rc > 0 && (*value)[rc - 1] == '\n')
-        (*value)[rc - 1] = '\0';
-
-    return 0;
+    return virCgroupGetValueRaw(keypath, value);
 }
 
 
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
index 6067f5cdc8..bdb3d493b1 100644
--- a/src/util/vircgrouppriv.h
+++ b/src/util/vircgrouppriv.h
@@ -62,6 +62,12 @@ struct _virCgroup {
     virCgroupV2Controller unified;
 };
 
+int virCgroupSetValueRaw(const char *path,
+                         const char *value);
+
+int virCgroupGetValueRaw(const char *path,
+                         char **value);
+
 int virCgroupSetValueStr(virCgroupPtr group,
                          int controller,
                          const char *key,
-- 
2.22.0