[PATCH 6/8] Add vshFileReadAll() helper.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This helper calls virFileReadAll(), and then outputs a
proper error if it fails for whatever reason.

Signed-off-by: Chris Lalancette <clalance@xxxxxxxxxx>
---
 tools/virsh.c |   65 ++++++++++++++++++++++++++++++++------------------------
 1 files changed, 37 insertions(+), 28 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 3c48eee..02ab7d3 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -294,6 +294,9 @@ static void vshDebug(vshControl *ctl, int level, const char *format, ...)
 /* XXX: add batch support */
 #define vshPrint(_ctl, ...)   fprintf(stdout, __VA_ARGS__)
 
+static int vshFileReadAll(vshControl *ctl, const char *filename, int size,
+                          char **buffer);
+
 static const char *vshDomainStateToString(int state);
 static const char *vshDomainVcpuStateToString(int state);
 static int vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
@@ -1154,7 +1157,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     if (vshCommandOptBool(cmd, "paused"))
@@ -1208,7 +1211,7 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     dom = virDomainDefineXML(ctl->conn, buffer);
@@ -2770,7 +2773,7 @@ cmdDomXMLFromNative(vshControl *ctl, const vshCmd *cmd)
     format = vshCommandOptString(cmd, "format", NULL);
     configFile = vshCommandOptString(cmd, "config", NULL);
 
-    if (virFileReadAll(configFile, 1024*1024, &configData) < 0)
+    if (vshFileReadAll(ctl, configFile, 1024*1024, &configData) < 0)
         return FALSE;
 
     xmlData = virConnectDomainXMLFromNative(ctl->conn, format, configData, flags);
@@ -2816,7 +2819,7 @@ cmdDomXMLToNative(vshControl *ctl, const vshCmd *cmd)
     format = vshCommandOptString(cmd, "format", NULL);
     xmlFile = vshCommandOptString(cmd, "xml", NULL);
 
-    if (virFileReadAll(xmlFile, 1024*1024, &xmlData) < 0)
+    if (vshFileReadAll(ctl, xmlFile, 1024*1024, &xmlData) < 0)
         return FALSE;
 
     configData = virConnectDomainXMLToNative(ctl->conn, format, xmlData, flags);
@@ -3165,7 +3168,7 @@ cmdNetworkCreate(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     network = virNetworkCreateXML(ctl->conn, buffer);
@@ -3213,7 +3216,7 @@ cmdNetworkDefine(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     network = virNetworkDefineXML(ctl->conn, buffer);
@@ -3936,7 +3939,7 @@ cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     iface = virInterfaceDefineXML(ctl->conn, buffer, 0);
@@ -4098,7 +4101,7 @@ cmdNWFilterDefine(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     nwfilter = virNWFilterDefineXML(ctl->conn, buffer);
@@ -4446,7 +4449,7 @@ cmdPoolCreate(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     pool = virStoragePoolCreateXML(ctl->conn, buffer, 0);
@@ -4499,7 +4502,7 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
         return FALSE;
     }
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     dev = virNodeDeviceCreateXML(ctl->conn, buffer, 0);
@@ -4711,7 +4714,7 @@ cmdPoolDefine(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     pool = virStoragePoolDefineXML(ctl->conn, buffer, 0);
@@ -5490,7 +5493,7 @@ cmdPoolDiscoverSources(vshControl * ctl, const vshCmd * cmd ATTRIBUTE_UNUSED)
     if (!vshConnectionUsability(ctl, ctl->conn))
         return FALSE;
 
-    if (srcSpecFile && virFileReadAll(srcSpecFile, VIRSH_MAX_XML_FILE, &srcSpec) < 0)
+    if (srcSpecFile && vshFileReadAll(ctl, srcSpecFile, VIRSH_MAX_XML_FILE, &srcSpec) < 0)
         return FALSE;
 
     srcList = virConnectFindStoragePoolSources(ctl->conn, type, srcSpec, 0);
@@ -5967,7 +5970,7 @@ cmdVolCreate(vshControl *ctl, const vshCmd *cmd)
         return FALSE;
     }
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
         virStoragePoolFree(pool);
         return FALSE;
     }
@@ -6028,9 +6031,8 @@ cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd)
     if (!(inputvol = vshCommandOptVol(ctl, cmd, "vol", "inputpool", NULL)))
         goto cleanup;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         goto cleanup;
-    }
 
     newvol = virStorageVolCreateXMLFrom(pool, buffer, inputvol, 0);
 
@@ -6603,7 +6605,7 @@ cmdSecretDefine(vshControl *ctl, const vshCmd *cmd)
     if (!from)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     res = virSecretDefineXML(ctl->conn, buffer, 0);
@@ -7545,7 +7547,7 @@ cmdAttachDevice(vshControl *ctl, const vshCmd *cmd)
         return FALSE;
     }
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
         virDomainFree(dom);
         return FALSE;
     }
@@ -7612,7 +7614,7 @@ cmdDetachDevice(vshControl *ctl, const vshCmd *cmd)
         return FALSE;
     }
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
         virDomainFree(dom);
         return FALSE;
     }
@@ -7679,7 +7681,7 @@ cmdUpdateDevice(vshControl *ctl, const vshCmd *cmd)
         return FALSE;
     }
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
         virDomainFree(dom);
         return FALSE;
     }
@@ -8269,7 +8271,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     result = virConnectCompareCPU(ctl->conn, buffer, 0);
@@ -8342,7 +8344,7 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd)
     if (!found)
         return FALSE;
 
-    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+    if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
         return FALSE;
 
     doc = xmlNewDoc(NULL);
@@ -8521,12 +8523,8 @@ editReadBackFile (vshControl *ctl, const char *filename)
 {
     char *ret;
 
-    if (virFileReadAll (filename, VIRSH_MAX_XML_FILE, &ret) == -1) {
-        vshError(ctl,
-                 _("%s: failed to read temporary file: %s"),
-                 filename, strerror(errno));
+    if (vshFileReadAll (ctl, filename, VIRSH_MAX_XML_FILE, &ret) == -1)
         return NULL;
-    }
     return ret;
 }
 
@@ -8809,9 +8807,8 @@ cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd)
     if (from == NULL)
         buffer = strdup("<domainsnapshot/>");
     else {
-        if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
+        if (vshFileReadAll(ctl, from, VIRSH_MAX_XML_FILE, &buffer) < 0)
             goto cleanup;
-        }
     }
     if (buffer == NULL) {
         vshError(ctl, "%s", _("Out of memory"));
@@ -10430,6 +10427,18 @@ _vshStrdup(vshControl *ctl, const char *s, const char *filename, int line)
     exit(EXIT_FAILURE);
 }
 
+static int
+vshFileReadAll(vshControl *ctl, const char *filename, int size, char **buffer)
+{
+    int ret;
+
+    ret = virFileReadAll(filename, size, buffer);
+    if (ret < 0)
+        vshError(ctl, _("Failed to read contents of '%s'"), filename);
+
+    return ret;
+}
+
 /*
  * Initialize connection.
  */
-- 
1.7.1.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]