]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virIndexToDiskName and fix mapping gap
authorMatthias Bolte <matthias.bolte@googlemail.com>
Thu, 3 Dec 2009 16:17:40 +0000 (17:17 +0100)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Thu, 3 Dec 2009 17:07:49 +0000 (18:07 +0100)
esxVMX_IndexToDiskName handles indices up to 701. This limit comes
from a mapping gap in virDiskNameToIndex:

  sdzy -> 700
  sdzz -> 701
  sdaaa -> 728
  sdaab -> 729

This line in virDiskNameToIndex causes this gap:

  idx = (idx + i) * 26;

Fixing it by altering this line to:

  idx = (idx + (i < 1 ? 0 : 1)) * 26;

Also add a new version of virIndexToDiskName that handles the inverse
mapping for arbitrary indices.

* src/esx/esx_vmx.[ch]: remove esxVMX_IndexToDiskName
* src/util/util.[ch]: add virIndexToDiskName and fix mapping gap
* tests/esxutilstest.c: update test to verify that the gap is fixed

src/esx/esx_vmx.c
src/esx/esx_vmx.h
src/util/util.c
src/util/util.h
tests/esxutilstest.c

index 9a9fe0a270a0188a0e43bcdf37f4bb5d0554cb3c..a16751393b7ab96da70c999e60c10ab7ab6c7b1e 100644 (file)
@@ -1211,31 +1211,6 @@ esxVMX_ParseSCSIController(virConnectPtr conn, virConfPtr conf, int controller,
 
 
 
-char *
-esxVMX_IndexToDiskName(virConnectPtr conn, int idx, const char *prefix)
-{
-    char *name = NULL;
-
-    if (idx < 0) {
-        ESX_ERROR(conn, VIR_ERR_INTERNAL_ERROR,
-                  "Disk index %d is negative", idx);
-    } else if (idx < 26) {
-        if (virAsprintf(&name, "%s%c", prefix, 'a' + idx) < 0)
-            virReportOOMError(conn);
-    } else if (idx < 702) {
-        if (virAsprintf(&name, "%s%c%c", prefix, 'a' + idx / 26 - 1,
-                        'a' + (idx % 26)) < 0)
-            virReportOOMError(conn);
-    } else {
-        ESX_ERROR(conn, VIR_ERR_INTERNAL_ERROR,
-                  "Disk index %d is too large", idx);
-    }
-
-    return name;
-}
-
-
-
 /*
 struct _virDomainDiskDef {
     int type;               // partly done
@@ -1337,8 +1312,8 @@ esxVMX_ParseDisk(virConnectPtr conn, esxVI_Context *ctx, virConfPtr conf,
             }
 
             (*def)->dst =
-               esxVMX_IndexToDiskName
-                 (conn, controller * 15 + (id < 7 ? id : id - 1), "sd");
+               virIndexToDiskName
+                 (controller * 15 + (id < 7 ? id : id - 1), "sd");
 
             if ((*def)->dst == NULL) {
                 goto failure;
@@ -1371,8 +1346,7 @@ esxVMX_ParseDisk(virConnectPtr conn, esxVI_Context *ctx, virConfPtr conf,
                 goto failure;
             }
 
-            (*def)->dst = esxVMX_IndexToDiskName(conn, controller * 2 + id,
-                                                 "hd");
+            (*def)->dst = virIndexToDiskName(controller * 2 + id, "hd");
 
             if ((*def)->dst == NULL) {
                 goto failure;
@@ -1398,7 +1372,7 @@ esxVMX_ParseDisk(virConnectPtr conn, esxVI_Context *ctx, virConfPtr conf,
                 goto failure;
             }
 
-            (*def)->dst = esxVMX_IndexToDiskName(conn, controller, "fd");
+            (*def)->dst = virIndexToDiskName(controller, "fd");
 
             if ((*def)->dst == NULL) {
                 goto failure;
index 4e11ae6444c49acc2a07c7ac26cce5b19866e23c..9a66e49f8bb56c2ab78ef3d76fe3ef5d10342f2e 100644 (file)
@@ -28,9 +28,6 @@
 #include "domain_conf.h"
 #include "esx_vi.h"
 
-char *
-esxVMX_IndexToDiskName(virConnectPtr conn, int idx, const char *prefix);
-
 int
 esxVMX_SCSIDiskNameToControllerAndID(virConnectPtr conn, const char *name,
                                      int *controller, int *id);
@@ -53,6 +50,7 @@ esxVMX_AbsolutePathToDatastoreRelatedPath(virConnectPtr conn,
                                           const char *absolutePath);
 
 
+
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * VMX -> Domain XML
  */
index e472e0c2cfc0b3ed16a371c1546d6008e2d00415..3a85c5284a2ed7cadba429e95946f4e90f25a43c 100644 (file)
@@ -1789,7 +1789,7 @@ int virDiskNameToIndex(const char *name) {
         return -1;
 
     for (i = 0; *ptr; i++) {
-        idx = (idx + i) * 26;
+        idx = (idx + (i < 1 ? 0 : 1)) * 26;
 
         if (!c_islower(*ptr))
             return -1;
@@ -1801,6 +1801,36 @@ int virDiskNameToIndex(const char *name) {
     return idx;
 }
 
+char *virIndexToDiskName(int idx, const char *prefix)
+{
+    char *name = NULL;
+    int i, k, offset;
+
+    if (idx < 0) {
+        ReportError(NULL, VIR_ERR_INTERNAL_ERROR,
+                    _("Disk index %d is negative"), idx);
+        return NULL;
+    }
+
+    for (i = 0, k = idx; k >= 0; ++i, k = k / 26 - 1) { }
+
+    offset = strlen(prefix);
+
+    if (VIR_ALLOC_N(name, offset + i + 1)) {
+        virReportOOMError(NULL);
+        return NULL;
+    }
+
+    strcpy(name, prefix);
+    name[offset + i] = '\0';
+
+    for (i = i - 1, k = idx; k >= 0; --i, k = k / 26 - 1) {
+        name[offset + i] = 'a' + (k % 26);
+    }
+
+    return name;
+}
+
 #ifndef AI_CANONIDN
 #define AI_CANONIDN 0
 #endif
index 8c9d401187079693963bc9c27ab8f3462acd49ad..49b27f2cddb22cf2e4841a5b28f1d4faf7dd3e4f 100644 (file)
@@ -186,7 +186,7 @@ void virGenerateMacAddr(const unsigned char *prefix,
                         unsigned char *addr);
 
 int virDiskNameToIndex(const char* str);
-
+char *virIndexToDiskName(int idx, const char *prefix);
 
 int virEnumFromString(const char *const*types,
                       unsigned int ntypes,
index 5633ee24e2014d55e07f491e0aeb34c0fe3d3e5b..058280dd7fd67246c600e4bb51298d921aed4b11 100644 (file)
@@ -9,8 +9,8 @@
 #include "internal.h"
 #include "memory.h"
 #include "testutils.h"
+#include "util.h"
 #include "esx/esx_util.h"
-#include "esx/esx_vmx.h"
 
 static char *progname;
 
@@ -47,7 +47,7 @@ testIndexToDiskName(const void *data ATTRIBUTE_UNUSED)
     for (i = 0; i < ARRAY_CARDINALITY(names); ++i) {
         VIR_FREE(name);
 
-        name = esxVMX_IndexToDiskName(NULL, i, "sd");
+        name = virIndexToDiskName(i, "sd");
 
         if (STRNEQ(names[i], name)) {
             virtTestDifference(stderr, names[i], name);
@@ -55,8 +55,31 @@ testIndexToDiskName(const void *data ATTRIBUTE_UNUSED)
 
             return -1;
         }
+    }
+
+    VIR_FREE(name);
+
+    return 0;
+}
+
+static int
+testDiskNameToIndex(const void *data ATTRIBUTE_UNUSED)
+{
+    int i, k;
+    char *name = NULL;
+
+    for (i = 0; i < 100000; ++i) {
+        VIR_FREE(name);
+
+        name = virIndexToDiskName(i, "sd");
+        k = virDiskNameToIndex(name);
+
+        if (k != i) {
+            if (virtTestGetDebug() > 0) {
+                fprintf(stderr, "\nExpect [%d]\n", i);
+                fprintf(stderr, "Actual [%d]\n", k);
+            }
 
-        if (virDiskNameToIndex(name) != i) {
             VIR_FREE(name);
 
             return -1;
@@ -170,6 +193,7 @@ mymain(int argc, char **argv)
         } while (0)
 
     DO_TEST(IndexToDiskName);
+    DO_TEST(DiskNameToIndex);
     DO_TEST(ParseDatastoreRelatedPath);
 
     return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;