upgradeprovision: do not hold references to messageElements
[Samba/vl.git] / lib / tdb2 / pytdb.c
blob87e5bdcd68e5cc05cb30b86dbee726b8d45a976c
1 /*
2 Unix SMB/CIFS implementation.
4 Python interface to tdb2. Simply modified from tdb1 version.
6 Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
8 Copyright (C) 2011 Rusty Russell <rusty@rustcorp.com.au>
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include <Python.h>
29 #include "replace.h"
30 #include "system/filesys.h"
32 #ifndef Py_RETURN_NONE
33 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
34 #endif
36 /* Include tdb headers */
37 #include <tdb2.h>
39 typedef struct {
40 PyObject_HEAD
41 struct tdb_context *ctx;
42 bool closed;
43 } PyTdbObject;
45 staticforward PyTypeObject PyTdb;
47 static void PyErr_SetTDBError(enum TDB_ERROR e)
49 PyErr_SetObject(PyExc_RuntimeError,
50 Py_BuildValue("(i,s)", e, tdb_errorstr(e)));
53 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
55 TDB_DATA ret;
56 ret.dptr = (unsigned char *)PyString_AsString(data);
57 ret.dsize = PyString_Size(data);
58 return ret;
61 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
63 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
64 data.dsize);
65 free(data.dptr);
66 return ret;
69 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret) \
70 if (ret != TDB_SUCCESS) { \
71 PyErr_SetTDBError(ret); \
72 return NULL; \
75 static void stderr_log(struct tdb_context *tdb,
76 enum tdb_log_level level,
77 enum TDB_ERROR ecode,
78 const char *message,
79 void *data)
81 fprintf(stderr, "%s:%s:%s\n",
82 tdb_name(tdb), tdb_errorstr(ecode), message);
85 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
87 char *name = NULL;
88 int tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
89 struct tdb_context *ctx;
90 PyTdbObject *ret;
91 union tdb_attribute logattr;
92 const char *kwnames[] = { "name", "tdb_flags", "flags", "mode", NULL };
94 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siii", (char **)kwnames, &name, &tdb_flags, &flags, &mode))
95 return NULL;
97 if (name == NULL) {
98 tdb_flags |= TDB_INTERNAL;
101 logattr.log.base.attr = TDB_ATTRIBUTE_LOG;
102 logattr.log.base.next = NULL;
103 logattr.log.fn = stderr_log;
104 ctx = tdb_open(name, tdb_flags, flags, mode, &logattr);
105 if (ctx == NULL) {
106 PyErr_SetFromErrno(PyExc_IOError);
107 return NULL;
110 ret = PyObject_New(PyTdbObject, &PyTdb);
111 if (!ret) {
112 tdb_close(ctx);
113 return NULL;
116 ret->ctx = ctx;
117 ret->closed = false;
118 return (PyObject *)ret;
121 static PyObject *obj_transaction_cancel(PyTdbObject *self)
123 tdb_transaction_cancel(self->ctx);
124 Py_RETURN_NONE;
127 static PyObject *obj_transaction_commit(PyTdbObject *self)
129 enum TDB_ERROR ret = tdb_transaction_commit(self->ctx);
130 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
131 Py_RETURN_NONE;
134 static PyObject *obj_transaction_prepare_commit(PyTdbObject *self)
136 enum TDB_ERROR ret = tdb_transaction_prepare_commit(self->ctx);
137 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
138 Py_RETURN_NONE;
141 static PyObject *obj_transaction_start(PyTdbObject *self)
143 enum TDB_ERROR ret = tdb_transaction_start(self->ctx);
144 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
145 Py_RETURN_NONE;
148 static PyObject *obj_lockall(PyTdbObject *self)
150 enum TDB_ERROR ret = tdb_lockall(self->ctx);
151 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
152 Py_RETURN_NONE;
155 static PyObject *obj_unlockall(PyTdbObject *self)
157 tdb_unlockall(self->ctx);
158 Py_RETURN_NONE;
161 static PyObject *obj_lockall_read(PyTdbObject *self)
163 enum TDB_ERROR ret = tdb_lockall_read(self->ctx);
164 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
165 Py_RETURN_NONE;
168 static PyObject *obj_unlockall_read(PyTdbObject *self)
170 tdb_unlockall_read(self->ctx);
171 Py_RETURN_NONE;
174 static PyObject *obj_close(PyTdbObject *self)
176 int ret;
177 if (self->closed)
178 Py_RETURN_NONE;
179 ret = tdb_close(self->ctx);
180 self->closed = true;
181 if (ret != 0) {
182 PyErr_SetTDBError(TDB_ERR_IO);
183 return NULL;
185 Py_RETURN_NONE;
188 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
190 TDB_DATA key, data;
191 PyObject *py_key;
192 enum TDB_ERROR ret;
193 if (!PyArg_ParseTuple(args, "O", &py_key))
194 return NULL;
196 key = PyString_AsTDB_DATA(py_key);
197 ret = tdb_fetch(self->ctx, key, &data);
198 if (ret == TDB_ERR_NOEXIST)
199 Py_RETURN_NONE;
200 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
201 return PyString_FromTDB_DATA(data);
204 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
206 TDB_DATA key, data;
207 PyObject *py_key, *py_data;
208 enum TDB_ERROR ret;
209 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
210 return NULL;
212 key = PyString_AsTDB_DATA(py_key);
213 data = PyString_AsTDB_DATA(py_data);
215 ret = tdb_append(self->ctx, key, data);
216 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
217 Py_RETURN_NONE;
220 static PyObject *obj_firstkey(PyTdbObject *self)
222 enum TDB_ERROR ret;
223 TDB_DATA key;
225 ret = tdb_firstkey(self->ctx, &key);
226 if (ret == TDB_ERR_NOEXIST)
227 Py_RETURN_NONE;
228 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
230 return PyString_FromTDB_DATA(key);
233 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
235 TDB_DATA key;
236 PyObject *py_key;
237 enum TDB_ERROR ret;
238 if (!PyArg_ParseTuple(args, "O", &py_key))
239 return NULL;
241 /* Malloc here, since tdb_nextkey frees. */
242 key.dsize = PyString_Size(py_key);
243 key.dptr = malloc(key.dsize);
244 memcpy(key.dptr, PyString_AsString(py_key), key.dsize);
246 ret = tdb_nextkey(self->ctx, &key);
247 if (ret == TDB_ERR_NOEXIST)
248 Py_RETURN_NONE;
249 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
251 return PyString_FromTDB_DATA(key);
254 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
256 TDB_DATA key;
257 PyObject *py_key;
258 enum TDB_ERROR ret;
259 if (!PyArg_ParseTuple(args, "O", &py_key))
260 return NULL;
262 key = PyString_AsTDB_DATA(py_key);
263 ret = tdb_delete(self->ctx, key);
264 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
265 Py_RETURN_NONE;
268 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
270 TDB_DATA key;
271 PyObject *py_key;
272 if (!PyArg_ParseTuple(args, "O", &py_key))
273 return NULL;
275 key = PyString_AsTDB_DATA(py_key);
276 if (tdb_exists(self->ctx, key))
277 return Py_True;
278 if (tdb_error(self->ctx) != TDB_ERR_NOEXIST)
279 PyErr_TDB_ERROR_IS_ERR_RAISE(tdb_error(self->ctx));
280 return Py_False;
283 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
285 TDB_DATA key, value;
286 enum TDB_ERROR ret;
287 int flag = TDB_REPLACE;
288 PyObject *py_key, *py_value;
290 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
291 return NULL;
293 key = PyString_AsTDB_DATA(py_key);
294 value = PyString_AsTDB_DATA(py_value);
296 ret = tdb_store(self->ctx, key, value, flag);
297 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
298 Py_RETURN_NONE;
301 static PyObject *obj_add_flag(PyTdbObject *self, PyObject *args)
303 unsigned flag;
305 if (!PyArg_ParseTuple(args, "I", &flag))
306 return NULL;
308 tdb_add_flag(self->ctx, flag);
309 Py_RETURN_NONE;
312 static PyObject *obj_remove_flag(PyTdbObject *self, PyObject *args)
314 unsigned flag;
316 if (!PyArg_ParseTuple(args, "I", &flag))
317 return NULL;
319 tdb_remove_flag(self->ctx, flag);
320 Py_RETURN_NONE;
323 typedef struct {
324 PyObject_HEAD
325 TDB_DATA current;
326 bool end;
327 PyTdbObject *iteratee;
328 } PyTdbIteratorObject;
330 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
332 enum TDB_ERROR e;
333 PyObject *ret;
334 if (self->end)
335 return NULL;
336 ret = PyString_FromStringAndSize((const char *)self->current.dptr,
337 self->current.dsize);
338 e = tdb_nextkey(self->iteratee->ctx, &self->current);
339 if (e == TDB_ERR_NOEXIST)
340 self->end = true;
341 else
342 PyErr_TDB_ERROR_IS_ERR_RAISE(e);
343 return ret;
346 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
348 Py_DECREF(self->iteratee);
349 PyObject_Del(self);
352 PyTypeObject PyTdbIterator = {
353 .tp_name = "Iterator",
354 .tp_basicsize = sizeof(PyTdbIteratorObject),
355 .tp_iternext = (iternextfunc)tdb_iter_next,
356 .tp_dealloc = (destructor)tdb_iter_dealloc,
357 .tp_flags = Py_TPFLAGS_DEFAULT,
358 .tp_iter = PyObject_SelfIter,
361 static PyObject *tdb_object_iter(PyTdbObject *self)
363 PyTdbIteratorObject *ret;
364 enum TDB_ERROR e;
366 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
367 if (!ret)
368 return NULL;
369 e = tdb_firstkey(self->ctx, &ret->current);
370 if (e == TDB_ERR_NOEXIST) {
371 ret->end = true;
372 } else {
373 PyErr_TDB_ERROR_IS_ERR_RAISE(e);
374 ret->end = false;
376 ret->iteratee = self;
377 Py_INCREF(self);
378 return (PyObject *)ret;
381 static PyObject *obj_clear(PyTdbObject *self)
383 enum TDB_ERROR ret = tdb_wipe_all(self->ctx);
384 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
385 Py_RETURN_NONE;
388 static PyObject *obj_enable_seqnum(PyTdbObject *self)
390 tdb_add_flag(self->ctx, TDB_SEQNUM);
391 Py_RETURN_NONE;
394 static PyMethodDef tdb_object_methods[] = {
395 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
396 "S.transaction_cancel() -> None\n"
397 "Cancel the currently active transaction." },
398 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
399 "S.transaction_commit() -> None\n"
400 "Commit the currently active transaction." },
401 { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
402 "S.transaction_prepare_commit() -> None\n"
403 "Prepare to commit the currently active transaction" },
404 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
405 "S.transaction_start() -> None\n"
406 "Start a new transaction." },
407 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
408 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
409 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
410 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
411 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
412 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
413 "Fetch a value." },
414 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
415 "Append data to an existing key." },
416 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
417 "Return the first key in this database." },
418 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
419 "Return the next key in this database." },
420 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
421 "Delete an entry." },
422 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
423 "Check whether key exists in this database." },
424 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
425 "Store data." },
426 { "add_flag", (PyCFunction)obj_add_flag, METH_VARARGS, "S.add_flag(flag) -> None" },
427 { "remove_flag", (PyCFunction)obj_remove_flag, METH_VARARGS, "S.remove_flag(flag) -> None" },
428 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
429 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
430 "Wipe the entire database." },
431 { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
432 "S.enable_seqnum() -> None" },
433 { NULL }
436 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
438 return PyInt_FromLong(tdb_get_flags(self->ctx));
441 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
443 return PyString_FromString(tdb_name(self->ctx));
446 static PyObject *obj_get_seqnum(PyTdbObject *self, void *closure)
448 return PyInt_FromLong(tdb_get_seqnum(self->ctx));
452 static PyGetSetDef tdb_object_getsetters[] = {
453 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
454 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
455 { (char *)"seqnum", (getter)obj_get_seqnum, NULL, NULL },
456 { NULL }
459 static PyObject *tdb_object_repr(PyTdbObject *self)
461 if (tdb_get_flags(self->ctx) & TDB_INTERNAL) {
462 return PyString_FromString("Tdb(<internal>)");
463 } else {
464 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
468 static void tdb_object_dealloc(PyTdbObject *self)
470 if (!self->closed)
471 tdb_close(self->ctx);
472 self->ob_type->tp_free(self);
475 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
477 TDB_DATA tkey, val;
478 enum TDB_ERROR ret;
480 if (!PyString_Check(key)) {
481 PyErr_SetString(PyExc_TypeError, "Expected string as key");
482 return NULL;
485 tkey.dptr = (unsigned char *)PyString_AsString(key);
486 tkey.dsize = PyString_Size(key);
488 ret = tdb_fetch(self->ctx, tkey, &val);
489 if (ret == TDB_ERR_NOEXIST) {
490 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
491 return NULL;
492 } else {
493 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
494 return PyString_FromTDB_DATA(val);
498 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
500 TDB_DATA tkey, tval;
501 enum TDB_ERROR ret;
502 if (!PyString_Check(key)) {
503 PyErr_SetString(PyExc_TypeError, "Expected string as key");
504 return -1;
507 tkey = PyString_AsTDB_DATA(key);
509 if (value == NULL) {
510 ret = tdb_delete(self->ctx, tkey);
511 } else {
512 if (!PyString_Check(value)) {
513 PyErr_SetString(PyExc_TypeError, "Expected string as value");
514 return -1;
517 tval = PyString_AsTDB_DATA(value);
519 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
522 if (ret != TDB_SUCCESS) {
523 PyErr_SetTDBError(ret);
524 return -1;
527 return ret;
530 static PyMappingMethods tdb_object_mapping = {
531 .mp_subscript = (binaryfunc)obj_getitem,
532 .mp_ass_subscript = (objobjargproc)obj_setitem,
534 static PyTypeObject PyTdb = {
535 .tp_name = "tdb.Tdb",
536 .tp_basicsize = sizeof(PyTdbObject),
537 .tp_methods = tdb_object_methods,
538 .tp_getset = tdb_object_getsetters,
539 .tp_new = py_tdb_open,
540 .tp_doc = "A TDB file",
541 .tp_repr = (reprfunc)tdb_object_repr,
542 .tp_dealloc = (destructor)tdb_object_dealloc,
543 .tp_as_mapping = &tdb_object_mapping,
544 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
545 .tp_iter = (getiterfunc)tdb_object_iter,
548 static PyMethodDef tdb_methods[] = {
549 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
550 "Open a TDB file." },
551 { NULL }
554 void inittdb(void);
555 void inittdb(void)
557 PyObject *m;
559 if (PyType_Ready(&PyTdb) < 0)
560 return;
562 if (PyType_Ready(&PyTdbIterator) < 0)
563 return;
565 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
566 if (m == NULL)
567 return;
569 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
570 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
571 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
573 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
574 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
575 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
576 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
577 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
578 PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(TDB_NOSYNC));
579 PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(TDB_SEQNUM));
580 PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING));
582 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
584 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
586 Py_INCREF(&PyTdb);
587 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
589 Py_INCREF(&PyTdbIterator);