libfdisk: add part_get_status operation

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-01-21 17:10:23 +01:00
parent fcf8880029
commit 47b8e7c002
7 changed files with 135 additions and 2 deletions

View 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,
};

View 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
};
/*

View 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,
};
/*

View 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
};

View 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);

View 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
*/

View 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);