static struct libscols_line *get_line_with_id(struct libscols_table *tb, int col_id, const char *id) { struct libscols_line *ln; struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD); while (scols_table_next_line(tb, itr, &ln) == 0) { struct libscols_cell *ce = scols_line_get_cell(ln, col_id); const char *data = ce ? scols_cell_get_data(ce) : NULL; if (data && strcmp(data, id) == 0) break; } scols_free_iter(itr); return ln; }
static void compose_tree(struct libscols_table *tb, int parent_col, int id_col) { struct libscols_line *ln; struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD); while (scols_table_next_line(tb, itr, &ln) == 0) { struct libscols_line *parent = NULL; struct libscols_cell *ce = scols_line_get_cell(ln, parent_col); const char *data = ce ? scols_cell_get_data(ce) : NULL; if (data) parent = get_line_with_id(tb, id_col, data); if (parent) scols_line_add_child(parent, ln); } scols_free_iter(itr); }
/* Reads partition in tree-like order from scols */ static int partition_from_scols(struct fdisk_table *tb, struct libscols_line *ln) { struct fdisk_partition *pa = scols_line_get_userdata(ln); fdisk_table_add_partition(tb, pa); fdisk_unref_partition(pa); if (scols_line_has_children(ln)) { struct libscols_line *chln; struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD); if (!itr) return -EINVAL; while (scols_line_next_child(ln, itr, &chln) == 0) partition_from_scols(tb, chln); scols_free_iter(itr); } return 0; }
/* It would be possible to use fdisk_table_to_string(), but we want some * extension to the output format, so let's do it without libfdisk */ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) { const struct fdisk_column *col; struct fdisk_partition *pa; struct fdisk_label *lb; struct fdisk_iter *itr = NULL; struct libscols_table *table = NULL; struct libscols_iter *s_itr = NULL; char *res = NULL; size_t i; int tree = 0; struct libscols_line *ln, *ln_cont = NULL; DBG(FRONTEND, ul_debug("table: convert to string")); assert(cf); assert(cf->cxt); assert(cf->cols); assert(tb); lb = fdisk_context_get_label(cf->cxt, NULL); assert(lb); itr = fdisk_new_iter(FDISK_ITER_FORWARD); if (!itr) goto done; /* get container (e.g. extended partition) */ while (fdisk_table_next_partition(tb, itr, &pa) == 0) { if (fdisk_partition_is_nested(pa)) { DBG(FRONTEND, ul_debug("table: nested detected, using tree")); tree = SCOLS_FL_TREE; break; } } table = scols_new_table(); if (!table) goto done; scols_table_enable_maxout(table, 1); /* headers */ for (i = 0; i < cf->ncols; i++) { col = fdisk_label_get_column(lb, cf->cols[i]); if (col) { int fl = col->scols_flags; if (tree && col->id == FDISK_COL_DEVICE) fl |= SCOLS_FL_TREE; if (!scols_table_new_column(table, col->name, col->width, fl)) goto done; } } /* data */ fdisk_reset_iter(itr, FDISK_ITER_FORWARD); while (fdisk_table_next_partition(tb, itr, &pa) == 0) { struct libscols_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL; ln = scols_table_new_line(table, parent); if (!ln) goto done; for (i = 0; i < cf->ncols; i++) { char *cdata = NULL; col = fdisk_label_get_column(lb, cf->cols[i]); if (!col) continue; if (fdisk_partition_to_string(pa, cf->cxt, col->id, &cdata)) continue; scols_line_refer_data(ln, i, cdata); } if (tree && fdisk_partition_is_container(pa)) ln_cont = ln; scols_line_set_userdata(ln, (void *) pa); fdisk_ref_partition(pa); } if (scols_table_is_empty(table)) goto done; scols_table_reduce_termwidth(table, ARROW_CURSOR_WIDTH); scols_print_table_to_string(table, &res); /* scols_* code might reorder lines, let's reorder @tb according to the * final output (it's no problem because partitions are addressed by * parno stored within struct fdisk_partition) */ /* remove all */ fdisk_reset_iter(itr, FDISK_ITER_FORWARD); while (fdisk_table_next_partition(tb, itr, &pa) == 0) fdisk_table_remove_partition(tb, pa); s_itr = scols_new_iter(SCOLS_ITER_FORWARD); if (!s_itr) goto done; /* add all in the right order (don't forget the output is tree) */ while (scols_table_next_line(table, s_itr, &ln) == 0) { if (scols_line_get_parent(ln)) continue; if (partition_from_scols(tb, ln)) break; } done: scols_unref_table(table); scols_free_iter(s_itr); fdisk_free_iter(itr); return res; }