From 950edd1a71d76a9fe6bbc72bba4efcb26cf723b7 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 12 Jun 2013 17:09:28 +0200 Subject: [PATCH] fdisk: use libfdisk to ask for partition type Signed-off-by: Karel Zak --- fdisks/fdisk-ask.c | 27 +++++++++++++++++++++++++++ fdisks/fdisk.c | 30 ++---------------------------- fdisks/fdisk.h | 3 ++- fdisks/fdiskbsdlabel.c | 2 +- libfdisk/src/libfdisk.h | 2 ++ libfdisk/src/parttype.c | 15 +++++++++++++++ 6 files changed, 49 insertions(+), 30 deletions(-) diff --git a/fdisks/fdisk-ask.c b/fdisks/fdisk-ask.c index 0c4e200f14..a7e86e1c33 100644 --- a/fdisks/fdisk-ask.c +++ b/fdisks/fdisk-ask.c @@ -220,3 +220,30 @@ int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask, } return rc; } + + +struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt) +{ + const char *q; + + if (!cxt || !cxt->label || !cxt->label->nparttypes) + return NULL; + + q = fdisk_is_parttype_string(cxt) ? + _("Partition type (type L to list all types): ") : + _("Hex code (type L to list all codes): "); + do { + char buf[256]; + int rc = get_user_reply(cxt, q, buf, sizeof(buf)); + + if (rc) + break; + + if (buf[1] == '\0' && toupper(*buf) == 'L') + list_partition_types(cxt); + else if (*buf) + return fdisk_parse_parttype(cxt, buf); + } while (1); + + return NULL; +} diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index d9c1753294..4d804e4be0 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -280,33 +280,6 @@ char read_chars(struct fdisk_context *cxt, char *mesg) return *line_ptr; } -struct fdisk_parttype *read_partition_type(struct fdisk_context *cxt) -{ - if (!cxt || !cxt->label || !cxt->label->nparttypes) - return NULL; - - do { - size_t sz; - - if (cxt->label->parttypes[0].typestr) - read_chars(cxt, _("Partition type (type L to list all types): ")); - else - read_chars(cxt, _("Hex code (type L to list all codes): ")); - - sz = strlen(line_ptr); - if (!sz || line_ptr[sz - 1] != '\n' || sz == 1) - continue; - line_ptr[sz - 1] = '\0'; - - if (tolower(*line_ptr) == 'l') - list_partition_types(cxt); - else - return fdisk_parse_parttype(cxt, line_ptr); - } while (1); - - return NULL; -} - /* deprecated in favour of fdisk_ask_number() */ unsigned int read_int_with_suffix(struct fdisk_context *cxt, @@ -453,6 +426,7 @@ read_int(struct fdisk_context *cxt, return read_int_with_suffix(cxt, low, dflt, high, base, mesg, NULL); } + static void toggle_dos_compatibility_flag(struct fdisk_context *cxt) { struct fdisk_label *lb = fdisk_context_get_label(cxt, "dos"); @@ -501,7 +475,7 @@ static void change_partition_type(struct fdisk_context *cxt) printf(_("Partition %zu does not exist yet!\n"), i + 1); else do { - t = read_partition_type(cxt); + t = ask_partition_type(cxt); if (!t) continue; diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index 32d6a2afbb..1d15c8213b 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -74,7 +74,8 @@ extern char *line_ptr; extern void list_partition_types(struct fdisk_context *cxt); extern int read_line(struct fdisk_context *cxt, int *asked); extern char read_char(struct fdisk_context *cxt, char *mesg); -extern struct fdisk_parttype *read_partition_type(struct fdisk_context *cxt); + +extern struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt); extern void reread_partition_table(struct fdisk_context *cxt, int leave); extern struct partition *get_part_table(int); extern unsigned int read_int(struct fdisk_context *cxt, diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index 4a8bf48dbc..8f891b0da4 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -674,7 +674,7 @@ static void xbsd_change_fstype (struct fdisk_context *cxt) if (xbsd_get_part_index(cxt, xbsd_dlabel.d_npartitions, &i)) return; - t = read_partition_type(cxt); + t = ask_partition_type(cxt); if (t) { xbsd_dlabel.d_partitions[i].p_fstype = t->type; diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index cfcc62b812..bd3fc60dfc 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -108,6 +108,8 @@ extern struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, cons extern void fdisk_free_parttype(struct fdisk_parttype *type); extern size_t fdisk_get_nparttypes(struct fdisk_context *cxt); +extern int fdisk_is_parttype_string(struct fdisk_context *cxt); + /* label.c */ extern int fdisk_dev_has_disklabel(struct fdisk_context *cxt); diff --git a/libfdisk/src/parttype.c b/libfdisk/src/parttype.c index e78b355943..8a13d72117 100644 --- a/libfdisk/src/parttype.c +++ b/libfdisk/src/parttype.c @@ -165,4 +165,19 @@ void fdisk_free_parttype(struct fdisk_parttype *t) } } +/** + * fdisk_is_parttype_string: + * @cxt: context + * + * Returns: 1 if the current label uses strings as partition type + * identifiers (e.g. GPT UUIDS) or 0. + */ +int fdisk_is_parttype_string(struct fdisk_context *cxt) +{ + assert(cxt); + assert(cxt->label); + if (cxt->label->parttypes && cxt->label->parttypes[0].typestr) + return 1; + return 0; +} -- 2.47.2