提交 4319444d 编写于 作者: P Peter Krempa

conf: snapshot: Refactor virDomainSnapshotDefAssignExternalNames

Get rid of one indentation level by negating condition and remove ugly
pointer arithmetic at the cost of one extra allocation.
上级 040a4fe7
...@@ -440,66 +440,60 @@ virDomainSnapshotDefParseString(const char *xmlStr, ...@@ -440,66 +440,60 @@ virDomainSnapshotDefParseString(const char *xmlStr,
static int static int
virDomainSnapshotDefAssignExternalNames(virDomainSnapshotDefPtr def) virDomainSnapshotDefAssignExternalNames(virDomainSnapshotDefPtr def)
{ {
const char *origpath;
char *tmppath;
char *tmp;
struct stat sb;
size_t i; size_t i;
int ret = -1;
for (i = 0; i < def->ndisks; i++) { for (i = 0; i < def->ndisks; i++) {
virDomainSnapshotDiskDefPtr disk = &def->disks[i]; virDomainSnapshotDiskDefPtr disk = &def->disks[i];
if (disk->snapshot == VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL && if (disk->snapshot != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL ||
!disk->src->path) { disk->src->path)
const char *original = virDomainDiskGetSource(def->dom->disks[i]); continue;
const char *tmp;
struct stat sb;
if (disk->src->type != VIR_STORAGE_TYPE_FILE) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("cannot generate external snapshot name "
"for disk '%s' on a '%s' device"),
disk->name,
virStorageTypeToString(disk->src->type));
goto cleanup;
}
if (!original) { if (disk->src->type != VIR_STORAGE_TYPE_FILE) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("cannot generate external snapshot name " _("cannot generate external snapshot name "
"for disk '%s' without source"), "for disk '%s' on a '%s' device"),
disk->name); disk->name, virStorageTypeToString(disk->src->type));
goto cleanup; return -1;
} }
if (stat(original, &sb) < 0 || !S_ISREG(sb.st_mode)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("source for disk '%s' is not a regular "
"file; refusing to generate external "
"snapshot name"),
disk->name);
goto cleanup;
}
tmp = strrchr(original, '.'); if (!(origpath = virDomainDiskGetSource(def->dom->disks[i]))) {
if (!tmp || strchr(tmp, '/')) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
if (virAsprintf(&disk->src->path, "%s.%s", original, _("cannot generate external snapshot name "
def->name) < 0) "for disk '%s' without source"),
goto cleanup; disk->name);
} else { return -1;
if ((tmp - original) > INT_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("integer overflow"));
goto cleanup;
}
if (virAsprintf(&disk->src->path, "%.*s.%s",
(int) (tmp - original), original,
def->name) < 0)
goto cleanup;
}
} }
}
ret = 0; if (stat(origpath, &sb) < 0 || !S_ISREG(sb.st_mode)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("source for disk '%s' is not a regular "
"file; refusing to generate external "
"snapshot name"),
disk->name);
return -1;
}
cleanup: if (VIR_STRDUP(tmppath, origpath) < 0)
return ret; return -1;
/* drop suffix of the file name */
if ((tmp = strrchr(tmppath, '.')) && !strchr(tmp, '/'))
*tmp = '\0';
if (virAsprintf(&disk->src->path, "%s.%s", tmppath, def->name) < 0) {
VIR_FREE(tmppath);
return -1;
}
VIR_FREE(tmppath);
}
return 0;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册