[PATCH] ctdb: Accept the key in hex format for the pstore command

Christof Schmitt cs at samba.org
Mon Jul 6 23:34:11 CEST 2015


On Fri, Jul 03, 2015 at 01:05:11PM +1000, Amitay Isaacs wrote:
>    Hi Christof,
> 
>    I would also add the hex key support for pfetch, pdelete and ptrans
>    commands, so the group of commands is consistent.
> 
>    Amitay.

Fair enough. What about the attached patches?

Christof
-------------- next part --------------
From f483a3966960ebcf703dbd02b80045843589c0b5 Mon Sep 17 00:00:00 2001
From: Christof Schmitt <cs at samba.org>
Date: Mon, 6 Jul 2015 13:07:33 -0700
Subject: [PATCH 1/2] ctdb: Create helper function for optional hex input

Signed-off-by: Christof Schmitt <cs at samba.org>
---
 ctdb/tools/ctdb.c |   58 +++++++++++++++++++++++++---------------------------
 1 files changed, 28 insertions(+), 30 deletions(-)

diff --git a/ctdb/tools/ctdb.c b/ctdb/tools/ctdb.c
index 9b7fb11..55deac2 100644
--- a/ctdb/tools/ctdb.c
+++ b/ctdb/tools/ctdb.c
@@ -180,12 +180,11 @@ static int h2i(char h)
 	return h - '0';
 }
 
-static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
+static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str, int len)
 {
-	int i, len;
+	int i;
 	TDB_DATA key = {NULL, 0};
 
-	len = strlen(str);
 	if (len & 0x01) {
 		DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
 		return key;
@@ -200,6 +199,20 @@ static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
 	return key;
 }
 
+static TDB_DATA strtodata(TALLOC_CTX *mem_ctx, const char *str, int len)
+{
+	TDB_DATA key;
+
+	if (!strncmp(str, "0x", 2)) {
+		key = hextodata(mem_ctx, str + 2, len - 2);
+	} else {
+		key.dptr  = talloc_memdup(mem_ctx, str, len);
+		key.dsize = len;
+	}
+
+	return key;
+}
+
 /* Parse a nodestring.  Parameter dd_ok controls what happens to nodes
  * that are disconnected or deleted.  If dd_ok is true those nodes are
  * included in the output list of nodes.  If dd_ok is false, those
@@ -4031,15 +4044,10 @@ static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv
 		return -1;
 	}
 
-	if (!strncmp(argv[1], "0x", 2)) {
-		key = hextodata(tmp_ctx, argv[1] + 2);
-		if (key.dsize == 0) {
-			printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-			return -1;
-		}
-	} else {
-		key.dptr  = discard_const(argv[1]);
-		key.dsize = strlen(argv[1]);
+	key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
+	if (key.dptr == NULL) {
+		printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
+		return -1;
 	}
 
 	data = tdb_fetch(tdb, key);
@@ -4098,26 +4106,16 @@ static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv
 		return -1;
 	}
 
-	if (!strncmp(argv[1], "0x", 2)) {
-		key = hextodata(tmp_ctx, argv[1] + 2);
-		if (key.dsize == 0) {
-			printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-			return -1;
-		}
-	} else {
-		key.dptr  = discard_const(argv[1]);
-		key.dsize = strlen(argv[1]);
+	key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
+	if (key.dptr == NULL) {
+		printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
+		return -1;
 	}
 
-	if (!strncmp(argv[2], "0x", 2)) {
-		value = hextodata(tmp_ctx, argv[2] + 2);
-		if (value.dsize == 0) {
-			printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
-			return -1;
-		}
-	} else {
-		value.dptr  = discard_const(argv[2]);
-		value.dsize = strlen(argv[2]);
+	value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
+	if (value.dptr == NULL) {
+		printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
+		return -1;
 	}
 
 	ZERO_STRUCT(header);
-- 
1.7.1


From 6b78e40308c70c28bc670bc567e1833b111b9ea4 Mon Sep 17 00:00:00 2001
From: Christof Schmitt <cs at samba.org>
Date: Mon, 6 Jul 2015 14:32:15 -0700
Subject: [PATCH 2/2] ctdb: Accept hex format for pstore, pdelete and ptrans commands

Signed-off-by: Christof Schmitt <cs at samba.org>
---
 ctdb/tools/ctdb.c |   19 +++++++++++++------
 1 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/ctdb/tools/ctdb.c b/ctdb/tools/ctdb.c
index 55deac2..628086f 100644
--- a/ctdb/tools/ctdb.c
+++ b/ctdb/tools/ctdb.c
@@ -4229,8 +4229,12 @@ static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv
 		return -1;
 	}
 
-	key.dptr  = discard_const(argv[1]);
-	key.dsize = strlen(argv[1]);
+	key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
+	if (key.dptr == NULL) {
+		printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
+		return -1;
+	}
+
 	ret = ctdb_transaction_store(h, key, data);
 	if (ret != 0) {
 		DEBUG(DEBUG_ERR,("Failed to store record\n"));
@@ -4295,8 +4299,12 @@ static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **arg
 		return -1;
 	}
 
-	key.dptr = discard_const(argv[1]);
-	key.dsize = strlen(argv[1]);
+	key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
+	if (key.dptr == NULL) {
+		printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
+		return -1;
+	}
+
 	ret = ctdb_transaction_store(h, key, tdb_null);
 	if (ret != 0) {
 		DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
@@ -4337,8 +4345,7 @@ static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
 		n = strcspn(t, "\"");
 		if (t[n] == '"') {
 			if (n > 0) {
-				data->dsize = n;
-				data->dptr = talloc_memdup(mem_ctx, t, n);
+				*data = strtodata(mem_ctx, t, n);
 				CTDB_NOMEM_ABORT(data->dptr);
 			}
 			ret = t + n + 1;
-- 
1.7.1



More information about the samba-technical mailing list