summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2022-12-14 17:33:15 +0100
committerJose M. Guisado <jguisado@soleta.eu>2022-12-15 17:37:07 +0100
commitbc6f5cbdffe19c62fd69791ea5e19a1900442adc (patch)
tree7c8d95eb062e3bb8713ce08eb2f3943d0ed66782
parent5eba6d4d65fb5a633f7d0c67a308410a97b6a3ad (diff)
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>
-rw-r--r--partition.c31
1 files changed, 31 insertions, 0 deletions
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}
};