[Libguestfs] [PATCH common 2/4] options: Generate cryptsetup mapnames beginning with "crypt..." not "luks..."

Richard W.M. Jones rjones at redhat.com
Mon Mar 30 14:58:47 UTC 2020


Since we're no longer only inspecting LUKS devices, use a more generic
name.  This rewrite also makes the function clearer.
---
 options/decrypt.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/options/decrypt.c b/options/decrypt.c
index d868f70..58f8df9 100644
--- a/options/decrypt.c
+++ b/options/decrypt.c
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <libintl.h>
 #include <error.h>
+#include <errno.h>
 #include <assert.h>
 
 #include "c-ctype.h"
@@ -37,31 +38,27 @@
 #include "options.h"
 
 /**
- * Make a LUKS map name from the partition name,
- * eg. C<"/dev/vda2" =E<gt> "luksvda2">
+ * Make a cryptsetup map name from the partition name,
+ * eg. C<"/dev/vda2" =E<gt> "cryptvda2">
  */
-static void
-make_mapname (const char *device, char *mapname, size_t len)
+static char *
+make_mapname (const char *device)
 {
   size_t i = 0;
-
-  if (len < 5)
-    abort ();
-  strcpy (mapname, "luks");
-  mapname += 4;
-  len -= 4;
+  char *ret;
 
   if (STRPREFIX (device, "/dev/"))
-    i = 5;
+    device += 5;
 
-  for (; device[i] != '\0' && len >= 1; ++i) {
-    if (c_isalnum (device[i])) {
-      *mapname++ = device[i];
-      len--;
-    }
+  if (asprintf (&ret, "crypt%s", &device[i]) == -1)
+    error (EXIT_FAILURE, errno, "asprintf");
+
+  for (i = 5; i < strlen (ret); ++i) {
+    if (!c_isalnum (ret[i]))
+      memmove (&ret[i], &ret[i+1], strlen (&ret[i]));
   }
 
-  *mapname = '\0';
+  return ret;
 }
 
 /**
@@ -83,8 +80,7 @@ inspect_do_decrypt (guestfs_h *g, struct key_store *ks)
   for (i = 0; partitions[i] != NULL; ++i) {
     CLEANUP_FREE char *type = guestfs_vfs_type (g, partitions[i]);
     if (type && STREQ (type, "crypto_LUKS")) {
-      char mapname[32];
-      make_mapname (partitions[i], mapname, sizeof mapname);
+      CLEANUP_FREE char *mapname = make_mapname (partitions[i]);
 
 #ifdef GUESTFS_HAVE_LUKS_UUID
       CLEANUP_FREE char *uuid = guestfs_luks_uuid (g, partitions[i]);
-- 
2.25.0




More information about the Libguestfs mailing list