From 0747a84d1cd7e49549b551ae7d02a3c619bd4ab2 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado Gomez" Date: Tue, 7 Jun 2022 16:18:15 +0200 Subject: context: add size_unit getset Size unit can be get or set using 'size_unit' context member. >>> for pa in cxt.partitions: ... cxt.partition_to_string(pa, fdisk.FDISK_FIELD_SIZE) ... '114.6G' >>> cxt.size_unit 0 >>> cxt.size_unit == fdisk.FDISK_SIZEUNIT_HUMAN True >>> cxt.size_unit = fdisk.FDISK_SIZEUNIT_BYTES >>> for pa in cxt.partitions: ... cxt.partition_to_string(pa, fdisk.FDISK_FIELD_SIZE) ... '123010531328' Use fdisk_get_size_unit to get size unit value. https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Context.html#fdisk-get-size-unit Use fdisk_set_size_unit to set size unit value. https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Context.html#fdisk-set-size-unit --- context.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/context.c b/context.c index 0d3e646..a85de66 100644 --- a/context.c +++ b/context.c @@ -182,6 +182,35 @@ static PyObject *Context_get_partitions(ContextObject *self) return list; } +static PyObject *Context_get_size_unit(ContextObject *self) +{ + return PyLong_FromLong(fdisk_get_size_unit(self->cxt)); +} +static int Context_set_size_unit(ContextObject *self, PyObject *value, void *closure) +{ + int cval, ret; + + if (value == NULL) { + PyErr_SetString(PyExc_TypeError, + "Cannot set unit size: null size type"); + return -1; + } + + if (!PyLong_Check(value)) { + PyErr_SetString(PyExc_TypeError, + "Cannot set unit size: invalid size type"); + return -1; + } + + cval = (int) PyLong_AsLong(value); + if (fdisk_set_size_unit(self->cxt, cval) < 0) { + PyErr_SetString(PyExc_TypeError, + "Cannot set unit size: invalid size type value"); + return -1; + } + + return 0; +} static PyGetSetDef Context_getseters[] = { {"nsectors", (getter)Context_get_nsectors, NULL, "context number of sectors", NULL}, {"sector_size", (getter)Context_get_sector_size, NULL, "context sector size", NULL}, @@ -189,6 +218,7 @@ static PyGetSetDef Context_getseters[] = { {"label", (getter)Context_get_label, NULL, "context label type", NULL}, {"nparts", (getter)Context_get_nparts, NULL, "context label number of existing partitions", NULL}, {"partitions", (getter)Context_get_partitions, NULL, "context partitions", NULL}, + {"size_unit", (getter)Context_get_size_unit, (setter)Context_set_size_unit, "context unit size", NULL}, {NULL} }; -- cgit v1.2.3-18-g5258