pytdb: Fix repr segfault for internal db
[metze/ctdb/wip.git] / lib / tdb / pytdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Python interface to tdb.
5
6    Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7    Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
8
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
12    
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.
17
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.
22
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/>.
25 */
26
27 #include "replace.h"
28 #include "system/filesys.h"
29
30 #include <Python.h>
31 #ifndef Py_RETURN_NONE
32 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
33 #endif
34
35 /* Include tdb headers */
36 #include <tdb.h>
37
38 typedef struct {
39         PyObject_HEAD
40         TDB_CONTEXT *ctx;
41         bool closed;
42 } PyTdbObject;
43
44 PyAPI_DATA(PyTypeObject) PyTdb;
45
46 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
47 {
48         PyErr_SetObject(PyExc_RuntimeError, 
49                 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
50 }
51
52 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
53 {
54         TDB_DATA ret;
55         ret.dptr = (unsigned char *)PyString_AsString(data);
56         ret.dsize = PyString_Size(data);
57         return ret;
58 }
59
60 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
61 {
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;
69     }
70 }
71
72 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
73         if (ret != 0) { \
74                 PyErr_SetTDBError(tdb); \
75                 return NULL; \
76         }
77
78 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
79 {
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 };
85
86         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
87                 return NULL;
88
89         ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
90         if (ctx == NULL) {
91                 PyErr_SetFromErrno(PyExc_IOError);
92                 return NULL;
93         }
94
95         ret = PyObject_New(PyTdbObject, &PyTdb);
96         ret->ctx = ctx;
97         ret->closed = false;
98         return (PyObject *)ret;
99 }
100
101 static PyObject *obj_transaction_cancel(PyTdbObject *self)
102 {
103         int ret = tdb_transaction_cancel(self->ctx);
104         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
105         Py_RETURN_NONE;
106 }
107
108 static PyObject *obj_transaction_commit(PyTdbObject *self)
109 {
110         int ret = tdb_transaction_commit(self->ctx);
111         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
112         Py_RETURN_NONE;
113 }
114
115 static PyObject *obj_transaction_start(PyTdbObject *self)
116 {
117         int ret = tdb_transaction_start(self->ctx);
118         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
119         Py_RETURN_NONE;
120 }
121
122 static PyObject *obj_reopen(PyTdbObject *self)
123 {
124         int ret = tdb_reopen(self->ctx);
125         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
126         Py_RETURN_NONE;
127 }
128
129 static PyObject *obj_lockall(PyTdbObject *self)
130 {
131         int ret = tdb_lockall(self->ctx);
132         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
133         Py_RETURN_NONE;
134 }
135
136 static PyObject *obj_unlockall(PyTdbObject *self)
137 {
138         int ret = tdb_unlockall(self->ctx);
139         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
140         Py_RETURN_NONE;
141 }
142
143 static PyObject *obj_lockall_read(PyTdbObject *self)
144 {
145         int ret = tdb_lockall_read(self->ctx);
146         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
147         Py_RETURN_NONE;
148 }
149
150 static PyObject *obj_unlockall_read(PyTdbObject *self)
151 {
152         int ret = tdb_unlockall_read(self->ctx);
153         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
154         Py_RETURN_NONE;
155 }
156
157 static PyObject *obj_close(PyTdbObject *self)
158 {
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;
166 }
167
168 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
169 {
170         TDB_DATA key;
171         PyObject *py_key;
172         if (!PyArg_ParseTuple(args, "O", &py_key))
173                 return NULL;
174
175         key = PyString_AsTDB_DATA(py_key);
176
177         return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
178 }
179
180 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
181 {
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;
187
188         key = PyString_AsTDB_DATA(py_key);
189         data = PyString_AsTDB_DATA(py_data);
190
191         ret = tdb_append(self->ctx, key, data);
192         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
193         Py_RETURN_NONE;
194 }
195
196 static PyObject *obj_firstkey(PyTdbObject *self)
197 {
198         return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
199 }
200
201 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
202 {
203         TDB_DATA key;
204         PyObject *py_key;
205         if (!PyArg_ParseTuple(args, "O", &py_key))
206                 return NULL;
207
208         key = PyString_AsTDB_DATA(py_key);
209         
210         return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
211 }
212
213 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
214 {
215         TDB_DATA key;
216         PyObject *py_key;
217         int ret;
218         if (!PyArg_ParseTuple(args, "O", &py_key))
219                 return NULL;
220
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;
225 }
226
227 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
228 {
229         TDB_DATA key;
230         int ret;
231         PyObject *py_key;
232         if (!PyArg_ParseTuple(args, "O", &py_key))
233                 return NULL;
234
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);
239         }
240
241         return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
242 }
243
244 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
245 {
246         TDB_DATA key, value;
247         int ret;
248         int flag = TDB_REPLACE;
249         PyObject *py_key, *py_value;
250
251         if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
252                 return NULL;
253
254         key = PyString_AsTDB_DATA(py_key);
255         value = PyString_AsTDB_DATA(py_value);
256
257         ret = tdb_store(self->ctx, key, value, flag);
258         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
259         Py_RETURN_NONE;
260 }
261
262 static PyObject *obj_add_flags(PyTdbObject *self, PyObject *args)
263 {
264         unsigned flags;
265
266         if (!PyArg_ParseTuple(args, "I", &flags))
267                 return NULL;
268
269         tdb_add_flags(self->ctx, flags);
270         Py_RETURN_NONE;
271 }
272
273 static PyObject *obj_remove_flags(PyTdbObject *self, PyObject *args)
274 {
275         unsigned flags;
276
277         if (!PyArg_ParseTuple(args, "I", &flags))
278                 return NULL;
279
280         tdb_remove_flags(self->ctx, flags);
281         Py_RETURN_NONE;
282 }
283
284 typedef struct {
285         PyObject_HEAD
286         TDB_DATA current;
287         PyTdbObject *iteratee;
288 } PyTdbIteratorObject;
289
290 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
291 {
292         TDB_DATA current;
293         PyObject *ret;
294         if (self->current.dptr == NULL && self->current.dsize == 0)
295                 return NULL;
296         current = self->current;
297         self->current = tdb_nextkey(self->iteratee->ctx, self->current);
298         ret = PyString_FromTDB_DATA(current);
299         return ret;
300 }
301
302 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
303 {
304         Py_DECREF(self->iteratee);
305         PyObject_Del(self);
306 }
307
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,
315 };
316
317 static PyObject *tdb_object_iter(PyTdbObject *self)
318 {
319         PyTdbIteratorObject *ret;       
320
321         ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
322         ret->current = tdb_firstkey(self->ctx);
323         ret->iteratee = self;
324         Py_INCREF(self);
325         return (PyObject *)ret;
326 }
327
328 static PyObject *obj_clear(PyTdbObject *self)
329 {
330         int ret = tdb_wipe_all(self->ctx);
331         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
332         Py_RETURN_NONE;
333 }
334
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"
352                 "Fetch a value." },
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"
364                 "Store data." },
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." },
370         { NULL }
371 };
372
373 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
374 {
375         return PyInt_FromLong(tdb_hash_size(self->ctx));
376 }
377
378 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
379 {
380         if (!PyInt_Check(max_dead))
381                 return -1;
382         tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
383         return 0;
384 }
385
386 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
387 {
388         return PyInt_FromLong(tdb_map_size(self->ctx));
389 }
390
391 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
392 {
393         return PyInt_FromLong(tdb_get_flags(self->ctx));
394 }
395
396 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
397 {
398         return PyString_FromString(tdb_name(self->ctx));
399 }
400
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."},
407         { NULL }
408 };
409
410 static PyObject *tdb_object_repr(PyTdbObject *self)
411 {
412         return PyString_FromFormat("Tdb('%s')",
413                 (tdb_get_flags(self->ctx) & TDB_INTERNAL) ? "<internal>"
414                                                           : tdb_name(self->ctx));
415 }
416
417 static void tdb_object_dealloc(PyTdbObject *self)
418 {
419         if (!self->closed)
420                 tdb_close(self->ctx);
421         PyObject_Del(self);
422 }
423
424 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
425 {
426         TDB_DATA tkey, val;
427         if (!PyString_Check(key)) {
428                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
429                 return NULL;
430         }
431
432         tkey.dptr = (unsigned char *)PyString_AsString(key);
433         tkey.dsize = PyString_Size(key);
434
435         val = tdb_fetch(self->ctx, tkey);
436         if (val.dptr == NULL) {
437                 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
438                 return NULL;
439         } else {
440                 return PyString_FromTDB_DATA(val);
441         }
442 }
443
444 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
445 {
446         TDB_DATA tkey, tval;
447         int ret;
448         if (!PyString_Check(key)) {
449                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
450                 return -1;
451         }
452
453         tkey = PyString_AsTDB_DATA(key);
454
455         if (value == NULL) { 
456                 ret = tdb_delete(self->ctx, tkey);
457         } else { 
458                 if (!PyString_Check(value)) {
459                         PyErr_SetString(PyExc_TypeError, "Expected string as value");
460                         return -1;
461                 }
462
463                 tval = PyString_AsTDB_DATA(value);
464
465                 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
466         }
467
468         if (ret != 0) {
469                 PyErr_SetTDBError(self->ctx);
470                 return -1;
471         } 
472
473         return ret;
474 }
475
476 static PyMappingMethods tdb_object_mapping = {
477         .mp_subscript = (binaryfunc)obj_getitem,
478         .mp_ass_subscript = (objobjargproc)obj_setitem,
479 };
480 PyTypeObject PyTdb = {
481         .tp_name = "Tdb",
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,
492 };
493
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." },
497         { NULL }
498 };
499
500 void inittdb(void)
501 {
502         PyObject *m;
503
504         if (PyType_Ready(&PyTdb) < 0)
505                 return;
506
507         if (PyType_Ready(&PyTdbIterator) < 0)
508                 return;
509
510         m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
511         if (m == NULL)
512                 return;
513
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));
517
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"));
526
527         Py_INCREF(&PyTdb);
528         PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
529
530         Py_INCREF(&PyTdbIterator);
531 }