]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add part_get_status operation
authorKarel Zak <kzak@redhat.com>
Mon, 21 Jan 2013 16:10:23 +0000 (17:10 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 11 Mar 2013 11:47:31 +0000 (12:47 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdiskdoslabel.c
fdisks/fdisksgilabel.c
fdisks/fdisksunlabel.c
fdisks/gpt.c
libfdisk/src/fdiskP.h
libfdisk/src/label.c
libfdisk/src/libfdisk.h

index 6655f3a07b6507043700c7818d74c5ba4dbba86d..e47346450fdf628b0df63b0ad4f8c6fbe753b085 100644 (file)
@@ -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,
 };
 
index 132e7fd02185e1166aa44454050fd22cdf8d216a..110441bd3d8ca68c86d96b51327ac1c97cb4fbec 100644 (file)
@@ -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
 };
 
 /*
index 39a0f024c427dbc44a1a561454eaa07f8c5d3768..a5fa11edeed8b61886552d43c5955f5b20c8098c 100644 (file)
@@ -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,
 };
 
 /*
index 115dcd4eb841463124be049cd432741c276b5f04..6070d479b3e5f2cefac13602a9c93a1b4388dd98 100644 (file)
@@ -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
 };
 
index 8f9905d81d347fcf9932d479839b40cba98c0e08..3a5ab4a7ebed148241235791eb3090b9f73a9649 100644 (file)
@@ -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);
index 502df69b44006f940a14d26ce2a6fcad612c4811..4716b94a65362cefa0aa8fbdb023c75f5978b15c 100644 (file)
@@ -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
  */
index 379fb4d47a6f3eae52907ca1baa9f53c2a5c890f..967d21e9a57b217a59db321a67f054def3c025ee 100644 (file)
@@ -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);