From: Karel Zak Date: Tue, 25 Sep 2012 10:12:28 +0000 (+0200) Subject: fdisk: add fdisk_get_partition_type() X-Git-Tag: v2.23-rc1~672 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=010186f2a29edc43b845e8de8f37cf49ad25666b;p=thirdparty%2Futil-linux.git fdisk: add fdisk_get_partition_type() Signed-off-by: Karel Zak --- diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 12ddf54b53..151790262b 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -220,14 +220,6 @@ void print_menu(enum menutype menu) printf(" %c %s\n", menulist[i].command, menulist[i].description); } -static int -get_sysid(struct fdisk_context *cxt, int i) { - return ( - disklabel == SUN_LABEL ? sun_get_sysid(cxt, i) : - disklabel == SGI_LABEL ? sgi_get_sysid(cxt, i) : - ptes[i].part_table->sys_ind); -} - void list_partition_types(struct fdisk_context *cxt) { struct fdisk_parttype *types; @@ -858,21 +850,15 @@ static void change_sysid(struct fdisk_context *cxt) { int i; struct fdisk_parttype *t, *org_t; - struct partition *p; i = get_existing_partition(cxt, 0, partitions); - if (i == -1) return; - p = ptes[i].part_table; - - /* TODO: add get_partition_type(xt, partn) to API */ - org_t = t = fdisk_get_parttype_from_code(cxt, get_sysid(cxt, i)); - /* if changing types T to 0 is allowed, then - the reverse change must be allowed, too */ - if (!t && disklabel != SGI_LABEL && disklabel != SUN_LABEL && !get_nr_sects(p)) + org_t = t = fdisk_get_partition_type(cxt, i); + if (!t) printf(_("Partition %d does not exist yet!\n"), i + 1); + else while (1) { t = read_partition_type(cxt); @@ -888,7 +874,7 @@ static void change_sysid(struct fdisk_context *cxt) continue; if (disklabel != SGI_LABEL && disklabel != SUN_LABEL) { - if (IS_EXTENDED (t->type) != IS_EXTENDED (p->sys_ind)) { + if (IS_EXTENDED (t->type) != IS_EXTENDED (t->type)) { printf(_("You cannot change a partition into" " an extended one or vice versa\n" "Delete it first.\n")); @@ -918,7 +904,7 @@ static void change_sysid(struct fdisk_context *cxt) if (disklabel == SGI_LABEL) { ptes[i].changed = sgi_change_sysid(cxt, i, t->type); } else { - p->sys_ind = t->type; + ptes[i].part_table->sys_ind = t->type; ptes[i].changed = 1; } diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index 03fb2c43a0..879f3d640d 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -174,6 +174,9 @@ struct fdisk_label { void (*part_add)(struct fdisk_context *cxt, int partnum, int parttype); /* delete partition */ void (*part_delete)(struct fdisk_context *cxt, int partnum); + /* get partition type */ + struct fdisk_parttype *(*part_get_type)(struct fdisk_context *cxt, int partnum); + }; /* @@ -201,6 +204,7 @@ extern int fdisk_add_partition(struct fdisk_context *cxt, int partnum, int partt extern int fdisk_write_disklabel(struct fdisk_context *cxt); extern int fdisk_verify_disklabel(struct fdisk_context *cxt); extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name); +extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum); extern size_t fdisk_get_nparttypes(struct fdisk_context *cxt); extern struct fdisk_parttype *fdisk_get_parttype_from_code(struct fdisk_context *cxt, @@ -209,6 +213,7 @@ extern struct fdisk_parttype *fdisk_get_parttype_from_string(struct fdisk_contex const char *str); extern struct fdisk_parttype *fdisk_parse_parttype(struct fdisk_context *cxt, const char *str); +extern struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, const char *typestr); extern void fdisk_free_parttype(struct fdisk_parttype *type); /* prototypes for fdisk.c */ diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index 81fb682a1b..3a23c02f09 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -843,6 +843,19 @@ alpha_bootblock_checksum (char *boot) } #endif /* __alpha__ */ +static struct fdisk_parttype *xbsd_get_parttype(struct fdisk_context *cxt, int n) +{ + struct fdisk_parttype *t; + + if (n >= xbsd_dlabel.d_npartitions) + return NULL; + + t = fdisk_get_parttype_from_code(cxt, xbsd_dlabel.d_partitions[n].p_fstype); + if (!t) + t = fdisk_new_unknown_parttype(xbsd_dlabel.d_partitions[n].p_fstype, NULL); + return t; +} + const struct fdisk_label bsd_label = { .name = "bsd", @@ -855,4 +868,5 @@ const struct fdisk_label bsd_label = .create = xbsd_create_disklabel, .part_add = xbsd_add_part, .part_delete = xbsd_delete_part, + .part_get_type = xbsd_get_parttype, }; diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index e32e30c212..3b2f9d5bfd 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -825,6 +825,21 @@ done: return rc; } +static struct fdisk_parttype *dos_get_parttype(struct fdisk_context *cxt, int partnum) +{ + struct fdisk_parttype *t; + struct partition *p; + + if (partnum >= partitions) + return NULL; + + p = ptes[partnum].part_table; + t = fdisk_get_parttype_from_code(cxt, p->sys_ind); + if (!t) + t = fdisk_new_unknown_parttype(p->sys_ind, NULL); + return t; +} + const struct fdisk_label dos_label = { .name = "dos", @@ -837,4 +852,5 @@ const struct fdisk_label dos_label = .create = dos_create_disklabel, .part_add = dos_add_partition, .part_delete = dos_delete_partition, + .part_get_type = dos_get_parttype, }; diff --git a/fdisks/fdisksgilabel.c b/fdisks/fdisksgilabel.c index 04bccbb3b5..a181a77091 100644 --- a/fdisks/fdisksgilabel.c +++ b/fdisks/fdisksgilabel.c @@ -194,8 +194,7 @@ sgi_list_table(struct fdisk_context *cxt, int xtra) { if (sgi_get_num_sectors(cxt, i) || debug) { uint32_t start = sgi_get_start_sector(cxt, i); uint32_t len = sgi_get_num_sectors(cxt, i); - struct fdisk_parttype *t = - fdisk_get_parttype_from_code(cxt, sgi_get_sysid(cxt, i)); + struct fdisk_parttype *t = fdisk_get_partition_type(cxt, i); kpi++; /* only count nonempty partitions */ printf( @@ -207,8 +206,10 @@ sgi_list_table(struct fdisk_context *cxt, int xtra) { /* start */ (long) scround(start), /* end */ (long) scround(start+len)-1, /* no odd flag on end */ (long) len, -/* type id */ t ? t->type : sgi_get_sysid(cxt, i), -/* type name */ t ? t->name : _("Unknown")); +/* type id */ t->type, +/* type name */ t->name); + + fdisk_free_parttype(t); } } printf(_("----- Bootinfo -----\nBootfile: %s\n" @@ -236,7 +237,7 @@ sgi_get_num_sectors(struct fdisk_context *cxt, int i) { return SSWAP32(sgilabel->partitions[i].num_sectors); } -int +static int sgi_get_sysid(struct fdisk_context *cxt, int i) { return SSWAP32(sgilabel->partitions[i].id); @@ -896,6 +897,19 @@ static sgiinfo *fill_sgiinfo(void) return info; } +static struct fdisk_parttype *sgi_get_parttype(struct fdisk_context *cxt, int n) +{ + struct fdisk_parttype *t; + + if (n >= partitions) + return NULL; + + t = fdisk_get_parttype_from_code(cxt, sgi_get_sysid(cxt, n)); + if (!t) + t = fdisk_new_unknown_parttype(sgi_get_sysid(cxt, n), NULL); + return t; +} + const struct fdisk_label sgi_label = { .name = "sgi", @@ -908,4 +922,5 @@ const struct fdisk_label sgi_label = .create = sgi_create_disklabel, .part_add = sgi_add_partition, .part_delete = sgi_delete_partition, + .part_get_type = sgi_get_parttype, }; diff --git a/fdisks/fdisksgilabel.h b/fdisks/fdisksgilabel.h index 9155a2400d..ce53ec151e 100644 --- a/fdisks/fdisksgilabel.h +++ b/fdisks/fdisksgilabel.h @@ -114,7 +114,6 @@ extern void sgi_list_table( struct fdisk_context *cxt, int xtra ); extern int sgi_change_sysid(struct fdisk_context *cxt, int i, int sys); extern unsigned int sgi_get_start_sector(struct fdisk_context *cxt, int i ); extern unsigned int sgi_get_num_sectors(struct fdisk_context *cxt, int i ); -extern int sgi_get_sysid(struct fdisk_context *cxt, int i ); extern void create_sgiinfo(struct fdisk_context *cxt); extern void sgi_set_ilfact( void ); extern void sgi_set_rspeed( void ); diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c index acc2808424..521c43a174 100644 --- a/fdisks/fdisksunlabel.c +++ b/fdisks/fdisksunlabel.c @@ -572,7 +572,7 @@ void sun_list_table(struct fdisk_context *cxt, int xtra) if (part->num_sectors) { uint32_t start = SSWAP32(part->start_cylinder) * cxt->geom.heads * cxt->geom.sectors; uint32_t len = SSWAP32(part->num_sectors); - struct fdisk_parttype *t = fdisk_get_parttype_from_code(cxt, SSWAP16(tag->tag)); + struct fdisk_parttype *t = fdisk_get_partition_type(cxt, i); printf( "%s %c%c %9lu %9lu %9lu%c %2x %s\n", @@ -582,8 +582,10 @@ void sun_list_table(struct fdisk_context *cxt, int xtra) /* start */ (unsigned long) scround(start), /* end */ (unsigned long) scround(start+len), /* odd flag on end */ (unsigned long) len / 2, len & 1 ? '+' : ' ', -/* type id */ SSWAP16(tag->tag), -/* type name */ t ? t->name : _("Unknown")); +/* type id */ t->type, +/* type name */ t->name); + + fdisk_free_parttype(t); } } } @@ -644,9 +646,17 @@ static int sun_write_disklabel(struct fdisk_context *cxt) return 0; } -int sun_get_sysid(struct fdisk_context *cxt, int i) +static struct fdisk_parttype *sun_get_parttype(struct fdisk_context *cxt, int n) { - return SSWAP16(sunlabel->part_tags[i].tag); + struct fdisk_parttype *t; + + if (n >= partitions) + return NULL; + + t = fdisk_get_parttype_from_code(cxt, SSWAP16(sunlabel->part_tags[n].tag)); + if (!t) + t = fdisk_new_unknown_parttype(SSWAP16(sunlabel->part_tags[n].tag), NULL); + return t; } const struct fdisk_label sun_label = @@ -661,4 +671,5 @@ const struct fdisk_label sun_label = .create = sun_create_disklabel, .part_add = sun_add_partition, .part_delete = sun_delete_partition, + .part_get_type = sun_get_parttype, }; diff --git a/fdisks/fdisksunlabel.h b/fdisks/fdisksunlabel.h index 31cb7e89d9..41945fba31 100644 --- a/fdisks/fdisksunlabel.h +++ b/fdisks/fdisksunlabel.h @@ -85,6 +85,5 @@ extern void sun_set_ilfact(struct fdisk_context *cxt); extern void sun_set_rspeed(struct fdisk_context *cxt); extern void sun_set_pcylcount(struct fdisk_context *cxt); extern void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask); -extern int sun_get_sysid(struct fdisk_context *cxt, int i); #endif /* FDISK_SUN_LABEL_H */ diff --git a/fdisks/utils.c b/fdisks/utils.c index 8e26375dcb..fd69fb72ca 100644 --- a/fdisks/utils.c +++ b/fdisks/utils.c @@ -568,7 +568,7 @@ struct fdisk_parttype *fdisk_get_parttype_from_string( return NULL; } -static struct fdisk_parttype *mk_unknown_partype(unsigned int type, const char *typestr) +struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, const char *typestr) { struct fdisk_parttype *t; @@ -638,7 +638,7 @@ struct fdisk_parttype *fdisk_parse_parttype( return &types[i]; } - return mk_unknown_partype(code, typestr); + return fdisk_new_unknown_parttype(code, typestr); } /* @@ -653,3 +653,18 @@ void fdisk_free_parttype(struct fdisk_parttype *t) free(t); } } + +/** + * fdisk_get_partition_type: + * @cxt: fdisk context + * @partnum: partition number + * + * Returns partition type + */ +struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum) +{ + if (!cxt || !cxt->label || !cxt->label->part_get_type) + return NULL; + + return cxt->label->part_get_type(cxt, partnum); +}