summaryrefslogtreecommitdiffstats
path: root/partition.c
diff options
context:
space:
mode:
authorJose M. Guisado Gomez <guigom@riseup.net>2022-04-06 11:31:30 +0200
committerJose M. Guisado Gomez <guigom@riseup.net>2022-04-06 12:56:11 +0200
commitca92f15e2a5836f0734c82d786dcead732ddad22 (patch)
treedef46ce5bf1f39293bd5fe90d32d644512b1dd5a /partition.c
Initial commit
Add sources, setup.py and .gitignore Build/Install: python setup.py build python setup.py install
Diffstat (limited to 'partition.c')
-rw-r--r--partition.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/partition.c b/partition.c
new file mode 100644
index 0000000..48f410a
--- /dev/null
+++ b/partition.c
@@ -0,0 +1,150 @@
+#include "fdisk.h"
+
+
+static PyMemberDef Partition_members[] = {
+ { NULL }
+};
+
+
+static void Partition_dealloc(PartitionObject *self)
+{
+ if (self->pa)
+ fdisk_unref_partition(self->pa);
+ Py_TYPE(self)->tp_free((PyObject *) self);
+}
+
+static PyObject *Partition_new(PyTypeObject *type,
+ PyObject *args __attribute__((unused)),
+ PyObject *kwds __attribute__((unused)))
+{
+ PartitionObject *self = (PartitionObject*) type->tp_alloc(type, 0);
+
+ if (self)
+ self->pa = NULL;
+
+ return (PyObject *)self;
+}
+
+#define Partition_HELP "Partition()"
+static int Partition_init(PartitionObject *self, PyObject *args, PyObject *kwds)
+{
+ /*
+ char *kwlist[] = {
+ "context",
+ NULL
+ };
+
+ if (!PyArg_ParseTupleAndKeywords(args,
+ kwds, "|O!", kwlist,
+ &ContextType, &cxt)) {
+ PyErr_SetString(PyExc_TypeError, "Error");
+ return -1;
+ }
+ */
+
+ self->pa = fdisk_new_partition();
+
+ return 0;
+}
+
+
+static PyMethodDef Partition_methods[] = {
+ {NULL}
+};
+
+
+static PyObject *Partition_get_partno(PartitionObject *self)
+{
+ if (fdisk_partition_has_partno(self->pa)) {
+ return PyLong_FromSize_t(fdisk_partition_get_partno(self->pa));
+ }
+ // Py_RETURN_NONE;
+ return Py_BuildValue("%d", -1);
+}
+static PyObject *Partition_get_size(PartitionObject *self)
+{
+ if (fdisk_partition_has_size(self->pa)) {
+ return PyLong_FromUnsignedLongLong(fdisk_partition_get_size(self->pa));
+ }
+ Py_RETURN_NONE;
+}
+static PyGetSetDef Partition_getseters[] = {
+ {"partno", (getter)Partition_get_partno, NULL, "partition number", NULL},
+ {"size", (getter)Partition_get_size, NULL, "number of sectors", NULL},
+ {NULL}
+};
+
+static PyObject *Partition_repr(PartitionObject *self)
+{
+ return PyUnicode_FromFormat("<libfdisk.Partition object at %p>",
+ self);
+}
+
+PyTypeObject PartitionType = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "libfdisk.Partition", /*tp_name*/
+ sizeof(PartitionObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)Partition_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ NULL, /*tp_getattr*/
+ NULL, /*tp_setattr*/
+ NULL, /*tp_compare*/
+ (reprfunc) Partition_repr,
+ NULL, /*tp_as_number*/
+ NULL, /*tp_as_sequence*/
+ NULL, /*tp_as_mapping*/
+ NULL, /*tp_hash */
+ NULL, /*tp_call*/
+ NULL, /*tp_str*/
+ NULL, /*tp_getattro*/
+ NULL, /*tp_setattro*/
+ NULL, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ Partition_HELP, /* tp_doc */
+ NULL, /* tp_traverse */
+ NULL, /* tp_clear */
+ NULL, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ NULL, /* tp_iter */
+ NULL, /* tp_iternext */
+ Partition_methods, /* tp_methods */
+ Partition_members, /* tp_members */
+ Partition_getseters, /* tp_getset */
+ NULL, /* tp_base */
+ NULL, /* tp_dict */
+ NULL, /* tp_descr_get */
+ NULL, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)Partition_init, /* tp_init */
+ NULL, /* tp_alloc */
+ Partition_new, /* tp_new */
+};
+
+PyObject *PyObjectResultPartition(struct fdisk_partition *pa)
+{
+ PartitionObject *result;
+
+ if (!pa) {
+ PyErr_SetString(PyExc_AssertionError, "pa assert failed");
+ return NULL;
+ }
+
+ result = PyObject_New(PartitionObject, &PartitionType);
+ if (!result) {
+ PyErr_SetString(PyExc_MemoryError, "Couldn't allocate Partition object");
+ return NULL;
+ }
+
+ result->pa = pa;
+ return (PyObject *) result;
+}
+
+void Partition_AddModuleObject(PyObject *mod)
+{
+ if (PyType_Ready(&PartitionType) < 0)
+ return;
+
+ Py_INCREF(&PartitionType);
+ PyModule_AddObject(mod, "Partition", (PyObject *)&PartitionType);
+}