]> xenbits.xensource.com Git - libvirt.git/commitdiff
Introduce virXMLSaveFile as a wrapper for virFileRewrite
authorJiri Denemark <jdenemar@redhat.com>
Thu, 6 Oct 2011 09:57:06 +0000 (11:57 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Thu, 27 Oct 2011 18:13:06 +0000 (20:13 +0200)
Every time we write XML into a file we call virEmitXMLWarning to write a
warning that the file is automatically generated. virXMLSaveFile
simplifies this into a single step and makes rewriting existing XML file
safe by using virFileRewrite internally.

src/libvirt_private.syms
src/util/util.c
src/util/xml.c
src/util/xml.h

index 3366ac971a15b6e1dfdef8186ba55c1178dec784..ad1ef1699b7c6d025d8be6c12c86d01ea548e53c 100644 (file)
@@ -1300,6 +1300,7 @@ virKeycodeValueTranslate;
 # xml.h
 virXMLParseHelper;
 virXMLPropString;
+virXMLSaveFile;
 virXPathBoolean;
 virXPathInt;
 virXPathLong;
index ae0dc56302c12b1a3f9b1f112836576ba97702cb..bd52b21632443016ba18b17014e14ebb9b9cae13 100644 (file)
@@ -2546,8 +2546,10 @@ OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:\n\
 or other application using the libvirt API.\n\
 -->\n\n";
 
-    if (fd < 0 || !name || !cmd)
+    if (fd < 0 || !name || !cmd) {
+        errno = EINVAL;
         return -1;
+    }
 
     len = strlen(prologue);
     if (safewrite(fd, prologue, len) != len)
index b0942da89ca9b358a8ea73bad05c70a08ee12cf9..4e98b052272810588523ce2efa13727d638c8a01 100644 (file)
 #include <stdarg.h>
 #include <limits.h>
 #include <math.h>               /* for isnan() */
+#include <sys/stat.h>
 
 #include "virterror_internal.h"
 #include "xml.h"
 #include "buf.h"
 #include "util.h"
 #include "memory.h"
+#include "virfile.h"
 
 #define VIR_FROM_THIS VIR_FROM_XML
 
@@ -797,3 +799,37 @@ error:
     }
     goto cleanup;
 }
+
+
+struct virXMLRewritFileData {
+    const char *warnName;
+    const char *warnCommand;
+    const char *xml;
+};
+
+static int
+virXMLRewriteFile(int fd, void *opaque)
+{
+    struct virXMLRewritFileData *data = opaque;
+
+    if (data->warnName && data->warnCommand) {
+        if (virEmitXMLWarning(fd, data->warnName, data->warnCommand) < 0)
+            return -1;
+    }
+
+    if (safewrite(fd, data->xml, strlen(data->xml)) < 0)
+        return -1;
+
+    return 0;
+}
+
+int
+virXMLSaveFile(const char *path,
+               const char *warnName,
+               const char *warnCommand,
+               const char *xml)
+{
+    struct virXMLRewritFileData data = { warnName, warnCommand, xml };
+
+    return virFileRewrite(path, S_IRUSR | S_IWUSR, virXMLRewriteFile, &data);
+}
index d30e06646a3cddaef25615689fe020e2c72b9075..c492063cf1f5a9384a8d02f11ec4ad200faa636d 100644 (file)
@@ -138,4 +138,9 @@ xmlDocPtr      virXMLParseHelper(int domcode,
 # define virXMLParseFileCtxt(filename, pctxt)                           \
     virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, pctxt)
 
+int virXMLSaveFile(const char *path,
+                   const char *warnName,
+                   const char *warnCommand,
+                   const char *xml);
+
 #endif                          /* __VIR_XML_H__ */