[PATCH] ctdb: Accept the key in hex format for the pstore command
Jeremy Allison
jra at samba.org
Tue Jul 7 01:21:10 CEST 2015
On Mon, Jul 06, 2015 at 03:21:39PM -0700, Christof Schmitt wrote:
> On Mon, Jul 06, 2015 at 02:49:59PM -0700, Jeremy Allison wrote:
> > On Mon, Jul 06, 2015 at 02:34:11PM -0700, Christof Schmitt wrote:
> > > 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?
> >
> > 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 ?
If you do that:
Reviewed-by: Jeremy Allison <jra at samba.org>
> From d7afb99e3c63cd87452835dda271224d3d53f4a5 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 | 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
> 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 pdelete and ptrans commands
>
> Signed-off-by: Christof Schmitt <cs at samba.org>
> ---
> 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
>
More information about the samba-technical
mailing list