From a745611d695016ede822380bce0aa92350fc80a3 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 14 Aug 2014 13:28:37 +0200 Subject: libfdisk: final parttype API cleanup Signed-off-by: Karel Zak --- disk-utils/cfdisk.c | 27 ++++++++++++++------------- disk-utils/fdisk.c | 52 +++++++++++++++++++++++++++++----------------------- 2 files changed, 43 insertions(+), 36 deletions(-) (limited to 'disk-utils') diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index e38052591..5b33af871 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -1509,9 +1509,9 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf, { struct cfdisk_menuitem *d, *cm; size_t i = 0, nitems, idx = 0; - struct fdisk_parttype *types, *t = NULL; + struct fdisk_parttype *t = NULL; struct fdisk_label *lb; - int has_typestr = 0; + int codetypes = 0; DBG(UI, ul_debug("asking for parttype.")); @@ -1519,27 +1519,28 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf, /* create cfdisk menu according to label types, note that the * last cm[] item has to be empty -- so nitems + 1 */ - if (fdisk_label_get_parttypes(lb, &types, &nitems) || !nitems) + nitems = fdisk_label_get_nparttypes(lb); + if (!nitems) return NULL; cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem)); if (!cm) return NULL; - has_typestr = fdisk_label_is_parttype_string(lb); + codetypes = fdisk_label_has_code_parttypes(lb); for (i = 0; i < nitems; i++) { - struct fdisk_parttype *x = &types[i]; + const struct fdisk_parttype *x = fdisk_label_get_parttype(lb, i); char *name; - if (!x || !x->name) - continue; - cm[i].userdata = x; - if (!has_typestr) - xasprintf(&name, "%2x %s", x->type, _(x->name)); + cm[i].userdata = (void *) x; + if (codetypes) + xasprintf(&name, "%2x %s", + fdisk_parttype_get_code(x), + _(fdisk_parttype_get_name(x))); else { - name = (char *) _(x->name); - cm[i].desc = x->typestr; + name = (char *) _(fdisk_parttype_get_name(x)); + cm[i].desc = fdisk_parttype_get_string(x); } cm[i].name = name; if (x == cur) @@ -1579,7 +1580,7 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf, done: menu_pop(cf); - if (!has_typestr) { + if (codetypes) { for (i = 0; i < nitems; i++) free((char *) cm[i].name); } diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index 6883b6c41..3b9431af3 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -354,12 +354,15 @@ int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask, struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt) { const char *q; - struct fdisk_label *lb = fdisk_get_label(cxt, NULL); + struct fdisk_label *lb; + + assert(cxt); + lb = fdisk_get_label(cxt, NULL); - if (!cxt || !cxt->label || !cxt->label->nparttypes) + if (!lb) return NULL; - q = fdisk_label_is_parttype_string(lb) ? + q = fdisk_label_has_code_parttypes(lb) ? _("Partition type (type L to list all types): ") : _("Hex code (type L to list all codes): "); do { @@ -372,7 +375,7 @@ struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt) if (buf[1] == '\0' && toupper(*buf) == 'L') list_partition_types(cxt); else if (*buf) - return fdisk_parse_parttype(cxt, buf); + return fdisk_label_parse_parttype(lb, buf); } while (1); return NULL; @@ -380,16 +383,18 @@ struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt) void list_partition_types(struct fdisk_context *cxt) { - struct fdisk_parttype *types; size_t ntypes = 0; + struct fdisk_label *lb = fdisk_get_label(cxt, NULL); - if (!cxt || !cxt->label || !cxt->label->parttypes) + assert(cxt); + lb = fdisk_get_label(cxt, NULL); + if (!lb) + return; + ntypes = fdisk_label_get_nparttypes(lb); + if (!ntypes) return; - types = cxt->label->parttypes; - ntypes = cxt->label->nparttypes; - - if (types[0].typestr == NULL) { + if (fdisk_label_has_code_parttypes(lb)) { /* * Prints in 4 columns in format */ @@ -397,8 +402,6 @@ void list_partition_types(struct fdisk_context *cxt) int i; size = ntypes; - if (types[ntypes - 1].name == NULL) - size--; for (i = 3; i >= 0; i--) last[3 - i] = done += (size + i - done) / (i + 1); @@ -408,16 +411,19 @@ void list_partition_types(struct fdisk_context *cxt) #define NAME_WIDTH 15 char name[NAME_WIDTH * MB_LEN_MAX]; size_t width = NAME_WIDTH; - struct fdisk_parttype *t = &types[next]; + const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, next); size_t ret; if (t->name) { - printf("%c%2x ", i ? ' ' : '\n', t->type); - ret = mbsalign(_(t->name), name, sizeof(name), - &width, MBS_ALIGN_LEFT, 0); + printf("%c%2x ", i ? ' ' : '\n', + fdisk_parttype_get_code(t)); + ret = mbsalign(_(fdisk_parttype_get_name(t)), + name, sizeof(name), + &width, MBS_ALIGN_LEFT, 0); if (ret == (size_t)-1 || ret >= sizeof(name)) - printf("%-15.15s", _(t->name)); + printf("%-15.15s", + _(fdisk_parttype_get_name(t))); else fputs(name, stdout); } @@ -433,13 +439,13 @@ void list_partition_types(struct fdisk_context *cxt) /* * Prints 1 column in format */ - struct fdisk_parttype *t; size_t i; - for (i = 0, t = types; t && i < ntypes; t++, i++) { - if (t->name) - printf("%3zu %-30s %s\n", i + 1, - t->name, t->typestr); + for (i = 0; i < ntypes; i++) { + const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, i); + printf("%3zu %-30s %s\n", i + 1, + fdisk_parttype_get_name(t), + fdisk_parttype_get_string(t)); } } putchar('\n'); @@ -492,7 +498,7 @@ void change_partition_type(struct fdisk_context *cxt) if (fdisk_set_partition_type(cxt, i, t) == 0) fdisk_sinfo(cxt, FDISK_INFO_SUCCESS, _("Changed type of partition '%s' to '%s'."), - old, t ? t->name : _("Unknown")); + old, t ? fdisk_parttype_get_name(t) : _("Unknown")); else fdisk_info(cxt, _("Type of partition %zu is unchanged: %s."), -- cgit v1.2.3-55-g7522