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/>.
32 /* Include tdb headers */
43 PyAPI_DATA(PyTypeObject) PyTdb;
45 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
47 PyErr_SetObject(PyExc_RuntimeError,
48 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
51 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
54 ret.dptr = (unsigned char *)PyString_AsString(data);
55 ret.dsize = PyString_Size(data);
59 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
61 if (data.dptr == NULL && data.dsize == 0) {
64 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
71 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
73 PyErr_SetTDBError(tdb); \
77 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
80 int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
83 const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
85 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
88 ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
90 PyErr_SetFromErrno(PyExc_IOError);
94 ret = PyObject_New(PyTdbObject, &PyTdb);
96 return (PyObject *)ret;
99 static PyObject *obj_transaction_cancel(PyTdbObject *self)
101 int ret = tdb_transaction_cancel(self->ctx);
102 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
106 static PyObject *obj_transaction_commit(PyTdbObject *self)
108 int ret = tdb_transaction_commit(self->ctx);
109 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
113 static PyObject *obj_transaction_recover(PyTdbObject *self)
115 int ret = tdb_transaction_recover(self->ctx);
116 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
120 static PyObject *obj_transaction_start(PyTdbObject *self)
122 int ret = tdb_transaction_start(self->ctx);
123 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
127 static PyObject *obj_reopen(PyTdbObject *self)
129 int ret = tdb_reopen(self->ctx);
130 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
134 static PyObject *obj_lockall(PyTdbObject *self)
136 int ret = tdb_lockall(self->ctx);
137 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
141 static PyObject *obj_unlockall(PyTdbObject *self)
143 int ret = tdb_unlockall(self->ctx);
144 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
148 static PyObject *obj_lockall_read(PyTdbObject *self)
150 int ret = tdb_lockall_read(self->ctx);
151 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
155 static PyObject *obj_unlockall_read(PyTdbObject *self)
157 int ret = tdb_unlockall_read(self->ctx);
158 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
162 static PyObject *obj_close(PyTdbObject *self)
164 int ret = tdb_close(self->ctx);
165 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
169 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
173 if (!PyArg_ParseTuple(args, "O", &py_key))
176 key = PyString_AsTDB_DATA(py_key);
178 return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
181 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
184 PyObject *py_key, *py_data;
186 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
189 key = PyString_AsTDB_DATA(py_key);
190 data = PyString_AsTDB_DATA(py_data);
192 ret = tdb_append(self->ctx, key, data);
193 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
197 static PyObject *obj_firstkey(PyTdbObject *self)
199 return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
202 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
206 if (!PyArg_ParseTuple(args, "O", &py_key))
209 key = PyString_AsTDB_DATA(py_key);
211 return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
214 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
219 if (!PyArg_ParseTuple(args, "O", &py_key))
222 key = PyString_AsTDB_DATA(py_key);
223 ret = tdb_delete(self->ctx, key);
224 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
228 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
233 if (!PyArg_ParseTuple(args, "O", &py_key))
236 key = PyString_AsTDB_DATA(py_key);
237 ret = tdb_exists(self->ctx, key);
238 if (ret != TDB_ERR_NOEXIST) {
239 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
242 return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
245 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
249 int flag = TDB_REPLACE;
250 PyObject *py_key, *py_value;
252 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
255 key = PyString_AsTDB_DATA(py_key);
256 value = PyString_AsTDB_DATA(py_value);
258 ret = tdb_store(self->ctx, key, value, flag);
259 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
267 PyTdbObject *iteratee;
268 } PyTdbIteratorObject;
270 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
274 if (self->current.dptr == NULL && self->current.dsize == 0)
276 current = self->current;
277 self->current = tdb_nextkey(self->iteratee->ctx, self->current);
278 ret = PyString_FromTDB_DATA(current);
282 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
284 Py_DECREF(self->iteratee);
288 PyTypeObject PyTdbIterator = {
289 .tp_name = "Iterator",
290 .tp_basicsize = sizeof(PyTdbIteratorObject),
291 .tp_iternext = (iternextfunc)tdb_iter_next,
292 .tp_dealloc = (destructor)tdb_iter_dealloc,
293 .tp_flags = Py_TPFLAGS_DEFAULT,
294 .tp_iter = PyObject_SelfIter,
297 static PyObject *tdb_object_iter(PyTdbObject *self)
299 PyTdbIteratorObject *ret;
301 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
302 ret->current = tdb_firstkey(self->ctx);
303 ret->iteratee = self;
305 return (PyObject *)ret;
308 static PyObject *obj_clear(PyTdbObject *self)
310 int ret = tdb_wipe_all(self->ctx);
311 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
315 static PyMethodDef tdb_object_methods[] = {
316 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
317 "S.transaction_cancel() -> None\n"
318 "Cancel the currently active transaction." },
319 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
320 "S.transaction_commit() -> None\n"
321 "Commit the currently active transaction." },
322 { "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
323 "S.transaction_recover() -> None\n"
324 "Recover the currently active transaction." },
325 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
326 "S.transaction_start() -> None\n"
327 "Start a new transaction." },
328 { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
329 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
330 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
331 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
332 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
333 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
334 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.fetch(key) -> value\n"
336 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
337 "Append data to an existing key." },
338 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
339 "Return the first key in this database." },
340 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
341 "Return the next key in this database." },
342 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
343 "Delete an entry." },
344 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
345 "Check whether key exists in this database." },
346 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
348 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
349 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
350 "Wipe the entire database." },
354 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
356 return PyInt_FromLong(tdb_hash_size(self->ctx));
359 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
361 if (!PyInt_Check(max_dead))
363 tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
367 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
369 return PyInt_FromLong(tdb_map_size(self->ctx));
372 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
374 return PyInt_FromLong(tdb_get_flags(self->ctx));
377 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
379 return PyString_FromString(tdb_name(self->ctx));
382 static PyGetSetDef tdb_object_getsetters[] = {
383 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
384 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
385 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
386 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
387 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
391 static PyObject *tdb_object_repr(PyTdbObject *self)
393 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
396 static void tdb_object_dealloc(PyTdbObject *self)
398 tdb_close(self->ctx);
402 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
405 if (!PyString_Check(key)) {
406 PyErr_SetString(PyExc_TypeError, "Expected string as key");
410 tkey.dptr = (unsigned char *)PyString_AsString(key);
411 tkey.dsize = PyString_Size(key);
413 val = tdb_fetch(self->ctx, tkey);
414 if (val.dptr == NULL) {
415 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
418 return PyString_FromTDB_DATA(val);
422 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
426 if (!PyString_Check(key)) {
427 PyErr_SetString(PyExc_TypeError, "Expected string as key");
431 tkey = PyString_AsTDB_DATA(key);
434 ret = tdb_delete(self->ctx, tkey);
436 if (!PyString_Check(value)) {
437 PyErr_SetString(PyExc_TypeError, "Expected string as value");
441 tval = PyString_AsTDB_DATA(value);
443 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
447 PyErr_SetTDBError(self->ctx);
454 static PyMappingMethods tdb_object_mapping = {
455 .mp_subscript = (binaryfunc)obj_getitem,
456 .mp_ass_subscript = (objobjargproc)obj_setitem,
458 PyTypeObject PyTdb = {
460 .tp_basicsize = sizeof(PyTdbObject),
461 .tp_methods = tdb_object_methods,
462 .tp_getset = tdb_object_getsetters,
463 .tp_new = py_tdb_open,
464 .tp_doc = "A TDB file",
465 .tp_repr = (reprfunc)tdb_object_repr,
466 .tp_dealloc = (destructor)tdb_object_dealloc,
467 .tp_as_mapping = &tdb_object_mapping,
468 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
469 .tp_iter = (getiterfunc)tdb_object_iter,
472 static PyMethodDef tdb_methods[] = {
473 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
474 "Open a TDB file." },
482 if (PyType_Ready(&PyTdb) < 0)
485 if (PyType_Ready(&PyTdbIterator) < 0)
488 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
492 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
493 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
494 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
496 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
497 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
498 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
499 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
500 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
501 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
502 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
503 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
506 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
508 Py_INCREF(&PyTdbIterator);