render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 2551cd750a1c135dba6a00e55694b4995f62af65 Mon Sep 17 00:00:00 2001
Message-Id: <2551cd750a1c135dba6a00e55694b4995f62af65@dist-git>
From: "Daniel P. Berrange" <berrange@redhat.com>
Date: Thu, 28 Jul 2016 15:25:57 -0400
Subject: [PATCH] virstoragefile: refactor virStorageFileMatchesNNN methods

https://bugzilla.redhat.com/show_bug.cgi?id=1301021

Refactor the virStorageFileMatchesNNN methods so that
they don't take a struct FileFormatInfo parameter, but
instead get the actual raw dat items they needs. This
will facilitate reuse in other contexts.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
(cherry picked from commit 970f42ab42b6dee68917f3492f9378035c5505c0)
Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/util/virstoragefile.c | 61 +++++++++++++++++++++++++++--------------------
 1 file changed, 35 insertions(+), 26 deletions(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 16de603..7dace8a 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -598,13 +598,12 @@ qedGetBackingStore(char **res,
 
 
 static bool
-virStorageFileMatchesMagic(int format,
+virStorageFileMatchesMagic(int magicOffset,
+                           const char *magic,
                            char *buf,
                            size_t buflen)
 {
     int mlen;
-    int magicOffset = fileTypeInfo[format].magicOffset;
-    const char *magic = fileTypeInfo[format].magic;
 
     if (magic == NULL)
         return false;
@@ -622,13 +621,13 @@ virStorageFileMatchesMagic(int format,
 
 
 static bool
-virStorageFileMatchesExtension(int format,
+virStorageFileMatchesExtension(const char *extension,
                                const char *path)
 {
-    if (fileTypeInfo[format].extension == NULL)
+    if (extension == NULL)
         return false;
 
-    if (virFileHasSuffix(path, fileTypeInfo[format].extension))
+    if (virFileHasSuffix(path, extension))
         return true;
 
     return false;
@@ -636,7 +635,10 @@ virStorageFileMatchesExtension(int format,
 
 
 static bool
-virStorageFileMatchesVersion(int format,
+virStorageFileMatchesVersion(int versionOffset,
+                             int versionSize,
+                             const int *versionNumbers,
+                             int endian,
                              char *buf,
                              size_t buflen)
 {
@@ -644,44 +646,42 @@ virStorageFileMatchesVersion(int format,
     size_t i;
 
     /* Validate version number info */
-    if (fileTypeInfo[format].versionOffset == -1)
+    if (versionOffset == -1)
         return false;
 
     /* -2 == non-versioned file format, so trivially match */
-    if (fileTypeInfo[format].versionOffset == -2)
+    if (versionOffset == -2)
         return true;
 
     /* A positive versionOffset, requires using a valid versionSize */
-    if (fileTypeInfo[format].versionSize != 2 &&
-        fileTypeInfo[format].versionSize != 4)
+    if (versionSize != 2 && versionSize != 4)
         return false;
 
-    if ((fileTypeInfo[format].versionOffset +
-         fileTypeInfo[format].versionSize) > buflen)
+    if ((versionOffset + versionSize) > buflen)
         return false;
 
-    if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN) {
-        if (fileTypeInfo[format].versionSize == 4)
+    if (endian == LV_LITTLE_ENDIAN) {
+        if (versionSize == 4)
             version = virReadBufInt32LE(buf +
-                                        fileTypeInfo[format].versionOffset);
+                                        versionOffset);
         else
             version = virReadBufInt16LE(buf +
-                                        fileTypeInfo[format].versionOffset);
+                                        versionOffset);
     } else {
-        if (fileTypeInfo[format].versionSize == 4)
+        if (versionSize == 4)
             version = virReadBufInt32BE(buf +
-                                        fileTypeInfo[format].versionOffset);
+                                        versionOffset);
         else
             version = virReadBufInt16BE(buf +
-                                        fileTypeInfo[format].versionOffset);
+                                        versionOffset);
     }
 
     for (i = 0;
-         i < FILE_TYPE_VERSIONS_LAST && fileTypeInfo[format].versionNumbers[i];
+         i < FILE_TYPE_VERSIONS_LAST && versionNumbers[i];
          i++) {
         VIR_DEBUG("Compare detected version %d vs one of the expected versions %d",
-                  version, fileTypeInfo[format].versionNumbers[i]);
-        if (version == fileTypeInfo[format].versionNumbers[i])
+                  version, versionNumbers[i]);
+        if (version == versionNumbers[i])
             return true;
     }
 
@@ -734,8 +734,16 @@ virStorageFileProbeFormatFromBuf(const char *path,
 
     /* First check file magic */
     for (i = 0; i < VIR_STORAGE_FILE_LAST; i++) {
-        if (virStorageFileMatchesMagic(i, buf, buflen)) {
-            if (!virStorageFileMatchesVersion(i, buf, buflen)) {
+        if (virStorageFileMatchesMagic(
+                fileTypeInfo[i].magicOffset,
+                fileTypeInfo[i].magic,
+                buf, buflen)) {
+            if (!virStorageFileMatchesVersion(
+                    fileTypeInfo[i].versionOffset,
+                    fileTypeInfo[i].versionSize,
+                    fileTypeInfo[i].versionNumbers,
+                    fileTypeInfo[i].endian,
+                    buf, buflen)) {
                 possibleFormat = i;
                 continue;
             }
@@ -751,7 +759,8 @@ virStorageFileProbeFormatFromBuf(const char *path,
 
     /* No magic, so check file extension */
     for (i = 0; i < VIR_STORAGE_FILE_LAST; i++) {
-        if (virStorageFileMatchesExtension(i, path)) {
+        if (virStorageFileMatchesExtension(
+                fileTypeInfo[i].extension, path)) {
             format = i;
             goto cleanup;
         }
-- 
2.9.2