|
|
6ae9ed |
From 2551cd750a1c135dba6a00e55694b4995f62af65 Mon Sep 17 00:00:00 2001
|
|
|
6ae9ed |
Message-Id: <2551cd750a1c135dba6a00e55694b4995f62af65@dist-git>
|
|
|
6ae9ed |
From: "Daniel P. Berrange" <berrange@redhat.com>
|
|
|
6ae9ed |
Date: Thu, 28 Jul 2016 15:25:57 -0400
|
|
|
6ae9ed |
Subject: [PATCH] virstoragefile: refactor virStorageFileMatchesNNN methods
|
|
|
6ae9ed |
|
|
|
6ae9ed |
https://bugzilla.redhat.com/show_bug.cgi?id=1301021
|
|
|
6ae9ed |
|
|
|
6ae9ed |
Refactor the virStorageFileMatchesNNN methods so that
|
|
|
6ae9ed |
they don't take a struct FileFormatInfo parameter, but
|
|
|
6ae9ed |
instead get the actual raw dat items they needs. This
|
|
|
6ae9ed |
will facilitate reuse in other contexts.
|
|
|
6ae9ed |
|
|
|
6ae9ed |
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
|
|
|
6ae9ed |
(cherry picked from commit 970f42ab42b6dee68917f3492f9378035c5505c0)
|
|
|
6ae9ed |
Signed-off-by: John Ferlan <jferlan@redhat.com>
|
|
|
6ae9ed |
---
|
|
|
6ae9ed |
src/util/virstoragefile.c | 61 +++++++++++++++++++++++++++--------------------
|
|
|
6ae9ed |
1 file changed, 35 insertions(+), 26 deletions(-)
|
|
|
6ae9ed |
|
|
|
6ae9ed |
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
|
|
|
6ae9ed |
index 16de603..7dace8a 100644
|
|
|
6ae9ed |
--- a/src/util/virstoragefile.c
|
|
|
6ae9ed |
+++ b/src/util/virstoragefile.c
|
|
|
6ae9ed |
@@ -598,13 +598,12 @@ qedGetBackingStore(char **res,
|
|
|
6ae9ed |
|
|
|
6ae9ed |
|
|
|
6ae9ed |
static bool
|
|
|
6ae9ed |
-virStorageFileMatchesMagic(int format,
|
|
|
6ae9ed |
+virStorageFileMatchesMagic(int magicOffset,
|
|
|
6ae9ed |
+ const char *magic,
|
|
|
6ae9ed |
char *buf,
|
|
|
6ae9ed |
size_t buflen)
|
|
|
6ae9ed |
{
|
|
|
6ae9ed |
int mlen;
|
|
|
6ae9ed |
- int magicOffset = fileTypeInfo[format].magicOffset;
|
|
|
6ae9ed |
- const char *magic = fileTypeInfo[format].magic;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
if (magic == NULL)
|
|
|
6ae9ed |
return false;
|
|
|
6ae9ed |
@@ -622,13 +621,13 @@ virStorageFileMatchesMagic(int format,
|
|
|
6ae9ed |
|
|
|
6ae9ed |
|
|
|
6ae9ed |
static bool
|
|
|
6ae9ed |
-virStorageFileMatchesExtension(int format,
|
|
|
6ae9ed |
+virStorageFileMatchesExtension(const char *extension,
|
|
|
6ae9ed |
const char *path)
|
|
|
6ae9ed |
{
|
|
|
6ae9ed |
- if (fileTypeInfo[format].extension == NULL)
|
|
|
6ae9ed |
+ if (extension == NULL)
|
|
|
6ae9ed |
return false;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
- if (virFileHasSuffix(path, fileTypeInfo[format].extension))
|
|
|
6ae9ed |
+ if (virFileHasSuffix(path, extension))
|
|
|
6ae9ed |
return true;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
return false;
|
|
|
6ae9ed |
@@ -636,7 +635,10 @@ virStorageFileMatchesExtension(int format,
|
|
|
6ae9ed |
|
|
|
6ae9ed |
|
|
|
6ae9ed |
static bool
|
|
|
6ae9ed |
-virStorageFileMatchesVersion(int format,
|
|
|
6ae9ed |
+virStorageFileMatchesVersion(int versionOffset,
|
|
|
6ae9ed |
+ int versionSize,
|
|
|
6ae9ed |
+ const int *versionNumbers,
|
|
|
6ae9ed |
+ int endian,
|
|
|
6ae9ed |
char *buf,
|
|
|
6ae9ed |
size_t buflen)
|
|
|
6ae9ed |
{
|
|
|
6ae9ed |
@@ -644,44 +646,42 @@ virStorageFileMatchesVersion(int format,
|
|
|
6ae9ed |
size_t i;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
/* Validate version number info */
|
|
|
6ae9ed |
- if (fileTypeInfo[format].versionOffset == -1)
|
|
|
6ae9ed |
+ if (versionOffset == -1)
|
|
|
6ae9ed |
return false;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
/* -2 == non-versioned file format, so trivially match */
|
|
|
6ae9ed |
- if (fileTypeInfo[format].versionOffset == -2)
|
|
|
6ae9ed |
+ if (versionOffset == -2)
|
|
|
6ae9ed |
return true;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
/* A positive versionOffset, requires using a valid versionSize */
|
|
|
6ae9ed |
- if (fileTypeInfo[format].versionSize != 2 &&
|
|
|
6ae9ed |
- fileTypeInfo[format].versionSize != 4)
|
|
|
6ae9ed |
+ if (versionSize != 2 && versionSize != 4)
|
|
|
6ae9ed |
return false;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
- if ((fileTypeInfo[format].versionOffset +
|
|
|
6ae9ed |
- fileTypeInfo[format].versionSize) > buflen)
|
|
|
6ae9ed |
+ if ((versionOffset + versionSize) > buflen)
|
|
|
6ae9ed |
return false;
|
|
|
6ae9ed |
|
|
|
6ae9ed |
- if (fileTypeInfo[format].endian == LV_LITTLE_ENDIAN) {
|
|
|
6ae9ed |
- if (fileTypeInfo[format].versionSize == 4)
|
|
|
6ae9ed |
+ if (endian == LV_LITTLE_ENDIAN) {
|
|
|
6ae9ed |
+ if (versionSize == 4)
|
|
|
6ae9ed |
version = virReadBufInt32LE(buf +
|
|
|
6ae9ed |
- fileTypeInfo[format].versionOffset);
|
|
|
6ae9ed |
+ versionOffset);
|
|
|
6ae9ed |
else
|
|
|
6ae9ed |
version = virReadBufInt16LE(buf +
|
|
|
6ae9ed |
- fileTypeInfo[format].versionOffset);
|
|
|
6ae9ed |
+ versionOffset);
|
|
|
6ae9ed |
} else {
|
|
|
6ae9ed |
- if (fileTypeInfo[format].versionSize == 4)
|
|
|
6ae9ed |
+ if (versionSize == 4)
|
|
|
6ae9ed |
version = virReadBufInt32BE(buf +
|
|
|
6ae9ed |
- fileTypeInfo[format].versionOffset);
|
|
|
6ae9ed |
+ versionOffset);
|
|
|
6ae9ed |
else
|
|
|
6ae9ed |
version = virReadBufInt16BE(buf +
|
|
|
6ae9ed |
- fileTypeInfo[format].versionOffset);
|
|
|
6ae9ed |
+ versionOffset);
|
|
|
6ae9ed |
}
|
|
|
6ae9ed |
|
|
|
6ae9ed |
for (i = 0;
|
|
|
6ae9ed |
- i < FILE_TYPE_VERSIONS_LAST && fileTypeInfo[format].versionNumbers[i];
|
|
|
6ae9ed |
+ i < FILE_TYPE_VERSIONS_LAST && versionNumbers[i];
|
|
|
6ae9ed |
i++) {
|
|
|
6ae9ed |
VIR_DEBUG("Compare detected version %d vs one of the expected versions %d",
|
|
|
6ae9ed |
- version, fileTypeInfo[format].versionNumbers[i]);
|
|
|
6ae9ed |
- if (version == fileTypeInfo[format].versionNumbers[i])
|
|
|
6ae9ed |
+ version, versionNumbers[i]);
|
|
|
6ae9ed |
+ if (version == versionNumbers[i])
|
|
|
6ae9ed |
return true;
|
|
|
6ae9ed |
}
|
|
|
6ae9ed |
|
|
|
6ae9ed |
@@ -734,8 +734,16 @@ virStorageFileProbeFormatFromBuf(const char *path,
|
|
|
6ae9ed |
|
|
|
6ae9ed |
/* First check file magic */
|
|
|
6ae9ed |
for (i = 0; i < VIR_STORAGE_FILE_LAST; i++) {
|
|
|
6ae9ed |
- if (virStorageFileMatchesMagic(i, buf, buflen)) {
|
|
|
6ae9ed |
- if (!virStorageFileMatchesVersion(i, buf, buflen)) {
|
|
|
6ae9ed |
+ if (virStorageFileMatchesMagic(
|
|
|
6ae9ed |
+ fileTypeInfo[i].magicOffset,
|
|
|
6ae9ed |
+ fileTypeInfo[i].magic,
|
|
|
6ae9ed |
+ buf, buflen)) {
|
|
|
6ae9ed |
+ if (!virStorageFileMatchesVersion(
|
|
|
6ae9ed |
+ fileTypeInfo[i].versionOffset,
|
|
|
6ae9ed |
+ fileTypeInfo[i].versionSize,
|
|
|
6ae9ed |
+ fileTypeInfo[i].versionNumbers,
|
|
|
6ae9ed |
+ fileTypeInfo[i].endian,
|
|
|
6ae9ed |
+ buf, buflen)) {
|
|
|
6ae9ed |
possibleFormat = i;
|
|
|
6ae9ed |
continue;
|
|
|
6ae9ed |
}
|
|
|
6ae9ed |
@@ -751,7 +759,8 @@ virStorageFileProbeFormatFromBuf(const char *path,
|
|
|
6ae9ed |
|
|
|
6ae9ed |
/* No magic, so check file extension */
|
|
|
6ae9ed |
for (i = 0; i < VIR_STORAGE_FILE_LAST; i++) {
|
|
|
6ae9ed |
- if (virStorageFileMatchesExtension(i, path)) {
|
|
|
6ae9ed |
+ if (virStorageFileMatchesExtension(
|
|
|
6ae9ed |
+ fileTypeInfo[i].extension, path)) {
|
|
|
6ae9ed |
format = i;
|
|
|
6ae9ed |
goto cleanup;
|
|
|
6ae9ed |
}
|
|
|
6ae9ed |
--
|
|
|
6ae9ed |
2.9.2
|
|
|
6ae9ed |
|