From 7271bc99db8dcb349464eabd50da3f41942f6822 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Wed, 14 Dec 2022 16:36:38 +0100 Subject: partition: add *_follow_default optional params Add optional parameters inside init function of partition. Optional parameters refer to: - partno_follow_default - start_follow_default - end_follow_default These options can be used in order to enable or disable default partno, start and end value when adding partitions. With those optional parameters enabled by default a user is able to add a partition into the context label without specifying any attribute. >>> import fdisk >>> cxt = fdisk.Context('./disk.bin', readonly=False) >>> cxt.create_disklabel('gpt') >>> pa = fdisk.Partition() >>> cxt.add_partition(pa) This enables: - "Filling" the rest of the disk with last partition - No need to track start/end sector for any following partition - No need to track next partno number for any following partition See: https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-partno-follow-default https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-start-follow-default https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-end-follow-default --- partition.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/partition.c b/partition.c index 6057f5b..59c359b 100644 --- a/partition.c +++ b/partition.c @@ -37,25 +37,43 @@ static PyObject *Partition_new(PyTypeObject *type, return (PyObject *)self; } -#define Partition_HELP "Partition()" +#define Partition_HELP "Partition(partno_follow_default=False, " \ + "start_follow_default=False, " \ + "end_follow_default=False)" static int Partition_init(PartitionObject *self, PyObject *args, PyObject *kwds) { - /* char *kwlist[] = { - "context", + "partno_follow_default", + "start_follow_default", + "end_follow_default", NULL }; - + int partno_follow_default = 0, + start_follow_default = 0, + end_follow_default = 0; + if (!PyArg_ParseTupleAndKeywords(args, - kwds, "|O!", kwlist, - &ContextType, &cxt)) { - PyErr_SetString(PyExc_TypeError, "Error"); + kwds, "|ppp", kwlist, + &partno_follow_default, + &start_follow_default, + &end_follow_default)) { + PyErr_SetString(PyExc_TypeError, "Invalid arguments for new partition"); return -1; } - */ self->pa = fdisk_new_partition(); - fdisk_partition_start_follow_default(self->pa, 1); + if (fdisk_partition_partno_follow_default(self->pa, partno_follow_default) < 0) { + PyErr_SetString(PyExc_RuntimeError, "Error setting partno_follow_default"); + return -1; + } + if (fdisk_partition_start_follow_default(self->pa, start_follow_default) < 0) { + PyErr_SetString(PyExc_RuntimeError, "Error setting start_follow_default"); + return -1; + } + if (fdisk_partition_end_follow_default(self->pa, end_follow_default) < 0) { + PyErr_SetString(PyExc_RuntimeError, "Error setting end_follow_default"); + return -1; + } return 0; } -- cgit v1.2.3-18-g5258