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