[08/30] storagefile: Push 'start' into qcow2GetBackingStoreFormat

Message ID 8f91d8e24e5076b4909516b7dc155e171a248f2e.1570482718.git.crobinso@redhat.com
State Accepted
Commit bd6b4646c70f74c8bfc9a63a151baca4d916bcc1
Headers show
Series
  • storagefile, security: qcow2 data_file support
Related show

Commit Message

Cole Robinson Oct. 7, 2019, 9:49 p.m.
This is a step towards making this qcow2GetBackingStoreFormat into
a generic qcow2 extensions parser

Signed-off-by: Cole Robinson <crobinso@redhat.com>

---
 src/util/virstoragefile.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Comments

Daniel Henrique Barboza Oct. 9, 2019, 9 p.m. | #1
On 10/7/19 6:49 PM, Cole Robinson wrote:
> This is a step towards making this qcow2GetBackingStoreFormat into

> a generic qcow2 extensions parser

>

> Signed-off-by: Cole Robinson <crobinso@redhat.com>

> ---


Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>


>   src/util/virstoragefile.c | 33 +++++++++++++++++----------------

>   1 file changed, 17 insertions(+), 16 deletions(-)

>

> diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c

> index a9a6c3e132..4a3c9df7a2 100644

> --- a/src/util/virstoragefile.c

> +++ b/src/util/virstoragefile.c

> @@ -430,10 +430,22 @@ static int

>   qcow2GetBackingStoreFormat(int *format,

>                              const char *buf,

>                              size_t buf_size,

> -                           size_t extension_start,

>                              size_t extension_end)

>   {

> -    size_t offset = extension_start;

> +    size_t offset;

> +    size_t extension_start;

> +    int version = virReadBufInt32BE(buf + QCOWX_HDR_VERSION);

> +

> +    if (version < 2) {

> +        /* QCow1 doesn't have the extensions capability

> +         * used to store backing format */

> +        return 0;

> +    }

> +

> +    if (version == 2)

> +        extension_start = QCOW2_HDR_TOTAL_SIZE;

> +    else

> +        extension_start = virReadBufInt32BE(buf + QCOW2v3_HDR_SIZE);

>   

>       /*

>        * The extensions take format of

> @@ -445,6 +457,7 @@ qcow2GetBackingStoreFormat(int *format,

>        * Unknown extensions can be ignored by skipping

>        * over "length" bytes in the data stream.

>        */

> +    offset = extension_start;

>       while (offset < (buf_size-8) &&

>              offset < (extension_end-8)) {

>           unsigned int magic = virReadBufInt32BE(buf + offset);

> @@ -487,8 +500,6 @@ qcowXGetBackingStore(char **res,

>   {

>       unsigned long long offset;

>       unsigned int size;

> -    unsigned long long start;

> -    int version;

>   

>       *res = NULL;

>       *format = VIR_STORAGE_FILE_AUTO;

> @@ -546,18 +557,8 @@ qcowXGetBackingStore(char **res,

>        * is stored at QCOW2v3_HDR_SIZE

>        */

>   

> -    version = virReadBufInt32BE(buf + QCOWX_HDR_VERSION);

> -    if (version >= 2) {

> -        /* QCow1 doesn't have the extensions capability

> -         * used to store backing format */

> -        if (version == 2)

> -            start = QCOW2_HDR_TOTAL_SIZE;

> -        else

> -            start = virReadBufInt32BE(buf + QCOW2v3_HDR_SIZE);

> -        if (qcow2GetBackingStoreFormat(format, buf, buf_size,

> -                                       start, offset) < 0)

> -            return BACKING_STORE_INVALID;

> -    }

> +    if (qcow2GetBackingStoreFormat(format, buf, buf_size, offset) < 0)

> +        return BACKING_STORE_INVALID;

>   

>       return BACKING_STORE_OK;

>   }


--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Patch

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index a9a6c3e132..4a3c9df7a2 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -430,10 +430,22 @@  static int
 qcow2GetBackingStoreFormat(int *format,
                            const char *buf,
                            size_t buf_size,
-                           size_t extension_start,
                            size_t extension_end)
 {
-    size_t offset = extension_start;
+    size_t offset;
+    size_t extension_start;
+    int version = virReadBufInt32BE(buf + QCOWX_HDR_VERSION);
+
+    if (version < 2) {
+        /* QCow1 doesn't have the extensions capability
+         * used to store backing format */
+        return 0;
+    }
+
+    if (version == 2)
+        extension_start = QCOW2_HDR_TOTAL_SIZE;
+    else
+        extension_start = virReadBufInt32BE(buf + QCOW2v3_HDR_SIZE);
 
     /*
      * The extensions take format of
@@ -445,6 +457,7 @@  qcow2GetBackingStoreFormat(int *format,
      * Unknown extensions can be ignored by skipping
      * over "length" bytes in the data stream.
      */
+    offset = extension_start;
     while (offset < (buf_size-8) &&
            offset < (extension_end-8)) {
         unsigned int magic = virReadBufInt32BE(buf + offset);
@@ -487,8 +500,6 @@  qcowXGetBackingStore(char **res,
 {
     unsigned long long offset;
     unsigned int size;
-    unsigned long long start;
-    int version;
 
     *res = NULL;
     *format = VIR_STORAGE_FILE_AUTO;
@@ -546,18 +557,8 @@  qcowXGetBackingStore(char **res,
      * is stored at QCOW2v3_HDR_SIZE
      */
 
-    version = virReadBufInt32BE(buf + QCOWX_HDR_VERSION);
-    if (version >= 2) {
-        /* QCow1 doesn't have the extensions capability
-         * used to store backing format */
-        if (version == 2)
-            start = QCOW2_HDR_TOTAL_SIZE;
-        else
-            start = virReadBufInt32BE(buf + QCOW2v3_HDR_SIZE);
-        if (qcow2GetBackingStoreFormat(format, buf, buf_size,
-                                       start, offset) < 0)
-            return BACKING_STORE_INVALID;
-    }
+    if (qcow2GetBackingStoreFormat(format, buf, buf_size, offset) < 0)
+        return BACKING_STORE_INVALID;
 
     return BACKING_STORE_OK;
 }