Discussion:
[PATCH] ctdb: Accept the key in hex format for the pstore command
(too old to reply)
Christof Schmitt
2015-07-02 20:16:35 UTC
Permalink
From 9864603a6529de70e08ef03928a2895e343f7c03 Mon Sep 17 00:00:00 2001
From: Christof Schmitt <***@samba.org>
Date: Thu, 2 Jul 2015 13:06:32 -0700
Subject: [PATCH] ctdb: Accept the key in hex format for the pstore command

This follows the same pattern as the tstore command, and it allows
specifying key strings with a trailing \0 character.

Signed-off-by: Christof Schmitt <***@samba.org>
---
ctdb/tools/ctdb.c | 13 +++++++++++--
1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/ctdb/tools/ctdb.c b/ctdb/tools/ctdb.c
index 9b7fb11..91ada44 100644
--- a/ctdb/tools/ctdb.c
+++ b/ctdb/tools/ctdb.c
@@ -4231,8 +4231,17 @@ 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]);
+ 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]);
+ }
+
ret = ctdb_transaction_store(h, key, data);
if (ret != 0) {
DEBUG(DEBUG_ERR,("Failed to store record\n"));
--
1.7.1
Amitay Isaacs
2015-07-03 03:05:11 UTC
Permalink
Hi Christof,

I would also add the hex key support for pfetch, pdelete and ptrans
commands, so the group of commands is consistent.

Amitay.
Christof Schmitt
2015-07-06 21:34:11 UTC
Permalink
Post by Amitay Isaacs
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
Jeremy Allison
2015-07-06 21:49:59 UTC
Permalink
Post by Christof Schmitt
Post by Amitay Isaacs
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?
Oops. Sorry, you might have to rebase now I pushed
your earlier patch.
Christof Schmitt
2015-07-06 22:21:39 UTC
Permalink
Post by Jeremy Allison
Post by Christof Schmitt
Post by Amitay Isaacs
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?
Oops. Sorry, you might have to rebase now I pushed
your earlier patch.
Yes, the first patch is in master. I rebased the missing parts on top,
see attached patches.

Christof
Jeremy Allison
2015-07-06 23:21:10 UTC
Permalink
Post by Christof Schmitt
Post by Jeremy Allison
Post by Christof Schmitt
Post by Amitay Isaacs
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?
Oops. Sorry, you might have to rebase now I pushed
your earlier patch.
Yes, the first patch is in master. I rebased the missing parts on top,
see attached patches.
As hextodata() and strtodata() are taking strings,
can you change the 'int len' parameters to 'size_t len'
parameters please ?
Post by Christof Schmitt
From d7afb99e3c63cd87452835dda271224d3d53f4a5 Mon Sep 17 00:00:00 2001
Date: Mon, 6 Jul 2015 13:07:33 -0700
Subject: [PATCH 1/2] ctdb: Create helper function for optional hex input
---
ctdb/tools/ctdb.c | 71 ++++++++++++++++++++++++-----------------------------
1 files changed, 32 insertions(+), 39 deletions(-)
diff --git a/ctdb/tools/ctdb.c b/ctdb/tools/ctdb.c
index 91ada44..a4036bd 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);
@@ -4231,15 +4229,10 @@ static int control_pstore(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;
}
ret = ctdb_transaction_store(h, key, data);
--
1.7.1
From 971ef49e50074515572fe5129613d3d38954cdc9 Mon Sep 17 00:00:00 2001
Date: Mon, 6 Jul 2015 14:32:15 -0700
Subject: [PATCH 2/2] ctdb: Accept hex format for pdelete and ptrans commands
---
ctdb/tools/ctdb.c | 11 +++++++----
1 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/ctdb/tools/ctdb.c b/ctdb/tools/ctdb.c
index a4036bd..628086f 100644
--- a/ctdb/tools/ctdb.c
+++ b/ctdb/tools/ctdb.c
@@ -4299,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"));
@@ -4341,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
Christof Schmitt
2015-07-07 22:19:13 UTC
Permalink
Post by Jeremy Allison
Post by Christof Schmitt
Post by Jeremy Allison
Post by Christof Schmitt
Post by Amitay Isaacs
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?
Oops. Sorry, you might have to rebase now I pushed
your earlier patch.
Yes, the first patch is in master. I rebased the missing parts on top,
see attached patches.
As hextodata() and strtodata() are taking strings,
can you change the 'int len' parameters to 'size_t len'
parameters please ?
Thank you. I made the change and pushed the patches to autobuild.

Christof
Post by Jeremy Allison
Post by Christof Schmitt
From d7afb99e3c63cd87452835dda271224d3d53f4a5 Mon Sep 17 00:00:00 2001
Date: Mon, 6 Jul 2015 13:07:33 -0700
Subject: [PATCH 1/2] ctdb: Create helper function for optional hex input
---
ctdb/tools/ctdb.c | 71 ++++++++++++++++++++++++-----------------------------
1 files changed, 32 insertions(+), 39 deletions(-)
diff --git a/ctdb/tools/ctdb.c b/ctdb/tools/ctdb.c
index 91ada44..a4036bd 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);
@@ -4231,15 +4229,10 @@ static int control_pstore(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;
}
ret = ctdb_transaction_store(h, key, data);
--
1.7.1
From 971ef49e50074515572fe5129613d3d38954cdc9 Mon Sep 17 00:00:00 2001
Date: Mon, 6 Jul 2015 14:32:15 -0700
Subject: [PATCH 2/2] ctdb: Accept hex format for pdelete and ptrans commands
---
ctdb/tools/ctdb.c | 11 +++++++----
1 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/ctdb/tools/ctdb.c b/ctdb/tools/ctdb.c
index a4036bd..628086f 100644
--- a/ctdb/tools/ctdb.c
+++ b/ctdb/tools/ctdb.c
@@ -4299,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"));
@@ -4341,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
Loading...