From: Karel Zak Date: Fri, 18 Jan 2013 10:43:56 +0000 (+0100) Subject: libfdisk: (gpt) introduce driver independent partitions counters X-Git-Tag: v2.23-rc1~149 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9fcd49d5517dffa1b98d29faff5090e88e9a68f0;p=thirdparty%2Futil-linux.git libfdisk: (gpt) introduce driver independent partitions counters label->nparts_{max,cur} to later replace global variable partitions Signed-off-by: Karel Zak --- diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 2f50296402..2b8538d905 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -1146,14 +1146,27 @@ static void new_partition(struct fdisk_context *cxt) { int partnum = 0; + assert(cxt); + assert(cxt->label); + if (warn_geometry(cxt)) return; if (fdisk_is_disklabel(cxt, SUN) || fdisk_is_disklabel(cxt, SGI) || - fdisk_is_disklabel(cxt, GPT)) - partnum = get_partition(cxt, 0, partitions); + fdisk_is_disklabel(cxt, GPT)) { + size_t dflt = 0; + + /* + * TODO: always use label->nparts_cur + 1 + * (currenly only few drivers maintain label->nparts_* counters) + */ + if (cxt->label->nparts_max) + dflt = cxt->label->nparts_cur + 1; + + partnum = get_partition_dflt(cxt, 0, partitions, dflt); + } fdisk_add_partition(cxt, partnum, NULL); } diff --git a/fdisks/gpt.c b/fdisks/gpt.c index 566e7ffa10..ba7d257d25 100644 --- a/fdisks/gpt.c +++ b/fdisks/gpt.c @@ -1024,17 +1024,6 @@ done: return totfound; } -/* - * Initialize fdisk-specific variables - call once probing passes! - */ -static void gpt_init(struct fdisk_context *cxt) -{ - struct fdisk_gpt_label *gpt = gpt_label(cxt); - - partitions = le32_to_cpu(gpt->pheader->npartition_entries); -} - - static int gpt_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) { int mbr_type; @@ -1069,7 +1058,10 @@ static int gpt_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) /* OK, probing passed, now initialize backup header and fdisk variables. */ gpt->bheader = gpt_read_header(cxt, last_lba(cxt), NULL); - gpt_init(cxt); + lb->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); + lb->nparts_cur = partitions_in_use(gpt->pheader, gpt->ents); + + partitions = lb->nparts_max; /* TODO: deprecated */ printf(_("\nWARNING: fdisk GPT support is currently new, and therefore " "in an experimental phase. Use at your own discretion.\n\n")); @@ -1428,8 +1420,8 @@ static int gpt_verify_disklabel(struct fdisk_context *cxt, struct fdisk_label *l /* Delete a single GPT partition, specified by partnum. */ static int gpt_delete_partition(struct fdisk_context *cxt, - struct fdisk_label *lb, - int partnum) + struct fdisk_label *lb, + int partnum) { struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; @@ -1446,6 +1438,7 @@ static int gpt_delete_partition(struct fdisk_context *cxt, else { gpt_recompute_crc(gpt->pheader, gpt->ents); gpt_recompute_crc(gpt->bheader, gpt->ents); + lb->nparts_cur--; } return 0; @@ -1610,8 +1603,10 @@ static int gpt_add_partition( if (gpt_create_new_partition(cxt, partnum, user_f, user_l, &uuid, ents) != 0) printf(_("Could not create partition %d\n"), partnum + 1); - else + else { printf(_("Created partition %d\n"), partnum + 1); + lb->nparts_cur++; + } return 0; } @@ -1619,8 +1614,7 @@ static int gpt_add_partition( /* * Create a new GPT disklabel - destroys any previous data. */ -static int gpt_create_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb) +static int gpt_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *lb) { int rc = 0; ssize_t entry_sz = 0; @@ -1661,7 +1655,10 @@ static int gpt_create_disklabel(struct fdisk_context *cxt, gpt_recompute_crc(gpt->pheader, gpt->ents); gpt_recompute_crc(gpt->bheader, gpt->ents); - gpt_init(cxt); + lb->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); + lb->nparts_cur = 0; + + partitions = lb->nparts_max; /* TODO: deprecated */ uid = &gpt->pheader->disk_guid; fprintf(stderr, ("Building a new GPT disklabel " diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index 104c67cb7b..39e76da309 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -159,14 +159,15 @@ struct fdisk_label_operations { * Generic label */ struct fdisk_label { - /* persistent information */ - const char *name; + const char *name; /* label name */ enum fdisk_labeltype id; /* FDISK_DISKLABEL_* */ - struct fdisk_parttype *parttypes; + struct fdisk_parttype *parttypes; /* supported partitions types */ size_t nparttypes; /* number of items in parttypes[] */ - const struct fdisk_label_operations *op; + size_t nparts_max; /* maximal number of partitions */ + size_t nparts_cur; /* number of currently used partitions */ + const struct fdisk_label_operations *op; }; /* label allocators */