2 Unix SMB/CIFS implementation.
4 Python 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
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/>.
28 #include "system/filesys.h"
31 #ifndef Py_RETURN_NONE
32 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
35 /* Include tdb headers */
44 PyAPI_DATA(PyTypeObject) PyTdb;
46 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
48 PyErr_SetObject(PyExc_RuntimeError,
49 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
52 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
55 ret.dptr = (unsigned char *)PyString_AsString(data);
56 ret.dsize = PyString_Size(data);
60 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
62 if (data.dptr == NULL && data.dsize == 0) {
65 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
72 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
74 PyErr_SetTDBError(tdb); \
78 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
81 int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
84 const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
86 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
89 ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
91 PyErr_SetFromErrno(PyExc_IOError);
95 ret = PyObject_New(PyTdbObject, &PyTdb);
98 return (PyObject *)ret;
101 static PyObject *obj_transaction_cancel(PyTdbObject *self)
103 int ret = tdb_transaction_cancel(self->ctx);
104 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
108 static PyObject *obj_transaction_commit(PyTdbObject *self)
110 int ret = tdb_transaction_commit(self->ctx);
111 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
115 static PyObject *obj_transaction_start(PyTdbObject *self)
117 int ret = tdb_transaction_start(self->ctx);
118 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
122 static PyObject *obj_reopen(PyTdbObject *self)
124 int ret = tdb_reopen(self->ctx);
125 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
129 static PyObject *obj_lockall(PyTdbObject *self)
131 int ret = tdb_lockall(self->ctx);
132 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
136 static PyObject *obj_unlockall(PyTdbObject *self)
138 int ret = tdb_unlockall(self->ctx);
139 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
143 static PyObject *obj_lockall_read(PyTdbObject *self)
145 int ret = tdb_lockall_read(self->ctx);
146 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
150 static PyObject *obj_unlockall_read(PyTdbObject *self)
152 int ret = tdb_unlockall_read(self->ctx);
153 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
157 static PyObject *obj_close(PyTdbObject *self)
162 ret = tdb_close(self->ctx);
164 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
168 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
172 if (!PyArg_ParseTuple(args, "O", &py_key))
175 key = PyString_AsTDB_DATA(py_key);
177 return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
180 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
183 PyObject *py_key, *py_data;
185 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
188 key = PyString_AsTDB_DATA(py_key);
189 data = PyString_AsTDB_DATA(py_data);
191 ret = tdb_append(self->ctx, key, data);
192 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
196 static PyObject *obj_firstkey(PyTdbObject *self)
198 return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
201 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
205 if (!PyArg_ParseTuple(args, "O", &py_key))
208 key = PyString_AsTDB_DATA(py_key);
210 return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
213 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
218 if (!PyArg_ParseTuple(args, "O", &py_key))
221 key = PyString_AsTDB_DATA(py_key);
222 ret = tdb_delete(self->ctx, key);
223 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
227 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
232 if (!PyArg_ParseTuple(args, "O", &py_key))
235 key = PyString_AsTDB_DATA(py_key);
236 ret = tdb_exists(self->ctx, key);
237 if (ret != TDB_ERR_NOEXIST) {
238 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
241 return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
244 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
248 int flag = TDB_REPLACE;
249 PyObject *py_key, *py_value;
251 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
254 key = PyString_AsTDB_DATA(py_key);
255 value = PyString_AsTDB_DATA(py_value);
257 ret = tdb_store(self->ctx, key, value, flag);
258 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
262 static PyObject *obj_add_flags(PyTdbObject *self, PyObject *args)
266 if (!PyArg_ParseTuple(args, "I", &flags))
269 tdb_add_flags(self->ctx, flags);
273 static PyObject *obj_remove_flags(PyTdbObject *self, PyObject *args)
277 if (!PyArg_ParseTuple(args, "I", &flags))
280 tdb_remove_flags(self->ctx, flags);
287 PyTdbObject *iteratee;
288 } PyTdbIteratorObject;
290 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
294 if (self->current.dptr == NULL && self->current.dsize == 0)
296 current = self->current;
297 self->current = tdb_nextkey(self->iteratee->ctx, self->current);
298 ret = PyString_FromTDB_DATA(current);
302 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
304 Py_DECREF(self->iteratee);
308 PyTypeObject PyTdbIterator = {
309 .tp_name = "Iterator",
310 .tp_basicsize = sizeof(PyTdbIteratorObject),
311 .tp_iternext = (iternextfunc)tdb_iter_next,
312 .tp_dealloc = (destructor)tdb_iter_dealloc,
313 .tp_flags = Py_TPFLAGS_DEFAULT,
314 .tp_iter = PyObject_SelfIter,
317 static PyObject *tdb_object_iter(PyTdbObject *self)
319 PyTdbIteratorObject *ret;
321 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
322 ret->current = tdb_firstkey(self->ctx);
323 ret->iteratee = self;
325 return (PyObject *)ret;
328 static PyObject *obj_clear(PyTdbObject *self)
330 int ret = tdb_wipe_all(self->ctx);
331 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
335 static PyMethodDef tdb_object_methods[] = {
336 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
337 "S.transaction_cancel() -> None\n"
338 "Cancel the currently active transaction." },
339 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
340 "S.transaction_commit() -> None\n"
341 "Commit the currently active transaction." },
342 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
343 "S.transaction_start() -> None\n"
344 "Start a new transaction." },
345 { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
346 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
347 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
348 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
349 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
350 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
351 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
353 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
354 "Append data to an existing key." },
355 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
356 "Return the first key in this database." },
357 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
358 "Return the next key in this database." },
359 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
360 "Delete an entry." },
361 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
362 "Check whether key exists in this database." },
363 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
365 { "add_flags", (PyCFunction)obj_add_flags, METH_VARARGS, "S.add_flags(flags) -> None" },
366 { "remove_flags", (PyCFunction)obj_remove_flags, METH_VARARGS, "S.remove_flags(flags) -> None" },
367 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
368 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
369 "Wipe the entire database." },
373 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
375 return PyInt_FromLong(tdb_hash_size(self->ctx));
378 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
380 if (!PyInt_Check(max_dead))
382 tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
386 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
388 return PyInt_FromLong(tdb_map_size(self->ctx));
391 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
393 return PyInt_FromLong(tdb_get_flags(self->ctx));
396 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
398 return PyString_FromString(tdb_name(self->ctx));
401 static PyGetSetDef tdb_object_getsetters[] = {
402 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
403 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
404 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
405 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
406 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
410 static PyObject *tdb_object_repr(PyTdbObject *self)
412 return PyString_FromFormat("Tdb('%s')",
413 (tdb_get_flags(self->ctx) & TDB_INTERNAL) ? "<internal>"
414 : tdb_name(self->ctx));
417 static void tdb_object_dealloc(PyTdbObject *self)
420 tdb_close(self->ctx);
424 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
427 if (!PyString_Check(key)) {
428 PyErr_SetString(PyExc_TypeError, "Expected string as key");
432 tkey.dptr = (unsigned char *)PyString_AsString(key);
433 tkey.dsize = PyString_Size(key);
435 val = tdb_fetch(self->ctx, tkey);
436 if (val.dptr == NULL) {
437 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
440 return PyString_FromTDB_DATA(val);
444 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
448 if (!PyString_Check(key)) {
449 PyErr_SetString(PyExc_TypeError, "Expected string as key");
453 tkey = PyString_AsTDB_DATA(key);
456 ret = tdb_delete(self->ctx, tkey);
458 if (!PyString_Check(value)) {
459 PyErr_SetString(PyExc_TypeError, "Expected string as value");
463 tval = PyString_AsTDB_DATA(value);
465 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
469 PyErr_SetTDBError(self->ctx);
476 static PyMappingMethods tdb_object_mapping = {
477 .mp_subscript = (binaryfunc)obj_getitem,
478 .mp_ass_subscript = (objobjargproc)obj_setitem,
480 PyTypeObject PyTdb = {
482 .tp_basicsize = sizeof(PyTdbObject),
483 .tp_methods = tdb_object_methods,
484 .tp_getset = tdb_object_getsetters,
485 .tp_new = py_tdb_open,
486 .tp_doc = "A TDB file",
487 .tp_repr = (reprfunc)tdb_object_repr,
488 .tp_dealloc = (destructor)tdb_object_dealloc,
489 .tp_as_mapping = &tdb_object_mapping,
490 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
491 .tp_iter = (getiterfunc)tdb_object_iter,
494 static PyMethodDef tdb_methods[] = {
495 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
496 "Open a TDB file." },
504 if (PyType_Ready(&PyTdb) < 0)
507 if (PyType_Ready(&PyTdbIterator) < 0)
510 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
514 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
515 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
516 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
518 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
519 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
520 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
521 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
522 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
523 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
524 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
525 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
528 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
530 Py_INCREF(&PyTdbIterator);