Skip to contentSkip to conversation

fdisk: API: add new partition to label operations

PATCH In review Davidlohr Bueso 2 participants 2 messages Jul 22, 2012 – Jul 24, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 6/10 fdisk: API: add new partition to label operations

From: Davidlohr Bueso <[email protected]>

---
 fdisks/fdisk.c         |   38 +++++-----------------
 fdisks/fdisk.h         |    3 ++
 fdisks/fdiskaixlabel.c |   10 ++++++
 fdisks/fdiskbsdlabel.c |   81 +++++++++++++++++++++++------------------------
 fdisks/fdiskdoslabel.c |   10 ++++--
 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         |   16 +++++++++
 12 files changed, 99 insertions(+), 80 deletions(-)
fdisks/fdisk.c +9 -29
index 7212840..2af84f1 100644
@@ -1423,39 +1423,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, parttype;
 
-	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_label_partition_new(cxt, partnum, LINUX_NATIVE);
 }
 
 static void write_table(struct fdisk_context *cxt)
fdisks/fdisk.h +3 -0
index 577303b..26612bc 100644
@@ -147,6 +147,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_new)(struct fdisk_context *cxt, int partnum, int parttype);
 	/* delete partition */
 	void (*part_delete)(struct fdisk_context *cxt, int partnum);
 };
@@ -170,6 +172,7 @@ extern void fdisk_geom_set_cyls(struct fdisk_context *cxt);
 extern const char *fdisk_error_name(enum fdisk_error errcode);
 extern void fdisk_error_fatal(struct fdisk_context *cxt, enum fdisk_error errcode);
 extern int fdisk_label_change(struct fdisk_context *cxt, const char *name);
+extern int fdisk_label_partition_new(struct fdisk_context *cxt, int partnum, int parttype);
 extern int fdisk_label_partition_delete(struct fdisk_context *cxt, int partnum);
 extern int fdisk_label_write_table(struct fdisk_context *cxt);
 
fdisks/fdiskaixlabel.c +10 -0
index d264027..7417894 100644
@@ -65,10 +65,20 @@ static int aix_probe_label(struct fdisk_context *cxt)
     return 0;
 }
 
+static void aix_new_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_new = aix_new_partition,
 	.part_delete = NULL,
 };
fdisks/fdiskbsdlabel.c +40 -41

110 lines not on this page · raw message

fdisks/fdiskdoslabel.c +7 -3
index 65609c5..67aec77 100644
@@ -598,10 +598,13 @@ static void add_logical(struct fdisk_context *cxt)
  * Ask the user for new partition type information (logical, extended).
  * This function calls the actual partition adding logic - dos_add_partition.
  */
-void dos_new_partition(struct fdisk_context *cxt)
+static void dos_new_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;
 
@@ -620,7 +623,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);
+		dos_add_partition(cxt, get_partition(cxt, 0, 4), parttype);
 	} else {
 		char c, dflt, line[LINE_LENGTH];
 
@@ -642,7 +645,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);
+				dos_add_partition(cxt, i, parttype);
 			return;
 		} else if (c == 'l' && extended_offset) {
 			add_logical(cxt);
@@ -702,5 +705,6 @@ const struct fdisk_label dos_label =
 	.name = "dos",
 	.probe = dos_probe_label,
 	.write = dos_write_table,
+	.part_new = dos_new_partition,
 	.part_delete = dos_delete_partition,
 };
fdisks/fdiskdoslabel.h +0 -2
index 8a2c6dd..964f91b 100644
@@ -48,7 +48,5 @@ 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);
 
 #endif
fdisks/fdiskmaclabel.c +10 -0
index 19c99f6..f678417 100644
@@ -80,10 +80,20 @@ IS_MAC:
     return 0;
 }
 
+static void mac_new_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_new = mac_new_partition,
 	.part_delete = NULL,
 };
fdisks/fdisksgilabel.c +2 -2
index 39ba58d..e65bd63 100644
@@ -644,8 +644,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;
@@ -885,5 +884,6 @@ const struct fdisk_label sgi_label =
 	.name = "sgi",
 	.probe = sgi_probe_label,
 	.write = sgi_write_table,
+	.part_new = sgi_add_partition,
 	.part_delete = sgi_delete_partition,
 };
fdisks/fdisksgilabel.h +0 -1
index 14f488f..139e74f 100644
@@ -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 );
fdisks/fdisksunlabel.c +2 -1
index 2cae2dd..2b543c6 100644
@@ -361,7 +361,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];
@@ -651,5 +651,6 @@ const struct fdisk_label sun_label =
 	.name = "sun",
 	.probe = sun_probe_label,
 	.write = sun_write_table,
+	.part_new = sun_add_partition,
 	.part_delete = sun_delete_partition,
 };
fdisks/fdisksunlabel.h +0 -1
index 0ede6e3..571633a 100644
@@ -81,7 +81,6 @@ extern void 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);
fdisks/utils.c +16 -0
index 9b5092d..0a44d40 100644
@@ -95,6 +95,22 @@ int fdisk_label_write_table(struct fdisk_context *cxt)
 }	
 
 /**
+ * fdisk_label_partition_new:
+ * @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_label_partition_new(struct fdisk_context *cxt, int partnum, int parttype)
+{
+	cxt->label->part_new(cxt, partnum, parttype);
+	return 0;
+}
+
+/**
  * fdisk_label_partition_delete:
  * @cxt: fdisk context
  * @partnum: partition number to delete
Signature · 1 line
1.7.4.1
maintainer
#1 lore raw
Quoted 1 line · Davidlohr Bueso
 12 files changed, 99 insertions(+), 80 deletions(-)

Applied with some changes, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com