From: Karel Zak Date: Tue, 11 Dec 2012 17:30:03 +0000 (+0100) Subject: libfdisk: move label identifier to label struct X-Git-Tag: v2.23-rc1~152 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=53b422ab169dc3be8e8eee4ccb5f8e67bff1a122;p=thirdparty%2Futil-linux.git libfdisk: move label identifier to label struct Signed-off-by: Karel Zak --- diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index b22f685896..2f50296402 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -208,11 +208,14 @@ is_garbage_table(void) { void print_menu(struct fdisk_context *cxt, enum menutype menu) { size_t i; + int id; puts(_("Command action")); + id = cxt && cxt->label ? cxt->label->id : FDISK_DISKLABEL_ANY; + for (i = 0; i < ARRAY_SIZE(menulist); i++) - if (menulist[i].label[menu] & cxt->disklabel) + if (menulist[i].label[menu] & id) printf(" %c %s\n", menulist[i].command, menulist[i].description); } @@ -1483,8 +1486,9 @@ static void command_prompt(struct fdisk_context *cxt) "disklabel mode.\n"), cxt->dev_path); bsd_command_prompt(cxt); + /* If we return we may want to make an empty DOS label? */ - cxt->disklabel = FDISK_DISKLABEL_DOS; + fdisk_context_switch_label(cxt, "dos"); } while (1) { @@ -1504,12 +1508,15 @@ static void command_prompt(struct fdisk_context *cxt) unknown_command(c); break; case 'b': + /* + * TODO: create child context for nexted partition tables + */ if (fdisk_is_disklabel(cxt, SGI)) sgi_set_bootfile(cxt); else if (fdisk_is_disklabel(cxt, DOS)) { - cxt->disklabel = FDISK_DISKLABEL_OSF; + fdisk_context_switch_label(cxt, "bsd"); bsd_command_prompt(cxt); - cxt->disklabel = FDISK_DISKLABEL_DOS; + fdisk_context_switch_label(cxt, "dos"); } else unknown_command(c); break; diff --git a/fdisks/fdiskaixlabel.c b/fdisks/fdiskaixlabel.c index 01a425362f..d87405a1f8 100644 --- a/fdisks/fdiskaixlabel.c +++ b/fdisks/fdiskaixlabel.c @@ -68,7 +68,6 @@ static int aix_probe_label( return 0; } other_endian = (aixlabel->magic == AIX_LABEL_MAGIC_SWAPPED); - cxt->disklabel = FDISK_DISKLABEL_AIX; partitions= 1016; volumes = 15; aix_info(); @@ -115,6 +114,7 @@ struct fdisk_label *fdisk_new_aix_label(struct fdisk_context *cxt) /* initialize generic part of the driver */ lb = (struct fdisk_label *) aix; lb->name = "aix"; + lb->id = FDISK_DISKLABEL_AIX; lb->op = &aix_operations; return lb; diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index 4008e6bf52..e1955e32c2 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -932,6 +932,7 @@ struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt) /* initialize generic part of the driver */ lb = (struct fdisk_label *) bsd; lb->name = "bsd"; + lb->id = FDISK_DISKLABEL_OSF; lb->op = &bsd_operations; lb->parttypes = xbsd_fstypes; lb->nparttypes = ARRAY_SIZE(xbsd_fstypes); diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index a6d6e2b35a..fde9a5da9d 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -178,7 +178,6 @@ void dos_init(struct fdisk_context *cxt) { int i; - cxt->disklabel = FDISK_DISKLABEL_DOS; partitions = 4; ext_index = 0; extended_offset = 0; @@ -990,6 +989,7 @@ struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt) /* initialize generic part of the driver */ lb = (struct fdisk_label *) dos; lb->name = "dos"; + lb->id = FDISK_DISKLABEL_DOS; lb->op = &dos_operations; lb->parttypes = dos_parttypes; lb->nparttypes = ARRAY_SIZE(dos_parttypes); diff --git a/fdisks/fdiskmaclabel.c b/fdisks/fdiskmaclabel.c index fe989caabc..4a0f4c86c8 100644 --- a/fdisks/fdiskmaclabel.c +++ b/fdisks/fdiskmaclabel.c @@ -84,7 +84,6 @@ mac_probe_label(struct fdisk_context *cxt, IS_MAC: other_endian = (maclabel->magic == MAC_LABEL_MAGIC_SWAPPED); // =? - cxt->disklabel = FDISK_DISKLABEL_MAC; partitions= 1016; // =? volumes = 15; // =? mac_info(); @@ -131,6 +130,7 @@ struct fdisk_label *fdisk_new_mac_label(struct fdisk_context *cxt) /* initialize generic part of the driver */ lb = (struct fdisk_label *) mac; lb->name = "mac"; + lb->id = FDISK_DISKLABEL_MAC; lb->op = &mac_operations; return lb; diff --git a/fdisks/fdisksgilabel.c b/fdisks/fdisksgilabel.c index fa9e592ff8..4b330bdb9f 100644 --- a/fdisks/fdisksgilabel.c +++ b/fdisks/fdisksgilabel.c @@ -165,7 +165,6 @@ sgi_probe_label(struct fdisk_context *cxt, fprintf(stderr, _("Detected sgi disklabel with wrong checksum.\n")); } - cxt->disklabel = FDISK_DISKLABEL_SGI; partitions= 16; volumes = 15; return 1; @@ -838,7 +837,6 @@ static int sgi_create_disklabel(struct fdisk_context *cxt, sgilabel->devparam.xylogics_writecont = SSWAP16(0); memset(&(sgilabel->directory), 0, sizeof(struct volume_directory)*15); memset(&(sgilabel->partitions), 0, sizeof(struct sgi_partition)*16); - cxt->disklabel = FDISK_DISKLABEL_SGI; partitions = 16; volumes = 15; sgi_set_entire(cxt); @@ -980,6 +978,7 @@ struct fdisk_label *fdisk_new_sgi_label(struct fdisk_context *cxt) /* initialize generic part of the driver */ lb = (struct fdisk_label *) sgi; lb->name = "sgi"; + lb->id = FDISK_DISKLABEL_SGI; lb->op = &sgi_operations; lb->parttypes = sgi_parttypes; lb->nparttypes = ARRAY_SIZE(sgi_parttypes); diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c index 28942e4086..eb56cda8bc 100644 --- a/fdisks/fdisksunlabel.c +++ b/fdisks/fdisksunlabel.c @@ -81,9 +81,8 @@ static void set_sun_partition(struct fdisk_context *cxt, print_partition_size(cxt, i + 1, start, stop, sysid); } -static void init(struct fdisk_context *cxt) +static void init(struct fdisk_context *cxt __attribute__((__unused__))) { - cxt->disklabel = FDISK_DISKLABEL_SUN; partitions = SUN_NUM_PARTITIONS; } @@ -738,6 +737,7 @@ struct fdisk_label *fdisk_new_sun_label(struct fdisk_context *cxt) /* initialize generic part of the driver */ lb = (struct fdisk_label *) sun; lb->name = "sun"; + lb->id = FDISK_DISKLABEL_SUN; lb->op = &sun_operations; lb->parttypes = sun_parttypes; lb->nparttypes = ARRAY_SIZE(sun_parttypes); diff --git a/fdisks/gpt.c b/fdisks/gpt.c index 0dd8ebe13d..ed86329935 100644 --- a/fdisks/gpt.c +++ b/fdisks/gpt.c @@ -1005,9 +1005,8 @@ done: /* * Initialize fdisk-specific variables - call once probing passes! */ -static void gpt_init(struct fdisk_context *cxt) +static void gpt_init(struct fdisk_context *cxt __attribute__((__unused__))) { - cxt->disklabel = FDISK_DISKLABEL_GPT; partitions = le32_to_cpu(pheader->npartition_entries); } @@ -1709,6 +1708,7 @@ struct fdisk_label *fdisk_new_gpt_label(struct fdisk_context *cxt) /* initialize generic part of the driver */ lb = (struct fdisk_label *) gpt; lb->name = "gpt"; + lb->id = FDISK_DISKLABEL_GPT; lb->op = &gpt_operations; lb->parttypes = gpt_parttypes; lb->nparttypes = ARRAY_SIZE(gpt_parttypes); diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c index 4adc7cf302..908889b4f1 100644 --- a/libfdisk/src/context.c +++ b/libfdisk/src/context.c @@ -49,6 +49,23 @@ struct fdisk_label *fdisk_context_get_label(struct fdisk_context *cxt, const cha return NULL; } +int __fdisk_context_switch_label(struct fdisk_context *cxt, + struct fdisk_label *lb) +{ + if (!lb) + return -EINVAL; + cxt->label = lb; + DBG(LABEL, dbgprint("--> switching context to %s!", lb->name)); + return 0; +} + +int fdisk_context_switch_label(struct fdisk_context *cxt, const char *name) +{ + return __fdisk_context_switch_label(cxt, + fdisk_context_get_label(cxt, name)); +} + + static void reset_context(struct fdisk_context *cxt) { size_t nlbs, i; diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index 00fc0cd540..104c67cb7b 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -161,6 +161,7 @@ struct fdisk_label_operations { struct fdisk_label { /* persistent information */ const char *name; + enum fdisk_labeltype id; /* FDISK_DISKLABEL_* */ struct fdisk_parttype *parttypes; size_t nparttypes; /* number of items in parttypes[] */ @@ -190,8 +191,6 @@ struct fdisk_context { unsigned long sector_size; /* logical size */ unsigned long alignment_offset; - enum fdisk_labeltype disklabel; /* current disklabel */ - /* alignment */ unsigned long grain; /* alignment unit */ sector_t first_lba; /* recommended begin of the first partition */ @@ -207,7 +206,9 @@ struct fdisk_context { * FIXME: use any enum rather than hardcoded number */ }; - +/* context.c */ +extern int __fdisk_context_switch_label(struct fdisk_context *cxt, + struct fdisk_label *lb); /* alignment.c */ extern sector_t fdisk_topology_get_first_lba(struct fdisk_context *cxt); diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index 57b7565914..e0ec9022f3 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -8,7 +8,6 @@ int fdisk_probe_labels(struct fdisk_context *cxt) { size_t i; - cxt->disklabel = FDISK_DISKLABEL_ANY; cxt->label = NULL; for (i = 0; i < cxt->nlabels; i++) { @@ -24,8 +23,7 @@ int fdisk_probe_labels(struct fdisk_context *cxt) continue; } - cxt->label = lb; - DBG(LABEL, dbgprint("detected a %s label", lb->name)); + __fdisk_context_switch_label(cxt, lb); return 0; } @@ -41,7 +39,7 @@ int fdisk_probe_labels(struct fdisk_context *cxt) */ int fdisk_dev_has_disklabel(struct fdisk_context *cxt) { - return cxt && cxt->disklabel != FDISK_DISKLABEL_ANY; + return cxt && cxt->label; } /** @@ -53,7 +51,7 @@ int fdisk_dev_has_disklabel(struct fdisk_context *cxt) */ int fdisk_dev_is_disklabel(struct fdisk_context *cxt, enum fdisk_labeltype l) { - return cxt && cxt->disklabel == l; + return cxt && cxt->label && cxt->label->id == l; } /** diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 9275876221..7fcd3d74e3 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -56,6 +56,9 @@ extern int fdisk_context_assign_device(struct fdisk_context *cxt, extern struct fdisk_label *fdisk_context_get_label(struct fdisk_context *cxt, const char *name); +extern int fdisk_context_switch_label(struct fdisk_context *cxt, + const char *name); + /* parttype.c */ extern struct fdisk_parttype *fdisk_get_parttype_from_code(struct fdisk_context *cxt, unsigned int code);