storage: prepare for refactoring

virStorageFileGetMetadataFromFD is the only caller of
virStorageFileGetMetadataFromBuf; and it doesn't care about the
difference between a return of 0 (total success) or 1
(metadata was inconsistent, but pointer was populated as best
as possible); only about a return of -1 (could not read metadata
or out of memory).  Changing the return type, and normalizing
the variable names used, will make merging the functions easier
in the next commit.

* src/util/virstoragefile.c (virStorageFileGetMetadataFromBuf):
Change return value, and rename some variables.
(virStorageFileGetMetadataFromFD): Rename some variables.
This commit is contained in:
Eric Blake 2013-02-09 06:41:01 -07:00
parent 5e4946d4d9
commit b7df4f92d6

View File

@ -657,13 +657,15 @@ cleanup:
} }
static int static virStorageFileMetadataPtr
virStorageFileGetMetadataFromBuf(int format, virStorageFileGetMetadataFromBuf(int format,
const char *path, const char *path,
unsigned char *buf, unsigned char *buf,
size_t buflen, size_t len,
virStorageFileMetadata *meta) virStorageFileMetadata *meta)
{ {
virStorageFileMetadata *ret = NULL;
VIR_DEBUG("path=%s format=%d", path, format); VIR_DEBUG("path=%s format=%d", path, format);
/* XXX we should consider moving virStorageBackendUpdateVolInfo /* XXX we should consider moving virStorageBackendUpdateVolInfo
@ -671,14 +673,13 @@ virStorageFileGetMetadataFromBuf(int format,
*/ */
if (format <= VIR_STORAGE_FILE_NONE || if (format <= VIR_STORAGE_FILE_NONE ||
format >= VIR_STORAGE_FILE_LAST || format >= VIR_STORAGE_FILE_LAST ||
!fileTypeInfo[format].magic) { !fileTypeInfo[format].magic)
return 0; goto done;
}
/* Optionally extract capacity from file */ /* Optionally extract capacity from file */
if (fileTypeInfo[format].sizeOffset != -1) { if (fileTypeInfo[format].sizeOffset != -1) {
if ((fileTypeInfo[format].sizeOffset + 8) > buflen) if ((fileTypeInfo[format].sizeOffset + 8) > len)
return 1; goto done;
if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN) if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN)
meta->capacity = virReadBufInt64LE(buf + meta->capacity = virReadBufInt64LE(buf +
@ -689,7 +690,7 @@ virStorageFileGetMetadataFromBuf(int format,
/* Avoid unlikely, but theoretically possible overflow */ /* Avoid unlikely, but theoretically possible overflow */
if (meta->capacity > (ULLONG_MAX / if (meta->capacity > (ULLONG_MAX /
fileTypeInfo[format].sizeMultiplier)) fileTypeInfo[format].sizeMultiplier))
return 1; goto done;
meta->capacity *= fileTypeInfo[format].sizeMultiplier; meta->capacity *= fileTypeInfo[format].sizeMultiplier;
} }
@ -704,14 +705,14 @@ virStorageFileGetMetadataFromBuf(int format,
if (fileTypeInfo[format].getBackingStore != NULL) { if (fileTypeInfo[format].getBackingStore != NULL) {
char *backing; char *backing;
int backingFormat; int backingFormat;
int ret = fileTypeInfo[format].getBackingStore(&backing, int store = fileTypeInfo[format].getBackingStore(&backing,
&backingFormat, &backingFormat,
buf, buflen); buf, len);
if (ret == BACKING_STORE_INVALID) if (store == BACKING_STORE_INVALID)
return 1; goto done;
if (ret == BACKING_STORE_ERROR) if (store == BACKING_STORE_ERROR)
return -1; goto cleanup;
meta->backingStoreIsFile = false; meta->backingStoreIsFile = false;
if (backing != NULL) { if (backing != NULL) {
@ -719,7 +720,7 @@ virStorageFileGetMetadataFromBuf(int format,
if (meta->backingStore == NULL) { if (meta->backingStore == NULL) {
virReportOOMError(); virReportOOMError();
VIR_FREE(backing); VIR_FREE(backing);
return -1; goto cleanup;
} }
if (virBackingStoreIsFile(backing)) { if (virBackingStoreIsFile(backing)) {
meta->backingStoreIsFile = true; meta->backingStoreIsFile = true;
@ -744,7 +745,10 @@ virStorageFileGetMetadataFromBuf(int format,
} }
} }
return 0; done:
ret = meta;
cleanup:
return ret;
} }
@ -858,7 +862,7 @@ virStorageFileGetMetadataFromFD(const char *path,
int format) int format)
{ {
virStorageFileMetadata *meta = NULL; virStorageFileMetadata *meta = NULL;
unsigned char *head = NULL; unsigned char *buf = NULL;
ssize_t len = STORAGE_MAX_HEAD; ssize_t len = STORAGE_MAX_HEAD;
virStorageFileMetadata *ret = NULL; virStorageFileMetadata *ret = NULL;
struct stat sb; struct stat sb;
@ -884,18 +888,18 @@ virStorageFileGetMetadataFromFD(const char *path,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(head, len) < 0) { if (VIR_ALLOC_N(buf, len) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
if ((len = read(fd, head, len)) < 0) { if ((len = read(fd, buf, len)) < 0) {
virReportSystemError(errno, _("cannot read header '%s'"), path); virReportSystemError(errno, _("cannot read header '%s'"), path);
goto cleanup; goto cleanup;
} }
if (format == VIR_STORAGE_FILE_AUTO) if (format == VIR_STORAGE_FILE_AUTO)
format = virStorageFileProbeFormatFromBuf(path, head, len); format = virStorageFileProbeFormatFromBuf(path, buf, len);
if (format <= VIR_STORAGE_FILE_NONE || if (format <= VIR_STORAGE_FILE_NONE ||
format >= VIR_STORAGE_FILE_LAST) { format >= VIR_STORAGE_FILE_LAST) {
@ -904,14 +908,14 @@ virStorageFileGetMetadataFromFD(const char *path,
goto cleanup; goto cleanup;
} }
if (virStorageFileGetMetadataFromBuf(format, path, head, len, meta) < 0) if (!virStorageFileGetMetadataFromBuf(format, path, buf, len, meta))
goto cleanup; goto cleanup;
ret = meta; ret = meta;
meta = NULL; meta = NULL;
cleanup: cleanup:
virStorageFileFreeMetadata(meta); virStorageFileFreeMetadata(meta);
VIR_FREE(head); VIR_FREE(buf);
return ret; return ret;
} }