[libvirt] [PATCH 1/9] virsh: Add new file for utility functions and move a few

Peter Krempa pkrempa at redhat.com
Tue Apr 11 15:53:47 UTC 2017


Don't accumulate helpers in virsh.c
---
 tools/Makefile.am            |  1 +
 tools/virsh-domain-monitor.c |  1 +
 tools/virsh-domain.c         |  1 +
 tools/virsh-util.c           | 66 ++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh-util.h           | 35 +++++++++++++++++++++++
 tools/virsh-volume.c         |  1 +
 tools/virsh.c                | 41 ---------------------------
 tools/virsh.h                |  4 ---
 8 files changed, 105 insertions(+), 45 deletions(-)
 create mode 100644 tools/virsh-util.c
 create mode 100644 tools/virsh-util.h

diff --git a/tools/Makefile.am b/tools/Makefile.am
index bfacaf214..56691c289 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -226,6 +226,7 @@ virsh_SOURCES =							\
 		virsh-pool.c virsh-pool.h			\
 		virsh-secret.c virsh-secret.h			\
 		virsh-snapshot.c virsh-snapshot.h		\
+		virsh-util.c virsh-util.h			\
 		virsh-volume.c virsh-volume.h			\
 		$(NULL)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 901a6ebef..4ade5651c 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -25,6 +25,7 @@

 #include <config.h>
 #include "virsh-domain-monitor.h"
+#include "virsh-util.h"

 #include <libxml/parser.h>
 #include <libxml/tree.h>
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 4b6c13ce4..7db74fecf 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -25,6 +25,7 @@

 #include <config.h>
 #include "virsh-domain.h"
+#include "virsh-util.h"

 #include <fcntl.h>
 #include <poll.h>
diff --git a/tools/virsh-util.c b/tools/virsh-util.c
new file mode 100644
index 000000000..98f16ff1a
--- /dev/null
+++ b/tools/virsh-util.c
@@ -0,0 +1,66 @@
+/*
+ * virsh-util.c: helpers for virsh
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include "virsh-util.h"
+
+#include "virfile.h"
+
+int
+virshDomainState(vshControl *ctl,
+                 virDomainPtr dom,
+                 int *reason)
+{
+    virDomainInfo info;
+    virshControlPtr priv = ctl->privData;
+
+    if (reason)
+        *reason = -1;
+
+    if (!priv->useGetInfo) {
+        int state;
+        if (virDomainGetState(dom, &state, reason, 0) < 0) {
+            virErrorPtr err = virGetLastError();
+            if (err && err->code == VIR_ERR_NO_SUPPORT)
+                priv->useGetInfo = true;
+            else
+                return -1;
+        } else {
+            return state;
+        }
+    }
+
+    /* fall back to virDomainGetInfo if virDomainGetState is not supported */
+    if (virDomainGetInfo(dom, &info) < 0)
+        return -1;
+    else
+        return info.state;
+}
+
+
+int
+virshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED,
+                const char *bytes,
+                size_t nbytes,
+                void *opaque)
+{
+    int *fd = opaque;
+
+    return safewrite(*fd, bytes, nbytes);
+}
diff --git a/tools/virsh-util.h b/tools/virsh-util.h
new file mode 100644
index 000000000..207d57859
--- /dev/null
+++ b/tools/virsh-util.h
@@ -0,0 +1,35 @@
+/*
+ * virsh-util.h: helpers for virsh
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef VIRSH_UTIL_H
+# define VIRSH_UTIL_H
+
+# include "virsh.h"
+
+int
+virshDomainState(vshControl *ctl,
+                 virDomainPtr dom,
+                 int *reason);
+
+int
+virshStreamSink(virStreamPtr st,
+                const char *bytes,
+                size_t nbytes,
+                void *opaque);
+
+#endif /* VIRSH_UTIL_H */
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index 32ffb8149..ddd41d229 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -25,6 +25,7 @@

 #include <config.h>
 #include "virsh-volume.h"
+#include "virsh-util.h"

 #include <fcntl.h>

diff --git a/tools/virsh.c b/tools/virsh.c
index 7eb51ab7d..31e23bd27 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -257,14 +257,6 @@ virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force)
     return 0;
 }

-int virshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED,
-                    const char *bytes, size_t nbytes, void *opaque)
-{
-    int *fd = opaque;
-
-    return safewrite(*fd, bytes, nbytes);
-}
-
 /* ---------------
  * Command Connect
  * ---------------
@@ -347,39 +339,6 @@ virshConnectionHandler(vshControl *ctl)
 }


-/* ---------------
- * Misc utils
- * ---------------
- */
-int
-virshDomainState(vshControl *ctl, virDomainPtr dom, int *reason)
-{
-    virDomainInfo info;
-    virshControlPtr priv = ctl->privData;
-
-    if (reason)
-        *reason = -1;
-
-    if (!priv->useGetInfo) {
-        int state;
-        if (virDomainGetState(dom, &state, reason, 0) < 0) {
-            virErrorPtr err = virGetLastError();
-            if (err && err->code == VIR_ERR_NO_SUPPORT)
-                priv->useGetInfo = true;
-            else
-                return -1;
-        } else {
-            return state;
-        }
-    }
-
-    /* fall back to virDomainGetInfo if virDomainGetState is not supported */
-    if (virDomainGetInfo(dom, &info) < 0)
-        return -1;
-    else
-        return info.state;
-}
-
 /*
  * Initialize connection.
  */
diff --git a/tools/virsh.h b/tools/virsh.h
index fd552bb3a..9e42ef9bb 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -145,9 +145,5 @@ typedef enum {
 } virshLookupByFlags;

 virConnectPtr virshConnect(vshControl *ctl, const char *uri, bool readonly);
-int virshDomainState(vshControl *ctl, virDomainPtr dom, int *reason);
-
-int virshStreamSink(virStreamPtr st, const char *bytes, size_t nbytes,
-                    void *opaque);

 #endif /* VIRSH_H */
-- 
2.12.2




More information about the libvir-list mailing list