]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: API: add new partition to label operations
authorDavidlohr Bueso <dave@gnu.org>
Tue, 24 Jul 2012 07:34:22 +0000 (09:34 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 24 Jul 2012 07:34:22 +0000 (09:34 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Davidlohr Bueso <dave@gnu.org>
12 files changed:
fdisks/fdisk.c
fdisks/fdisk.h
fdisks/fdiskaixlabel.c
fdisks/fdiskbsdlabel.c
fdisks/fdiskdoslabel.c
fdisks/fdiskdoslabel.h
fdisks/fdiskmaclabel.c
fdisks/fdisksgilabel.c
fdisks/fdisksgilabel.h
fdisks/fdisksunlabel.c
fdisks/fdisksunlabel.h
fdisks/utils.c

index f25f2aec05962bf64ad661411250ea75e4ae1d30..355e313cbfbbb989bfb2d25213a1d07c5a82c67c 100644 (file)
@@ -1385,39 +1385,19 @@ void print_partition_size(struct fdisk_context *cxt,
 
 static void new_partition(struct fdisk_context *cxt)
 {
-       if (warn_geometry(cxt))
-               return;
-
-       if (disklabel == SUN_LABEL) {
-               add_sun_partition(cxt, get_partition(cxt, 0, partitions), LINUX_NATIVE);
-               return;
-       }
+       int partnum = 0;
 
-       if (disklabel == SGI_LABEL) {
-               sgi_add_partition(cxt, get_partition(cxt, 0, partitions), LINUX_NATIVE);
-               return;
-       }
-
-       if (disklabel == AIX_LABEL) {
-               printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
-                        "\n\tIf you want to add DOS-type partitions, create"
-                        "\n\ta new empty DOS partition table first. (Use o.)"
-                        "\n\tWARNING: "
-                        "This will destroy the present disk contents.\n"));
+       if (warn_geometry(cxt))
                return;
-       }
 
-       if (disklabel == MAC_LABEL) {
-               printf(_("\tSorry - this fdisk cannot handle Mac disk labels."
-                        "\n\tIf you want to add DOS-type partitions, create"
-                        "\n\ta new empty DOS partition table first. (Use o.)"
-                        "\n\tWARNING: "
-                        "This will destroy the present disk contents.\n"));
-                return;
-       }
+       if (disklabel == SUN_LABEL || disklabel == SGI_LABEL)
+               partnum = get_partition(cxt, 0, partitions);
 
-       /* default to DOS/BSD */
-       dos_new_partition(cxt);
+       /*
+        * Use default LINUX_NATIVE partition type, DOS labels
+        * may override this internally.
+        */
+       fdisk_add_partition(cxt, partnum, LINUX_NATIVE);
 }
 
 static void write_table(struct fdisk_context *cxt)
index 9ce2c25b06e727b0b4220afffc37752eeaa27f3b..975ac4d9a357fb6c2c8b1cccf5a5395eb331b0a4 100644 (file)
@@ -139,6 +139,8 @@ struct fdisk_label {
        int (*probe)(struct fdisk_context *cxt);
        /* write in-memory changes to disk */
        int (*write)(struct fdisk_context *cxt);
+       /* new partition */
+       void (*part_add)(struct fdisk_context *cxt, int partnum, int parttype);
        /* delete partition */
        void (*part_delete)(struct fdisk_context *cxt, int partnum);
 };
@@ -165,6 +167,7 @@ extern int fdisk_context_set_user_geometry(struct fdisk_context *cxt,
                            unsigned int sectors);
 extern int fdisk_create_default_disklabel(struct fdisk_context *cxt);
 extern int fdisk_delete_partition(struct fdisk_context *cxt, int partnum);
+extern int fdisk_add_partition(struct fdisk_context *cxt, int partnum, int parttype);
 extern int fdisk_write_disklabel(struct fdisk_context *cxt);
 
 /* prototypes for fdisk.c */
index c8cb660e0c4fec2c9c45d9ef4adcd94be765d209..c01af3dbc0bcfdbb7a031d3d373c3baf90d70cef 100644 (file)
@@ -65,10 +65,20 @@ static int aix_probe_label(struct fdisk_context *cxt)
     return 1;
 }
 
+static void aix_add_partition(struct fdisk_context *cxt, int partnum, int parttype)
+{
+       printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
+                "\n\tIf you want to add DOS-type partitions, create"
+                "\n\ta new empty DOS partition table first. (Use o.)"
+                "\n\tWARNING: "
+                "This will destroy the present disk contents.\n"));
+}
+
 const struct fdisk_label aix_label =
 {
        .name = "aix",
        .probe = aix_probe_label,
        .write = NULL,
+       .part_add = aix_add_partition,
        .part_delete = NULL,
 };
index 50bd840d78aa00d925f8a2fe7b96510863f97227..7e8f2ba12017ddb761ada2f32a92af753b7a08cc 100644 (file)
@@ -62,7 +62,6 @@
 #include "fdiskbsdlabel.h"
 
 static void xbsd_delete_part (struct fdisk_context *cxt, int partnum);
-static void xbsd_new_part (struct fdisk_context *cxt);
 static int xbsd_create_disklabel (struct fdisk_context *cxt);
 static void xbsd_edit_disklabel (void);
 static void xbsd_write_bootstrap (struct fdisk_context *cxt);
@@ -154,6 +153,43 @@ static int xbsd_write_disklabel (struct fdisk_context *cxt)
        return 0;
 }
 
+static void xbsd_add_part (struct fdisk_context *cxt, int partnum, int parttype)
+{
+       unsigned int begin, end;
+       char mesg[256];
+       int i;
+
+       if (!xbsd_check_new_partition (&i))
+               return;
+
+#if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)
+       begin = get_start_sect(xbsd_part);
+       end = begin + get_nr_sects(xbsd_part) - 1;
+#else
+       begin = 0;
+       end = xbsd_dlabel.d_secperunit - 1;
+#endif
+
+       snprintf (mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
+       begin = read_int (cxt, bsd_cround (begin), bsd_cround (begin), bsd_cround (end),
+                         0, mesg);
+
+       if (display_in_cyl_units)
+               begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
+
+       snprintf (mesg, sizeof(mesg), _("Last %s or +size or +sizeM or +sizeK"),
+                 str_units(SINGULAR));
+       end = read_int (cxt, bsd_cround (begin), bsd_cround (end), bsd_cround (end),
+                       bsd_cround (begin), mesg);
+
+       if (display_in_cyl_units)
+               end = end * xbsd_dlabel.d_secpercyl - 1;
+
+       xbsd_dlabel.d_partitions[i].p_size   = end - begin + 1;
+       xbsd_dlabel.d_partitions[i].p_offset = begin;
+       xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
+}
+
 void
 bsd_command_prompt (struct fdisk_context *cxt)
 {
@@ -210,8 +246,8 @@ bsd_command_prompt (struct fdisk_context *cxt)
        xbsd_list_types ();
        break;
       case 'n':
-       xbsd_new_part (cxt);
-       break;
+             xbsd_add_part (cxt, 0, 0);
+             break;
       case 'p':
              xbsd_print_disklabel (cxt, 0);
        break;
@@ -254,44 +290,6 @@ static void xbsd_delete_part(struct fdisk_context *cxt, int partnum)
                        xbsd_dlabel.d_npartitions--;
 }
 
-static void
-xbsd_new_part (struct fdisk_context *cxt)
-{
-  unsigned int begin, end;
-  char mesg[256];
-  int i;
-
-  if (!xbsd_check_new_partition (&i))
-    return;
-
-#if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)
-  begin = get_start_sect(xbsd_part);
-  end = begin + get_nr_sects(xbsd_part) - 1;
-#else
-  begin = 0;
-  end = xbsd_dlabel.d_secperunit - 1;
-#endif
-
-  snprintf (mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
-  begin = read_int (cxt, bsd_cround (begin), bsd_cround (begin), bsd_cround (end),
-                   0, mesg);
-
-  if (display_in_cyl_units)
-    begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
-
-  snprintf (mesg, sizeof(mesg), _("Last %s or +size or +sizeM or +sizeK"),
-          str_units(SINGULAR));
-  end = read_int (cxt, bsd_cround (begin), bsd_cround (end), bsd_cround (end),
-                 bsd_cround (begin), mesg);
-
-  if (display_in_cyl_units)
-    end = end * xbsd_dlabel.d_secpercyl - 1;
-
-  xbsd_dlabel.d_partitions[i].p_size   = end - begin + 1;
-  xbsd_dlabel.d_partitions[i].p_offset = begin;
-  xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
-}
-
 void
 xbsd_print_disklabel (struct fdisk_context *cxt, int show_all) {
   struct xbsd_disklabel *lp = &xbsd_dlabel;
@@ -848,5 +846,6 @@ const struct fdisk_label bsd_label =
        .name = "bsd",
        .probe = osf_probe_label,
        .write = xbsd_write_disklabel,
+       .part_add= xbsd_add_part,
        .part_delete = xbsd_delete_part,
 };
index 398e5e0402a832add3dea31875307731d88a9f46..fde31a7a816b20e4fad52ae5e28cb3ae1cc77037 100644 (file)
@@ -481,7 +481,7 @@ static sector_t align_lba_in_range(struct fdisk_context *cxt,
        return lba;
 }
 
-void dos_add_partition(struct fdisk_context *cxt, int n, int sys)
+static void add_partition(struct fdisk_context *cxt, int n, int sys)
 {
        char mesg[256];         /* 48 does not suffice in Japanese */
        int i, read = 0;
@@ -640,17 +640,22 @@ static void add_logical(struct fdisk_context *cxt)
                partitions++;
        }
        printf(_("Adding logical partition %d\n"), partitions);
-       dos_add_partition(cxt, partitions - 1, LINUX_NATIVE);
+       add_partition(cxt, partitions - 1, LINUX_NATIVE);
 }
 
 /*
  * Ask the user for new partition type information (logical, extended).
- * This function calls the actual partition adding logic - dos_add_partition.
+ * This function calls the actual partition adding logic - add_partition.
+ *
+ * API callback.
  */
-void dos_new_partition(struct fdisk_context *cxt)
+static void dos_add_partition(struct fdisk_context *cxt, int partnum, int parttype)
 {
        int i, free_primary = 0;
 
+       /* default */
+       parttype = LINUX_NATIVE;
+
        for (i = 0; i < 4; i++)
                free_primary += !ptes[i].part_table->sys_ind;
 
@@ -669,7 +674,7 @@ void dos_new_partition(struct fdisk_context *cxt)
        } else if (partitions >= MAXIMUM_PARTS) {
                printf(_("All logical partitions are in use\n"));
                printf(_("Adding a primary partition\n"));
-               dos_add_partition(cxt, get_partition(cxt, 0, 4), LINUX_NATIVE);
+               add_partition(cxt, get_partition(cxt, 0, 4), parttype);
        } else {
                char c, dflt, line[LINE_LENGTH];
 
@@ -691,7 +696,7 @@ void dos_new_partition(struct fdisk_context *cxt)
                if (c == 'p') {
                        int i = get_nonexisting_partition(cxt, 0, 4);
                        if (i >= 0)
-                               dos_add_partition(cxt, i, LINUX_NATIVE);
+                               add_partition(cxt, i, parttype);
                        return;
                } else if (c == 'l' && extended_offset) {
                        add_logical(cxt);
@@ -699,7 +704,7 @@ void dos_new_partition(struct fdisk_context *cxt)
                } else if (c == 'e' && !extended_offset) {
                        int i = get_nonexisting_partition(cxt, 0, 4);
                        if (i >= 0)
-                               dos_add_partition(cxt, i, EXTENDED);
+                               add_partition(cxt, i, EXTENDED);
                        return;
                } else
                        printf(_("Invalid partition type `%c'\n"), c);
@@ -752,5 +757,6 @@ const struct fdisk_label dos_label =
        .name = "dos",
        .probe = dos_probe_label,
        .write = dos_write_disklabel,
+       .part_add = dos_add_partition,
        .part_delete = dos_delete_partition,
 };
index d7361a806cb4f83be37ee240774d8d2d28b26a80..77009dca094d25fe8cd02db0bb8ebc7a7f519e8b 100644 (file)
@@ -42,8 +42,6 @@ extern void dos_print_mbr_id(struct fdisk_context *cxt);
 extern void dos_set_mbr_id(struct fdisk_context *cxt);
 extern int is_dos_partition(int t);
 extern void dos_init(struct fdisk_context *cxt);
-extern void dos_add_partition(struct fdisk_context *cxt, int n, int sys);
-extern void dos_new_partition(struct fdisk_context *cxt);
 
 extern int mbr_is_valid_magic(unsigned char *b);
 
index 4e80d395bdb38b8493cfa2c3f7d80327f4cede0f..d69a92098453f9988c4b5099cb2d3f6bc4ec7380 100644 (file)
@@ -80,10 +80,20 @@ IS_MAC:
     return 1;
 }
 
+static void mac_add_partition(struct fdisk_context *cxt, int partnum, int parttype)
+{
+       printf(_("\tSorry - this fdisk cannot handle Mac disk labels."
+                "\n\tIf you want to add DOS-type partitions, create"
+                "\n\ta new empty DOS partition table first. (Use o.)"
+                "\n\tWARNING: "
+                "This will destroy the present disk contents.\n"));
+}
+
 const struct fdisk_label mac_label =
 {
        .name = "mac",
        .probe = mac_probe_label,
        .write = NULL,
+       .part_add = mac_add_partition,
        .part_delete = NULL,
 };
index 836b9d4a7ad22c78034caaa98350c743e3727578..62fae99dfe3a70057284dece71408dcbc5a7f535 100644 (file)
@@ -657,8 +657,7 @@ static void sgi_delete_partition(struct fdisk_context *cxt, int partnum)
        sgi_set_partition(cxt, partnum, 0, 0, 0);
 }
 
-void
-sgi_add_partition(struct fdisk_context *cxt, int n, int sys)
+static void sgi_add_partition(struct fdisk_context *cxt, int n, int sys)
 {
        char mesg[256];
        unsigned int first=0, last=0;
@@ -903,5 +902,6 @@ const struct fdisk_label sgi_label =
        .name = "sgi",
        .probe = sgi_probe_label,
        .write = sgi_write_disklabel,
+       .part_add = sgi_add_partition,
        .part_delete = sgi_delete_partition,
 };
index 412ec7422c8159a7d445bc06f1d5ae5289cea746..83cf55effd0f34607bd77016df431e6163dfccda 100644 (file)
@@ -116,7 +116,6 @@ 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    sgi_add_partition( struct fdisk_context *cxt, int n, int sys );
 extern void    create_sgilabel( struct fdisk_context *cxt );
 extern void    create_sgiinfo(struct fdisk_context *cxt);
 extern int     verify_sgi(struct fdisk_context *cxt, int verbose );
index 3b0b757adb5a2d48adef4b9cfb42c898118ef7a6..9743725758f262a6e4a73f5f9205c81bb59480a6 100644 (file)
@@ -362,7 +362,7 @@ void verify_sun(struct fdisk_context *cxt)
         printf(_("Unused gap - sectors %d-%d\n"), start, stop);
 }
 
-void add_sun_partition(struct fdisk_context *cxt, int n, int sys)
+static void sun_add_partition(struct fdisk_context *cxt, int n, int sys)
 {
        uint32_t starts[SUN_NUM_PARTITIONS], lens[SUN_NUM_PARTITIONS];
        struct sun_partition *part = &sunlabel->partitions[n];
@@ -652,5 +652,6 @@ const struct fdisk_label sun_label =
        .name = "sun",
        .probe = sun_probe_label,
        .write = sun_write_disklabel,
+       .part_add = sun_add_partition,
        .part_delete = sun_delete_partition,
 };
index d33a5aa6f81932e99b5f6a1fa96553e63fdb4931..47c7cf86986958de32cf659073e93ba1fc1c19d8 100644 (file)
@@ -81,7 +81,6 @@ extern int create_sunlabel(struct fdisk_context *cxt);
 extern int sun_change_sysid(struct fdisk_context *cxt, int i, uint16_t sys);
 extern void sun_list_table(struct fdisk_context *cxt, int xtra);
 extern void verify_sun(struct fdisk_context *cxt);
-extern void add_sun_partition(struct fdisk_context *cxt, int n, int sys);
 extern void sun_set_alt_cyl(struct fdisk_context *cxt);
 extern void sun_set_ncyl(struct fdisk_context *cxt, int cyl);
 extern void sun_set_xcyl(struct fdisk_context *cxt);
index 08f0fe6bd6c95e7d3c451b1d1cd90f2c4312ea2f..4ab9f630ac62c360429cd998c9d34312fe089f68 100644 (file)
@@ -63,6 +63,25 @@ int fdisk_write_disklabel(struct fdisk_context *cxt)
        return cxt->label->write(cxt);
 }
 
+/*
+ * fdisk_add_partition:
+ * @cxt: fdisk context
+ * @partnum: partition number to create
+ * @parttype: partition type to create
+ *
+ * Creates a new partition, with number @partnum and type @parttype.
+ *
+ * Returns 0.
+ */
+int fdisk_add_partition(struct fdisk_context *cxt, int partnum, int parttype)
+{
+       if (!cxt)
+               return -EINVAL;
+
+       cxt->label->part_add(cxt, partnum, parttype);
+       return 0;
+}
+
 /**
  * fdisk_delete_partition:
  * @cxt: fdisk context