提交 cae6619a 编写于 作者: E Eric Blake

snapshot: Refactor virDomainSnapshotDefFormat

Split out an internal helper that produces format into a
virBuffer, similar to what domain_conf.c does, and making
the next patch easier to write.
Signed-off-by: NEric Blake <eblake@redhat.com>
Reviewed-by: NJohn Ferlan <jferlan@redhat.com>
上级 c5029559
......@@ -703,92 +703,110 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf,
}
char *
virDomainSnapshotDefFormat(const char *uuidstr,
virDomainSnapshotDefPtr def,
virCapsPtr caps,
virDomainXMLOptionPtr xmlopt,
unsigned int flags)
/* Append XML describing def into buf. Return 0 on success, or -1 on
* failure with buf cleared. */
static int
virDomainSnapshotDefFormatInternal(virBufferPtr buf,
const char *uuidstr,
virDomainSnapshotDefPtr def,
virCapsPtr caps,
virDomainXMLOptionPtr xmlopt,
unsigned int flags)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
size_t i;
int domainflags = VIR_DOMAIN_DEF_FORMAT_INACTIVE;
virCheckFlags(VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE |
VIR_DOMAIN_SNAPSHOT_FORMAT_INTERNAL, NULL);
if (flags & VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE)
domainflags |= VIR_DOMAIN_DEF_FORMAT_SECURE;
virBufferAddLit(&buf, "<domainsnapshot>\n");
virBufferAdjustIndent(&buf, 2);
virBufferAddLit(buf, "<domainsnapshot>\n");
virBufferAdjustIndent(buf, 2);
virBufferEscapeString(&buf, "<name>%s</name>\n", def->name);
virBufferEscapeString(buf, "<name>%s</name>\n", def->name);
if (def->description)
virBufferEscapeString(&buf, "<description>%s</description>\n",
virBufferEscapeString(buf, "<description>%s</description>\n",
def->description);
virBufferAsprintf(&buf, "<state>%s</state>\n",
virBufferAsprintf(buf, "<state>%s</state>\n",
virDomainSnapshotStateTypeToString(def->state));
if (def->parent) {
virBufferAddLit(&buf, "<parent>\n");
virBufferAdjustIndent(&buf, 2);
virBufferEscapeString(&buf, "<name>%s</name>\n", def->parent);
virBufferAdjustIndent(&buf, -2);
virBufferAddLit(&buf, "</parent>\n");
virBufferAddLit(buf, "<parent>\n");
virBufferAdjustIndent(buf, 2);
virBufferEscapeString(buf, "<name>%s</name>\n", def->parent);
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</parent>\n");
}
virBufferAsprintf(&buf, "<creationTime>%lld</creationTime>\n",
virBufferAsprintf(buf, "<creationTime>%lld</creationTime>\n",
def->creationTime);
if (def->memory) {
virBufferAsprintf(&buf, "<memory snapshot='%s'",
virBufferAsprintf(buf, "<memory snapshot='%s'",
virDomainSnapshotLocationTypeToString(def->memory));
virBufferEscapeString(&buf, " file='%s'", def->file);
virBufferAddLit(&buf, "/>\n");
virBufferEscapeString(buf, " file='%s'", def->file);
virBufferAddLit(buf, "/>\n");
}
if (def->ndisks) {
virBufferAddLit(&buf, "<disks>\n");
virBufferAdjustIndent(&buf, 2);
virBufferAddLit(buf, "<disks>\n");
virBufferAdjustIndent(buf, 2);
for (i = 0; i < def->ndisks; i++) {
if (virDomainSnapshotDiskDefFormat(&buf, &def->disks[i], xmlopt) < 0)
if (virDomainSnapshotDiskDefFormat(buf, &def->disks[i], xmlopt) < 0)
goto error;
}
virBufferAdjustIndent(&buf, -2);
virBufferAddLit(&buf, "</disks>\n");
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</disks>\n");
}
if (def->dom) {
if (virDomainDefFormatInternal(def->dom, caps, domainflags, &buf,
if (virDomainDefFormatInternal(def->dom, caps, domainflags, buf,
xmlopt) < 0)
goto error;
} else if (uuidstr) {
virBufferAddLit(&buf, "<domain>\n");
virBufferAdjustIndent(&buf, 2);
virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", uuidstr);
virBufferAdjustIndent(&buf, -2);
virBufferAddLit(&buf, "</domain>\n");
virBufferAddLit(buf, "<domain>\n");
virBufferAdjustIndent(buf, 2);
virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr);
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</domain>\n");
}
if (virSaveCookieFormatBuf(&buf, def->cookie,
if (virSaveCookieFormatBuf(buf, def->cookie,
virDomainXMLOptionGetSaveCookie(xmlopt)) < 0)
goto error;
if (flags & VIR_DOMAIN_SNAPSHOT_FORMAT_INTERNAL)
virBufferAsprintf(&buf, "<active>%d</active>\n", def->current);
virBufferAsprintf(buf, "<active>%d</active>\n", def->current);
virBufferAdjustIndent(&buf, -2);
virBufferAddLit(&buf, "</domainsnapshot>\n");
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</domainsnapshot>\n");
if (virBufferCheckError(&buf) < 0)
return NULL;
if (virBufferCheckError(buf) < 0)
goto error;
return virBufferContentAndReset(&buf);
return 0;
error:
virBufferFreeAndReset(&buf);
return NULL;
virBufferFreeAndReset(buf);
return -1;
}
char *
virDomainSnapshotDefFormat(const char *uuidstr,
virDomainSnapshotDefPtr def,
virCapsPtr caps,
virDomainXMLOptionPtr xmlopt,
unsigned int flags)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
virCheckFlags(VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE |
VIR_DOMAIN_SNAPSHOT_FORMAT_INTERNAL, NULL);
if (virDomainSnapshotDefFormatInternal(&buf, uuidstr, def, caps,
xmlopt, flags) < 0)
return NULL;
return virBufferContentAndReset(&buf);
}
/* Snapshot Obj functions */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册