s3-lib: Remove redefinition of RL_COMPLETION_CAST.
[Samba/gbeck.git] / lib / tdb / pytdb.c
blob7a9205b815cbaf8c580fcb3fb1d679bf600ce11e
1 /*
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
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 "replace.h"
28 #include "system/filesys.h"
30 #include <Python.h>
31 #ifndef Py_RETURN_NONE
32 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
33 #endif
35 /* Include tdb headers */
36 #include <tdb.h>
38 typedef struct {
39 PyObject_HEAD
40 TDB_CONTEXT *ctx;
41 bool closed;
42 } PyTdbObject;
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)
54 TDB_DATA ret;
55 ret.dptr = (unsigned char *)PyString_AsString(data);
56 ret.dsize = PyString_Size(data);
57 return ret;
60 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
62 if (data.dptr == NULL && data.dsize == 0) {
63 Py_RETURN_NONE;
64 } else {
65 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
66 data.dsize);
67 free(data.dptr);
68 return ret;
72 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
73 if (ret != 0) { \
74 PyErr_SetTDBError(tdb); \
75 return NULL; \
78 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
80 char *name;
81 int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
82 TDB_CONTEXT *ctx;
83 PyTdbObject *ret;
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))
87 return NULL;
89 ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
90 if (ctx == NULL) {
91 PyErr_SetFromErrno(PyExc_IOError);
92 return NULL;
95 ret = PyObject_New(PyTdbObject, &PyTdb);
96 ret->ctx = ctx;
97 ret->closed = false;
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);
105 Py_RETURN_NONE;
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);
112 Py_RETURN_NONE;
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);
119 Py_RETURN_NONE;
122 static PyObject *obj_reopen(PyTdbObject *self)
124 int ret = tdb_reopen(self->ctx);
125 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
126 Py_RETURN_NONE;
129 static PyObject *obj_lockall(PyTdbObject *self)
131 int ret = tdb_lockall(self->ctx);
132 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
133 Py_RETURN_NONE;
136 static PyObject *obj_unlockall(PyTdbObject *self)
138 int ret = tdb_unlockall(self->ctx);
139 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
140 Py_RETURN_NONE;
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);
147 Py_RETURN_NONE;
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);
154 Py_RETURN_NONE;
157 static PyObject *obj_close(PyTdbObject *self)
159 int ret;
160 if (self->closed)
161 Py_RETURN_NONE;
162 ret = tdb_close(self->ctx);
163 self->closed = true;
164 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
165 Py_RETURN_NONE;
168 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
170 TDB_DATA key;
171 PyObject *py_key;
172 if (!PyArg_ParseTuple(args, "O", &py_key))
173 return NULL;
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)
182 TDB_DATA key, data;
183 PyObject *py_key, *py_data;
184 int ret;
185 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
186 return NULL;
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);
193 Py_RETURN_NONE;
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)
203 TDB_DATA key;
204 PyObject *py_key;
205 if (!PyArg_ParseTuple(args, "O", &py_key))
206 return NULL;
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)
215 TDB_DATA key;
216 PyObject *py_key;
217 int ret;
218 if (!PyArg_ParseTuple(args, "O", &py_key))
219 return NULL;
221 key = PyString_AsTDB_DATA(py_key);
222 ret = tdb_delete(self->ctx, key);
223 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
224 Py_RETURN_NONE;
227 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
229 TDB_DATA key;
230 int ret;
231 PyObject *py_key;
232 if (!PyArg_ParseTuple(args, "O", &py_key))
233 return NULL;
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)
246 TDB_DATA key, value;
247 int ret;
248 int flag = TDB_REPLACE;
249 PyObject *py_key, *py_value;
251 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
252 return NULL;
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);
259 Py_RETURN_NONE;
263 typedef struct {
264 PyObject_HEAD
265 TDB_DATA current;
266 PyTdbObject *iteratee;
267 } PyTdbIteratorObject;
269 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
271 TDB_DATA current;
272 PyObject *ret;
273 if (self->current.dptr == NULL && self->current.dsize == 0)
274 return NULL;
275 current = self->current;
276 self->current = tdb_nextkey(self->iteratee->ctx, self->current);
277 ret = PyString_FromTDB_DATA(current);
278 return ret;
281 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
283 Py_DECREF(self->iteratee);
284 PyObject_Del(self);
287 PyTypeObject PyTdbIterator = {
288 .tp_name = "Iterator",
289 .tp_basicsize = sizeof(PyTdbIteratorObject),
290 .tp_iternext = (iternextfunc)tdb_iter_next,
291 .tp_dealloc = (destructor)tdb_iter_dealloc,
292 .tp_flags = Py_TPFLAGS_DEFAULT,
293 .tp_iter = PyObject_SelfIter,
296 static PyObject *tdb_object_iter(PyTdbObject *self)
298 PyTdbIteratorObject *ret;
300 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
301 ret->current = tdb_firstkey(self->ctx);
302 ret->iteratee = self;
303 Py_INCREF(self);
304 return (PyObject *)ret;
307 static PyObject *obj_clear(PyTdbObject *self)
309 int ret = tdb_wipe_all(self->ctx);
310 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
311 Py_RETURN_NONE;
314 static PyMethodDef tdb_object_methods[] = {
315 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
316 "S.transaction_cancel() -> None\n"
317 "Cancel the currently active transaction." },
318 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
319 "S.transaction_commit() -> None\n"
320 "Commit the currently active transaction." },
321 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
322 "S.transaction_start() -> None\n"
323 "Start a new transaction." },
324 { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
325 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
326 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
327 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
328 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
329 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
330 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
331 "Fetch a value." },
332 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
333 "Append data to an existing key." },
334 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
335 "Return the first key in this database." },
336 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
337 "Return the next key in this database." },
338 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
339 "Delete an entry." },
340 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
341 "Check whether key exists in this database." },
342 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
343 "Store data." },
344 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
345 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
346 "Wipe the entire database." },
347 { NULL }
350 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
352 return PyInt_FromLong(tdb_hash_size(self->ctx));
355 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
357 if (!PyInt_Check(max_dead))
358 return -1;
359 tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
360 return 0;
363 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
365 return PyInt_FromLong(tdb_map_size(self->ctx));
368 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
370 return PyInt_FromLong(tdb_get_flags(self->ctx));
373 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
375 return PyString_FromString(tdb_name(self->ctx));
378 static PyGetSetDef tdb_object_getsetters[] = {
379 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
380 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
381 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
382 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
383 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
384 { NULL }
387 static PyObject *tdb_object_repr(PyTdbObject *self)
389 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
392 static void tdb_object_dealloc(PyTdbObject *self)
394 if (!self->closed)
395 tdb_close(self->ctx);
396 PyObject_Del(self);
399 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
401 TDB_DATA tkey, val;
402 if (!PyString_Check(key)) {
403 PyErr_SetString(PyExc_TypeError, "Expected string as key");
404 return NULL;
407 tkey.dptr = (unsigned char *)PyString_AsString(key);
408 tkey.dsize = PyString_Size(key);
410 val = tdb_fetch(self->ctx, tkey);
411 if (val.dptr == NULL) {
412 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
413 return NULL;
414 } else {
415 return PyString_FromTDB_DATA(val);
419 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
421 TDB_DATA tkey, tval;
422 int ret;
423 if (!PyString_Check(key)) {
424 PyErr_SetString(PyExc_TypeError, "Expected string as key");
425 return -1;
428 tkey = PyString_AsTDB_DATA(key);
430 if (value == NULL) {
431 ret = tdb_delete(self->ctx, tkey);
432 } else {
433 if (!PyString_Check(value)) {
434 PyErr_SetString(PyExc_TypeError, "Expected string as value");
435 return -1;
438 tval = PyString_AsTDB_DATA(value);
440 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
443 if (ret != 0) {
444 PyErr_SetTDBError(self->ctx);
445 return -1;
448 return ret;
451 static PyMappingMethods tdb_object_mapping = {
452 .mp_subscript = (binaryfunc)obj_getitem,
453 .mp_ass_subscript = (objobjargproc)obj_setitem,
455 PyTypeObject PyTdb = {
456 .tp_name = "Tdb",
457 .tp_basicsize = sizeof(PyTdbObject),
458 .tp_methods = tdb_object_methods,
459 .tp_getset = tdb_object_getsetters,
460 .tp_new = py_tdb_open,
461 .tp_doc = "A TDB file",
462 .tp_repr = (reprfunc)tdb_object_repr,
463 .tp_dealloc = (destructor)tdb_object_dealloc,
464 .tp_as_mapping = &tdb_object_mapping,
465 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
466 .tp_iter = (getiterfunc)tdb_object_iter,
469 static PyMethodDef tdb_methods[] = {
470 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
471 "Open a TDB file." },
472 { NULL }
475 void inittdb(void)
477 PyObject *m;
479 if (PyType_Ready(&PyTdb) < 0)
480 return;
482 if (PyType_Ready(&PyTdbIterator) < 0)
483 return;
485 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
486 if (m == NULL)
487 return;
489 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
490 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
491 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
493 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
494 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
495 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
496 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
497 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
498 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
499 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
500 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
502 Py_INCREF(&PyTdb);
503 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
505 Py_INCREF(&PyTdbIterator);