]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: (gpt) introduce driver independent partitions counters
authorKarel Zak <kzak@redhat.com>
Fri, 18 Jan 2013 10:43:56 +0000 (11:43 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 11 Mar 2013 11:47:29 +0000 (12:47 +0100)
 label->nparts_{max,cur} to later replace global variable partitions

Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
fdisks/gpt.c
libfdisk/src/fdiskP.h

index 2f5029640243ce4409a886b2c3bb268c375df183..2b8538d905d6abb2ce9e2bdf546293ab18e92549 100644 (file)
@@ -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);
 }
 
index 566e7ffa10deaf579311e3374b8ab04eabc47645..ba7d257d25631a6e553abbd7c6f7d88ecdcfb0e9 100644 (file)
@@ -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 "
index 104c67cb7bcf92fa7e88b0a8b9369c15e6de1313..39e76da309cae401135208de6f32b4dfacdb0d2e 100644 (file)
@@ -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 */