libfdisk: remove fdisk_get_partition_type()

Let's use more generic:

	fdisk_get_partition()
        fdisk_partition_get_parttype()

rather than fdisk_get_partition_type().

The patch also improves fdisk_get_partition() semantic to allocate
a new partition struct if the argument is NULL.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-11-28 12:56:12 +01:00
parent 27d4d07292
commit 851515216f
4 changed files with 39 additions and 52 deletions

View File

@ -160,7 +160,9 @@ void toggle_dos_compatibility_flag(struct fdisk_context *cxt)
void change_partition_type(struct fdisk_context *cxt)
{
size_t i;
struct fdisk_parttype *t = NULL, *org_t = NULL;
struct fdisk_parttype *t = NULL;
struct fdisk_partition *pa = NULL;
const char *old = NULL;
assert(cxt);
assert(cxt->label);
@ -168,28 +170,30 @@ void change_partition_type(struct fdisk_context *cxt)
if (fdisk_ask_partnum(cxt, &i, FALSE))
return;
org_t = t = fdisk_get_partition_type(cxt, i);
if (!t)
fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
else {
do {
t = ask_partition_type(cxt);
} while (!t);
if (fdisk_get_partition(cxt, i, &pa)) {
fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
return;
}
if (fdisk_set_partition_type(cxt, i, t) == 0)
fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
_("Changed type of partition '%s' to '%s'."),
org_t ? org_t->name : _("Unknown"),
t ? t->name : _("Unknown"));
else
fdisk_info(cxt,
_("Type of partition %zu is unchanged: %s."),
i + 1,
org_t ? org_t->name : _("Unknown"));
}
t = (struct fdisk_parttype *) fdisk_partition_get_type(pa);
old = t ? t->name : _("Unknown");
fdisk_free_parttype(t);
fdisk_free_parttype(org_t);
do {
t = ask_partition_type(cxt);
} while (!t);
fdisk_partition_set_type(pa, t);
if (fdisk_set_partition_type(cxt, i, t) == 0)
fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
_("Changed type of partition '%s' to '%s'."),
old, t ? t->name : _("Unknown"));
else
fdisk_info(cxt,
_("Type of partition %zu is unchanged: %s."),
i + 1, old);
fdisk_free_partition(pa);
}
void list_disk_geometry(struct fdisk_context *cxt)

View File

@ -199,7 +199,7 @@ int fdisk_verify_disklabel(struct fdisk_context *cxt)
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
struct fdisk_partition *pa)
struct fdisk_partition **pa)
{
int rc;
@ -208,12 +208,17 @@ int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
if (!cxt->label->op->get_part)
return -ENOSYS;
fdisk_reset_partition(pa);
pa->cxt = cxt;
pa->partno = partno;
if (!*pa) {
*pa = fdisk_new_partition();
if (!*pa)
return -ENOMEM;
} else
fdisk_reset_partition(*pa);
(*pa)->cxt = cxt;
(*pa)->partno = partno;
rc = cxt->label->op->get_part(cxt, partno, pa);
if (rc == 0 && fdisk_partition_is_used(pa))
rc = cxt->label->op->get_part(cxt, partno, *pa);
if (rc == 0 && fdisk_partition_is_used(*pa))
DBG(LABEL, dbgprint("get partition %zu", partno));
return rc;
}
@ -294,11 +299,6 @@ int fdisk_list_partitions(struct fdisk_context *cxt, int *cols, size_t ncols)
rc = -ENOMEM;
goto done;
}
pa = fdisk_new_partition();
if (!pa) {
rc = -ENOMEM;
goto done;
}
/* define table columns */
for (j = 0; j < ncols; j++) {
@ -312,7 +312,7 @@ int fdisk_list_partitions(struct fdisk_context *cxt, int *cols, size_t ncols)
for (i = 0; i < cxt->label->nparts_max; i++) {
struct tt_line *ln;
rc = fdisk_get_partition(cxt, i, pa);
rc = fdisk_get_partition(cxt, i, &pa);
if (rc)
continue;
if (!fdisk_partition_is_used(pa))
@ -498,23 +498,6 @@ int fdisk_set_disklabel_id(struct fdisk_context *cxt)
return cxt->label->op->set_id(cxt);
}
/**
* fdisk_get_partition_type:
* @cxt: fdisk context
* @partnum: partition number
*
* Returns partition type or NULL upon failure.
*/
struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt,
size_t partnum)
{
if (!cxt || !cxt->label || !cxt->label->op->part_get_type)
return NULL;
DBG(LABEL, dbgprint("partition: %zd: get type", partnum));
return cxt->label->op->part_get_type(cxt, partnum);
}
/**
* fdisk_set_partition_type:
* @cxt: fdisk context

View File

@ -153,12 +153,11 @@ extern int fdisk_locate_disklabel(struct fdisk_context *cxt, int n, const char *
extern int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id);
extern int fdisk_set_disklabel_id(struct fdisk_context *cxt);
extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition *pa);
extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition **pa);
extern int fdisk_add_partition(struct fdisk_context *cxt, struct fdisk_parttype *t);
extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum);
extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, size_t partnum);
extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum,
struct fdisk_parttype *t);

View File

@ -92,6 +92,7 @@ int fdisk_partition_set_type(struct fdisk_partition *pa, struct fdisk_parttype *
{
if (!pa)
return -EINVAL;
fdisk_free_parttype(pa->type);
pa->type = type;
return 0;
}