Use virStorageFileReadHeader() to read headers of storage files possibly
on remote storate to retrieve the image metadata.
The backend information is now parsed by
virStorageFileGetMetadataInternal which is now exported from the util
source and virStorageFileGetMetadataFromFDInternal now doesn't need to
be exported.
---
src/libvirt_private.syms | 2 +-
src/storage/storage_driver.c | 26 +++++++-------------------
src/util/virstoragefile.c | 5 ++---
src/util/virstoragefile.h | 9 ++++++---
4 files changed, 16 insertions(+), 26 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 78221aa..90bd48c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1849,7 +1849,7 @@ virStorageFileFormatTypeToString;
virStorageFileGetLVMKey;
virStorageFileGetMetadataFromBuf;
virStorageFileGetMetadataFromFD;
-virStorageFileGetMetadataFromFDInternal;
+virStorageFileGetMetadataInternal;
virStorageFileGetSCSIKey;
virStorageFileIsClusterFS;
virStorageFileParseChainIndex;
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index cdf01e7..f2c548d 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -3105,10 +3105,11 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
bool allow_probe,
virHashTablePtr cycle)
{
- int fd;
int ret = -1;
struct stat st;
const char *uniqueName;
+ char *buf = NULL;
+ ssize_t headerLen;
virStorageSourcePtr backingStore = NULL;
int backingFormat;
@@ -3136,26 +3137,13 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
if (virHashAddEntry(cycle, uniqueName, (void *)1) < 0)
goto cleanup;
- if (virStorageSourceGetActualType(src) != VIR_STORAGE_TYPE_NETWORK) {
- if ((fd = virFileOpenAs(src->path, O_RDONLY, 0, uid, gid, 0)) < 0) {
- virReportSystemError(-fd, _("Failed to open file '%s'"),
- src->path);
- goto cleanup;
- }
-
- if (virStorageFileGetMetadataFromFDInternal(src, fd,
- &backingFormat) < 0) {
- VIR_FORCE_CLOSE(fd);
- goto cleanup;
- }
+ if ((headerLen = virStorageFileReadHeader(src, VIR_STORAGE_MAX_HEADER,
+ &buf)) < 0)
+ goto cleanup;
- if (VIR_CLOSE(fd) < 0)
- VIR_WARN("could not close file %s", src->path);
- } else {
- /* TODO: currently we call this only for local storage */
- ret = 0;
+ if (virStorageFileGetMetadataInternal(src, buf, headerLen,
+ &backingFormat) < 0)
goto cleanup;
- }
/* check whether we need to go deeper */
if (!src->backingStoreRaw) {
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 347c2c3..365320b 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -738,8 +738,7 @@ qcow2GetFeatures(virBitmapPtr *features,
* with information about the file and its backing store. Return format
* of the backing store as BACKING_FORMAT. PATH and FORMAT have to be
* pre-populated in META */
-static int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
-ATTRIBUTE_NONNULL(4)
+int
virStorageFileGetMetadataInternal(virStorageSourcePtr meta,
char *buf,
size_t len,
@@ -960,7 +959,7 @@ virStorageFileGetMetadataFromBuf(const char *path,
/* Internal version that also supports a containing directory name. */
-int
+static int
virStorageFileGetMetadataFromFDInternal(virStorageSourcePtr meta,
int fd,
int *backingFormat)
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index ae27936..936b0ac 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -263,9 +263,12 @@ struct _virStorageSource {
int virStorageFileProbeFormat(const char *path, uid_t uid, gid_t gid);
-int virStorageFileGetMetadataFromFDInternal(virStorageSourcePtr meta,
- int fd,
- int *backingFormat);
+int virStorageFileGetMetadataInternal(virStorageSourcePtr meta,
+ char *buf,
+ size_t len,
+ int *backingFormat)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4);
+
virStorageSourcePtr virStorageFileGetMetadataFromFD(const char *path,
int fd,
int format);
--
1.9.2