提交 fef8127c 编写于 作者: J Jiri Denemark

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.
上级 559644dd
......@@ -1300,6 +1300,7 @@ virKeycodeValueTranslate;
# xml.h
virXMLParseHelper;
virXMLPropString;
virXMLSaveFile;
virXPathBoolean;
virXPathInt;
virXPathLong;
......
......@@ -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)
......
......@@ -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);
}
......@@ -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__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册