From 0f639e54dfca37879545fc2e0efa8a509ca87b72 Mon Sep 17 00:00:00 2001 From: Davidlohr Bueso Date: Tue, 24 Jul 2012 09:34:22 +0200 Subject: [PATCH] fdisk: API: add new partition to label operations Signed-off-by: Karel Zak Signed-off-by: Davidlohr Bueso --- fdisks/fdisk.c | 38 +++++--------------- fdisks/fdisk.h | 3 ++ fdisks/fdiskaixlabel.c | 10 ++++++ fdisks/fdiskbsdlabel.c | 81 +++++++++++++++++++++--------------------- fdisks/fdiskdoslabel.c | 20 +++++++---- fdisks/fdiskdoslabel.h | 2 -- fdisks/fdiskmaclabel.c | 10 ++++++ fdisks/fdisksgilabel.c | 4 +-- fdisks/fdisksgilabel.h | 1 - fdisks/fdisksunlabel.c | 3 +- fdisks/fdisksunlabel.h | 1 - fdisks/utils.c | 19 ++++++++++ 12 files changed, 108 insertions(+), 84 deletions(-) diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index f25f2aec0..355e313cb 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -1385,39 +1385,19 @@ void print_partition_size(struct fdisk_context *cxt, static void new_partition(struct fdisk_context *cxt) { + int partnum = 0; + if (warn_geometry(cxt)) return; - if (disklabel == SUN_LABEL) { - add_sun_partition(cxt, get_partition(cxt, 0, partitions), LINUX_NATIVE); - return; - } + if (disklabel == SUN_LABEL || disklabel == SGI_LABEL) + partnum = get_partition(cxt, 0, partitions); - 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")); - 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; - } - - /* 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) diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index 9ce2c25b0..975ac4d9a 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -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 */ diff --git a/fdisks/fdiskaixlabel.c b/fdisks/fdiskaixlabel.c index c8cb660e0..c01af3dbc 100644 --- a/fdisks/fdiskaixlabel.c +++ b/fdisks/fdiskaixlabel.c @@ -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, }; diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index 50bd840d7..7e8f2ba12 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -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, }; diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index 398e5e040..fde31a7a8 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -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, }; diff --git a/fdisks/fdiskdoslabel.h b/fdisks/fdiskdoslabel.h index d7361a806..77009dca0 100644 --- a/fdisks/fdiskdoslabel.h +++ b/fdisks/fdiskdoslabel.h @@ -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); diff --git a/fdisks/fdiskmaclabel.c b/fdisks/fdiskmaclabel.c index 4e80d395b..d69a92098 100644 --- a/fdisks/fdiskmaclabel.c +++ b/fdisks/fdiskmaclabel.c @@ -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, }; diff --git a/fdisks/fdisksgilabel.c b/fdisks/fdisksgilabel.c index 836b9d4a7..62fae99df 100644 --- a/fdisks/fdisksgilabel.c +++ b/fdisks/fdisksgilabel.c @@ -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, }; diff --git a/fdisks/fdisksgilabel.h b/fdisks/fdisksgilabel.h index 412ec7422..83cf55eff 100644 --- a/fdisks/fdisksgilabel.h +++ b/fdisks/fdisksgilabel.h @@ -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 ); diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c index 3b0b757ad..974372575 100644 --- a/fdisks/fdisksunlabel.c +++ b/fdisks/fdisksunlabel.c @@ -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, }; diff --git a/fdisks/fdisksunlabel.h b/fdisks/fdisksunlabel.h index d33a5aa6f..47c7cf869 100644 --- a/fdisks/fdisksunlabel.h +++ b/fdisks/fdisksunlabel.h @@ -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); diff --git a/fdisks/utils.c b/fdisks/utils.c index 08f0fe6bd..4ab9f630a 100644 --- a/fdisks/utils.c +++ b/fdisks/utils.c @@ -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