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.
This commit is contained in:
Jiri Denemark 2011-10-06 11:57:06 +02:00
parent 559644ddd2
commit fef8127c5f
4 changed files with 45 additions and 1 deletions

View File

@ -1300,6 +1300,7 @@ virKeycodeValueTranslate;
# xml.h
virXMLParseHelper;
virXMLPropString;
virXMLSaveFile;
virXPathBoolean;
virXPathInt;
virXPathLong;

View 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)

View File

@ -16,12 +16,14 @@
#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);
}

View 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__ */