]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: add fdisk_get_partition_type()
authorKarel Zak <kzak@redhat.com>
Tue, 25 Sep 2012 10:12:28 +0000 (12:12 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 27 Sep 2012 11:38:13 +0000 (13:38 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
fdisks/fdisk.h
fdisks/fdiskbsdlabel.c
fdisks/fdiskdoslabel.c
fdisks/fdisksgilabel.c
fdisks/fdisksgilabel.h
fdisks/fdisksunlabel.c
fdisks/fdisksunlabel.h
fdisks/utils.c

index 12ddf54b53e9e4db9702ba3353123bb347b0f879..151790262b50791be683f0159ea615b47fb600f6 100644 (file)
@@ -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;
                        }
 
index 03fb2c43a0a958aeecdec40bbf1fb9341c09ffaa..879f3d640df6ce691835fb9725d5e3291748eb17 100644 (file)
@@ -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 */
index 81fb682a1b79079452a64bb6ffa2d0f262b3ae3d..3a23c02f099cd6efa688a777959744af0684a892 100644 (file)
@@ -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,
 };
index e32e30c2121365e1a56ac0afaecb2f8590b59a59..3b2f9d5bfdf0860ecf441dd1350c1391a373625a 100644 (file)
@@ -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,
 };
index 04bccbb3b5800991f6021c0e9ecf6ff53902d73f..a181a77091f4e89964a18331f61fedba9b282338 100644 (file)
@@ -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,
 };
index 9155a2400d6cf9ee6644b0d5aade38df9dafd5ca..ce53ec151e447027ea64b79d2f81397e6071cb36 100644 (file)
@@ -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 );
index acc28084243192fe2bb855d0e5f600b54a35043d..521c43a174d2da1abf8cf3b4eac82b35419259ef 100644 (file)
@@ -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,
 };
index 31cb7e89d92d25d2422799a075a11a657a878e75..41945fba315a915fc36052730220fc6f96f9099a 100644 (file)
@@ -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 */
index 8e26375dcbafd490df8e718d20f54aa530fc8857..fd69fb72ca2566cf2672eeb188119b926de3e2a1 100644 (file)
@@ -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);
+}