partition: add partition type getset

Partition type getset enables modifying and getting the type
of a give libfdisk partition.

The type of a partition is defined using PartType, which
can only be instanced via label specific functions
get_parttype_from_string or get_parttype_from_code.

For example, to set the type of a new partitions to 'EFI System':

>>> import fdisk
>>> pa = fdisk.Partition()
>>> pa.type
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> cxt.create_disklabel('gpt')
>>> efitype = cxt.label.get_parttype_from_string("c12a7328-f81f-11d2-ba4b-00a0c93ec93b")
>>> pa.type = efitype

Following the previous example, getting its current partition type:

>>> pa.type
<libfdisk.PartType object at 0x7f2f0a9a12d0, name=EFI System>
master
Jose M. Guisado 2022-12-14 17:33:15 +01:00
parent 5eba6d4d65
commit bc6f5cbdff
1 changed files with 31 additions and 0 deletions

View File

@ -143,9 +143,40 @@ static int Partition_set_size(PartitionObject *self, PyObject *value, void *clos
return 0;
}
static PyObject *Partition_get_type(PartitionObject *self)
{
struct fdisk_parttype *t;
t = fdisk_partition_get_type(self->pa);
if (t)
return PyObjectResultPartType(t);
Py_RETURN_NONE;
}
static int Partition_set_type(PartitionObject *self, PyObject *value, void *closure)
{
if (!value) {
PyErr_SetString(PyExc_TypeError,
"partition type assertion error");
return -1;
}
if (!PyObject_TypeCheck(value, &PartTypeType)) {
PyErr_SetString(PyExc_TypeError,
"invalid partition type");
return -1;
}
if (fdisk_partition_set_type(self->pa, ((PartTypeObject *) value)->type) < 0) {
PyErr_SetString(PyExc_TypeError,
"libfdisk reported error setting partition type");
return -1;
}
return 0;
}
static PyGetSetDef Partition_getseters[] = {
{"partno", (getter)Partition_get_partno, (setter)Partition_set_partno, "partition number", NULL},
{"size", (getter)Partition_get_size, (setter)Partition_set_size, "number of sectors", NULL},
{"type", (getter)Partition_get_type, (setter)Partition_set_type, "number of sectors", NULL},
{NULL}
};