tdb2: create tdb2 versions of various testing TDBs.
[bbaumbach/samba-autobuild/.git] / lib / tdb2 / pytdb.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Python interface to tdb2.  Simply modified from tdb1 version.
5
6    Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7    Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
8    Copyright (C) 2011 Rusty Russell <rusty@rustcorp.com.au>
9
10      ** NOTE! The following LGPL license applies to the tdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include <Python.h>
29 #include "replace.h"
30 #include "system/filesys.h"
31
32 #ifndef Py_RETURN_NONE
33 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
34 #endif
35
36 /* Include tdb headers */
37 #include <tdb2.h>
38
39 typedef struct {
40         PyObject_HEAD
41         struct tdb_context *ctx;
42         bool closed;
43 } PyTdbObject;
44
45 staticforward PyTypeObject PyTdb;
46
47 static void PyErr_SetTDBError(enum TDB_ERROR e)
48 {
49         PyErr_SetObject(PyExc_RuntimeError,
50                 Py_BuildValue("(i,s)", e, tdb_errorstr(e)));
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         PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
64                                                    data.dsize);
65         free(data.dptr);
66         return ret;
67 }
68
69 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret) \
70         if (ret != TDB_SUCCESS) { \
71                 PyErr_SetTDBError(ret); \
72                 return NULL; \
73         }
74
75 static void stderr_log(struct tdb_context *tdb,
76                        enum tdb_log_level level,
77                        const char *message,
78                        void *data)
79 {
80         fprintf(stderr, "%s:%s\n", tdb_name(tdb), message);
81 }
82
83 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
84 {
85         char *name = NULL;
86         int tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
87         struct tdb_context *ctx;
88         PyTdbObject *ret;
89         union tdb_attribute logattr;
90         const char *kwnames[] = { "name", "tdb_flags", "flags", "mode", NULL };
91
92         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siii", (char **)kwnames, &name, &tdb_flags, &flags, &mode))
93                 return NULL;
94
95         if (name == NULL) {
96                 tdb_flags |= TDB_INTERNAL;
97         }
98
99         logattr.log.base.attr = TDB_ATTRIBUTE_LOG;
100         logattr.log.base.next = NULL;
101         logattr.log.fn = stderr_log;
102         ctx = tdb_open(name, tdb_flags, flags, mode, &logattr);
103         if (ctx == NULL) {
104                 PyErr_SetFromErrno(PyExc_IOError);
105                 return NULL;
106         }
107
108         ret = PyObject_New(PyTdbObject, &PyTdb);
109         if (!ret) {
110                 tdb_close(ctx);
111                 return NULL;
112         }
113
114         ret->ctx = ctx;
115         ret->closed = false;
116         return (PyObject *)ret;
117 }
118
119 static PyObject *obj_transaction_cancel(PyTdbObject *self)
120 {
121         tdb_transaction_cancel(self->ctx);
122         Py_RETURN_NONE;
123 }
124
125 static PyObject *obj_transaction_commit(PyTdbObject *self)
126 {
127         enum TDB_ERROR ret = tdb_transaction_commit(self->ctx);
128         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
129         Py_RETURN_NONE;
130 }
131
132 static PyObject *obj_transaction_prepare_commit(PyTdbObject *self)
133 {
134         enum TDB_ERROR ret = tdb_transaction_prepare_commit(self->ctx);
135         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
136         Py_RETURN_NONE;
137 }
138
139 static PyObject *obj_transaction_start(PyTdbObject *self)
140 {
141         enum TDB_ERROR ret = tdb_transaction_start(self->ctx);
142         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
143         Py_RETURN_NONE;
144 }
145
146 static PyObject *obj_lockall(PyTdbObject *self)
147 {
148         enum TDB_ERROR ret = tdb_lockall(self->ctx);
149         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
150         Py_RETURN_NONE;
151 }
152
153 static PyObject *obj_unlockall(PyTdbObject *self)
154 {
155         tdb_unlockall(self->ctx);
156         Py_RETURN_NONE;
157 }
158
159 static PyObject *obj_lockall_read(PyTdbObject *self)
160 {
161         enum TDB_ERROR ret = tdb_lockall_read(self->ctx);
162         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
163         Py_RETURN_NONE;
164 }
165
166 static PyObject *obj_unlockall_read(PyTdbObject *self)
167 {
168         tdb_unlockall_read(self->ctx);
169         Py_RETURN_NONE;
170 }
171
172 static PyObject *obj_close(PyTdbObject *self)
173 {
174         enum TDB_ERROR ret;
175         if (self->closed)
176                 Py_RETURN_NONE;
177         ret = tdb_close(self->ctx);
178         self->closed = true;
179         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
180         Py_RETURN_NONE;
181 }
182
183 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
184 {
185         TDB_DATA key, data;
186         PyObject *py_key;
187         enum TDB_ERROR ret;
188         if (!PyArg_ParseTuple(args, "O", &py_key))
189                 return NULL;
190
191         key = PyString_AsTDB_DATA(py_key);
192         ret = tdb_fetch(self->ctx, key, &data);
193         if (ret == TDB_ERR_NOEXIST)
194                 Py_RETURN_NONE;
195         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
196         return PyString_FromTDB_DATA(data);
197 }
198
199 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
200 {
201         TDB_DATA key, data;
202         PyObject *py_key, *py_data;
203         enum TDB_ERROR ret;
204         if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
205                 return NULL;
206
207         key = PyString_AsTDB_DATA(py_key);
208         data = PyString_AsTDB_DATA(py_data);
209
210         ret = tdb_append(self->ctx, key, data);
211         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
212         Py_RETURN_NONE;
213 }
214
215 static PyObject *obj_firstkey(PyTdbObject *self)
216 {
217         enum TDB_ERROR ret;
218         TDB_DATA key;
219
220         ret = tdb_firstkey(self->ctx, &key);
221         if (ret == TDB_ERR_NOEXIST)
222                 Py_RETURN_NONE;
223         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
224
225         return PyString_FromTDB_DATA(key);
226 }
227
228 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
229 {
230         TDB_DATA key;
231         PyObject *py_key;
232         enum TDB_ERROR ret;
233         if (!PyArg_ParseTuple(args, "O", &py_key))
234                 return NULL;
235
236         /* Malloc here, since tdb_nextkey frees. */
237         key.dsize = PyString_Size(py_key);
238         key.dptr = malloc(key.dsize);
239         memcpy(key.dptr, PyString_AsString(py_key), key.dsize);
240
241         ret = tdb_nextkey(self->ctx, &key);
242         if (ret == TDB_ERR_NOEXIST)
243                 Py_RETURN_NONE;
244         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
245
246         return PyString_FromTDB_DATA(key);
247 }
248
249 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
250 {
251         TDB_DATA key;
252         PyObject *py_key;
253         enum TDB_ERROR ret;
254         if (!PyArg_ParseTuple(args, "O", &py_key))
255                 return NULL;
256
257         key = PyString_AsTDB_DATA(py_key);
258         ret = tdb_delete(self->ctx, key);
259         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
260         Py_RETURN_NONE;
261 }
262
263 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
264 {
265         TDB_DATA key;
266         enum TDB_ERROR ret;
267         PyObject *py_key;
268         if (!PyArg_ParseTuple(args, "O", &py_key))
269                 return NULL;
270
271         key = PyString_AsTDB_DATA(py_key);
272         ret = tdb_exists(self->ctx, key);
273         if (ret == TDB_ERR_NOEXIST)
274                 return Py_False;
275         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
276         return Py_True;
277 }
278
279 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
280 {
281         TDB_DATA key, value;
282         enum TDB_ERROR ret;
283         int flag = TDB_REPLACE;
284         PyObject *py_key, *py_value;
285
286         if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
287                 return NULL;
288
289         key = PyString_AsTDB_DATA(py_key);
290         value = PyString_AsTDB_DATA(py_value);
291
292         ret = tdb_store(self->ctx, key, value, flag);
293         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
294         Py_RETURN_NONE;
295 }
296
297 static PyObject *obj_add_flag(PyTdbObject *self, PyObject *args)
298 {
299         unsigned flag;
300
301         if (!PyArg_ParseTuple(args, "I", &flag))
302                 return NULL;
303
304         tdb_add_flag(self->ctx, flag);
305         Py_RETURN_NONE;
306 }
307
308 static PyObject *obj_remove_flag(PyTdbObject *self, PyObject *args)
309 {
310         unsigned flag;
311
312         if (!PyArg_ParseTuple(args, "I", &flag))
313                 return NULL;
314
315         tdb_remove_flag(self->ctx, flag);
316         Py_RETURN_NONE;
317 }
318
319 typedef struct {
320         PyObject_HEAD
321         TDB_DATA current;
322         bool end;
323         PyTdbObject *iteratee;
324 } PyTdbIteratorObject;
325
326 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
327 {
328         enum TDB_ERROR e;
329         PyObject *ret;
330         if (self->end)
331                 return NULL;
332         ret = PyString_FromStringAndSize((const char *)self->current.dptr,
333                                          self->current.dsize);
334         e = tdb_nextkey(self->iteratee->ctx, &self->current);
335         if (e == TDB_ERR_NOEXIST)
336                 self->end = true;
337         else
338                 PyErr_TDB_ERROR_IS_ERR_RAISE(e);
339         return ret;
340 }
341
342 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
343 {
344         Py_DECREF(self->iteratee);
345         PyObject_Del(self);
346 }
347
348 PyTypeObject PyTdbIterator = {
349         .tp_name = "Iterator",
350         .tp_basicsize = sizeof(PyTdbIteratorObject),
351         .tp_iternext = (iternextfunc)tdb_iter_next,
352         .tp_dealloc = (destructor)tdb_iter_dealloc,
353         .tp_flags = Py_TPFLAGS_DEFAULT,
354         .tp_iter = PyObject_SelfIter,
355 };
356
357 static PyObject *tdb_object_iter(PyTdbObject *self)
358 {
359         PyTdbIteratorObject *ret;
360         enum TDB_ERROR e;
361
362         ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
363         if (!ret)
364                 return NULL;
365         e = tdb_firstkey(self->ctx, &ret->current);
366         if (e == TDB_ERR_NOEXIST) {
367                 ret->end = true;
368         } else {
369                 PyErr_TDB_ERROR_IS_ERR_RAISE(e);
370                 ret->end = false;
371         }
372         ret->iteratee = self;
373         Py_INCREF(self);
374         return (PyObject *)ret;
375 }
376
377 static PyObject *obj_clear(PyTdbObject *self)
378 {
379         enum TDB_ERROR ret = tdb_wipe_all(self->ctx);
380         PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
381         Py_RETURN_NONE;
382 }
383
384 static PyObject *obj_enable_seqnum(PyTdbObject *self)
385 {
386         tdb_add_flag(self->ctx, TDB_SEQNUM);
387         Py_RETURN_NONE;
388 }
389
390 static PyMethodDef tdb_object_methods[] = {
391         { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
392                 "S.transaction_cancel() -> None\n"
393                 "Cancel the currently active transaction." },
394         { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
395                 "S.transaction_commit() -> None\n"
396                 "Commit the currently active transaction." },
397         { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
398                 "S.transaction_prepare_commit() -> None\n"
399                 "Prepare to commit the currently active transaction" },
400         { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
401                 "S.transaction_start() -> None\n"
402                 "Start a new transaction." },
403         { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
404         { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
405         { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
406         { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
407         { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
408         { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
409                 "Fetch a value." },
410         { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
411                 "Append data to an existing key." },
412         { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
413                 "Return the first key in this database." },
414         { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
415                 "Return the next key in this database." },
416         { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
417                 "Delete an entry." },
418         { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
419                 "Check whether key exists in this database." },
420         { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
421                 "Store data." },
422         { "add_flag", (PyCFunction)obj_add_flag, METH_VARARGS, "S.add_flag(flag) -> None" },
423         { "remove_flag", (PyCFunction)obj_remove_flag, METH_VARARGS, "S.remove_flag(flag) -> None" },
424         { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
425         { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
426                 "Wipe the entire database." },
427         { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
428                 "S.enable_seqnum() -> None" },
429         { NULL }
430 };
431
432 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
433 {
434         return PyInt_FromLong(tdb_get_flags(self->ctx));
435 }
436
437 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
438 {
439         return PyString_FromString(tdb_name(self->ctx));
440 }
441
442 static PyObject *obj_get_seqnum(PyTdbObject *self, void *closure)
443 {
444         return PyInt_FromLong(tdb_get_seqnum(self->ctx));
445 }
446
447
448 static PyGetSetDef tdb_object_getsetters[] = {
449         { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
450         { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
451         { (char *)"seqnum", (getter)obj_get_seqnum, NULL, NULL },
452         { NULL }
453 };
454
455 static PyObject *tdb_object_repr(PyTdbObject *self)
456 {
457         if (tdb_get_flags(self->ctx) & TDB_INTERNAL) {
458                 return PyString_FromString("Tdb(<internal>)");
459         } else {
460                 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
461         }
462 }
463
464 static void tdb_object_dealloc(PyTdbObject *self)
465 {
466         if (!self->closed)
467                 tdb_close(self->ctx);
468         self->ob_type->tp_free(self);
469 }
470
471 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
472 {
473         TDB_DATA tkey, val;
474         enum TDB_ERROR ret;
475
476         if (!PyString_Check(key)) {
477                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
478                 return NULL;
479         }
480
481         tkey.dptr = (unsigned char *)PyString_AsString(key);
482         tkey.dsize = PyString_Size(key);
483
484         ret = tdb_fetch(self->ctx, tkey, &val);
485         if (ret == TDB_ERR_NOEXIST) {
486                 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
487                 return NULL;
488         } else {
489                 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
490                 return PyString_FromTDB_DATA(val);
491         }
492 }
493
494 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
495 {
496         TDB_DATA tkey, tval;
497         enum TDB_ERROR ret;
498         if (!PyString_Check(key)) {
499                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
500                 return -1;
501         }
502
503         tkey = PyString_AsTDB_DATA(key);
504
505         if (value == NULL) {
506                 ret = tdb_delete(self->ctx, tkey);
507         } else {
508                 if (!PyString_Check(value)) {
509                         PyErr_SetString(PyExc_TypeError, "Expected string as value");
510                         return -1;
511                 }
512
513                 tval = PyString_AsTDB_DATA(value);
514
515                 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
516         }
517
518         if (ret != TDB_SUCCESS) {
519                 PyErr_SetTDBError(ret);
520                 return -1;
521         }
522
523         return ret;
524 }
525
526 static PyMappingMethods tdb_object_mapping = {
527         .mp_subscript = (binaryfunc)obj_getitem,
528         .mp_ass_subscript = (objobjargproc)obj_setitem,
529 };
530 static PyTypeObject PyTdb = {
531         .tp_name = "Tdb",
532         .tp_basicsize = sizeof(PyTdbObject),
533         .tp_methods = tdb_object_methods,
534         .tp_getset = tdb_object_getsetters,
535         .tp_new = py_tdb_open,
536         .tp_doc = "A TDB file",
537         .tp_repr = (reprfunc)tdb_object_repr,
538         .tp_dealloc = (destructor)tdb_object_dealloc,
539         .tp_as_mapping = &tdb_object_mapping,
540         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
541         .tp_iter = (getiterfunc)tdb_object_iter,
542 };
543
544 static PyMethodDef tdb_methods[] = {
545         { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
546                 "Open a TDB file." },
547         { NULL }
548 };
549
550 void inittdb(void);
551 void inittdb(void)
552 {
553         PyObject *m;
554
555         if (PyType_Ready(&PyTdb) < 0)
556                 return;
557
558         if (PyType_Ready(&PyTdbIterator) < 0)
559                 return;
560
561         m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
562         if (m == NULL)
563                 return;
564
565         PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
566         PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
567         PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
568
569         PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
570         PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
571         PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
572         PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
573         PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
574         PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(TDB_NOSYNC));
575         PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(TDB_SEQNUM));
576         PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING));
577
578         PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
579
580         PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
581
582         Py_INCREF(&PyTdb);
583         PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
584
585         Py_INCREF(&PyTdbIterator);
586 }