2 Unix SMB/CIFS implementation.
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
39 /* Include tdb headers */
48 PyAPI_DATA(PyTypeObject) PyTdb;
50 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
52 PyErr_SetObject(PyExc_RuntimeError,
53 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
56 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
59 ret.dptr = (unsigned char *)PyString_AsString(data);
60 ret.dsize = PyString_Size(data);
64 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
66 if (data.dptr == NULL && data.dsize == 0) {
69 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
76 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
78 PyErr_SetTDBError(tdb); \
82 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
85 int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
88 const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
90 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
93 ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
95 PyErr_SetFromErrno(PyExc_IOError);
99 ret = PyObject_New(PyTdbObject, &PyTdb);
102 return (PyObject *)ret;
105 static PyObject *obj_transaction_cancel(PyTdbObject *self)
107 int ret = tdb_transaction_cancel(self->ctx);
108 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
112 static PyObject *obj_transaction_commit(PyTdbObject *self)
114 int ret = tdb_transaction_commit(self->ctx);
115 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
119 static PyObject *obj_transaction_recover(PyTdbObject *self)
121 int ret = tdb_transaction_recover(self->ctx);
122 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
126 static PyObject *obj_transaction_start(PyTdbObject *self)
128 int ret = tdb_transaction_start(self->ctx);
129 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
133 static PyObject *obj_reopen(PyTdbObject *self)
135 int ret = tdb_reopen(self->ctx);
136 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
140 static PyObject *obj_lockall(PyTdbObject *self)
142 int ret = tdb_lockall(self->ctx);
143 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
147 static PyObject *obj_unlockall(PyTdbObject *self)
149 int ret = tdb_unlockall(self->ctx);
150 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
154 static PyObject *obj_lockall_read(PyTdbObject *self)
156 int ret = tdb_lockall_read(self->ctx);
157 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
161 static PyObject *obj_unlockall_read(PyTdbObject *self)
163 int ret = tdb_unlockall_read(self->ctx);
164 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
168 static PyObject *obj_close(PyTdbObject *self)
173 ret = tdb_close(self->ctx);
175 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
179 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
183 if (!PyArg_ParseTuple(args, "O", &py_key))
186 key = PyString_AsTDB_DATA(py_key);
188 return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
191 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
194 PyObject *py_key, *py_data;
196 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
199 key = PyString_AsTDB_DATA(py_key);
200 data = PyString_AsTDB_DATA(py_data);
202 ret = tdb_append(self->ctx, key, data);
203 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
207 static PyObject *obj_firstkey(PyTdbObject *self)
209 return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
212 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
216 if (!PyArg_ParseTuple(args, "O", &py_key))
219 key = PyString_AsTDB_DATA(py_key);
221 return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
224 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
229 if (!PyArg_ParseTuple(args, "O", &py_key))
232 key = PyString_AsTDB_DATA(py_key);
233 ret = tdb_delete(self->ctx, key);
234 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
238 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
243 if (!PyArg_ParseTuple(args, "O", &py_key))
246 key = PyString_AsTDB_DATA(py_key);
247 ret = tdb_exists(self->ctx, key);
248 if (ret != TDB_ERR_NOEXIST) {
249 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
252 return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
255 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
259 int flag = TDB_REPLACE;
260 PyObject *py_key, *py_value;
262 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
265 key = PyString_AsTDB_DATA(py_key);
266 value = PyString_AsTDB_DATA(py_value);
268 ret = tdb_store(self->ctx, key, value, flag);
269 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
277 PyTdbObject *iteratee;
278 } PyTdbIteratorObject;
280 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
284 if (self->current.dptr == NULL && self->current.dsize == 0)
286 current = self->current;
287 self->current = tdb_nextkey(self->iteratee->ctx, self->current);
288 ret = PyString_FromTDB_DATA(current);
292 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
294 Py_DECREF(self->iteratee);
298 PyTypeObject PyTdbIterator = {
299 .tp_name = "Iterator",
300 .tp_basicsize = sizeof(PyTdbIteratorObject),
301 .tp_iternext = (iternextfunc)tdb_iter_next,
302 .tp_dealloc = (destructor)tdb_iter_dealloc,
303 .tp_flags = Py_TPFLAGS_DEFAULT,
304 .tp_iter = PyObject_SelfIter,
307 static PyObject *tdb_object_iter(PyTdbObject *self)
309 PyTdbIteratorObject *ret;
311 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
312 ret->current = tdb_firstkey(self->ctx);
313 ret->iteratee = self;
315 return (PyObject *)ret;
318 static PyObject *obj_clear(PyTdbObject *self)
320 int ret = tdb_wipe_all(self->ctx);
321 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
325 static PyMethodDef tdb_object_methods[] = {
326 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
327 "S.transaction_cancel() -> None\n"
328 "Cancel the currently active transaction." },
329 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
330 "S.transaction_commit() -> None\n"
331 "Commit the currently active transaction." },
332 { "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
333 "S.transaction_recover() -> None\n"
334 "Recover the currently active transaction." },
335 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
336 "S.transaction_start() -> None\n"
337 "Start a new transaction." },
338 { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
339 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
340 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
341 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
342 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
343 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
344 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.fetch(key) -> value\n"
346 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
347 "Append data to an existing key." },
348 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
349 "Return the first key in this database." },
350 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
351 "Return the next key in this database." },
352 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
353 "Delete an entry." },
354 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
355 "Check whether key exists in this database." },
356 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
358 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
359 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
360 "Wipe the entire database." },
364 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
366 return PyInt_FromLong(tdb_hash_size(self->ctx));
369 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
371 if (!PyInt_Check(max_dead))
373 tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
377 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
379 return PyInt_FromLong(tdb_map_size(self->ctx));
382 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
384 return PyInt_FromLong(tdb_get_flags(self->ctx));
387 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
389 return PyString_FromString(tdb_name(self->ctx));
392 static PyGetSetDef tdb_object_getsetters[] = {
393 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
394 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
395 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
396 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
397 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
401 static PyObject *tdb_object_repr(PyTdbObject *self)
403 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
406 static void tdb_object_dealloc(PyTdbObject *self)
409 tdb_close(self->ctx);
413 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
416 if (!PyString_Check(key)) {
417 PyErr_SetString(PyExc_TypeError, "Expected string as key");
421 tkey.dptr = (unsigned char *)PyString_AsString(key);
422 tkey.dsize = PyString_Size(key);
424 val = tdb_fetch(self->ctx, tkey);
425 if (val.dptr == NULL) {
426 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
429 return PyString_FromTDB_DATA(val);
433 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
437 if (!PyString_Check(key)) {
438 PyErr_SetString(PyExc_TypeError, "Expected string as key");
442 tkey = PyString_AsTDB_DATA(key);
445 ret = tdb_delete(self->ctx, tkey);
447 if (!PyString_Check(value)) {
448 PyErr_SetString(PyExc_TypeError, "Expected string as value");
452 tval = PyString_AsTDB_DATA(value);
454 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
458 PyErr_SetTDBError(self->ctx);
465 static PyMappingMethods tdb_object_mapping = {
466 .mp_subscript = (binaryfunc)obj_getitem,
467 .mp_ass_subscript = (objobjargproc)obj_setitem,
469 PyTypeObject PyTdb = {
471 .tp_basicsize = sizeof(PyTdbObject),
472 .tp_methods = tdb_object_methods,
473 .tp_getset = tdb_object_getsetters,
474 .tp_new = py_tdb_open,
475 .tp_doc = "A TDB file",
476 .tp_repr = (reprfunc)tdb_object_repr,
477 .tp_dealloc = (destructor)tdb_object_dealloc,
478 .tp_as_mapping = &tdb_object_mapping,
479 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
480 .tp_iter = (getiterfunc)tdb_object_iter,
483 static PyMethodDef tdb_methods[] = {
484 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
485 "Open a TDB file." },
493 if (PyType_Ready(&PyTdb) < 0)
496 if (PyType_Ready(&PyTdbIterator) < 0)
499 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
503 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
504 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
505 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
507 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
508 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
509 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
510 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
511 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
512 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
513 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
514 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
517 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
519 Py_INCREF(&PyTdbIterator);