diff options
author | Kyle Evans <kevans@FreeBSD.org> | 2018-08-03 01:46:46 +0000 |
---|---|---|
committer | Kyle Evans <kevans@FreeBSD.org> | 2018-08-03 01:46:46 +0000 |
commit | 970fd6543a37e0b3cb92c18744d1cf43660b0b78 (patch) | |
tree | b0c23f195171adb4b3657a09706c4583026c2cf2 /sbin/bectl/bectl.c | |
parent | 513044c1814d68d3480c84ac6ff74d2d7fb8d283 (diff) | |
download | src-970fd6543a37e0b3cb92c18744d1cf43660b0b78.tar.gz src-970fd6543a37e0b3cb92c18744d1cf43660b0b78.zip |
bectl(8): Move dataset printing out into its own function
This may later get reused for printing snapshot and/or origin snapshot
information.
Notes
Notes:
svn path=/projects/bectl/; revision=337225
Diffstat (limited to 'sbin/bectl/bectl.c')
-rw-r--r-- | sbin/bectl/bectl.c | 132 |
1 files changed, 77 insertions, 55 deletions
diff --git a/sbin/bectl/bectl.c b/sbin/bectl/bectl.c index f40e7ff9c466..0001d876af22 100644 --- a/sbin/bectl/bectl.c +++ b/sbin/bectl/bectl.c @@ -43,12 +43,19 @@ #include <be.h> -#define HEADER_BE "BE" +#define HEADER_BE "BE" #define HEADER_ACTIVE "Active" #define HEADER_MOUNT "Mountpoint" #define HEADER_SPACE "Space" #define HEADER_CREATED "Created" +struct printc { + int be_colsz; + int active_colsz_def; + int mount_colsz; + int space_colsz; +}; + static int bectl_cmd_activate(int argc, char *argv[]); static int bectl_cmd_create(int argc, char *argv[]); static int bectl_cmd_destroy(int argc, char *argv[]); @@ -56,6 +63,7 @@ static int bectl_cmd_export(int argc, char *argv[]); static int bectl_cmd_import(int argc, char *argv[]); static int bectl_cmd_add(int argc, char *argv[]); static int bectl_cmd_jail(int argc, char *argv[]); +static void print_dataset(nvpair_t *cur, struct printc *pc); static int bectl_cmd_list(int argc, char *argv[]); static int bectl_cmd_mount(int argc, char *argv[]); static int bectl_cmd_rename(int argc, char *argv[]); @@ -410,18 +418,74 @@ bectl_cmd_jail(int argc, char *argv[]) return (0); } +static void +print_dataset(nvpair_t *cur, struct printc *pc) +{ +#define BUFSZ 64 + char buf[BUFSZ]; + unsigned long long ctimenum, space; + nvlist_t *dsprops; + char *creation, *mnt, *name, *spacestr; + int active_colsz; + boolean_t active_now, active_reboot; + + name = nvpair_name(cur); + /* XXX TODO: Some views show snapshots */ + if (strchr(name, '@') != NULL) + return; + printf("%*s ", pc->be_colsz, name); + + active_colsz = pc->active_colsz_def; + nvpair_value_nvlist(cur, &dsprops); + if (nvlist_lookup_boolean_value(dsprops, "active", + &active_now) == 0 && active_now) { + printf("N"); + active_colsz--; + } + if (nvlist_lookup_boolean_value(dsprops, "nextboot", + &active_reboot) == 0 && active_reboot) { + printf("R"); + active_colsz--; + } + if (active_colsz == pc->active_colsz_def) { + printf("-"); + active_colsz--; + } + printf("%*s ", -active_colsz, " "); + if (nvlist_lookup_string(dsprops, "mountpoint", &mnt) == 0) + printf("%*s ", pc->mount_colsz, mnt); + else + printf("%*s ", pc->mount_colsz, "-"); + + if (nvlist_lookup_string(dsprops, "used", &spacestr) == 0) { + space = strtoull(spacestr, NULL, 10); + + /* Alas, there's more to it,. */ + humanize_number(buf, 6, space, "", HN_AUTOSCALE, + HN_DECIMAL | HN_NOSPACE | HN_B); + printf("%*s ", pc->space_colsz, buf); + } else + printf("%*s ", pc->space_colsz, "-"); + + if (nvlist_lookup_string(dsprops, "creation", &creation) == 0) { + ctimenum = strtoull(creation, NULL, 10); + strftime(buf, BUFSZ, "%Y-%m-%d %H:%M", + localtime((time_t *)&ctimenum)); + printf("%s", buf); + } + + printf("\n"); +#undef BUFSZ +} static int bectl_cmd_list(int argc, char *argv[]) { -#define BUFSZ 64 + struct printc pc; nvpair_t *cur; - nvlist_t *props, *dsprops; - unsigned long long ctimenum, space; + nvlist_t *props; size_t be_maxcol; - int active_colsz, active_colsz_def, be_colsz, mount_colsz, opt, space_colsz; - char buf[BUFSZ], *creation, *mnt, *spacestr; - boolean_t active_now, active_reboot; + int opt; bool show_all_datasets, show_space, hide_headers, show_snaps; props = NULL; @@ -470,62 +534,20 @@ bectl_cmd_list(int argc, char *argv[]) be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur))); } - be_colsz = -be_maxcol; + pc.be_colsz = -be_maxcol; /* To be made negative after calculating final col sz */ - active_colsz_def = strlen(HEADER_ACTIVE); - mount_colsz = -(int)strlen(HEADER_MOUNT); - space_colsz = -(int)strlen(HEADER_SPACE); - printf("%*s %s %s %s %s\n", be_colsz, HEADER_BE, HEADER_ACTIVE, + pc.active_colsz_def = strlen(HEADER_ACTIVE); + pc.mount_colsz = -(int)strlen(HEADER_MOUNT); + pc.space_colsz = -(int)strlen(HEADER_SPACE); + printf("%*s %s %s %s %s\n", pc.be_colsz, HEADER_BE, HEADER_ACTIVE, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED); - buf[5] = '\0'; - cur = NULL; for (cur = nvlist_next_nvpair(props, NULL); cur != NULL; cur = nvlist_next_nvpair(props, cur)) { - printf("%*s ", be_colsz, nvpair_name(cur)); - // NR - active_colsz = active_colsz_def; - nvpair_value_nvlist(cur, &dsprops); - if (nvlist_lookup_boolean_value(dsprops, "active", - &active_now) == 0 && active_now) { - printf("N"); - active_colsz--; - } - if (nvlist_lookup_boolean_value(dsprops, "nextboot", - &active_reboot) == 0 && active_reboot) { - printf("R"); - active_colsz--; - } - if (active_colsz == active_colsz_def) { - printf("-"); - active_colsz--; - } - printf("%*s ", -active_colsz, " "); - if (nvlist_lookup_string(dsprops, "mountpoint", &mnt) == 0) - printf("%*s ", mount_colsz, mnt); - else - printf("%*s ", mount_colsz, "-"); - // used - if (nvlist_lookup_string(dsprops, "used", &spacestr) == 0) { - space = strtoull(spacestr, NULL, 10); - humanize_number(buf, 6, space, "", HN_AUTOSCALE, - HN_DECIMAL | HN_NOSPACE | HN_B); - printf("%*s ", space_colsz, buf); - } else - printf("%*s ", space_colsz, "-"); - - if (nvlist_lookup_string(dsprops, "creation", &creation) == 0) { - ctimenum = strtoull(creation, NULL, 10); - strftime(buf, BUFSZ, "%Y-%m-%d %H:%M", - localtime((time_t *)&ctimenum)); - printf("%s", buf); - } - - printf("\n"); + print_dataset(cur, &pc); } be_prop_list_free(props); return (0); -#undef BUFSZ } |