summaryrefslogtreecommitdiffstats
path: root/partition.c
blob: 59c359b6cc6c5b024a4cbf1da969e6763dddd38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * (C) 2022 Soleta Consulting S.L. <info@soleta.eu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Author: Jose M. Guisado <jguisado@soleta.eu>
 */


#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(partno_follow_default=False, " \
			"start_follow_default=False, " \
			"end_follow_default=False)"
static int Partition_init(PartitionObject *self, PyObject *args, PyObject *kwds)
{
	char *kwlist[] = {
		"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, "|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();
	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;
}


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;
}
static int Partition_set_partno(PartitionObject *self, PyObject *value, void *closure)
{
	size_t num;

	if (value == NULL) {
		fdisk_partition_unset_partno(self->pa);
		return 0;
	}

	if (!PyLong_Check(value)) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return -1;
	}
	num = PyLong_AsSize_t(value);
	if (fdisk_partition_set_partno(self->pa, num) < 0) {
		PyErr_SetString(PyExc_TypeError,
				"libfdisk reported error setting partno");
		return -1;
	}

	return 0;
}
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 int Partition_set_size(PartitionObject *self, PyObject *value, void *closure)
{
	uint64_t sectors;

	if (value == NULL) {
		fdisk_partition_unset_size(self->pa);
		return 0;
	}

	if (!PyLong_Check(value)) {
		PyErr_SetString(PyExc_TypeError,
				ARG_ERR);
		return -1;
	}
	sectors = PyLong_AsUnsignedLongLong(value);
	if (fdisk_partition_set_size(self->pa, sectors) < 0) {
		PyErr_SetString(PyExc_TypeError,
				"libfdisk reported error setting partition size");
		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},
	{NULL}
};

static PyObject *Partition_repr(PartitionObject *self)
{
	size_t partno;

	if (fdisk_partition_has_partno(self->pa)) {
		partno = fdisk_partition_get_partno(self->pa);
		return PyUnicode_FromFormat("<libfdisk.Partition object at %p, partno=%zu>",
				self, partno);
	}

	return PyUnicode_FromFormat("<libfdisk.Partition object at %p, partno=None>",
				    self);
}

PyTypeObject PartitionType = {
	PyVarObject_HEAD_INIT(NULL, 0)
	.tp_name = "libfdisk.Partition",
	.tp_basicsize = sizeof(PartitionObject),
	.tp_itemsize = 0,
	.tp_dealloc = (destructor)Partition_dealloc,
	.tp_repr = (reprfunc) Partition_repr,
	.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
	.tp_doc = PyDoc_STR(Partition_HELP),
	.tp_methods = Partition_methods,
	.tp_members = Partition_members,
	.tp_getset = Partition_getseters,
	.tp_init = (initproc)Partition_init,
	.tp_new = Partition_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);
}