7a9205b815cbaf8c580fcb3fb1d679bf600ce11e
[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
263 typedef struct {
264         PyObject_HEAD
265         TDB_DATA current;
266         PyTdbObject *iteratee;
267 } PyTdbIteratorObject;
268
269 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
270 {
271         TDB_DATA current;
272         PyObject *ret;
273         if (self->current.dptr == NULL && self->current.dsize == 0)
274                 return NULL;
275         current = self->current;
276         self->current = tdb_nextkey(self->iteratee->ctx, self->current);
277         ret = PyString_FromTDB_DATA(current);
278         return ret;
279 }
280
281 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
282 {
283         Py_DECREF(self->iteratee);
284         PyObject_Del(self);
285 }
286
287 PyTypeObject PyTdbIterator = {
288         .tp_name = "Iterator",
289         .tp_basicsize = sizeof(PyTdbIteratorObject),
290         .tp_iternext = (iternextfunc)tdb_iter_next,
291         .tp_dealloc = (destructor)tdb_iter_dealloc,
292         .tp_flags = Py_TPFLAGS_DEFAULT,
293         .tp_iter = PyObject_SelfIter,
294 };
295
296 static PyObject *tdb_object_iter(PyTdbObject *self)
297 {
298         PyTdbIteratorObject *ret;       
299
300         ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
301         ret->current = tdb_firstkey(self->ctx);
302         ret->iteratee = self;
303         Py_INCREF(self);
304         return (PyObject *)ret;
305 }
306
307 static PyObject *obj_clear(PyTdbObject *self)
308 {
309         int ret = tdb_wipe_all(self->ctx);
310         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
311         Py_RETURN_NONE;
312 }
313
314 static PyMethodDef tdb_object_methods[] = {
315         { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS, 
316                 "S.transaction_cancel() -> None\n"
317                 "Cancel the currently active transaction." },
318         { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
319                 "S.transaction_commit() -> None\n"
320                 "Commit the currently active transaction." },
321         { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
322                 "S.transaction_start() -> None\n"
323                 "Start a new transaction." },
324         { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
325         { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
326         { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
327         { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
328         { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
329         { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
330         { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
331                 "Fetch a value." },
332         { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
333                 "Append data to an existing key." },
334         { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
335                 "Return the first key in this database." },
336         { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
337                 "Return the next key in this database." },
338         { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
339                 "Delete an entry." },
340         { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
341                 "Check whether key exists in this database." },
342         { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
343                 "Store data." },
344         { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
345         { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
346                 "Wipe the entire database." },
347         { NULL }
348 };
349
350 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
351 {
352         return PyInt_FromLong(tdb_hash_size(self->ctx));
353 }
354
355 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
356 {
357         if (!PyInt_Check(max_dead))
358                 return -1;
359         tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
360         return 0;
361 }
362
363 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
364 {
365         return PyInt_FromLong(tdb_map_size(self->ctx));
366 }
367
368 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
369 {
370         return PyInt_FromLong(tdb_get_flags(self->ctx));
371 }
372
373 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
374 {
375         return PyString_FromString(tdb_name(self->ctx));
376 }
377
378 static PyGetSetDef tdb_object_getsetters[] = {
379         { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
380         { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
381         { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
382         { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
383         { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
384         { NULL }
385 };
386
387 static PyObject *tdb_object_repr(PyTdbObject *self)
388 {
389         return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
390 }
391
392 static void tdb_object_dealloc(PyTdbObject *self)
393 {
394         if (!self->closed)
395                 tdb_close(self->ctx);
396         PyObject_Del(self);
397 }
398
399 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
400 {
401         TDB_DATA tkey, val;
402         if (!PyString_Check(key)) {
403                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
404                 return NULL;
405         }
406
407         tkey.dptr = (unsigned char *)PyString_AsString(key);
408         tkey.dsize = PyString_Size(key);
409
410         val = tdb_fetch(self->ctx, tkey);
411         if (val.dptr == NULL) {
412                 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
413                 return NULL;
414         } else {
415                 return PyString_FromTDB_DATA(val);
416         }
417 }
418
419 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
420 {
421         TDB_DATA tkey, tval;
422         int ret;
423         if (!PyString_Check(key)) {
424                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
425                 return -1;
426         }
427
428         tkey = PyString_AsTDB_DATA(key);
429
430         if (value == NULL) { 
431                 ret = tdb_delete(self->ctx, tkey);
432         } else { 
433                 if (!PyString_Check(value)) {
434                         PyErr_SetString(PyExc_TypeError, "Expected string as value");
435                         return -1;
436                 }
437
438                 tval = PyString_AsTDB_DATA(value);
439
440                 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
441         }
442
443         if (ret != 0) {
444                 PyErr_SetTDBError(self->ctx);
445                 return -1;
446         } 
447
448         return ret;
449 }
450
451 static PyMappingMethods tdb_object_mapping = {
452         .mp_subscript = (binaryfunc)obj_getitem,
453         .mp_ass_subscript = (objobjargproc)obj_setitem,
454 };
455 PyTypeObject PyTdb = {
456         .tp_name = "Tdb",
457         .tp_basicsize = sizeof(PyTdbObject),
458         .tp_methods = tdb_object_methods,
459         .tp_getset = tdb_object_getsetters,
460         .tp_new = py_tdb_open,
461         .tp_doc = "A TDB file",
462         .tp_repr = (reprfunc)tdb_object_repr,
463         .tp_dealloc = (destructor)tdb_object_dealloc,
464         .tp_as_mapping = &tdb_object_mapping,
465         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
466         .tp_iter = (getiterfunc)tdb_object_iter,
467 };
468
469 static PyMethodDef tdb_methods[] = {
470         { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
471                 "Open a TDB file." },
472         { NULL }
473 };
474
475 void inittdb(void)
476 {
477         PyObject *m;
478
479         if (PyType_Ready(&PyTdb) < 0)
480                 return;
481
482         if (PyType_Ready(&PyTdbIterator) < 0)
483                 return;
484
485         m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
486         if (m == NULL)
487                 return;
488
489         PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
490         PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
491         PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
492
493         PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
494         PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
495         PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
496         PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
497         PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
498         PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
499         PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
500         PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
501
502         Py_INCREF(&PyTdb);
503         PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
504
505         Py_INCREF(&PyTdbIterator);
506 }