util: storage: Allow specifying format for virStorageFileGetMetadataFromBuf

To allow reusing this function in the qemu driver we need to allow
specifying the storage format. Also separate return of the backing store
path now isn't necessary.
This commit is contained in:
Peter Krempa 2014-07-07 11:38:28 +02:00
parent d3047061d0
commit 25924dec0f
3 changed files with 23 additions and 24 deletions

View File

@ -294,10 +294,13 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlusterStatePtr state,
goto cleanup; goto cleanup;
if (!(meta = virStorageFileGetMetadataFromBuf(name, header, len, if (!(meta = virStorageFileGetMetadataFromBuf(name, header, len,
&vol->backingStore.path, VIR_STORAGE_FILE_AUTO,
&vol->backingStore.format))) &vol->backingStore.format)))
goto cleanup; goto cleanup;
vol->backingStore.path = meta->backingStoreRaw;
meta->backingStoreRaw = NULL;
vol->target.format = meta->format; vol->target.format = meta->format;
if (vol->backingStore.path && if (vol->backingStore.path &&
vol->backingStore.format < 0) vol->backingStore.format < 0)

View File

@ -930,19 +930,20 @@ virStorageFileMetadataNew(const char *path,
* @path: name of file, for error messages * @path: name of file, for error messages
* @buf: header bytes from @path * @buf: header bytes from @path
* @len: length of @buf * @len: length of @buf
* @backing: output malloc'd name of backing image, if any * @format: format of the storage file
* @backingFormat: format of @backing * @backingFormat: format of @backing
* *
* Extract metadata about the storage volume, including probing its * Extract metadata about the storage volume with the specified image format.
* format. Does not recurse. Callers are advised not to trust the * If image format is VIR_STORAGE_FILE_AUTO, it will probe to automatically
* learned format if a guest has ever used the volume when it was * identify the format. Does not recurse.
* raw, since a malicious guest can turn a raw file into any
* other non-raw format at will.
* *
* If the returned @backingFormat is VIR_STORAGE_FILE_AUTO * Callers are advised never to use VIR_STORAGE_FILE_AUTO as a format on a file
* it indicates the image didn't specify an explicit format for its * that might be raw if that file will then be passed to a guest, since a
* backing store. Callers are advised against probing for the * malicious guest can turn a raw file into any other non-raw format at will.
* backing store format in this case. *
* If the returned @backingFormat is VIR_STORAGE_FILE_AUTO it indicates the
* image didn't specify an explicit format for its backing store. Callers are
* advised against probing for the backing store format in this case.
* *
* Caller MUST free the result after use via virStorageSourceFree. * Caller MUST free the result after use via virStorageSourceFree.
*/ */
@ -950,25 +951,20 @@ virStorageSourcePtr
virStorageFileGetMetadataFromBuf(const char *path, virStorageFileGetMetadataFromBuf(const char *path,
char *buf, char *buf,
size_t len, size_t len,
char **backing, int format,
int *backingFormat) int *backingFormat)
{ {
virStorageSourcePtr ret = NULL; virStorageSourcePtr ret = NULL;
virStorageSourcePtr meta = NULL;
if (!(meta = virStorageFileMetadataNew(path, VIR_STORAGE_FILE_AUTO))) if (!(ret = virStorageFileMetadataNew(path, format)))
return NULL; return NULL;
if (virStorageFileGetMetadataInternal(meta, buf, len, if (virStorageFileGetMetadataInternal(ret, buf, len,
backingFormat) < 0) backingFormat) < 0) {
goto cleanup; virStorageSourceFree(ret);
if (VIR_STRDUP(*backing, meta->backingStoreRaw) < 0) return NULL;
goto cleanup; }
ret = meta;
meta = NULL;
cleanup:
virStorageSourceFree(meta);
return ret; return ret;
} }

View File

@ -290,7 +290,7 @@ virStorageSourcePtr virStorageFileGetMetadataFromFD(const char *path,
virStorageSourcePtr virStorageFileGetMetadataFromBuf(const char *path, virStorageSourcePtr virStorageFileGetMetadataFromBuf(const char *path,
char *buf, char *buf,
size_t len, size_t len,
char **backing, int format,
int *backingFormat) int *backingFormat)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4)
ATTRIBUTE_NONNULL(5); ATTRIBUTE_NONNULL(5);