]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: API: add delete partition to label operations
authorDavidlohr Bueso <dave@gnu.org>
Mon, 23 Jul 2012 16:15:57 +0000 (18:15 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 23 Jul 2012 16:15:57 +0000 (18:15 +0200)
Signed-off-by: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
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 0c837a315b372eace190ab8def71fb97a886e180..ec31b14d868e9a6c022ce3b5e0a52095d45c9eeb 100644 (file)
@@ -818,25 +818,14 @@ toggle_dos_compatibility_flag(struct fdisk_context *cxt) {
        update_sector_offset(cxt);
 }
 
-static void
-delete_partition(struct fdisk_context *cxt, int i)
+static void delete_partition(struct fdisk_context *cxt, int partnum)
 {
-       if (i < 0)
+       if (partnum < 0 || warn_geometry(cxt))
                return;
 
-       if (warn_geometry(cxt))
-               return;         /* C/H/S not set */
-
-       ptes[i].changed = 1;
-
-       if (disklabel == DOS_LABEL)
-               dos_delete_partition(i);
-       else if (disklabel == SUN_LABEL)
-               sun_delete_partition(cxt, i);
-       else if (disklabel == SGI_LABEL)
-               sgi_delete_partition(cxt, i);
-
-       printf(_("Partition %d is deleted\n"), i + 1);
+       ptes[partnum].changed = 1;
+       fdisk_delete_partition(cxt, partnum);
+       printf(_("Partition %d is deleted\n"), partnum + 1);
 }
 
 static void change_sysid(struct fdisk_context *cxt)
index a2a6fb55deb62eb59676f2cef0ac9c0a17512e30..2a04b8372ccddcee09cdf9743c7a6bfc2c7f4f9e 100644 (file)
@@ -137,6 +137,8 @@ struct fdisk_label {
 
        /* probe disk label */
        int (*probe)(struct fdisk_context *cxt);
+       /* delete partition */
+       void (*part_delete)(struct fdisk_context *cxt, int partnum);
 };
 
 /*
@@ -160,6 +162,7 @@ extern int fdisk_context_set_user_geometry(struct fdisk_context *cxt,
                            unsigned int cylinders, unsigned int heads,
                            unsigned int sectors);
 extern int fdisk_create_default_disklabel(struct fdisk_context *cxt);
+extern int fdisk_delete_partition(struct fdisk_context *cxt, int partnum);
 
 /* prototypes for fdisk.c */
 extern char *disk_device, *line_ptr;
index bdc73e64a0dca8ce3aeb422a5f6225477f6285b8..658b7e4cfc41f1e51bfc6811d403aca122b9f6a6 100644 (file)
@@ -69,4 +69,5 @@ const struct fdisk_label aix_label =
 {
        .name = "aix",
        .probe = aix_probe_label,
+       .part_delete = NULL,
 };
index 548a86d3bc513382ff678716e22dd989ac066354..ae83d2e7f4fbf1bd6dfa081f3f5e33da30cc8faa 100644 (file)
@@ -61,7 +61,7 @@
 #define DKTYPENAMES
 #include "fdiskbsdlabel.h"
 
-static void xbsd_delete_part (void);
+static void xbsd_delete_part (struct fdisk_context *cxt, int partnum);
 static void xbsd_new_part (struct fdisk_context *cxt);
 static void xbsd_write_disklabel (struct fdisk_context *cxt);
 static int xbsd_create_disklabel (struct fdisk_context *cxt);
@@ -184,8 +184,8 @@ bsd_command_prompt (struct fdisk_context *cxt)
     putchar ('\n');
     switch (tolower (read_char (_("BSD disklabel command (m for help): ")))) {
       case 'd':
-       xbsd_delete_part ();
-       break;
+             xbsd_delete_part(cxt, xbsd_get_part_index(xbsd_dlabel.d_npartitions));
+             break;
       case 'e':
        xbsd_edit_disklabel ();
        break;
@@ -230,18 +230,14 @@ bsd_command_prompt (struct fdisk_context *cxt)
   }
 }
 
-static void
-xbsd_delete_part (void)
+static void xbsd_delete_part(struct fdisk_context *cxt, int partnum)
 {
-  int i;
-
-  i = xbsd_get_part_index (xbsd_dlabel.d_npartitions);
-  xbsd_dlabel.d_partitions[i].p_size   = 0;
-  xbsd_dlabel.d_partitions[i].p_offset = 0;
-  xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED;
-  if (xbsd_dlabel.d_npartitions == i + 1)
-    while (xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size == 0)
-      xbsd_dlabel.d_npartitions--;
+       xbsd_dlabel.d_partitions[partnum].p_size   = 0;
+       xbsd_dlabel.d_partitions[partnum].p_offset = 0;
+       xbsd_dlabel.d_partitions[partnum].p_fstype = BSD_FS_UNUSED;
+       if (xbsd_dlabel.d_npartitions == partnum + 1)
+               while (!xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size)
+                       xbsd_dlabel.d_npartitions--;
 }
 
 static void
@@ -850,4 +846,5 @@ const struct fdisk_label bsd_label =
 {
        .name = "bsd",
        .probe = osf_probe_label,
+       .part_delete = xbsd_delete_part,
 };
index 103aa6c6b76b8c370ab580d23d8c56d2c8e50441..d9369b6917e224562057fdc9884be8a59d8525cc 100644 (file)
@@ -131,6 +131,61 @@ void dos_init(struct fdisk_context *cxt)
        warn_alignment(cxt);
 }
 
+static void dos_delete_partition(struct fdisk_context *cxt, int partnum)
+{
+       struct pte *pe = &ptes[partnum];
+       struct partition *p = pe->part_table;
+       struct partition *q = pe->ext_pointer;
+
+       /* Note that for the fifth partition (partnum == 4) we don't actually
+          decrement partitions. */
+
+       if (partnum < 4) {
+               if (IS_EXTENDED (p->sys_ind) && partnum == ext_index) {
+                       partitions = 4;
+                       ptes[ext_index].ext_pointer = NULL;
+                       extended_offset = 0;
+               }
+               clear_partition(p);
+       } else if (!q->sys_ind && partnum > 4) {
+               /* the last one in the chain - just delete */
+               --partitions;
+               --partnum;
+               clear_partition(ptes[partnum].ext_pointer);
+               ptes[partnum].changed = 1;
+       } else {
+               /* not the last one - further ones will be moved down */
+               if (partnum > 4) {
+                       /* delete this link in the chain */
+                       p = ptes[partnum-1].ext_pointer;
+                       *p = *q;
+                       set_start_sect(p, get_start_sect(q));
+                       set_nr_sects(p, get_nr_sects(q));
+                       ptes[partnum-1].changed = 1;
+               } else if (partitions > 5) {    /* 5 will be moved to 4 */
+                       /* the first logical in a longer chain */
+                       struct pte *pe = &ptes[5];
+
+                       if (pe->part_table) /* prevent SEGFAULT */
+                               set_start_sect(pe->part_table,
+                                              get_partition_start(pe) -
+                                              extended_offset);
+                       pe->offset = extended_offset;
+                       pe->changed = 1;
+               }
+
+               if (partitions > 5) {
+                       partitions--;
+                       while (partnum < partitions) {
+                               ptes[partnum] = ptes[partnum+1];
+                               partnum++;
+                       }
+               } else
+                       /* the only logical: clear only */
+                       clear_partition(ptes[partnum].part_table);
+       }
+}
+
 static void read_extended(struct fdisk_context *cxt, int ext)
 {
        int i;
@@ -219,7 +274,7 @@ static void read_extended(struct fdisk_context *cxt, int ext)
                if (!get_nr_sects(pe->part_table) &&
                    (partitions > 5 || ptes[4].part_table->sys_ind)) {
                        printf(_("omitting empty partition (%d)\n"), i+1);
-                       dos_delete_partition(i);
+                       dos_delete_partition(cxt, i);
                        goto remove;    /* numbering changed */
                }
        }
@@ -273,61 +328,6 @@ void dos_set_mbr_id(struct fdisk_context *cxt)
        dos_print_mbr_id(cxt);
 }
 
-void dos_delete_partition(int i)
-{
-       struct pte *pe = &ptes[i];
-       struct partition *p = pe->part_table;
-       struct partition *q = pe->ext_pointer;
-
-       /* Note that for the fifth partition (i == 4) we don't actually
-          decrement partitions. */
-
-       if (i < 4) {
-               if (IS_EXTENDED (p->sys_ind) && i == ext_index) {
-                       partitions = 4;
-                       ptes[ext_index].ext_pointer = NULL;
-                       extended_offset = 0;
-               }
-               clear_partition(p);
-       } else if (!q->sys_ind && i > 4) {
-               /* the last one in the chain - just delete */
-               --partitions;
-               --i;
-               clear_partition(ptes[i].ext_pointer);
-               ptes[i].changed = 1;
-       } else {
-               /* not the last one - further ones will be moved down */
-               if (i > 4) {
-                       /* delete this link in the chain */
-                       p = ptes[i-1].ext_pointer;
-                       *p = *q;
-                       set_start_sect(p, get_start_sect(q));
-                       set_nr_sects(p, get_nr_sects(q));
-                       ptes[i-1].changed = 1;
-               } else if (partitions > 5) {    /* 5 will be moved to 4 */
-                       /* the first logical in a longer chain */
-                       struct pte *pe = &ptes[5];
-
-                       if (pe->part_table) /* prevent SEGFAULT */
-                               set_start_sect(pe->part_table,
-                                              get_partition_start(pe) -
-                                              extended_offset);
-                       pe->offset = extended_offset;
-                       pe->changed = 1;
-               }
-
-               if (partitions > 5) {
-                       partitions--;
-                       while (i < partitions) {
-                               ptes[i] = ptes[i+1];
-                               i++;
-                       }
-               } else
-                       /* the only logical: clear only */
-                       clear_partition(ptes[i].part_table);
-       }
-}
-
 static void get_partition_table_geometry(struct fdisk_context *cxt,
                        unsigned int *ph, unsigned int *ps)
 {
@@ -735,4 +735,5 @@ const struct fdisk_label dos_label =
 {
        .name = "dos",
        .probe = dos_probe_label,
+       .part_delete = dos_delete_partition,
 };
index 6096d733316ac8ac7bcd02f340ac4d5b01aa48d9..2d3620019f79e4e6b2db9f3d5c6abaac1399fd80 100644 (file)
@@ -40,7 +40,6 @@ static inline sector_t get_partition_start(struct pte *pe)
 extern int create_doslabel(struct fdisk_context *cxt);
 extern void dos_print_mbr_id(struct fdisk_context *cxt);
 extern void dos_set_mbr_id(struct fdisk_context *cxt);
-extern void dos_delete_partition(int i);
 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);
index 9b1b6b2a0bdce513c175485f2de41da5013dda88..c579a58f1e338e393556594dc70fd332c8207fa3 100644 (file)
@@ -84,4 +84,5 @@ const struct fdisk_label mac_label =
 {
        .name = "mac",
        .probe = mac_probe_label,
+       .part_delete = NULL,
 };
index 6108271e27da31e3c0b5820679ee254a79c15bc9..0ee3a83afc6a932bb45e6cdc9d7011366d50cf5c 100644 (file)
@@ -638,10 +638,9 @@ sgi_set_volhdr(struct fdisk_context *cxt)
        }
 }
 
-void
-sgi_delete_partition(struct fdisk_context *cxt, int i)
+static void sgi_delete_partition(struct fdisk_context *cxt, int partnum)
 {
-       sgi_set_partition(cxt, i, 0, 0, 0);
+       sgi_set_partition(cxt, partnum, 0, 0, 0);
 }
 
 void
@@ -886,4 +885,5 @@ const struct fdisk_label sgi_label =
 {
        .name = "sgi",
        .probe = sgi_probe_label,
+       .part_delete = sgi_delete_partition,
 };
index 571ebbb22a474b58c241fbf8057efaafd2bfcb04..96e96548e9679a404df6f94279a7f505ea13e238 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_delete_partition( 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);
index e39b90cca11fe47df296b2d50e476d23172141e3..7c369f3e64ddaeb59af608ef9c46694708fc46fd 100644 (file)
@@ -483,13 +483,13 @@ and is of type `Whole disk'\n"));
        set_sun_partition(cxt, n, first, last, sys);
 }
 
-void sun_delete_partition(struct fdisk_context *cxt, int i)
+static void sun_delete_partition(struct fdisk_context *cxt, int partnum)
 {
-       struct sun_partition *part = &sunlabel->partitions[i];
-       struct sun_tag_flag *tag = &sunlabel->part_tags[i];
+       struct sun_partition *part = &sunlabel->partitions[partnum];
+       struct sun_tag_flag *tag = &sunlabel->part_tags[partnum];
        unsigned int nsec;
 
-       if (i == 2 &&
+       if (partnum == 2 &&
            tag->tag == SSWAP16(SUN_TAG_BACKUP) &&
            !part->start_cylinder &&
            (nsec = SSWAP32(part->num_sectors))
@@ -649,4 +649,5 @@ const struct fdisk_label sun_label =
 {
        .name = "sun",
        .probe = sun_probe_label,
+       .part_delete = sun_delete_partition,
 };
index 33689d16023bceeaa2acc7e105346b0114ef2c4e..d416401ba97c89acdeeecba2d797fc263b938e5e 100644 (file)
@@ -78,7 +78,6 @@ struct sun_disk_label {
 /* fdisksunlabel.c */
 extern struct systypes sun_sys_types[];
 extern int create_sunlabel(struct fdisk_context *cxt);
-extern void sun_delete_partition(struct fdisk_context *cxt, int i);
 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);
index 64abae6df0719aa18af0efcca9bc3d016602efe3..932fa16f003d83d184bc1782070125e07043aa24 100644 (file)
@@ -47,6 +47,26 @@ static const struct fdisk_label *labels[] =
        &mac_label,
 };
 
+/**
+ * fdisk_delete_partition:
+ * @cxt: fdisk context
+ * @partnum: partition number to delete
+ *
+ * Deletes a @partnum partition.
+ *
+ * Returns 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_delete_partition(struct fdisk_context *cxt, int partnum)
+{
+       if (!cxt)
+               return -EINVAL;
+
+       DBG(LABEL, dbgprint("deleting %s partition number %d",
+                               cxt->label->name, partnum));
+       cxt->label->part_delete(cxt, partnum);
+       return 0;
+}
+
 static int __probe_labels(struct fdisk_context *cxt)
 {
        int i;
@@ -60,7 +80,7 @@ static int __probe_labels(struct fdisk_context *cxt)
 
                cxt->label = labels[i];
 
-               DBG(LABEL, dbgprint("detected a %s label\n", cxt->label->name));
+               DBG(LABEL, dbgprint("detected a %s label", cxt->label->name));
                return 0;
        }