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