diff mbox

[v4,15/32] blockdev: Move vmdk probe to its own file

Message ID 1468523008-30013-16-git-send-email-clord@redhat.com
State New
Headers show

Commit Message

clord@redhat.com July 14, 2016, 7:03 p.m. UTC
Isolates vmdk probe as part of the modularization process.

Signed-off-by: Colin Lord <clord@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
---
 block/Makefile.objs   |  2 +-
 block/vmdk-probe.c    | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/vmdk.c          | 60 ++-------------------------------------------------
 block/vmdk.h          |  7 ++++++
 include/block/probe.h |  1 +
 5 files changed, 71 insertions(+), 59 deletions(-)
 create mode 100644 block/vmdk-probe.c
 create mode 100644 block/vmdk.h
diff mbox

Patch

diff --git a/block/Makefile.objs b/block/Makefile.objs
index 4363667..f86ab63 100644
--- a/block/Makefile.objs
+++ b/block/Makefile.objs
@@ -26,7 +26,7 @@  block-obj-y += write-threshold.o
 block-obj-y += crypto.o
 block-obj-y += bochs-probe.o cloop-probe.o crypto-probe.o dmg-probe.o
 block-obj-y += parallels-probe.o qcow-probe.o qcow2-probe.o qed-probe.o
-block-obj-y += raw-probe.o vdi-probe.o vhdx-probe.o
+block-obj-y += raw-probe.o vdi-probe.o vhdx-probe.o vmdk-probe.o
 
 common-obj-y += stream.o
 common-obj-y += backup.o
diff --git a/block/vmdk-probe.c b/block/vmdk-probe.c
new file mode 100644
index 0000000..77b06af
--- /dev/null
+++ b/block/vmdk-probe.c
@@ -0,0 +1,60 @@ 
+#include "qemu/osdep.h"
+#include "block/block_int.h"
+#include "block/probe.h"
+#include "vmdk.h"
+
+int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
+{
+    uint32_t magic;
+
+    if (buf_size < 4) {
+        return 0;
+    }
+    magic = be32_to_cpu(*(uint32_t *)buf);
+    if (magic == VMDK3_MAGIC ||
+        magic == VMDK4_MAGIC) {
+        return 100;
+    } else {
+        const char *p = (const char *)buf;
+        const char *end = p + buf_size;
+        while (p < end) {
+            if (*p == '#') {
+                /* skip comment line */
+                while (p < end && *p != '\n') {
+                    p++;
+                }
+                p++;
+                continue;
+            }
+            if (*p == ' ') {
+                while (p < end && *p == ' ') {
+                    p++;
+                }
+                /* skip '\r' if windows line endings used. */
+                if (p < end && *p == '\r') {
+                    p++;
+                }
+                /* only accept blank lines before 'version=' line */
+                if (p == end || *p != '\n') {
+                    return 0;
+                }
+                p++;
+                continue;
+            }
+            if (end - p >= strlen("version=X\n")) {
+                if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
+                    strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
+                    return 100;
+                }
+            }
+            if (end - p >= strlen("version=X\r\n")) {
+                if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
+                    strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
+                    return 100;
+                }
+            }
+            return 0;
+        }
+        return 0;
+    }
+}
diff --git a/block/vmdk.c b/block/vmdk.c
index d73f431..d2699b0 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -26,6 +26,8 @@ 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "block/block_int.h"
+#include "block/probe.h"
+#include "vmdk.h"
 #include "sysemu/block-backend.h"
 #include "qapi/qmp/qerror.h"
 #include "qemu/error-report.h"
@@ -35,8 +37,6 @@ 
 #include "qemu/cutils.h"
 #include <zlib.h>
 
-#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
-#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
 #define VMDK4_COMPRESSION_DEFLATE 1
 #define VMDK4_FLAG_NL_DETECT (1 << 0)
 #define VMDK4_FLAG_RGD (1 << 1)
@@ -151,62 +151,6 @@  enum {
     MARKER_FOOTER           = 3,
 };
 
-static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
-{
-    uint32_t magic;
-
-    if (buf_size < 4) {
-        return 0;
-    }
-    magic = be32_to_cpu(*(uint32_t *)buf);
-    if (magic == VMDK3_MAGIC ||
-        magic == VMDK4_MAGIC) {
-        return 100;
-    } else {
-        const char *p = (const char *)buf;
-        const char *end = p + buf_size;
-        while (p < end) {
-            if (*p == '#') {
-                /* skip comment line */
-                while (p < end && *p != '\n') {
-                    p++;
-                }
-                p++;
-                continue;
-            }
-            if (*p == ' ') {
-                while (p < end && *p == ' ') {
-                    p++;
-                }
-                /* skip '\r' if windows line endings used. */
-                if (p < end && *p == '\r') {
-                    p++;
-                }
-                /* only accept blank lines before 'version=' line */
-                if (p == end || *p != '\n') {
-                    return 0;
-                }
-                p++;
-                continue;
-            }
-            if (end - p >= strlen("version=X\n")) {
-                if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
-                    strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
-                    return 100;
-                }
-            }
-            if (end - p >= strlen("version=X\r\n")) {
-                if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
-                    strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
-                    return 100;
-                }
-            }
-            return 0;
-        }
-        return 0;
-    }
-}
-
 #define SECTOR_SIZE 512
 #define DESC_SIZE (20 * SECTOR_SIZE)    /* 20 sectors of 512 bytes each */
 #define BUF_SIZE 4096
diff --git a/block/vmdk.h b/block/vmdk.h
new file mode 100644
index 0000000..d631468
--- /dev/null
+++ b/block/vmdk.h
@@ -0,0 +1,7 @@ 
+#ifndef VMDK_H
+#define VMDK_H
+
+#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
+#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
+
+#endif
diff --git a/include/block/probe.h b/include/block/probe.h
index e901d8f..392515d 100644
--- a/include/block/probe.h
+++ b/include/block/probe.h
@@ -13,5 +13,6 @@  int bdrv_qed_probe(const uint8_t *buf, int buf_size, const char *filename);
 int raw_probe(const uint8_t *buf, int buf_size, const char *filename);
 int vdi_probe(const uint8_t *buf, int buf_size, const char *filename);
 int vhdx_probe(const uint8_t *buf, int buf_size, const char *filename);
+int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename);
 
 #endif