summaryrefslogtreecommitdiffstats
path: root/context.c
blob: ba63b4a9c4f498ff061359d68e49f088124a8e97 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * (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 Context_members[] = {
	{ NULL }
};

static void Context_dealloc(ContextObject *self)
{
	if (!self->cxt) /* if init fails */
		return;

	fdisk_unref_context(self->cxt);
	Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyObject *Context_new(PyTypeObject *type,
			 PyObject *args __attribute__((unused)),
			 PyObject *kwds __attribute__((unused)))
{
	ContextObject *self = (ContextObject*) type->tp_alloc(type, 0);

	if (self) {
		self->cxt = NULL;
		self->tb = NULL;
	}

	return (PyObject *)self;
}

#define Context_HELP "Context(device=None, details=True, readonly=False)"
static int Context_init(ContextObject *self, PyObject *args, PyObject *kwds)
{
	static char *kwlist[] = {
		"device", "details", "readonly",
		NULL
	};
	int details = 1, readonly = 0, rc = 0;
	char *device = NULL;

	if (!PyArg_ParseTupleAndKeywords(args,
					kwds, "|spp", kwlist,
					&device, &details, &readonly)) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return -1;
	}

	if (self->cxt)
		fdisk_unref_context(self->cxt);

	self->cxt = fdisk_new_context();
	if (!self->cxt) {
		PyErr_SetString(PyExc_MemoryError, "Couldn't allocate context");
		return -1;
	}

	if (device && (rc = fdisk_assign_device(self->cxt, device, readonly))) {
		set_PyErr_from_rc(-rc);
		return -1;
	}
	if (details && (rc = fdisk_enable_details(self->cxt, details))) {
		set_PyErr_from_rc(-rc);
		return -1;
	}
	fdisk_get_partitions(self->cxt, &self->tb);

	return 0;
}

#define Context_assign_device_HELP "assign_device(device, readonly=False)\n\n" \
	"Open the device, discovery topology, geometry, detect disklabel " \
	"and switch the current label driver to reflect the probing result. "
static PyObject *Context_assign_device(ContextObject *self, PyObject *args, PyObject *kwds)
{
	static char *kwlist[] = { "", "readonly", NULL };
	int rc, readonly = 0;
	char *device;

	if (!self->cxt) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return NULL;
	}

	if (!PyArg_ParseTupleAndKeywords(args,
					 kwds, "s|$p", kwlist,
					 &device, &readonly)) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return NULL;
	}

	if ((rc = fdisk_assign_device(self->cxt, device, readonly)) < 0) {
		set_PyErr_from_rc(-rc);
		return NULL;
	}
	fdisk_get_partitions(self->cxt, &self->tb);

	Py_INCREF(Py_None);
	return Py_None;
}

#define Context_partition_to_string_HELP "partition_to_string(pa, field)\n\n" \
	"Retrieve partition field using fdisk_partition_to_string." \
	"Field constants are available as FDISK_LABEL_*"
static PyObject *Context_partition_to_string(ContextObject *self, PyObject *args, PyObject *kwds)
{
	struct fdisk_partition *pa;
	enum fdisk_fieldtype field;
	PartitionObject *part;
	PyObject *ret;
	char *data;

	if (!PyArg_ParseTuple(args, "O!i", &PartitionType, &part, &field)) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return NULL;
	}

	pa = part->pa;

	fdisk_partition_to_string(pa, self->cxt, field, &data);
	ret = Py_BuildValue("s", data);
	free(data);

	return ret;
}

#define Context_create_disklabel_HELP "create_disklabel(label)\n\n" \
	"Creates a new disk label of type name . If name is NULL, " \
	"then it will create a default system label type, either SUN or DOS."
static PyObject *Context_create_disklabel(ContextObject *self, PyObject *args, PyObject *kwds)
{
	char *label_name = NULL;

	if (!PyArg_ParseTuple(args, "|s", &label_name)) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return NULL;
	}

	if (fdisk_create_disklabel(self->cxt, label_name)) {
		PyErr_Format(PyExc_RuntimeError, "Error creating label %s", label_name);
		return NULL;
	}

	Py_RETURN_NONE;
}

#define Context_write_disklabel_HELP "write_disklabel()\n\n" \
	"This function wipes the device (if enabled by fdisk_enable_wipe()) " \
	"and then it writes in-memory changes to disk. Be careful!"
static PyObject *Context_write_disklabel(ContextObject *self, PyObject *args, PyObject *kwds)
{
	int ret;

	ret = fdisk_write_disklabel(self->cxt);
	if (ret < 0) {
		PyErr_Format(PyExc_RuntimeError, "Error writing label to disk: %s", strerror(-ret));
		return NULL;
	}

	Py_RETURN_NONE;
}

#define Context_add_partition_HELP "add_partition(fdisk.Partition)\n\n" \
	"Adds partition to context. Returns partno of the new partition."
static PyObject *Context_add_partition(ContextObject *self, PyObject *args, PyObject *kwds)
{
	PartitionObject *partobj;
	size_t partno;
	int rc;

	if (!PyArg_ParseTuple(args, "O!", &PartitionType, &partobj)) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return NULL;
	}

	if (!partobj->pa) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return NULL;
	}

	rc = fdisk_add_partition(self->cxt, partobj->pa, &partno);
	if (rc < 0) {
		PyErr_Format(PyExc_RuntimeError, "Error adding partition to context: %s", strerror(-rc));
		return NULL;
	}

	return Py_BuildValue("n", partno);
}

static PyMethodDef Context_methods[] = {
	{"assign_device",	(PyCFunction)Context_assign_device, METH_VARARGS | METH_KEYWORDS, Context_assign_device_HELP},
	{"partition_to_string",	(PyCFunction)Context_partition_to_string, METH_VARARGS, Context_partition_to_string_HELP},
	{"create_disklabel",	(PyCFunction)Context_create_disklabel, METH_VARARGS, Context_create_disklabel_HELP},
	{"write_disklabel",	(PyCFunction)Context_write_disklabel, METH_NOARGS, Context_write_disklabel_HELP},
	{"add_partition",	(PyCFunction)Context_add_partition, METH_VARARGS, Context_add_partition_HELP},
	{NULL}
};

static PyObject *Context_get_nsectors(ContextObject *self)
{
	return PyLong_FromUnsignedLong(fdisk_get_nsectors(self->cxt));
}

static PyObject *Context_get_sector_size(ContextObject *self)
{
	return PyLong_FromUnsignedLong(fdisk_get_sector_size(self->cxt));
}

static PyObject *Context_get_devname(ContextObject *self)
{
	return PyObjectResultStr(fdisk_get_devname(self->cxt));
}

static PyObject *Context_get_label(ContextObject *self)
{
	struct fdisk_context *cxt = self->cxt;

	if (fdisk_has_label(cxt)) {
		return PyObjectResultLabel(fdisk_get_label(cxt, NULL));
	} else {
		Py_RETURN_NONE;
	}
}

static PyObject *Context_get_nparts(ContextObject *self)
{
	return PyLong_FromLong(fdisk_table_get_nents(self->tb));
}

static PyObject *Context_get_partitions(ContextObject *self)
{
	PyObject *p, *list = PyList_New(0); /* XXX: null if failed*/
	struct fdisk_partition *pa;
	struct fdisk_iter *itr;
	struct fdisk_table *tb;
	/* char *data; */
	
	tb = self->tb;
	itr = fdisk_new_iter(FDISK_ITER_FORWARD);

	while(fdisk_table_next_partition(tb, itr, &pa) == 0) {
		/* const char *name = fdisk_partition_get_name(pa);*/
		p = PyObjectResultPartition(pa);
		PyList_Append(list, p);
		/*free(data);*/
	}	

	fdisk_free_iter(itr);

	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 szunit;

	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;
	}

	szunit = (int) PyLong_AsLong(value);
	if (fdisk_set_size_unit(self->cxt, szunit) < 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},
	{"devname",	(getter)Context_get_devname, NULL, "context devname", NULL},
	{"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}
};

static PyObject *Context_repr(ContextObject *self)
{
	PyObject *lbo = Py_None;

	if (fdisk_has_label(self->cxt))
		lbo = PyObjectResultLabel(fdisk_get_label(self->cxt, NULL));

	return PyUnicode_FromFormat("<libfdisk.Context object at %p, label=%R, details=%s, readonly=%s>",
				    self,
				    lbo,
				    fdisk_is_details(self->cxt) ? "True" : "False",
				    fdisk_is_readonly(self->cxt) ? "True" : "False");
}

PyTypeObject ContextType = {
	PyVarObject_HEAD_INIT(NULL, 0)
	.tp_name = "libfdisk.Context",
	.tp_basicsize = sizeof(ContextObject),
	.tp_dealloc = (destructor)Context_dealloc,
	.tp_repr = (reprfunc) Context_repr,
	.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
	.tp_doc = Context_HELP,
	.tp_methods = Context_methods,
	.tp_members = Context_members,
	.tp_getset = Context_getseters,
	.tp_init = (initproc)Context_init,
	.tp_new = Context_new,
};

void Context_AddModuleObject(PyObject *mod)
{
	if (PyType_Ready(&ContextType) < 0)
		return;

	Py_INCREF(&ContextType);
	PyModule_AddObject(mod, "Context", (PyObject *)&ContextType);
}