1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <stdbool.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <unistd.h> 36 37 #include <be.h> 38 39 #include "bectl.h" 40 41 struct sort_column { 42 const char *name; 43 const char *val; 44 nvlist_t *nvl; 45 }; 46 47 struct printc { 48 int active_colsz_def; 49 int be_colsz; 50 int current_indent; 51 int mount_colsz; 52 int space_colsz; 53 bool script_fmt; 54 bool show_all_datasets; 55 bool show_snaps; 56 bool show_space; 57 }; 58 59 static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops); 60 static void print_padding(const char *fval, int colsz, struct printc *pc); 61 static int print_snapshots(const char *dsname, struct printc *pc); 62 static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc); 63 static void print_headers(nvlist_t *props, struct printc *pc); 64 static unsigned long long dataset_space(const char *oname); 65 66 #define HEADER_BE "BE" 67 #define HEADER_BEPLUS "BE/Dataset/Snapshot" 68 #define HEADER_ACTIVE "Active" 69 #define HEADER_MOUNT "Mountpoint" 70 #define HEADER_SPACE "Space" 71 #define HEADER_CREATED "Created" 72 73 /* Spaces */ 74 #define INDENT_INCREMENT 2 75 76 /* 77 * Given a set of dataset properties (for a BE dataset), populate originprops 78 * with the origin's properties. 79 */ 80 static const char * 81 get_origin_props(nvlist_t *dsprops, nvlist_t **originprops) 82 { 83 const char *propstr; 84 85 if (nvlist_lookup_string(dsprops, "origin", &propstr) == 0) { 86 if (be_prop_list_alloc(originprops) != 0) { 87 fprintf(stderr, 88 "bectl list: failed to allocate origin prop nvlist\n"); 89 return (NULL); 90 } 91 if (be_get_dataset_props(be, propstr, *originprops) != 0) { 92 /* XXX TODO: Real errors */ 93 fprintf(stderr, 94 "bectl list: failed to fetch origin properties\n"); 95 return (NULL); 96 } 97 98 return (propstr); 99 } 100 return (NULL); 101 } 102 103 static void 104 print_padding(const char *fval, int colsz, struct printc *pc) 105 { 106 107 /* -H flag handling; all delimiters/padding are a single tab */ 108 if (pc->script_fmt) { 109 printf("\t"); 110 return; 111 } 112 113 if (fval != NULL) 114 colsz -= strlen(fval); 115 printf("%*s ", colsz, ""); 116 } 117 118 static unsigned long long 119 dataset_space(const char *oname) 120 { 121 unsigned long long space; 122 char *dsname, *sep; 123 const char *propstr; 124 nvlist_t *dsprops; 125 126 space = 0; 127 dsname = strdup(oname); 128 if (dsname == NULL) 129 return (0); 130 131 /* Truncate snapshot to dataset name, as needed */ 132 if ((sep = strchr(dsname, '@')) != NULL) 133 *sep = '\0'; 134 135 if (be_prop_list_alloc(&dsprops) != 0) { 136 free(dsname); 137 return (0); 138 } 139 140 if (be_get_dataset_props(be, dsname, dsprops) != 0) { 141 nvlist_free(dsprops); 142 free(dsname); 143 return (0); 144 } 145 146 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) 147 space = strtoull(propstr, NULL, 10); 148 149 nvlist_free(dsprops); 150 free(dsname); 151 return (space); 152 } 153 154 static int 155 print_snapshots(const char *dsname, struct printc *pc) 156 { 157 nvpair_t *cur; 158 nvlist_t *props, *sprops; 159 160 if (be_prop_list_alloc(&props) != 0) { 161 fprintf(stderr, "bectl list: failed to allocate snapshot nvlist\n"); 162 return (1); 163 } 164 if (be_get_dataset_snapshots(be, dsname, props) != 0) { 165 fprintf(stderr, "bectl list: failed to fetch boot ds snapshots\n"); 166 return (1); 167 } 168 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL; 169 cur = nvlist_next_nvpair(props, cur)) { 170 nvpair_value_nvlist(cur, &sprops); 171 print_info(nvpair_name(cur), sprops, pc); 172 } 173 return (0); 174 } 175 176 static void 177 print_info(const char *name, nvlist_t *dsprops, struct printc *pc) 178 { 179 #define BUFSZ 64 180 char buf[BUFSZ]; 181 unsigned long long ctimenum, space; 182 nvlist_t *originprops; 183 const char *oname, *dsname, *propstr; 184 int active_colsz; 185 boolean_t active_now, active_reboot, bootonce; 186 187 dsname = NULL; 188 originprops = NULL; 189 printf("%*s%s", pc->current_indent, "", name); 190 nvlist_lookup_string(dsprops, "dataset", &dsname); 191 192 /* Recurse at the base level if we're breaking info down */ 193 if (pc->current_indent == 0 && (pc->show_all_datasets || 194 pc->show_snaps)) { 195 printf("\n"); 196 if (dsname == NULL) 197 /* XXX TODO: Error? */ 198 return; 199 /* 200 * Whether we're dealing with -a or -s, we'll always print the 201 * dataset name/information followed by its origin. For -s, we 202 * additionally iterate through all snapshots of this boot 203 * environment and also print their information. 204 */ 205 pc->current_indent += INDENT_INCREMENT; 206 print_info(dsname, dsprops, pc); 207 pc->current_indent += INDENT_INCREMENT; 208 if ((oname = get_origin_props(dsprops, &originprops)) != NULL) { 209 print_info(oname, originprops, pc); 210 nvlist_free(originprops); 211 } 212 213 /* Back up a level; snapshots at the same level as dataset */ 214 pc->current_indent -= INDENT_INCREMENT; 215 if (pc->show_snaps) 216 print_snapshots(dsname, pc); 217 pc->current_indent = 0; 218 return; 219 } else 220 print_padding(name, pc->be_colsz - pc->current_indent, pc); 221 222 active_colsz = pc->active_colsz_def; 223 if (nvlist_lookup_boolean_value(dsprops, "active", 224 &active_now) == 0 && active_now) { 225 printf("N"); 226 active_colsz--; 227 } 228 if (nvlist_lookup_boolean_value(dsprops, "nextboot", 229 &active_reboot) == 0 && active_reboot) { 230 printf("R"); 231 active_colsz--; 232 } 233 if (nvlist_lookup_boolean_value(dsprops, "bootonce", 234 &bootonce) == 0 && bootonce) { 235 printf("T"); 236 active_colsz--; 237 } 238 if (active_colsz == pc->active_colsz_def) { 239 printf("-"); 240 active_colsz--; 241 } 242 print_padding(NULL, active_colsz, pc); 243 if (nvlist_lookup_string(dsprops, "mounted", &propstr) == 0) { 244 printf("%s", propstr); 245 print_padding(propstr, pc->mount_colsz, pc); 246 } else { 247 printf("%s", "-"); 248 print_padding("-", pc->mount_colsz, pc); 249 } 250 251 oname = get_origin_props(dsprops, &originprops); 252 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) { 253 /* 254 * The space used column is some composition of: 255 * - The "used" property of the dataset 256 * - The "used" property of the origin snapshot (not -a or -s) 257 * - The "used" property of the origin dataset (-D flag only) 258 * 259 * The -D flag is ignored if -a or -s are specified. 260 */ 261 space = strtoull(propstr, NULL, 10); 262 263 if (!pc->show_all_datasets && !pc->show_snaps && 264 originprops != NULL && 265 nvlist_lookup_string(originprops, "used", &propstr) == 0) 266 space += strtoull(propstr, NULL, 10); 267 268 if (pc->show_space && oname != NULL) 269 space += dataset_space(oname); 270 271 /* Alas, there's more to it,. */ 272 be_nicenum(space, buf, 6); 273 printf("%s", buf); 274 print_padding(buf, pc->space_colsz, pc); 275 } else { 276 printf("-"); 277 print_padding("-", pc->space_colsz, pc); 278 } 279 280 if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) { 281 ctimenum = strtoull(propstr, NULL, 10); 282 strftime(buf, BUFSZ, "%Y-%m-%d %H:%M", 283 localtime((time_t *)&ctimenum)); 284 printf("%s", buf); 285 } 286 287 printf("\n"); 288 if (originprops != NULL) 289 be_prop_list_free(originprops); 290 #undef BUFSZ 291 } 292 293 static void 294 print_headers(nvlist_t *props, struct printc *pc) 295 { 296 const char *chosen_be_header, *propstr; 297 nvpair_t *cur; 298 nvlist_t *dsprops; 299 size_t be_maxcol, mount_colsz; 300 301 if (pc->show_all_datasets || pc->show_snaps) 302 chosen_be_header = HEADER_BEPLUS; 303 else 304 chosen_be_header = HEADER_BE; 305 be_maxcol = strlen(chosen_be_header); 306 mount_colsz = strlen(HEADER_MOUNT); 307 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL; 308 cur = nvlist_next_nvpair(props, cur)) { 309 be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur))); 310 nvpair_value_nvlist(cur, &dsprops); 311 312 if (nvlist_lookup_string(dsprops, "mounted", &propstr) == 0) 313 mount_colsz = MAX(mount_colsz, strlen(propstr)); 314 if (!pc->show_all_datasets && !pc->show_snaps) 315 continue; 316 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0) 317 continue; 318 be_maxcol = MAX(be_maxcol, strlen(propstr) + INDENT_INCREMENT); 319 if (nvlist_lookup_string(dsprops, "origin", &propstr) != 0) 320 continue; 321 be_maxcol = MAX(be_maxcol, 322 strlen(propstr) + INDENT_INCREMENT * 2); 323 } 324 325 pc->be_colsz = be_maxcol; 326 pc->active_colsz_def = strlen(HEADER_ACTIVE); 327 pc->mount_colsz = mount_colsz; 328 pc->space_colsz = strlen(HEADER_SPACE); 329 printf("%*s %s %*s %s %s\n", -pc->be_colsz, chosen_be_header, 330 HEADER_ACTIVE, -pc->mount_colsz, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED); 331 332 /* 333 * All other invocations in which we aren't using the default header 334 * will produce quite a bit of input. Throw an extra blank line after 335 * the header to make it look nicer. 336 */ 337 if (strcmp(chosen_be_header, HEADER_BE) != 0) 338 printf("\n"); 339 } 340 341 /* 342 * Sort the given nvlist of boot environments by property. 343 */ 344 static int 345 prop_list_sort(nvlist_t *props, char *property, bool reverse) 346 { 347 nvpair_t *nvp; 348 nvlist_t *nvl; 349 int i, nvp_count; 350 uint64_t lval, rval; 351 struct sort_column sc_prev, sc_next; 352 353 /* a temporary list to work with */ 354 nvlist_dup(props, &nvl, 0); 355 356 nvp_count = fnvlist_num_pairs(nvl); 357 for (i = 0; i < nvp_count; i++) { 358 359 nvp = nvlist_next_nvpair(nvl, NULL); 360 nvpair_value_nvlist(nvp, &sc_prev.nvl); 361 nvlist_lookup_string(sc_prev.nvl, "name", &sc_prev.name); 362 nvlist_lookup_string(sc_prev.nvl, property, &sc_prev.val); 363 364 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 365 366 nvpair_value_nvlist(nvp, &sc_next.nvl); 367 nvlist_lookup_string(sc_next.nvl, "name", &sc_next.name); 368 nvlist_lookup_string(sc_next.nvl, property, &sc_next.val); 369 370 /* properties that use numerical comparison */ 371 if (strcmp(property, "creation") == 0 || 372 strcmp(property, "used") == 0 || 373 strcmp(property, "usedds") == 0 || 374 strcmp(property, "usedsnap") == 0 || 375 strcmp(property, "usedrefreserv") == 0) { 376 377 lval = strtoull(sc_prev.val, NULL, 10); 378 rval = strtoull(sc_next.val, NULL, 10); 379 380 if ((lval < rval && reverse) || 381 (lval > rval && !reverse)) 382 sc_prev = sc_next; 383 } 384 385 /* properties that use string comparison */ 386 else if (strcmp(property, "name") == 0 || 387 strcmp(property, "origin") == 0) { 388 if ((strcmp(sc_prev.val, sc_next.val) < 0 && reverse) || 389 (strcmp(sc_prev.val, sc_next.val) > 0 && !reverse)) 390 sc_prev = sc_next; 391 } 392 } 393 394 /* 395 * The 'props' nvlist has been created to only have unique names. 396 * When a name is added, any existing nvlist's with the same name 397 * will be removed. Eventually, all existing nvlist's are replaced 398 * in sorted order. 399 */ 400 nvlist_add_nvlist(props, sc_prev.name, sc_prev.nvl); 401 nvlist_remove_all(nvl, sc_prev.name); 402 } 403 404 be_prop_list_free(nvl); 405 406 return 0; 407 } 408 409 int 410 bectl_cmd_list(int argc, char *argv[]) 411 { 412 struct printc pc; 413 nvpair_t *cur; 414 nvlist_t *dsprops, *props; 415 int opt, printed; 416 char *column; 417 bool reverse; 418 419 column = NULL; 420 props = NULL; 421 printed = 0; 422 bzero(&pc, sizeof(pc)); 423 reverse = false; 424 while ((opt = getopt(argc, argv, "aDHsc:C:")) != -1) { 425 switch (opt) { 426 case 'a': 427 pc.show_all_datasets = true; 428 break; 429 case 'D': 430 pc.show_space = true; 431 break; 432 case 'H': 433 pc.script_fmt = true; 434 break; 435 case 's': 436 pc.show_snaps = true; 437 break; 438 case 'c': 439 if (column != NULL) 440 free(column); 441 column = strdup(optarg); 442 reverse = false; 443 break; 444 case 'C': 445 if (column != NULL) 446 free(column); 447 column = strdup(optarg); 448 reverse = true; 449 break; 450 default: 451 fprintf(stderr, "bectl list: unknown option '-%c'\n", 452 optopt); 453 return (usage(false)); 454 } 455 } 456 457 argc -= optind; 458 459 if (argc != 0) { 460 fprintf(stderr, "bectl list: extra argument provided\n"); 461 return (usage(false)); 462 } 463 464 if (be_prop_list_alloc(&props) != 0) { 465 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n"); 466 return (1); 467 } 468 if (be_get_bootenv_props(be, props) != 0) { 469 /* XXX TODO: Real errors */ 470 fprintf(stderr, "bectl list: failed to fetch boot environments\n"); 471 return (1); 472 } 473 474 /* List boot environments in alphabetical order by default */ 475 if (column == NULL) 476 column = strdup("name"); 477 478 prop_list_sort(props, column, reverse); 479 480 /* Force -D off if either -a or -s are specified */ 481 if (pc.show_all_datasets || pc.show_snaps) 482 pc.show_space = false; 483 if (!pc.script_fmt) 484 print_headers(props, &pc); 485 486 /* Print boot environments */ 487 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL; 488 cur = nvlist_next_nvpair(props, cur)) { 489 nvpair_value_nvlist(cur, &dsprops); 490 491 if (printed > 0 && (pc.show_all_datasets || pc.show_snaps)) 492 printf("\n"); 493 494 print_info(nvpair_name(cur), dsprops, &pc); 495 printed++; 496 } 497 498 free(column); 499 be_prop_list_free(props); 500 501 return (0); 502 } 503 504