Skip to content

Commit

Permalink
cmd/flux-kvs: add flux kvs dirat
Browse files Browse the repository at this point in the history
Add flux-kvs dirat [-r] treeobj key

This command demonstrates walking a snapshot using
kvs_get_dirat(), kvs_get_symlinkat(), and kvs_getat().
  • Loading branch information
garlick committed Sep 21, 2016
1 parent 22f0f2e commit 2603e98
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/cmd/flux-kvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void cmd_dirsize (flux_t h, int argc, char **argv);
void cmd_get_treeobj (flux_t h, int argc, char **argv);
void cmd_put_treeobj (flux_t h, int argc, char **argv);
void cmd_getat (flux_t h, int argc, char **argv);
void cmd_dirat (flux_t h, int argc, char **argv);


void usage (void)
Expand Down Expand Up @@ -95,6 +96,7 @@ void usage (void)
" flux-kvs get-treeobj key\n"
" flux-kvs put-treeobj key=treeobj\n"
" flux-kvs getat treeobj key\n"
" flux-kvs dirat [-r] treeobj [key]\n"
);
exit (1);
}
Expand Down Expand Up @@ -170,6 +172,8 @@ int main (int argc, char *argv[])
cmd_put_treeobj (h, argc - optind, argv + optind);
else if (!strcmp (cmd, "getat"))
cmd_getat (h, argc - optind, argv + optind);
else if (!strcmp (cmd, "dirat"))
cmd_dirat (h, argc - optind, argv + optind);
else
usage ();

Expand Down Expand Up @@ -609,6 +613,63 @@ void cmd_dir (flux_t h, int argc, char **argv)
log_msg_exit ("dir: specify zero or one directory");
}

static void dump_kvs_dirat (flux_t h, bool ropt,
const char *treeobj, const char *path)
{
kvsdir_t *dir;
kvsitr_t *itr;
const char *name;
char *key;

if (kvs_get_dirat (h, treeobj, path, &dir) < 0)
log_err_exit ("%s", path);

itr = kvsitr_create (dir);
while ((name = kvsitr_next (itr))) {
key = kvsdir_key_at (dir, name);
if (kvsdir_issymlink (dir, name)) {
char *link;
if (kvs_get_symlinkat (h, treeobj, key, &link) < 0)
log_err_exit ("%s", key);
printf ("%s -> %s\n", key, link);
free (link);

} else if (kvsdir_isdir (dir, name)) {
if (ropt)
dump_kvs_dirat (h, ropt, treeobj, key);
else
printf ("%s.\n", key);
} else {
char *json_str;
if (kvs_getat (h, treeobj, key, &json_str) < 0)
log_err_exit ("%s", key);
dump_kvs_val (key, json_str);
free (json_str);
}
free (key);
}
kvsitr_destroy (itr);
kvsdir_destroy (dir);

}

void cmd_dirat (flux_t h, int argc, char **argv)
{
bool ropt = false;

if (argc > 0 && !strcmp (argv[0], "-r")) {
ropt = true;
argc--;
argv++;
}
if (argc == 1)
dump_kvs_dirat (h, ropt, argv[0], ".");
else if (argc == 2)
dump_kvs_dirat (h, ropt, argv[0], argv[1]);
else
log_msg_exit ("dirat: specify treeobj and zero or one directory");
}

void cmd_dirsize (flux_t h, int argc, char **argv)
{
kvsdir_t *dir = NULL;
Expand Down

0 comments on commit 2603e98

Please sign in to comment.