Avoid using a utility header for Python replacements included in Samba,
[Samba/ekacnet.git] / lib / tdb / pytdb.c
blob15a8d8a3e212c0995bf6acc194c4c7e875e23854
1 /*
2 Unix SMB/CIFS implementation.
4 Swig interface to tdb.
6 Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
9 ** NOTE! The following LGPL license applies to the tdb
10 ** library. This does NOT imply that all of Samba is released
11 ** under the LGPL
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #include <Python.h>
28 #ifndef Py_RETURN_NONE
29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
30 #endif
32 #ifdef HAVE_FSTAT
33 #undef HAVE_FSTAT
34 #endif
36 /* Include tdb headers */
37 #include <stdint.h>
38 #include <signal.h>
39 #include <tdb.h>
40 #include <fcntl.h>
41 #include <stdbool.h>
43 typedef struct {
44 PyObject_HEAD
45 TDB_CONTEXT *ctx;
46 bool closed;
47 } PyTdbObject;
49 PyAPI_DATA(PyTypeObject) PyTdb;
51 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
53 PyErr_SetObject(PyExc_RuntimeError,
54 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
57 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
59 TDB_DATA ret;
60 ret.dptr = (unsigned char *)PyString_AsString(data);
61 ret.dsize = PyString_Size(data);
62 return ret;
65 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
67 if (data.dptr == NULL && data.dsize == 0) {
68 Py_RETURN_NONE;
69 } else {
70 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
71 data.dsize);
72 free(data.dptr);
73 return ret;
77 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
78 if (ret != 0) { \
79 PyErr_SetTDBError(tdb); \
80 return NULL; \
83 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
85 char *name;
86 int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
87 TDB_CONTEXT *ctx;
88 PyTdbObject *ret;
89 const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
91 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
92 return NULL;
94 ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
95 if (ctx == NULL) {
96 PyErr_SetFromErrno(PyExc_IOError);
97 return NULL;
100 ret = PyObject_New(PyTdbObject, &PyTdb);
101 ret->ctx = ctx;
102 ret->closed = false;
103 return (PyObject *)ret;
106 static PyObject *obj_transaction_cancel(PyTdbObject *self)
108 int ret = tdb_transaction_cancel(self->ctx);
109 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
110 Py_RETURN_NONE;
113 static PyObject *obj_transaction_commit(PyTdbObject *self)
115 int ret = tdb_transaction_commit(self->ctx);
116 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
117 Py_RETURN_NONE;
120 static PyObject *obj_transaction_recover(PyTdbObject *self)
122 int ret = tdb_transaction_recover(self->ctx);
123 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
124 Py_RETURN_NONE;
127 static PyObject *obj_transaction_start(PyTdbObject *self)
129 int ret = tdb_transaction_start(self->ctx);
130 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
131 Py_RETURN_NONE;
134 static PyObject *obj_reopen(PyTdbObject *self)
136 int ret = tdb_reopen(self->ctx);
137 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
138 Py_RETURN_NONE;
141 static PyObject *obj_lockall(PyTdbObject *self)
143 int ret = tdb_lockall(self->ctx);
144 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
145 Py_RETURN_NONE;
148 static PyObject *obj_unlockall(PyTdbObject *self)
150 int ret = tdb_unlockall(self->ctx);
151 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
152 Py_RETURN_NONE;
155 static PyObject *obj_lockall_read(PyTdbObject *self)
157 int ret = tdb_lockall_read(self->ctx);
158 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
159 Py_RETURN_NONE;
162 static PyObject *obj_unlockall_read(PyTdbObject *self)
164 int ret = tdb_unlockall_read(self->ctx);
165 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
166 Py_RETURN_NONE;
169 static PyObject *obj_close(PyTdbObject *self)
171 int ret;
172 if (self->closed)
173 Py_RETURN_NONE;
174 ret = tdb_close(self->ctx);
175 self->closed = true;
176 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
177 Py_RETURN_NONE;
180 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
182 TDB_DATA key;
183 PyObject *py_key;
184 if (!PyArg_ParseTuple(args, "O", &py_key))
185 return NULL;
187 key = PyString_AsTDB_DATA(py_key);
189 return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
192 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
194 TDB_DATA key, data;
195 PyObject *py_key, *py_data;
196 int ret;
197 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
198 return NULL;
200 key = PyString_AsTDB_DATA(py_key);
201 data = PyString_AsTDB_DATA(py_data);
203 ret = tdb_append(self->ctx, key, data);
204 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
205 Py_RETURN_NONE;
208 static PyObject *obj_firstkey(PyTdbObject *self)
210 return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
213 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
215 TDB_DATA key;
216 PyObject *py_key;
217 if (!PyArg_ParseTuple(args, "O", &py_key))
218 return NULL;
220 key = PyString_AsTDB_DATA(py_key);
222 return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
225 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
227 TDB_DATA key;
228 PyObject *py_key;
229 int ret;
230 if (!PyArg_ParseTuple(args, "O", &py_key))
231 return NULL;
233 key = PyString_AsTDB_DATA(py_key);
234 ret = tdb_delete(self->ctx, key);
235 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
236 Py_RETURN_NONE;
239 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
241 TDB_DATA key;
242 int ret;
243 PyObject *py_key;
244 if (!PyArg_ParseTuple(args, "O", &py_key))
245 return NULL;
247 key = PyString_AsTDB_DATA(py_key);
248 ret = tdb_exists(self->ctx, key);
249 if (ret != TDB_ERR_NOEXIST) {
250 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
253 return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
256 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
258 TDB_DATA key, value;
259 int ret;
260 int flag = TDB_REPLACE;
261 PyObject *py_key, *py_value;
263 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
264 return NULL;
266 key = PyString_AsTDB_DATA(py_key);
267 value = PyString_AsTDB_DATA(py_value);
269 ret = tdb_store(self->ctx, key, value, flag);
270 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
271 Py_RETURN_NONE;
275 typedef struct {
276 PyObject_HEAD
277 TDB_DATA current;
278 PyTdbObject *iteratee;
279 } PyTdbIteratorObject;
281 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
283 TDB_DATA current;
284 PyObject *ret;
285 if (self->current.dptr == NULL && self->current.dsize == 0)
286 return NULL;
287 current = self->current;
288 self->current = tdb_nextkey(self->iteratee->ctx, self->current);
289 ret = PyString_FromTDB_DATA(current);
290 return ret;
293 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
295 Py_DECREF(self->iteratee);
296 PyObject_Del(self);
299 PyTypeObject PyTdbIterator = {
300 .tp_name = "Iterator",
301 .tp_basicsize = sizeof(PyTdbIteratorObject),
302 .tp_iternext = (iternextfunc)tdb_iter_next,
303 .tp_dealloc = (destructor)tdb_iter_dealloc,
304 .tp_flags = Py_TPFLAGS_DEFAULT,
305 .tp_iter = PyObject_SelfIter,
308 static PyObject *tdb_object_iter(PyTdbObject *self)
310 PyTdbIteratorObject *ret;
312 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
313 ret->current = tdb_firstkey(self->ctx);
314 ret->iteratee = self;
315 Py_INCREF(self);
316 return (PyObject *)ret;
319 static PyObject *obj_clear(PyTdbObject *self)
321 int ret = tdb_wipe_all(self->ctx);
322 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
323 Py_RETURN_NONE;
326 static PyMethodDef tdb_object_methods[] = {
327 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
328 "S.transaction_cancel() -> None\n"
329 "Cancel the currently active transaction." },
330 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
331 "S.transaction_commit() -> None\n"
332 "Commit the currently active transaction." },
333 { "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
334 "S.transaction_recover() -> None\n"
335 "Recover the currently active transaction." },
336 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
337 "S.transaction_start() -> None\n"
338 "Start a new transaction." },
339 { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
340 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
341 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
342 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
343 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
344 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
345 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.fetch(key) -> value\n"
346 "Fetch a value." },
347 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
348 "Append data to an existing key." },
349 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
350 "Return the first key in this database." },
351 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
352 "Return the next key in this database." },
353 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
354 "Delete an entry." },
355 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
356 "Check whether key exists in this database." },
357 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
358 "Store data." },
359 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
360 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
361 "Wipe the entire database." },
362 { NULL }
365 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
367 return PyInt_FromLong(tdb_hash_size(self->ctx));
370 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
372 if (!PyInt_Check(max_dead))
373 return -1;
374 tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
375 return 0;
378 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
380 return PyInt_FromLong(tdb_map_size(self->ctx));
383 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
385 return PyInt_FromLong(tdb_get_flags(self->ctx));
388 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
390 return PyString_FromString(tdb_name(self->ctx));
393 static PyGetSetDef tdb_object_getsetters[] = {
394 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
395 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
396 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
397 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
398 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
399 { NULL }
402 static PyObject *tdb_object_repr(PyTdbObject *self)
404 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
407 static void tdb_object_dealloc(PyTdbObject *self)
409 if (!self->closed)
410 tdb_close(self->ctx);
411 PyObject_Del(self);
414 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
416 TDB_DATA tkey, val;
417 if (!PyString_Check(key)) {
418 PyErr_SetString(PyExc_TypeError, "Expected string as key");
419 return NULL;
422 tkey.dptr = (unsigned char *)PyString_AsString(key);
423 tkey.dsize = PyString_Size(key);
425 val = tdb_fetch(self->ctx, tkey);
426 if (val.dptr == NULL) {
427 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
428 return NULL;
429 } else {
430 return PyString_FromTDB_DATA(val);
434 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
436 TDB_DATA tkey, tval;
437 int ret;
438 if (!PyString_Check(key)) {
439 PyErr_SetString(PyExc_TypeError, "Expected string as key");
440 return -1;
443 tkey = PyString_AsTDB_DATA(key);
445 if (value == NULL) {
446 ret = tdb_delete(self->ctx, tkey);
447 } else {
448 if (!PyString_Check(value)) {
449 PyErr_SetString(PyExc_TypeError, "Expected string as value");
450 return -1;
453 tval = PyString_AsTDB_DATA(value);
455 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
458 if (ret != 0) {
459 PyErr_SetTDBError(self->ctx);
460 return -1;
463 return ret;
466 static PyMappingMethods tdb_object_mapping = {
467 .mp_subscript = (binaryfunc)obj_getitem,
468 .mp_ass_subscript = (objobjargproc)obj_setitem,
470 PyTypeObject PyTdb = {
471 .tp_name = "Tdb",
472 .tp_basicsize = sizeof(PyTdbObject),
473 .tp_methods = tdb_object_methods,
474 .tp_getset = tdb_object_getsetters,
475 .tp_new = py_tdb_open,
476 .tp_doc = "A TDB file",
477 .tp_repr = (reprfunc)tdb_object_repr,
478 .tp_dealloc = (destructor)tdb_object_dealloc,
479 .tp_as_mapping = &tdb_object_mapping,
480 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
481 .tp_iter = (getiterfunc)tdb_object_iter,
484 static PyMethodDef tdb_methods[] = {
485 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
486 "Open a TDB file." },
487 { NULL }
490 void inittdb(void)
492 PyObject *m;
494 if (PyType_Ready(&PyTdb) < 0)
495 return;
497 if (PyType_Ready(&PyTdbIterator) < 0)
498 return;
500 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
501 if (m == NULL)
502 return;
504 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
505 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
506 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
508 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
509 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
510 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
511 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
512 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
513 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
514 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
515 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
517 Py_INCREF(&PyTdb);
518 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
520 Py_INCREF(&PyTdbIterator);