From f6ca59ae282b04459d77ef901161ad15d8447569 Mon Sep 17 00:00:00 2001 From: Albert Chu Date: Tue, 10 Apr 2018 11:34:02 -0700 Subject: [PATCH] cmd/flux-kvs: Update dir and ls usage With the dir and ls commands in flux-kvs, if a user specifies a namespace prefix without a key, assume the user desires the root (i.e. ".") directory within that namespace. Update/adjust unit tests appropriately. Fixes #1434 --- src/cmd/flux-kvs.c | 54 ++++++++++++++++++++++------------------- t/t1004-kvs-namespace.t | 19 +++++++++++++-- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/cmd/flux-kvs.c b/src/cmd/flux-kvs.c index b3605da24187..002f26969e29 100644 --- a/src/cmd/flux-kvs.c +++ b/src/cmd/flux-kvs.c @@ -1054,6 +1054,27 @@ int cmd_dropcache (optparse_t *p, int argc, char **argv) return (0); } +static char *process_key (const char *key, char **key_suffix) +{ + char *nkey; + char *ptr; + + if (!(nkey = malloc (strlen (key) + 2))) // room for decoration char + null + log_err_exit ("malloc"); + strcpy (nkey, key); + + if (!strncmp (nkey, "ns:", 3) + && (ptr = strchr (nkey, '/'))) { + /* No key suffix, decorate with default '.' */ + if (*(ptr + 1) == '\0') + strcat (nkey, "."); + if (key_suffix) + (*key_suffix) = ptr + 1; + } + + return nkey; +} + static void dump_kvs_val (const char *key, int maxcol, const char *value) { json_t *o; @@ -1130,7 +1151,7 @@ int cmd_dir (optparse_t *p, int argc, char **argv) int maxcol = get_window_width (p, STDOUT_FILENO); bool Ropt; bool dopt; - const char *key; + char *key; flux_future_t *f; const flux_kvsdir_t *dir; int optindex; @@ -1139,9 +1160,9 @@ int cmd_dir (optparse_t *p, int argc, char **argv) Ropt = optparse_hasopt (p, "recursive"); dopt = optparse_hasopt (p, "directory"); if (optindex == argc) - key = "."; + key = process_key (".", NULL); else if (optindex == (argc - 1)) - key = argv[optindex]; + key = process_key (argv[optindex], NULL); else log_msg_exit ("dir: specify zero or one directory"); @@ -1158,6 +1179,7 @@ int cmd_dir (optparse_t *p, int argc, char **argv) log_err_exit ("%s", key); dump_kvs_dir (dir, maxcol, Ropt, dopt); flux_future_destroy (f); + free (key); return (0); } @@ -1347,21 +1369,6 @@ static int sort_cmp (void *item1, void *item2) return strcmp (item1, item2); } -static void contains_namespace_prefix (const char *key, char **key_suffix) -{ - char *ptr; - - if (!strncmp (key, "ns:", 3) - && (ptr = strchr (key, '/'))) { - - /* No key suffix */ - if (*(ptr + 1) == '\0') - log_err_exit ("%s: %s\n", key, flux_strerror (EINVAL)); - - (*key_suffix) = ptr + 1; - } -} - /* Put key in 'dirs' or 'singles' list, depending on whether * its contents are to be listed or not. If -F is specified, * 'singles' key names are decorated based on their type. @@ -1375,15 +1382,12 @@ static int categorize_key (optparse_t *p, const char *key, char *nkey; json_t *treeobj = NULL; bool require_directory = false; - char *key_ptr; - - if (!(nkey = malloc (strlen (key) + 2))) // room for decoration char + null - log_err_exit ("malloc"); - strcpy (nkey, key); + char *key_ptr = NULL; - key_ptr = nkey; + nkey = process_key (key, &key_ptr); - contains_namespace_prefix (nkey, &key_ptr); + if (!key_ptr) + key_ptr = nkey; /* If the key has a "." suffix, strip it off, but require * that the key be a directory type. diff --git a/t/t1004-kvs-namespace.t b/t/t1004-kvs-namespace.t index b51666531134..ac246241318c 100755 --- a/t/t1004-kvs-namespace.t +++ b/t/t1004-kvs-namespace.t @@ -427,11 +427,26 @@ EOF test_cmp expected output ' +# Note double period, will be resolved in issue #1391 fix +test_expect_success 'kvs: namespace prefix works with dir, no key specified' ' + flux kvs dir ns:${NAMESPACEPREFIX}-1/ | sort >output && + cat >expected <output && + cat >expected <