From fef8127c5f4660f442adc02e28ebbe024d0fa6b6 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Thu, 6 Oct 2011 11:57:06 +0200 Subject: [PATCH] Introduce virXMLSaveFile as a wrapper for virFileRewrite 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 | 1 + src/util/util.c | 4 +++- src/util/xml.c | 36 ++++++++++++++++++++++++++++++++++++ src/util/xml.h | 5 +++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3366ac971a..ad1ef1699b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1300,6 +1300,7 @@ virKeycodeValueTranslate; # xml.h virXMLParseHelper; virXMLPropString; +virXMLSaveFile; virXPathBoolean; virXPathInt; virXPathLong; diff --git a/src/util/util.c b/src/util/util.c index ae0dc56302..bd52b21632 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -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) diff --git a/src/util/xml.c b/src/util/xml.c index b0942da89c..4e98b05227 100644 --- a/src/util/xml.c +++ b/src/util/xml.c @@ -16,12 +16,14 @@ #include #include #include /* for isnan() */ +#include #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); +} diff --git a/src/util/xml.h b/src/util/xml.h index d30e06646a..c492063cf1 100644 --- a/src/util/xml.h +++ b/src/util/xml.h @@ -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__ */ -- GitLab