From bc6f5cbdffe19c62fd69791ea5e19a1900442adc Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Wed, 14 Dec 2022 17:33:15 +0100 Subject: 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 --- partition.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'partition.c') diff --git a/partition.c b/partition.c index 59c359b..45c391a 100644 --- a/partition.c +++ b/partition.c @@ -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} }; -- cgit v1.2.3-18-g5258