diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index c845c2d8e..c9e934e88 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -360,87 +360,6 @@ static int command_dump(struct sfdisk *sf, int argc, char **argv) return 0; } -/* appply things from @tpl template to the on-disk partition @n */ -static int sfdisk_modify_partition(struct sfdisk *sf, - struct fdisk_partition *tpl, - size_t n) -{ - struct fdisk_partition *pa = NULL; - const struct fdisk_parttype *type; - const char *data; - sector_t num; - size_t new_partno = 0; - int rc = 0; - - /* get the current partition */ - rc = fdisk_get_partition(sf->cxt, n, &pa); - if (rc) - goto done; - - assert(n == fdisk_partition_get_partno(pa)); - - /* uuid */ - data = fdisk_partition_get_uuid(tpl); - if (data) { - rc = fdisk_partition_set_uuid(pa, data); - if (rc) - goto done; - } - - /* name */ - data = fdisk_partition_get_name(tpl); - if (data) { - rc = fdisk_partition_set_name(pa, data); - if (rc) - goto done; - } - - /* attributes - data = fdisk_partition_get_attrs(tpl); - if (data) { - rc = fdisk_partition_set_attrs(pa, data); - if (rc) - goto done; - }*/ - - /* type */ - type = fdisk_partition_get_type(tpl); - if (type) { - rc = fdisk_partition_set_type(pa, type); - if (rc) - goto done; - } - - /* size */ - num = fdisk_partition_get_size(tpl); - if (num) { - rc = fdisk_partition_set_size(pa, num); - if (rc) - goto done; - } - - /* start */ - num = fdisk_partition_get_start(tpl); - if (num) { - rc = fdisk_partition_set_start(pa, num); - if (rc) - goto done; - } - - /* drop the old partition */ - rc = fdisk_delete_partition(sf->cxt, n); - if (rc) - goto done; - - /* add a new partition */ - rc = fdisk_add_partition(sf->cxt, pa, &new_partno); - assert(new_partno == n); -done: - fdisk_unref_partition(pa); - return rc; -} - - static void sfdisk_print_partition(struct sfdisk *sf, size_t n) { struct fdisk_partition *pa = NULL; @@ -685,7 +604,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) created = !rc; } if (!rc && partno >= 0) { /* -N , modify partition */ - rc = sfdisk_modify_partition(sf, pa, partno); + rc = fdisk_set_partition(sf->cxt, partno, pa); if (rc == 0) rc = SFDISK_DONE_ASK; break; diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c index 69ca73d85..07238b4b5 100644 --- a/libfdisk/src/dos.c +++ b/libfdisk/src/dos.c @@ -843,11 +843,6 @@ static void set_partition(struct fdisk_context *cxt, dos_partition_set_start(p, start - offset); dos_partition_set_size(p, stop - start + 1); - if (!doext) { - struct fdisk_parttype *t = - fdisk_label_get_parttype_from_code(cxt->label, sysid); - fdisk_info_new_partition(cxt, i + 1, start, stop, t); - } if (is_dos_compatible(cxt) && (start/(cxt->geom.sectors*cxt->geom.heads) > 1023)) start = cxt->geom.heads*cxt->geom.sectors*1024 - 1; set_hsc(p->bh, p->bs, p->bc, start); @@ -1142,6 +1137,15 @@ static int add_partition(struct fdisk_context *cxt, size_t n, MBR_DOS_EXTENDED_PARTITION, 0); } + /* report */ + { + struct fdisk_parttype *t = + fdisk_label_get_parttype_from_code(cxt->label, sys); + fdisk_info_new_partition(cxt, n + 1, start, stop, t); + fdisk_free_parttype(t); + } + + if (IS_EXTENDED(sys)) { struct pte *pen = self_pte(cxt, n); @@ -1807,6 +1811,44 @@ static int dos_get_partition(struct fdisk_context *cxt, size_t n, return 0; } +static int dos_set_partition(struct fdisk_context *cxt, size_t n, + struct fdisk_partition *pa) +{ + struct dos_partition *p; + struct pte *pe; + sector_t start, size; + + assert(cxt); + assert(pa); + assert(cxt->label); + assert(fdisk_is_label(cxt, DOS)); + + if (n >= cxt->label->nparts_max) + return -EINVAL; + + if (pa->type && IS_EXTENDED(pa->type->code)) { + fdisk_warnx(cxt, _("You cannot change a partition into an " + "extended one or vice versa. Delete it first.")); + return -EINVAL; + } + + if (pa->type && !pa->type->code) + fdisk_warnx(cxt, _("Type 0 means free space to many systems. " + "Having partitions of type 0 is probably unwise.")); + pe = self_pte(cxt, n); + p = self_partition(cxt, n); + + start = pa->start ? pa->start : get_abs_partition_start(pe); + size = pa->size ? pa->size : dos_partition_get_size(p); + + set_partition(cxt, n, 0, start, start + size - 1, + pa->type ? pa->type->code : p->sys_ind, + pa->boot); + + partition_set_changed(cxt, n, 1); + return 0; +} + static void print_chain_of_logicals(struct fdisk_context *cxt) { size_t i; @@ -2114,6 +2156,7 @@ static const struct fdisk_label_operations dos_operations = .set_id = dos_set_disklabel_id, .get_part = dos_get_partition, + .set_part = dos_set_partition, .add_part = dos_add_partition, .del_part = dos_delete_partition,