From: Karel Zak Date: Mon, 21 Jan 2013 16:10:23 +0000 (+0100) Subject: libfdisk: add part_get_status operation X-Git-Tag: v2.23-rc1~133 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47b8e7c00207dfcb34de66f1f9458268d4a90f9d;p=thirdparty%2Futil-linux.git libfdisk: add part_get_status operation Signed-off-by: Karel Zak --- diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index 6655f3a07b..e47346450f 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -1396,6 +1396,32 @@ void dos_toggle_active(struct fdisk_context *cxt, int i) fdisk_label_set_changed(cxt->label, 1); } + +static int dos_get_partition_status( + struct fdisk_context *cxt, + struct fdisk_label *lb __attribute__((__unused__)), + int i, + int *status) +{ + struct pte *pe; + struct partition *p; + + assert(cxt); + assert(fdisk_is_disklabel(cxt, DOS)); + + if (!status || i < 0 || i >= partitions) + return -EINVAL; + + *status = FDISK_PARTSTAT_NONE; + pe = &ptes[i]; + p = pe->part_table; + + if (p && !is_cleared_partition(p)) + *status = FDISK_PARTSTAT_USED; + + return 0; +} + static const struct fdisk_label_operations dos_operations = { .probe = dos_probe_label, @@ -1406,6 +1432,9 @@ static const struct fdisk_label_operations dos_operations = .part_delete = dos_delete_partition, .part_get_type = dos_get_parttype, .part_set_type = dos_set_parttype, + + .part_get_status = dos_get_partition_status, + .reset_alignment = dos_reset_alignment, }; diff --git a/fdisks/fdisksgilabel.c b/fdisks/fdisksgilabel.c index 132e7fd021..110441bd3d 100644 --- a/fdisks/fdisksgilabel.c +++ b/fdisks/fdisksgilabel.c @@ -950,6 +950,29 @@ static int sgi_set_parttype(struct fdisk_context *cxt, return 0; } + +static int sgi_get_partition_status( + struct fdisk_context *cxt, + struct fdisk_label *lb __attribute__((__unused__)), + int i, + int *status) +{ + + assert(cxt); + assert(fdisk_is_disklabel(cxt, SGI)); + + if (!status || i < 0 || i >= partitions) + return -EINVAL; + + *status = FDISK_PARTSTAT_NONE; + + if (sgilabel->partitions[i].num_sectors && + sgilabel->partitions[i].num_sectors) + *status = FDISK_PARTSTAT_USED; + + return 0; +} + static const struct fdisk_label_operations sgi_operations = { .probe = sgi_probe_label, @@ -959,7 +982,9 @@ static const struct fdisk_label_operations sgi_operations = .part_add = sgi_add_partition, .part_delete = sgi_delete_partition, .part_get_type = sgi_get_parttype, - .part_set_type = sgi_set_parttype + .part_set_type = sgi_set_parttype, + + .part_get_status = sgi_get_partition_status }; /* diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c index 39a0f024c4..a5fa11edee 100644 --- a/fdisks/fdisksunlabel.c +++ b/fdisks/fdisksunlabel.c @@ -829,6 +829,27 @@ static int sun_reset_alignment(struct fdisk_context *cxt, return 0; } + +static int sun_get_partition_status( + struct fdisk_context *cxt, + struct fdisk_label *lb __attribute__((__unused__)), + int i, + int *status) +{ + struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + + if (!status || i < 0 || (size_t) i >= lb->nparts_max) + return -EINVAL; + + *status = FDISK_PARTSTAT_NONE; + + if (sunlabel->partitions[i].num_sectors) + *status = FDISK_PARTSTAT_USED; + + return 0; +} + + const struct fdisk_label_operations sun_operations = { .probe = sun_probe_label, @@ -839,7 +860,10 @@ const struct fdisk_label_operations sun_operations = .part_delete = sun_delete_partition, .part_get_type = sun_get_parttype, .part_set_type = sun_set_parttype, - .reset_alignment = sun_reset_alignment + + .part_get_status = sun_get_partition_status, + + .reset_alignment = sun_reset_alignment, }; /* diff --git a/fdisks/gpt.c b/fdisks/gpt.c index 115dcd4eb8..6070d479b3 100644 --- a/fdisks/gpt.c +++ b/fdisks/gpt.c @@ -1729,6 +1729,29 @@ static int gpt_set_partition_type( return 0; } +static int gpt_get_partition_status( + struct fdisk_context *cxt, + struct fdisk_label *lb __attribute__((__unused__)), + int i, + int *status) +{ + struct fdisk_gpt_label *gpt = gpt_label(cxt); + struct gpt_entry *e; + + if (!cxt || !gpt || i < 0 || !status + || (uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) + return -EINVAL; + + e = &gpt->ents[i]; + *status = FDISK_PARTSTAT_NONE; + + if (!partition_unused(&gpt->ents[i]) || gpt_partition_size(e)) + *status = FDISK_PARTSTAT_USED; + + return 0; +} + + /* * Deinitialize fdisk-specific variables */ @@ -1761,6 +1784,8 @@ static const struct fdisk_label_operations gpt_operations = .part_get_type = gpt_get_partition_type, .part_set_type = gpt_set_partition_type, + .part_get_status = gpt_get_partition_status, + .deinit = gpt_deinit }; diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index 8f9905d81d..3a5ab4a7eb 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -145,6 +145,11 @@ struct fdisk_label_operations { int (*part_set_type)(struct fdisk_context *cxt, struct fdisk_label *lb, int partnum, struct fdisk_parttype *t); + + /* returns FDISK_PARTSTAT_* flags */ + int (*part_get_status)(struct fdisk_context *cxt, struct fdisk_label *lb, + int partnum, int *status); + /* refresh alignment setting */ int (*reset_alignment)(struct fdisk_context *cxt, struct fdisk_label *lb); diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index 502df69b44..4716b94a65 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -229,6 +229,25 @@ size_t fdisk_get_nparttypes(struct fdisk_context *cxt) return cxt->label->nparttypes; } +/** + * fdisk_partition_is_used: + * @cxt: fdisk context + * @partnum: partition number + * @status: returns FDISK_PARTSTAT_* flags + * + * Returns 0 on success, otherwise, a corresponding error. + */ +int fdisk_partition_get_status(struct fdisk_context *cxt, int partnum, int *status) +{ + if (!cxt || !cxt->label) + return -EINVAL; + if (!cxt->label->op->part_get_status) + return -ENOSYS; + + return cxt->label->op->part_get_status(cxt, cxt->label, partnum, status); +} + + /* * Resets the current used label driver to initial state */ diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 379fb4d47a..967d21e9a5 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -43,6 +43,11 @@ enum fdisk_labeltype { FDISK_DISKLABEL_ANY = -1 }; +enum { + FDISK_PARTSTAT_NONE, + FDISK_PARTSTAT_USED /* partition used */ +}; + /* init.c */ extern void fdisk_init_debug(int mask); @@ -90,6 +95,7 @@ extern int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum, extern void fdisk_label_set_changed(struct fdisk_label *lb, int changed); extern int fdisk_label_is_changed(struct fdisk_label *lb); +extern int fdisk_partition_get_status(struct fdisk_context *cxt, int partnum, int *status); /* alignment.c */ extern int fdisk_reset_alignment(struct fdisk_context *cxt);