render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 811785bdc008e9d0df7e6f3e723c5f67b40cf6de Mon Sep 17 00:00:00 2001
Message-Id: <811785bdc008e9d0df7e6f3e723c5f67b40cf6de@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Fri, 28 Feb 2020 10:24:33 +0100
Subject: [PATCH] virStorageFileGetMetadataRecurse: Extract storage access
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Extract the code that directly deals with storage. This allows further
simplification and clarification of virStorageFileGetMetadataRecurse.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
(cherry picked from commit 01adad0932a583b1e2183dd4401bddd8607e77c3)

https://bugzilla.redhat.com/show_bug.cgi?id=1798148
Message-Id: <716c58c00cec06dd469b079df124bc896665f169.1582881363.git.pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/virstoragefile.c | 71 ++++++++++++++++++++++++++-------------
 1 file changed, 47 insertions(+), 24 deletions(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 46d55eda96..7295cebd08 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -4955,31 +4955,18 @@ virStorageFileReportBrokenChain(int errcode,
 }
 
 
-/* Recursive workhorse for virStorageFileGetMetadata.  */
 static int
-virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
-                                 virStorageSourcePtr parent,
-                                 uid_t uid, gid_t gid,
-                                 bool report_broken,
-                                 virHashTablePtr cycle,
-                                 unsigned int depth)
+virStorageFileGetMetadataRecurseReadHeader(virStorageSourcePtr src,
+                                           virStorageSourcePtr parent,
+                                           uid_t uid,
+                                           gid_t gid,
+                                           char **buf,
+                                           size_t *headerLen,
+                                           virHashTablePtr cycle)
 {
     int ret = -1;
     const char *uniqueName;
-    ssize_t headerLen;
-    int backingFormat;
-    int rv;
-    g_autofree char *buf = NULL;
-    g_autoptr(virStorageSource) backingStore = NULL;
-
-    VIR_DEBUG("path=%s format=%d uid=%u gid=%u",
-              NULLSTR(src->path), src->format,
-              (unsigned int)uid, (unsigned int)gid);
-
-    /* exit if we can't load information about the current image */
-    rv = virStorageFileSupportsBackingChainTraversal(src);
-    if (rv <= 0)
-        return rv;
+    ssize_t len;
 
     if (virStorageFileInitAs(src, uid, gid) < 0)
         return -1;
@@ -5002,10 +4989,47 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
     if (virHashAddEntry(cycle, uniqueName, NULL) < 0)
         goto cleanup;
 
-    if ((headerLen = virStorageFileRead(src, 0, VIR_STORAGE_MAX_HEADER,
-                                        &buf)) < 0)
+    if ((len = virStorageFileRead(src, 0, VIR_STORAGE_MAX_HEADER, buf)) < 0)
         goto cleanup;
 
+    *headerLen = len;
+    ret = 0;
+
+ cleanup:
+    virStorageFileDeinit(src);
+    return ret;
+}
+
+
+/* Recursive workhorse for virStorageFileGetMetadata.  */
+static int
+virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
+                                 virStorageSourcePtr parent,
+                                 uid_t uid, gid_t gid,
+                                 bool report_broken,
+                                 virHashTablePtr cycle,
+                                 unsigned int depth)
+{
+    int ret = -1;
+    size_t headerLen;
+    int backingFormat;
+    int rv;
+    g_autofree char *buf = NULL;
+    g_autoptr(virStorageSource) backingStore = NULL;
+
+    VIR_DEBUG("path=%s format=%d uid=%u gid=%u",
+              NULLSTR(src->path), src->format,
+              (unsigned int)uid, (unsigned int)gid);
+
+    /* exit if we can't load information about the current image */
+    rv = virStorageFileSupportsBackingChainTraversal(src);
+    if (rv <= 0)
+        return rv;
+
+    if (virStorageFileGetMetadataRecurseReadHeader(src, parent, uid, gid,
+                                                   &buf, &headerLen, cycle) < 0)
+        return -1;
+
     if (virStorageFileGetMetadataInternal(src, buf, headerLen,
                                           &backingFormat) < 0)
         goto cleanup;
@@ -5081,7 +5105,6 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
     ret = 0;
 
  cleanup:
-    virStorageFileDeinit(src);
     return ret;
 }
 
-- 
2.25.1