py: Properly increase the reference counter of Py_None.
[samba.git] / lib / tdb / pytdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Swig 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 <Python.h>
28 #ifdef HAVE_FSTAT
29 #undef HAVE_FSTAT
30 #endif
31
32 /* Include tdb headers */
33 #include <stdint.h>
34 #include <signal.h>
35 #include <tdb.h>
36 #include <fcntl.h>
37 #include <stdbool.h>
38
39 typedef struct {
40         PyObject_HEAD
41         TDB_CONTEXT *ctx;
42         bool closed;
43 } PyTdbObject;
44
45 PyAPI_DATA(PyTypeObject) PyTdb;
46
47 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
48 {
49         PyErr_SetObject(PyExc_RuntimeError, 
50                 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
51 }
52
53 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
54 {
55         TDB_DATA ret;
56         ret.dptr = (unsigned char *)PyString_AsString(data);
57         ret.dsize = PyString_Size(data);
58         return ret;
59 }
60
61 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
62 {
63         if (data.dptr == NULL && data.dsize == 0) {
64                 Py_RETURN_NONE;
65         } else {
66                 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr, 
67                                                                                                    data.dsize);
68                 free(data.dptr);
69                 return ret;
70     }
71 }
72
73 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
74         if (ret != 0) { \
75                 PyErr_SetTDBError(tdb); \
76                 return NULL; \
77         }
78
79 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
80 {
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 };
86
87         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
88                 return NULL;
89
90         ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
91         if (ctx == NULL) {
92                 PyErr_SetFromErrno(PyExc_IOError);
93                 return NULL;
94         }
95
96         ret = PyObject_New(PyTdbObject, &PyTdb);
97         ret->ctx = ctx;
98         ret->closed = false;
99         return (PyObject *)ret;
100 }
101
102 static PyObject *obj_transaction_cancel(PyTdbObject *self)
103 {
104         int ret = tdb_transaction_cancel(self->ctx);
105         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
106         Py_RETURN_NONE;
107 }
108
109 static PyObject *obj_transaction_commit(PyTdbObject *self)
110 {
111         int ret = tdb_transaction_commit(self->ctx);
112         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
113         Py_RETURN_NONE;
114 }
115
116 static PyObject *obj_transaction_recover(PyTdbObject *self)
117 {
118         int ret = tdb_transaction_recover(self->ctx);
119         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
120         Py_RETURN_NONE;
121 }
122
123 static PyObject *obj_transaction_start(PyTdbObject *self)
124 {
125         int ret = tdb_transaction_start(self->ctx);
126         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
127         Py_RETURN_NONE;
128 }
129
130 static PyObject *obj_reopen(PyTdbObject *self)
131 {
132         int ret = tdb_reopen(self->ctx);
133         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
134         Py_RETURN_NONE;
135 }
136
137 static PyObject *obj_lockall(PyTdbObject *self)
138 {
139         int ret = tdb_lockall(self->ctx);
140         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
141         Py_RETURN_NONE;
142 }
143
144 static PyObject *obj_unlockall(PyTdbObject *self)
145 {
146         int ret = tdb_unlockall(self->ctx);
147         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
148         Py_RETURN_NONE;
149 }
150
151 static PyObject *obj_lockall_read(PyTdbObject *self)
152 {
153         int ret = tdb_lockall_read(self->ctx);
154         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
155         Py_RETURN_NONE;
156 }
157
158 static PyObject *obj_unlockall_read(PyTdbObject *self)
159 {
160         int ret = tdb_unlockall_read(self->ctx);
161         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
162         Py_RETURN_NONE;
163 }
164
165 static PyObject *obj_close(PyTdbObject *self)
166 {
167         int ret;
168         if (self->closed)
169                 Py_RETURN_NONE;
170         ret = tdb_close(self->ctx);
171         self->closed = true;
172         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
173         Py_RETURN_NONE;
174 }
175
176 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
177 {
178         TDB_DATA key;
179         PyObject *py_key;
180         if (!PyArg_ParseTuple(args, "O", &py_key))
181                 return NULL;
182
183         key = PyString_AsTDB_DATA(py_key);
184
185         return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
186 }
187
188 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
189 {
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;
195
196         key = PyString_AsTDB_DATA(py_key);
197         data = PyString_AsTDB_DATA(py_data);
198
199         ret = tdb_append(self->ctx, key, data);
200         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
201         Py_RETURN_NONE;
202 }
203
204 static PyObject *obj_firstkey(PyTdbObject *self)
205 {
206         return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
207 }
208
209 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
210 {
211         TDB_DATA key;
212         PyObject *py_key;
213         if (!PyArg_ParseTuple(args, "O", &py_key))
214                 return NULL;
215
216         key = PyString_AsTDB_DATA(py_key);
217         
218         return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
219 }
220
221 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
222 {
223         TDB_DATA key;
224         PyObject *py_key;
225         int ret;
226         if (!PyArg_ParseTuple(args, "O", &py_key))
227                 return NULL;
228
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         Py_RETURN_NONE;
233 }
234
235 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
236 {
237         TDB_DATA key;
238         int ret;
239         PyObject *py_key;
240         if (!PyArg_ParseTuple(args, "O", &py_key))
241                 return NULL;
242
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);
247         }
248
249         return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
250 }
251
252 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
253 {
254         TDB_DATA key, value;
255         int ret;
256         int flag = TDB_REPLACE;
257         PyObject *py_key, *py_value;
258
259         if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
260                 return NULL;
261
262         key = PyString_AsTDB_DATA(py_key);
263         value = PyString_AsTDB_DATA(py_value);
264
265         ret = tdb_store(self->ctx, key, value, flag);
266         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
267         Py_RETURN_NONE;
268 }
269
270
271 typedef struct {
272         PyObject_HEAD
273         TDB_DATA current;
274         PyTdbObject *iteratee;
275 } PyTdbIteratorObject;
276
277 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
278 {
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;
287 }
288
289 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
290 {
291         Py_DECREF(self->iteratee);
292         PyObject_Del(self);
293 }
294
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,
302 };
303
304 static PyObject *tdb_object_iter(PyTdbObject *self)
305 {
306         PyTdbIteratorObject *ret;       
307
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;
313 }
314
315 static PyObject *obj_clear(PyTdbObject *self)
316 {
317         int ret = tdb_wipe_all(self->ctx);
318         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
319         Py_RETURN_NONE;
320 }
321
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 }
359 };
360
361 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
362 {
363         return PyInt_FromLong(tdb_hash_size(self->ctx));
364 }
365
366 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
367 {
368         if (!PyInt_Check(max_dead))
369                 return -1;
370         tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
371         return 0;
372 }
373
374 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
375 {
376         return PyInt_FromLong(tdb_map_size(self->ctx));
377 }
378
379 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
380 {
381         return PyInt_FromLong(tdb_get_flags(self->ctx));
382 }
383
384 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
385 {
386         return PyString_FromString(tdb_name(self->ctx));
387 }
388
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 }
396 };
397
398 static PyObject *tdb_object_repr(PyTdbObject *self)
399 {
400         return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
401 }
402
403 static void tdb_object_dealloc(PyTdbObject *self)
404 {
405         if (!self->closed)
406                 tdb_close(self->ctx);
407         PyObject_Del(self);
408 }
409
410 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
411 {
412         TDB_DATA tkey, val;
413         if (!PyString_Check(key)) {
414                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
415                 return NULL;
416         }
417
418         tkey.dptr = (unsigned char *)PyString_AsString(key);
419         tkey.dsize = PyString_Size(key);
420
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);
427         }
428 }
429
430 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
431 {
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;
437         }
438
439         tkey = PyString_AsTDB_DATA(key);
440
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;
447                 }
448
449                 tval = PyString_AsTDB_DATA(value);
450
451                 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
452         }
453
454         if (ret != 0) {
455                 PyErr_SetTDBError(self->ctx);
456                 return -1;
457         } 
458
459         return ret;
460 }
461
462 static PyMappingMethods tdb_object_mapping = {
463         .mp_subscript = (binaryfunc)obj_getitem,
464         .mp_ass_subscript = (objobjargproc)obj_setitem,
465 };
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,
478 };
479
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 }
484 };
485
486 void inittdb(void)
487 {
488         PyObject *m;
489
490         if (PyType_Ready(&PyTdb) < 0)
491                 return;
492
493         if (PyType_Ready(&PyTdbIterator) < 0)
494                 return;
495
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;
499
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));
503
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"));
512
513         Py_INCREF(&PyTdb);
514         PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
515
516         Py_INCREF(&PyTdbIterator);
517 }