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

[Libvir] PATCH: Make virsh use virFileReadAll



This patch makes virsh use the virFileReadAll API from util.c, instead of
duplicating it. This means we need to export this symbol from the library.

diff -r 1eb02a424697 src/libvirt_sym.version
--- a/src/libvirt_sym.version	Sat Jan 19 13:45:07 2008 -0500
+++ b/src/libvirt_sym.version	Sat Jan 19 14:28:41 2008 -0500
@@ -131,5 +131,7 @@
 	__virDomainMigratePerform;
 	__virDomainMigrateFinish;
 
+        __virFileReadAll;
+
     local: *;
 };
diff -r 1eb02a424697 src/util.c
--- a/src/util.c	Sat Jan 19 13:45:07 2008 -0500
+++ b/src/util.c	Sat Jan 19 14:28:41 2008 -0500
@@ -316,9 +316,9 @@ ssize_t safewrite(int fd, const void *bu
 }
 
 
-int virFileReadAll(const char *path,
-                   int maxlen,
-                   char **buf)
+int __virFileReadAll(const char *path,
+                     int maxlen,
+                     char **buf)
 {
     FILE *fh;
     struct stat st;
diff -r 1eb02a424697 src/util.h
--- a/src/util.h	Sat Jan 19 13:45:07 2008 -0500
+++ b/src/util.h	Sat Jan 19 14:28:41 2008 -0500
@@ -33,9 +33,10 @@ int saferead(int fd, void *buf, size_t c
 int saferead(int fd, void *buf, size_t count);
 ssize_t safewrite(int fd, const void *buf, size_t count);
 
-int virFileReadAll(const char *path,
-		   int maxlen,
-                   char **buf);
+int __virFileReadAll(const char *path,
+		     int maxlen,
+		     char **buf);
+#define virFileReadAll(p,m,b) __virFileReadAll((p),(m),(b))
 
 int virFileMatchesNameSuffix(const char *file,
                              const char *name,
diff -r 1eb02a424697 src/virsh.c
--- a/src/virsh.c	Sat Jan 19 13:45:07 2008 -0500
+++ b/src/virsh.c	Sat Jan 19 14:28:41 2008 -0500
@@ -48,6 +48,7 @@
 
 #include "internal.h"
 #include "console.h"
+#include "util.h"
 
 static char *progname;
 
@@ -55,6 +56,8 @@ static char *progname;
 #define TRUE 1
 #define FALSE 0
 #endif
+
+#define VIRSH_MAX_XML_FILE 10*1024*1024
 
 #define VSH_PROMPT_RW    "virsh # "
 #define VSH_PROMPT_RO    "virsh > "
@@ -858,66 +861,6 @@ static vshCmdOptDef opts_create[] = {
     {NULL, 0, 0, NULL}
 };
 
-/* Read in a whole file and return it as a string.
- * If it fails, it logs an error and returns NULL.
- * String must be freed by caller.
- */
-static char *
-readFile (vshControl *ctl, const char *filename)
-{
-    char *retval;
-    int len = 0, fd;
-
-    if ((fd = open(filename, O_RDONLY)) == -1) {
-        vshError (ctl, FALSE, _("Failed to open '%s': %s"),
-                  filename, strerror (errno));
-        return NULL;
-    }
-
-    if (!(retval = malloc(len + 1)))
-        goto out_of_memory;
-
-    while (1) {
-        char buffer[1024];
-        char *new;
-        int ret;
-
-        if ((ret = read(fd, buffer, sizeof(buffer))) == 0)
-            break;
-
-        if (ret == -1) {
-            if (errno == EINTR)
-                continue;
-
-            vshError (ctl, FALSE, _("Failed to open '%s': read: %s"),
-                      filename, strerror (errno));
-            goto error;
-        }
-
-        if (!(new = realloc(retval, len + ret + 1)))
-            goto out_of_memory;
-
-        retval = new;
-
-        memcpy(retval + len, buffer, ret);
-        len += ret;
-   }
-
-   retval[len] = '\0';
-   return retval;
-
- out_of_memory:
-   vshError (ctl, FALSE, _("Error allocating memory: %s"),
-             strerror(errno));
-
- error:
-   if (retval)
-     free(retval);
-   close(fd);
-
-   return NULL;
-}
-
 static int
 cmdCreate(vshControl * ctl, vshCmd * cmd)
 {
@@ -934,8 +877,8 @@ cmdCreate(vshControl * ctl, vshCmd * cmd
     if (!found)
         return FALSE;
 
-    buffer = readFile (ctl, from);
-    if (buffer == NULL) return FALSE;
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+        return FALSE;
 
     dom = virDomainCreateLinux(ctl->conn, buffer, 0);
     free (buffer);
@@ -982,8 +925,8 @@ cmdDefine(vshControl * ctl, vshCmd * cmd
     if (!found)
         return FALSE;
 
-    buffer = readFile (ctl, from);
-    if (buffer == NULL) return FALSE;
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+        return FALSE;
 
     dom = virDomainDefineXML(ctl->conn, buffer);
     free (buffer);
@@ -2372,8 +2315,8 @@ cmdNetworkCreate(vshControl * ctl, vshCm
     if (!found)
         return FALSE;
 
-    buffer = readFile (ctl, from);
-    if (buffer == NULL) return FALSE;
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+        return FALSE;
 
     network = virNetworkCreateXML(ctl->conn, buffer);
     free (buffer);
@@ -2420,8 +2363,8 @@ cmdNetworkDefine(vshControl * ctl, vshCm
     if (!found)
         return FALSE;
 
-    buffer = readFile (ctl, from);
-    if (buffer == NULL) return FALSE;
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+        return FALSE;
 
     network = virNetworkDefineXML(ctl->conn, buffer);
     free (buffer);
@@ -3107,8 +3050,8 @@ cmdAttachDevice(vshControl * ctl, vshCmd
         return FALSE;
     }
 
-    buffer = readFile (ctl, from);
-    if (buffer == NULL) return FALSE;
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+        return FALSE;
 
     ret = virDomainAttachDevice(dom, buffer);
     free (buffer);
@@ -3161,8 +3104,8 @@ cmdDetachDevice(vshControl * ctl, vshCmd
         return FALSE;
     }
 
-    buffer = readFile (ctl, from);
-    if (buffer == NULL) return FALSE;
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+        return FALSE;
 
     ret = virDomainDetachDevice(dom, buffer);
     free (buffer);



Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 


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