From 36cde667083d381eb247dcd5581e84b51d9e9fba Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 15 Aug 2019 14:49:49 +0200 Subject: [PATCH] util: storage: Modify return value of virStorageSourceNewFromBacking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Return the storage source definition via a pointer in the arguments and document the returned values. This will simplify the possibility to ignore certain backing store types which are not representable by libvirt. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/storage/storage_util.c | 2 +- src/util/virstoragefile.c | 61 ++++++++++++++++++++++++-------------- src/util/virstoragefile.h | 4 ++- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index 3dc5ad2969..6165dd43b2 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -3391,7 +3391,7 @@ storageBackendProbeTarget(virStorageSourcePtr target, return -1; if (meta->backingStoreRaw) { - if (!(target->backingStore = virStorageSourceNewFromBacking(meta))) + if (virStorageSourceNewFromBacking(meta, &target->backingStore) < 0) return -1; target->backingStore->format = backingStoreFormat; diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 86d75f4403..5e02ea7bf5 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -3681,42 +3681,57 @@ virStorageSourceNewFromBackingAbsolute(const char *path) } -virStorageSourcePtr -virStorageSourceNewFromBacking(virStorageSourcePtr parent) +/** + * virStorageSourceNewFromBacking: + * @parent: storage source parent + * @backing: returned backing store definition + * + * Creates a storage source which describes the backing image of @parent and + * fills it into @backing depending on the 'backingStoreRaw' property of @parent + * and other data. Note that for local storage this function accesses the file + * to update the actual type of the backing store. + * + * Returns 0 and fills @backing, or -1 on error (with appropriate error reported). + */ +int +virStorageSourceNewFromBacking(virStorageSourcePtr parent, + virStorageSourcePtr *backing) { struct stat st; - virStorageSourcePtr ret = NULL; VIR_AUTOUNREF(virStorageSourcePtr) def = NULL; + *backing = NULL; + if (virStorageIsRelative(parent->backingStoreRaw)) def = virStorageSourceNewFromBackingRelative(parent, parent->backingStoreRaw); else def = virStorageSourceNewFromBackingAbsolute(parent->backingStoreRaw); - if (def) { - /* possibly update local type */ - if (def->type == VIR_STORAGE_TYPE_FILE) { - if (stat(def->path, &st) == 0) { - if (S_ISDIR(st.st_mode)) { - def->type = VIR_STORAGE_TYPE_DIR; - def->format = VIR_STORAGE_FILE_DIR; - } else if (S_ISBLK(st.st_mode)) { - def->type = VIR_STORAGE_TYPE_BLOCK; - } + if (!def) + return -1; + + /* possibly update local type */ + if (def->type == VIR_STORAGE_TYPE_FILE) { + if (stat(def->path, &st) == 0) { + if (S_ISDIR(st.st_mode)) { + def->type = VIR_STORAGE_TYPE_DIR; + def->format = VIR_STORAGE_FILE_DIR; + } else if (S_ISBLK(st.st_mode)) { + def->type = VIR_STORAGE_TYPE_BLOCK; } } - - /* copy parent's labelling and other top level stuff */ - if (virStorageSourceInitChainElement(def, parent, true) < 0) - return NULL; - - def->readonly = true; - def->detected = true; } - VIR_STEAL_PTR(ret, def); - return ret; + /* copy parent's labelling and other top level stuff */ + if (virStorageSourceInitChainElement(def, parent, true) < 0) + return -1; + + def->readonly = true; + def->detected = true; + + VIR_STEAL_PTR(*backing, def); + return 0; } @@ -4893,7 +4908,7 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src, goto cleanup; if (src->backingStoreRaw) { - if (!(backingStore = virStorageSourceNewFromBacking(src))) + if (virStorageSourceNewFromBacking(src, &backingStore) < 0) goto cleanup; if (backingFormat == VIR_STORAGE_FILE_AUTO) diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index b65cd4c382..c43d87a160 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -444,7 +444,9 @@ int virStorageSourceUpdateCapacity(virStorageSourcePtr src, char *buf, ssize_t len, bool probe); -virStorageSourcePtr virStorageSourceNewFromBacking(virStorageSourcePtr parent); +int virStorageSourceNewFromBacking(virStorageSourcePtr parent, + virStorageSourcePtr *backing); + virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src, bool backingChain) ATTRIBUTE_NONNULL(1);