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