summaryrefslogtreecommitdiffstats
path: root/label.c
blob: d0ac04c7d7b93b5c80f4e2529764dd95d9b680a8 (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
/*
 * (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 Label_members[] = {
	{ NULL }
};

static void Label_dealloc(LabelObject *self)
{
	Py_TYPE(self)->tp_free((PyObject *) self);
}

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

	if (self) {
		self->lb = NULL;
	}

	return (PyObject *)self;
}

#define Label_HELP "Label(context=None)"
static int Label_init(LabelObject *self, PyObject *args, PyObject *kwds)
{
	static char *kwlist[] = { "context", NULL };
	ContextObject *cxt = NULL;
	struct fdisk_label *lb;
	
	if (!PyArg_ParseTupleAndKeywords(args,
					kwds, "|O!", kwlist,
					&ContextType, &cxt)) {
		PyErr_SetString(PyExc_TypeError, "Error");
		return -1;
	}

	if (cxt && (lb = fdisk_get_label(cxt->cxt, NULL))) {
		self->lb = lb;
	}

	return 0;
}

#define Label_get_parttype_from_code_HELP "get_parttype_from_code(code)\n\n" \
	"Search for partition type in label-specific table."
static PyObject *Label_get_parttype_from_code(LabelObject *self, PyObject *args, PyObject *kwds)
{
	struct fdisk_parttype *ptype;
	unsigned int ptype_code;

	if (!PyArg_ParseTuple(args, "I", &ptype_code)) {
		PyErr_SetString(PyExc_TypeError, ARG_ERR);
		return NULL;
	}

	ptype = fdisk_label_get_parttype_from_code(self->lb, ptype_code);
	if (!ptype) {
		PyErr_Format(PyExc_RuntimeError, "No match for parttype with code: %d", ptype_code);
		return NULL;
	}

	return PyObjectResultPartType(ptype);
}

#define Label_get_parttype_from_string_HELP "get_parttype_from_string(uuid)\n\n" \
	"Search by string for partition type in label-specific table."
static PyObject *Label_get_parttype_from_string(LabelObject *self, PyObject *args, PyObject *kwds)
{
	struct fdisk_parttype *ptype = NULL;
	char *str;

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

	ptype = fdisk_label_get_parttype_from_string(self->lb, str);
	if (!ptype) {
		PyErr_Format(PyExc_RuntimeError, "No match for parttype with string: %s", str);
		return NULL;
	}

	return PyObjectResultPartType(ptype);
}

static PyMethodDef Label_methods[] = {
	{"get_parttype_from_code",	(PyCFunction)Label_get_parttype_from_code, METH_VARARGS, Label_get_parttype_from_code_HELP},
	{"get_parttype_from_string",	(PyCFunction)Label_get_parttype_from_string, METH_VARARGS, Label_get_parttype_from_string_HELP},
	{NULL}
};

static PyObject *Label_get_type(LabelObject *self)
{
	return PyLong_FromLong(fdisk_label_get_type(self->lb));
}

static PyObject *Label_get_name(LabelObject *self)
{
	return PyObjectResultStr(fdisk_label_get_name(self->lb));
}

static PyGetSetDef Label_getseters[] = {
	{"type",	(getter)Label_get_type, NULL, "label type", NULL},
	{"name",	(getter)Label_get_name, NULL, "label name", NULL},
	{NULL}
};

static PyObject *Label_repr(LabelObject *self)
{
	return PyUnicode_FromFormat("<libfdisk.Label object at %p, name=%s>",
			self, fdisk_label_get_name(self->lb));
}

PyTypeObject LabelType = {
	PyVarObject_HEAD_INIT(NULL, 0)
	.tp_name = "libfdisk.Label",
	.tp_basicsize = sizeof(LabelObject),
	.tp_dealloc = (destructor)Label_dealloc,
	.tp_repr = (reprfunc) Label_repr,
	.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
	.tp_doc = Label_HELP,
	.tp_methods = Label_methods,
	.tp_members = Label_members,
	.tp_getset = Label_getseters,
	.tp_init = (initproc)Label_init,
	.tp_new = Label_new,
};

PyObject *PyObjectResultLabel(struct fdisk_label *lb)
{
        LabelObject *result;

        if (!lb) {
                PyErr_SetString(PyExc_AssertionError, "lb assert failed");
                return NULL;
        }


        result = PyObject_New(LabelObject, &LabelType);
        if (!result) {
                PyErr_SetString(PyExc_MemoryError, "Couldn't allocate Label object");
                return NULL;
        }

        /* Py_INCREF(result); */

        result->lb = lb;
        return (PyObject *) result;
}

void Label_AddModuleObject(PyObject *mod)
{
	if (PyType_Ready(&LabelType) < 0)
		return;

	Py_INCREF(&LabelType);
	PyModule_AddObject(mod, "Label", (PyObject *)&LabelType);
}