Fix all warnings in source3 with gcc4.3.
[Samba/nascimento.git] / lib / tdb / pytdb.c
blobb7087c4bcc5ca0af5e4f31242bd199bf88f130d9
1 /*
2 Unix SMB/CIFS implementation.
4 Swig 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 <Python.h>
28 #ifdef HAVE_FSTAT
29 #undef HAVE_FSTAT
30 #endif
32 /* Include tdb headers */
33 #include <stdint.h>
34 #include <signal.h>
35 #include <tdb.h>
36 #include <fcntl.h>
37 #include <stdbool.h>
39 typedef struct {
40 PyObject_HEAD
41 TDB_CONTEXT *ctx;
42 bool closed;
43 } PyTdbObject;
45 PyAPI_DATA(PyTypeObject) PyTdb;
47 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
49 PyErr_SetObject(PyExc_RuntimeError,
50 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
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 if (data.dptr == NULL && data.dsize == 0) {
64 return Py_None;
65 } else {
66 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
67 data.dsize);
68 free(data.dptr);
69 return ret;
73 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
74 if (ret != 0) { \
75 PyErr_SetTDBError(tdb); \
76 return NULL; \
79 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
81 char *name;
82 int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
83 TDB_CONTEXT *ctx;
84 PyTdbObject *ret;
85 const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
87 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
88 return NULL;
90 ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
91 if (ctx == NULL) {
92 PyErr_SetFromErrno(PyExc_IOError);
93 return NULL;
96 ret = PyObject_New(PyTdbObject, &PyTdb);
97 ret->ctx = ctx;
98 ret->closed = false;
99 return (PyObject *)ret;
102 static PyObject *obj_transaction_cancel(PyTdbObject *self)
104 int ret = tdb_transaction_cancel(self->ctx);
105 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
106 return Py_None;
109 static PyObject *obj_transaction_commit(PyTdbObject *self)
111 int ret = tdb_transaction_commit(self->ctx);
112 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
113 return Py_None;
116 static PyObject *obj_transaction_recover(PyTdbObject *self)
118 int ret = tdb_transaction_recover(self->ctx);
119 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
120 return Py_None;
123 static PyObject *obj_transaction_start(PyTdbObject *self)
125 int ret = tdb_transaction_start(self->ctx);
126 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
127 return Py_None;
130 static PyObject *obj_reopen(PyTdbObject *self)
132 int ret = tdb_reopen(self->ctx);
133 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
134 return Py_None;
137 static PyObject *obj_lockall(PyTdbObject *self)
139 int ret = tdb_lockall(self->ctx);
140 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
141 return Py_None;
144 static PyObject *obj_unlockall(PyTdbObject *self)
146 int ret = tdb_unlockall(self->ctx);
147 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
148 return Py_None;
151 static PyObject *obj_lockall_read(PyTdbObject *self)
153 int ret = tdb_lockall_read(self->ctx);
154 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
155 return Py_None;
158 static PyObject *obj_unlockall_read(PyTdbObject *self)
160 int ret = tdb_unlockall_read(self->ctx);
161 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
162 return Py_None;
165 static PyObject *obj_close(PyTdbObject *self)
167 int ret;
168 if (self->closed)
169 return Py_None;
170 ret = tdb_close(self->ctx);
171 self->closed = true;
172 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
173 return Py_None;
176 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
178 TDB_DATA key;
179 PyObject *py_key;
180 if (!PyArg_ParseTuple(args, "O", &py_key))
181 return NULL;
183 key = PyString_AsTDB_DATA(py_key);
185 return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
188 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
190 TDB_DATA key, data;
191 PyObject *py_key, *py_data;
192 int ret;
193 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
194 return NULL;
196 key = PyString_AsTDB_DATA(py_key);
197 data = PyString_AsTDB_DATA(py_data);
199 ret = tdb_append(self->ctx, key, data);
200 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
201 return Py_None;
204 static PyObject *obj_firstkey(PyTdbObject *self)
206 return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
209 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
211 TDB_DATA key;
212 PyObject *py_key;
213 if (!PyArg_ParseTuple(args, "O", &py_key))
214 return NULL;
216 key = PyString_AsTDB_DATA(py_key);
218 return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
221 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
223 TDB_DATA key;
224 PyObject *py_key;
225 int ret;
226 if (!PyArg_ParseTuple(args, "O", &py_key))
227 return NULL;
229 key = PyString_AsTDB_DATA(py_key);
230 ret = tdb_delete(self->ctx, key);
231 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
232 return Py_None;
235 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
237 TDB_DATA key;
238 int ret;
239 PyObject *py_key;
240 if (!PyArg_ParseTuple(args, "O", &py_key))
241 return NULL;
243 key = PyString_AsTDB_DATA(py_key);
244 ret = tdb_exists(self->ctx, key);
245 if (ret != TDB_ERR_NOEXIST) {
246 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
249 return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
252 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
254 TDB_DATA key, value;
255 int ret;
256 int flag = TDB_REPLACE;
257 PyObject *py_key, *py_value;
259 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
260 return NULL;
262 key = PyString_AsTDB_DATA(py_key);
263 value = PyString_AsTDB_DATA(py_value);
265 ret = tdb_store(self->ctx, key, value, flag);
266 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
267 return Py_None;
271 typedef struct {
272 PyObject_HEAD
273 TDB_DATA current;
274 PyTdbObject *iteratee;
275 } PyTdbIteratorObject;
277 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
279 TDB_DATA current;
280 PyObject *ret;
281 if (self->current.dptr == NULL && self->current.dsize == 0)
282 return NULL;
283 current = self->current;
284 self->current = tdb_nextkey(self->iteratee->ctx, self->current);
285 ret = PyString_FromTDB_DATA(current);
286 return ret;
289 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
291 Py_DECREF(self->iteratee);
292 PyObject_Del(self);
295 PyTypeObject PyTdbIterator = {
296 .tp_name = "Iterator",
297 .tp_basicsize = sizeof(PyTdbIteratorObject),
298 .tp_iternext = (iternextfunc)tdb_iter_next,
299 .tp_dealloc = (destructor)tdb_iter_dealloc,
300 .tp_flags = Py_TPFLAGS_DEFAULT,
301 .tp_iter = PyObject_SelfIter,
304 static PyObject *tdb_object_iter(PyTdbObject *self)
306 PyTdbIteratorObject *ret;
308 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
309 ret->current = tdb_firstkey(self->ctx);
310 ret->iteratee = self;
311 Py_INCREF(self);
312 return (PyObject *)ret;
315 static PyObject *obj_clear(PyTdbObject *self)
317 int ret = tdb_wipe_all(self->ctx);
318 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
319 return Py_None;
322 static PyMethodDef tdb_object_methods[] = {
323 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
324 "S.transaction_cancel() -> None\n"
325 "Cancel the currently active transaction." },
326 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
327 "S.transaction_commit() -> None\n"
328 "Commit the currently active transaction." },
329 { "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
330 "S.transaction_recover() -> None\n"
331 "Recover the currently active transaction." },
332 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
333 "S.transaction_start() -> None\n"
334 "Start a new transaction." },
335 { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
336 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
337 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
338 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
339 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
340 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
341 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.fetch(key) -> value\n"
342 "Fetch a value." },
343 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
344 "Append data to an existing key." },
345 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
346 "Return the first key in this database." },
347 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
348 "Return the next key in this database." },
349 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
350 "Delete an entry." },
351 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
352 "Check whether key exists in this database." },
353 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
354 "Store data." },
355 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
356 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
357 "Wipe the entire database." },
358 { NULL }
361 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
363 return PyInt_FromLong(tdb_hash_size(self->ctx));
366 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
368 if (!PyInt_Check(max_dead))
369 return -1;
370 tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
371 return 0;
374 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
376 return PyInt_FromLong(tdb_map_size(self->ctx));
379 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
381 return PyInt_FromLong(tdb_get_flags(self->ctx));
384 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
386 return PyString_FromString(tdb_name(self->ctx));
389 static PyGetSetDef tdb_object_getsetters[] = {
390 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
391 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
392 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
393 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
394 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
395 { NULL }
398 static PyObject *tdb_object_repr(PyTdbObject *self)
400 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
403 static void tdb_object_dealloc(PyTdbObject *self)
405 if (!self->closed)
406 tdb_close(self->ctx);
407 PyObject_Del(self);
410 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
412 TDB_DATA tkey, val;
413 if (!PyString_Check(key)) {
414 PyErr_SetString(PyExc_TypeError, "Expected string as key");
415 return NULL;
418 tkey.dptr = (unsigned char *)PyString_AsString(key);
419 tkey.dsize = PyString_Size(key);
421 val = tdb_fetch(self->ctx, tkey);
422 if (val.dptr == NULL) {
423 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
424 return NULL;
425 } else {
426 return PyString_FromTDB_DATA(val);
430 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
432 TDB_DATA tkey, tval;
433 int ret;
434 if (!PyString_Check(key)) {
435 PyErr_SetString(PyExc_TypeError, "Expected string as key");
436 return -1;
439 tkey = PyString_AsTDB_DATA(key);
441 if (value == NULL) {
442 ret = tdb_delete(self->ctx, tkey);
443 } else {
444 if (!PyString_Check(value)) {
445 PyErr_SetString(PyExc_TypeError, "Expected string as value");
446 return -1;
449 tval = PyString_AsTDB_DATA(value);
451 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
454 if (ret != 0) {
455 PyErr_SetTDBError(self->ctx);
456 return -1;
459 return ret;
462 static PyMappingMethods tdb_object_mapping = {
463 .mp_subscript = (binaryfunc)obj_getitem,
464 .mp_ass_subscript = (objobjargproc)obj_setitem,
466 PyTypeObject PyTdb = {
467 .tp_name = "Tdb",
468 .tp_basicsize = sizeof(PyTdbObject),
469 .tp_methods = tdb_object_methods,
470 .tp_getset = tdb_object_getsetters,
471 .tp_new = py_tdb_open,
472 .tp_doc = "A TDB file",
473 .tp_repr = (reprfunc)tdb_object_repr,
474 .tp_dealloc = (destructor)tdb_object_dealloc,
475 .tp_as_mapping = &tdb_object_mapping,
476 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
477 .tp_iter = (getiterfunc)tdb_object_iter,
480 static PyMethodDef tdb_methods[] = {
481 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
482 "Open a TDB file." },
483 { NULL }
486 void inittdb(void)
488 PyObject *m;
490 if (PyType_Ready(&PyTdb) < 0)
491 return;
493 if (PyType_Ready(&PyTdbIterator) < 0)
494 return;
496 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
497 if (m == NULL)
498 return;
500 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
501 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
502 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
504 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
505 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
506 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
507 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
508 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
509 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
510 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
511 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
513 Py_INCREF(&PyTdb);
514 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
516 Py_INCREF(&PyTdbIterator);