/** * fdisk_reset_table: * @tb: tab pointer * * Removes all entries (partitions) from the table. The parititons with zero * reference count will be deallocated. This function does not modify partition * table. * * Returns: 0 on success or negative number in case of error. */ int fdisk_reset_table(struct fdisk_table *tb) { if (!tb) return -EINVAL; DBG(TAB, ul_debugobj(tb, "reset")); while (!list_empty(&tb->parts)) { struct fdisk_partition *pa = list_entry(tb->parts.next, struct fdisk_partition, parts); fdisk_table_remove_partition(tb, pa); } tb->nents = 0; 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; }