c480ed
From 7ff748b22516848e8432d56ffa25a20452569648 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <7ff748b22516848e8432d56ffa25a20452569648@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Tue, 2 Jul 2019 15:13:24 +0200
c480ed
Subject: [PATCH] util: vircgroup: introduce virCgroup(Get|Set)ValueRaw
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
If we need to get a path of specific file and we need to check its
c480ed
existence before we use it then we can reuse that path to get/set
c480ed
values instead of calling the existing get/set value functions which
c480ed
would be building the path again.
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
(cherry picked from commit 3f741f9ace878e8aa9f537992b877a1e059d53a9)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1658890
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <176473826dad107b8880a33355b59dcefcb75e63.1562073117.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroup.c     | 74 +++++++++++++++++++++++++---------------
c480ed
 src/util/vircgrouppriv.h |  6 ++++
c480ed
 2 files changed, 52 insertions(+), 28 deletions(-)
c480ed
c480ed
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c480ed
index 3c99934b25..769e23a523 100644
c480ed
--- a/src/util/vircgroup.c
c480ed
+++ b/src/util/vircgroup.c
c480ed
@@ -460,28 +460,22 @@ virCgroupGetBlockDevString(const char *path)
c480ed
 
c480ed
 
c480ed
 int
c480ed
-virCgroupSetValueStr(virCgroupPtr group,
c480ed
-                     int controller,
c480ed
-                     const char *key,
c480ed
+virCgroupSetValueRaw(const char *path,
c480ed
                      const char *value)
c480ed
 {
c480ed
-    VIR_AUTOFREE(char *) keypath = NULL;
c480ed
-    char *tmp = NULL;
c480ed
+    char *tmp;
c480ed
 
c480ed
-    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
c480ed
-        return -1;
c480ed
-
c480ed
-    VIR_DEBUG("Set value '%s' to '%s'", keypath, value);
c480ed
-    if (virFileWriteStr(keypath, value, 0) < 0) {
c480ed
+    VIR_DEBUG("Set value '%s' to '%s'", path, value);
c480ed
+    if (virFileWriteStr(path, value, 0) < 0) {
c480ed
         if (errno == EINVAL &&
c480ed
-            (tmp = strrchr(keypath, '/'))) {
c480ed
+            (tmp = strrchr(path, '/'))) {
c480ed
             virReportSystemError(errno,
c480ed
                                  _("Invalid value '%s' for '%s'"),
c480ed
                                  value, tmp + 1);
c480ed
             return -1;
c480ed
         }
c480ed
         virReportSystemError(errno,
c480ed
-                             _("Unable to write to '%s'"), keypath);
c480ed
+                             _("Unable to write to '%s'"), path);
c480ed
         return -1;
c480ed
     }
c480ed
 
c480ed
@@ -489,6 +483,45 @@ virCgroupSetValueStr(virCgroupPtr group,
c480ed
 }
c480ed
 
c480ed
 
c480ed
+int
c480ed
+virCgroupGetValueRaw(const char *path,
c480ed
+                     char **value)
c480ed
+{
c480ed
+    int rc;
c480ed
+
c480ed
+    *value = NULL;
c480ed
+
c480ed
+    VIR_DEBUG("Get value %s", path);
c480ed
+
c480ed
+    if ((rc = virFileReadAll(path, 1024*1024, value)) < 0) {
c480ed
+        virReportSystemError(errno,
c480ed
+                             _("Unable to read from '%s'"), path);
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    /* Terminated with '\n' has sometimes harmful effects to the caller */
c480ed
+    if (rc > 0 && (*value)[rc - 1] == '\n')
c480ed
+        (*value)[rc - 1] = '\0';
c480ed
+
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+
c480ed
+int
c480ed
+virCgroupSetValueStr(virCgroupPtr group,
c480ed
+                     int controller,
c480ed
+                     const char *key,
c480ed
+                     const char *value)
c480ed
+{
c480ed
+    VIR_AUTOFREE(char *) keypath = NULL;
c480ed
+
c480ed
+    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
+    return virCgroupSetValueRaw(keypath, value);
c480ed
+}
c480ed
+
c480ed
+
c480ed
 int
c480ed
 virCgroupGetValueStr(virCgroupPtr group,
c480ed
                      int controller,
c480ed
@@ -496,26 +529,11 @@ virCgroupGetValueStr(virCgroupPtr group,
c480ed
                      char **value)
c480ed
 {
c480ed
     VIR_AUTOFREE(char *) keypath = NULL;
c480ed
-    int rc;
c480ed
-
c480ed
-    *value = NULL;
c480ed
 
c480ed
     if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
c480ed
         return -1;
c480ed
 
c480ed
-    VIR_DEBUG("Get value %s", keypath);
c480ed
-
c480ed
-    if ((rc = virFileReadAll(keypath, 1024*1024, value)) < 0) {
c480ed
-        virReportSystemError(errno,
c480ed
-                             _("Unable to read from '%s'"), keypath);
c480ed
-        return -1;
c480ed
-    }
c480ed
-
c480ed
-    /* Terminated with '\n' has sometimes harmful effects to the caller */
c480ed
-    if (rc > 0 && (*value)[rc - 1] == '\n')
c480ed
-        (*value)[rc - 1] = '\0';
c480ed
-
c480ed
-    return 0;
c480ed
+    return virCgroupGetValueRaw(keypath, value);
c480ed
 }
c480ed
 
c480ed
 
c480ed
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
c480ed
index 6067f5cdc8..bdb3d493b1 100644
c480ed
--- a/src/util/vircgrouppriv.h
c480ed
+++ b/src/util/vircgrouppriv.h
c480ed
@@ -62,6 +62,12 @@ struct _virCgroup {
c480ed
     virCgroupV2Controller unified;
c480ed
 };
c480ed
 
c480ed
+int virCgroupSetValueRaw(const char *path,
c480ed
+                         const char *value);
c480ed
+
c480ed
+int virCgroupGetValueRaw(const char *path,
c480ed
+                         char **value);
c480ed
+
c480ed
 int virCgroupSetValueStr(virCgroupPtr group,
c480ed
                          int controller,
c480ed
                          const char *key,
c480ed
-- 
c480ed
2.22.0
c480ed