1 /*
2  * Python object wrapper of libvslvm_stripe_t
3  *
4  * Copyright (C) 2014-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <common.h>
23 #include <types.h>
24 
25 #if defined( HAVE_STDLIB_H ) || defined( HAVE_WINAPI )
26 #include <stdlib.h>
27 #endif
28 
29 #include "pyvslvm_error.h"
30 #include "pyvslvm_integer.h"
31 #include "pyvslvm_libcerror.h"
32 #include "pyvslvm_libvslvm.h"
33 #include "pyvslvm_python.h"
34 #include "pyvslvm_segment.h"
35 #include "pyvslvm_stripe.h"
36 #include "pyvslvm_unused.h"
37 
38 PyMethodDef pyvslvm_stripe_object_methods[] = {
39 
40 	/* Sentinel */
41 	{ NULL, NULL, 0, NULL }
42 };
43 
44 PyGetSetDef pyvslvm_stripe_object_get_set_definitions[] = {
45 
46 	/* Sentinel */
47 	{ NULL, NULL, NULL, NULL, NULL }
48 };
49 
50 PyTypeObject pyvslvm_stripe_type_object = {
51 	PyVarObject_HEAD_INIT( NULL, 0 )
52 
53 	/* tp_name */
54 	"pyvslvm.stripe",
55 	/* tp_basicsize */
56 	sizeof( pyvslvm_stripe_t ),
57 	/* tp_itemsize */
58 	0,
59 	/* tp_dealloc */
60 	(destructor) pyvslvm_stripe_free,
61 	/* tp_print */
62 	0,
63 	/* tp_getattr */
64 	0,
65 	/* tp_setattr */
66 	0,
67 	/* tp_compare */
68 	0,
69 	/* tp_repr */
70 	0,
71 	/* tp_as_number */
72 	0,
73 	/* tp_as_sequence */
74 	0,
75 	/* tp_as_mapping */
76 	0,
77 	/* tp_hash */
78 	0,
79 	/* tp_call */
80 	0,
81 	/* tp_str */
82 	0,
83 	/* tp_getattro */
84 	0,
85 	/* tp_setattro */
86 	0,
87 	/* tp_as_buffer */
88 	0,
89 	/* tp_flags */
90 	Py_TPFLAGS_DEFAULT,
91 	/* tp_doc */
92 	"pyvslvm stripe object (wraps libvslvm_stripe_t)",
93 	/* tp_traverse */
94 	0,
95 	/* tp_clear */
96 	0,
97 	/* tp_richcompare */
98 	0,
99 	/* tp_weaklistoffset */
100 	0,
101 	/* tp_iter */
102 	0,
103 	/* tp_iternext */
104 	0,
105 	/* tp_methods */
106 	pyvslvm_stripe_object_methods,
107 	/* tp_members */
108 	0,
109 	/* tp_getset */
110 	pyvslvm_stripe_object_get_set_definitions,
111 	/* tp_base */
112 	0,
113 	/* tp_dict */
114 	0,
115 	/* tp_descr_get */
116 	0,
117 	/* tp_descr_set */
118 	0,
119 	/* tp_dictoffset */
120 	0,
121 	/* tp_init */
122 	(initproc) pyvslvm_stripe_init,
123 	/* tp_alloc */
124 	0,
125 	/* tp_new */
126 	0,
127 	/* tp_free */
128 	0,
129 	/* tp_is_gc */
130 	0,
131 	/* tp_bases */
132 	NULL,
133 	/* tp_mro */
134 	NULL,
135 	/* tp_cache */
136 	NULL,
137 	/* tp_subclasses */
138 	NULL,
139 	/* tp_weaklist */
140 	NULL,
141 	/* tp_del */
142 	0
143 };
144 
145 /* Creates a new stripe object
146  * Returns a Python object if successful or NULL on error
147  */
pyvslvm_stripe_new(libvslvm_stripe_t * stripe,pyvslvm_segment_t * segment_object)148 PyObject *pyvslvm_stripe_new(
149            libvslvm_stripe_t *stripe,
150            pyvslvm_segment_t *segment_object )
151 {
152 	pyvslvm_stripe_t *pyvslvm_stripe = NULL;
153 	static char *function            = "pyvslvm_stripe_new";
154 
155 	if( stripe == NULL )
156 	{
157 		PyErr_Format(
158 		 PyExc_TypeError,
159 		 "%s: invalid stripe.",
160 		 function );
161 
162 		return( NULL );
163 	}
164 	pyvslvm_stripe = PyObject_New(
165 	                  struct pyvslvm_stripe,
166 	                  &pyvslvm_stripe_type_object );
167 
168 	if( pyvslvm_stripe == NULL )
169 	{
170 		PyErr_Format(
171 		 PyExc_MemoryError,
172 		 "%s: unable to initialize stripe.",
173 		 function );
174 
175 		goto on_error;
176 	}
177 	if( pyvslvm_stripe_init(
178 	     pyvslvm_stripe ) != 0 )
179 	{
180 		PyErr_Format(
181 		 PyExc_MemoryError,
182 		 "%s: unable to initialize stripe.",
183 		 function );
184 
185 		goto on_error;
186 	}
187 	pyvslvm_stripe->stripe         = stripe;
188 	pyvslvm_stripe->segment_object = segment_object;
189 
190 	Py_IncRef(
191 	 (PyObject *) pyvslvm_stripe->segment_object );
192 
193 	return( (PyObject *) pyvslvm_stripe );
194 
195 on_error:
196 	if( pyvslvm_stripe != NULL )
197 	{
198 		Py_DecRef(
199 		 (PyObject *) pyvslvm_stripe );
200 	}
201 	return( NULL );
202 }
203 
204 /* Initializes a stripe object
205  * Returns 0 if successful or -1 on error
206  */
pyvslvm_stripe_init(pyvslvm_stripe_t * pyvslvm_stripe)207 int pyvslvm_stripe_init(
208      pyvslvm_stripe_t *pyvslvm_stripe )
209 {
210 	static char *function = "pyvslvm_stripe_init";
211 
212 	if( pyvslvm_stripe == NULL )
213 	{
214 		PyErr_Format(
215 		 PyExc_TypeError,
216 		 "%s: invalid stripe.",
217 		 function );
218 
219 		return( -1 );
220 	}
221 	/* Make sure libvslvm stripe is set to NULL
222 	 */
223 	pyvslvm_stripe->stripe = NULL;
224 
225 	return( 0 );
226 }
227 
228 /* Frees a stripe object
229  */
pyvslvm_stripe_free(pyvslvm_stripe_t * pyvslvm_stripe)230 void pyvslvm_stripe_free(
231       pyvslvm_stripe_t *pyvslvm_stripe )
232 {
233 	libcerror_error_t *error    = NULL;
234 	struct _typeobject *ob_type = NULL;
235 	static char *function       = "pyvslvm_stripe_free";
236 
237 	if( pyvslvm_stripe == NULL )
238 	{
239 		PyErr_Format(
240 		 PyExc_TypeError,
241 		 "%s: invalid stripe.",
242 		 function );
243 
244 		return;
245 	}
246 	if( pyvslvm_stripe->stripe == NULL )
247 	{
248 		PyErr_Format(
249 		 PyExc_TypeError,
250 		 "%s: invalid stripe - missing libvslvm stripe.",
251 		 function );
252 
253 		return;
254 	}
255 	ob_type = Py_TYPE(
256 	           pyvslvm_stripe );
257 
258 	if( ob_type == NULL )
259 	{
260 		PyErr_Format(
261 		 PyExc_ValueError,
262 		 "%s: missing ob_type.",
263 		 function );
264 
265 		return;
266 	}
267 	if( ob_type->tp_free == NULL )
268 	{
269 		PyErr_Format(
270 		 PyExc_ValueError,
271 		 "%s: invalid ob_type - missing tp_free.",
272 		 function );
273 
274 		return;
275 	}
276 	if( libvslvm_stripe_free(
277 	     &( pyvslvm_stripe->stripe ),
278 	     &error ) != 1 )
279 	{
280 		pyvslvm_error_raise(
281 		 error,
282 		 PyExc_IOError,
283 		 "%s: unable to free libvslvm stripe.",
284 		 function );
285 
286 		libcerror_error_free(
287 		 &error );
288 	}
289 	if( pyvslvm_stripe->segment_object != NULL )
290 	{
291 		Py_DecRef(
292 		 (PyObject *) pyvslvm_stripe->segment_object );
293 	}
294 	ob_type->tp_free(
295 	 (PyObject*) pyvslvm_stripe );
296 }
297 
298