]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: move label identifier to label struct
authorKarel Zak <kzak@redhat.com>
Tue, 11 Dec 2012 17:30:03 +0000 (18:30 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 11 Mar 2013 11:47:29 +0000 (12:47 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
12 files changed:
fdisks/fdisk.c
fdisks/fdiskaixlabel.c
fdisks/fdiskbsdlabel.c
fdisks/fdiskdoslabel.c
fdisks/fdiskmaclabel.c
fdisks/fdisksgilabel.c
fdisks/fdisksunlabel.c
fdisks/gpt.c
libfdisk/src/context.c
libfdisk/src/fdiskP.h
libfdisk/src/label.c
libfdisk/src/libfdisk.h

index b22f685896f9caef2c5f333aa70e1980269e77f6..2f5029640243ce4409a886b2c3bb268c375df183 100644 (file)
@@ -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;
index 01a425362f45811fc7af93f04b5f28c1b902f931..d87405a1f8d097e6c1fc49dc69158335df54f473 100644 (file)
@@ -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;
index 4008e6bf52e777495e9f724e8c2310e5739d727d..e1955e32c2292bd8eeee8212ab45bb28623ef6de 100644 (file)
@@ -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);
index a6d6e2b35a895d373c424a4c7708f6dc67a80eee..fde9a5da9d294e9c31e43e1187599b01301ff5df 100644 (file)
@@ -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);
index fe989caabcf0d29693fb9568d1afe3fb149e43d5..4a0f4c86c806e0043f2d914aa32103e8a0b76171 100644 (file)
@@ -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;
index fa9e592ff86f327de18b3835d31b360974bfddab..4b330bdb9fdc043fce0105e67a24c5eb5fe72458 100644 (file)
@@ -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);
index 28942e4086ae69b4c1e5e624fe2505be0048b8d0..eb56cda8bc655b6821c812deeba2537081d3f44e 100644 (file)
@@ -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);
index 0dd8ebe13d66b4fab045e58fd3209a8d706be2c3..ed863299351d99a0ae8f40983d8de94976999eeb 100644 (file)
@@ -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);
index 4adc7cf302b3ee05d59edb6814e01dfb3a01ccd3..908889b4f120e4bd442f4b84c617de7cc63d71d1 100644 (file)
@@ -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;
index 00fc0cd5407d27f6c15879c7f6638e122f15c1de..104c67cb7bcf92fa7e88b0a8b9369c15e6de1313 100644 (file)
@@ -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);
index 57b7565914e6afc9c007454d8ac3ae3b7dfa5194..e0ec9022f3e7cd357d7d2f67bb3c41db5addce9b 100644 (file)
@@ -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;
 }
 
 /**
index 927587622165e2d384f3fc337e155d7cf410c539..7fcd3d74e3e8a9a34252b72c255e274eb46c0852 100644 (file)
@@ -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);