s4-dsdb: when replacing linked attribute take always the new dn as the old dn might...
[Samba/id10ts.git] / lib / tdb2 / pytdb.c
blobc760045508c3e624ed9f4bbe197dfbf1db749668
1 /*
2 Unix SMB/CIFS implementation.
4 Python interface to tdb2. Simply modified from tdb1 version.
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>
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
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.
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.
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/>.
28 #include <Python.h>
29 #include "replace.h"
30 #include "system/filesys.h"
32 #ifndef Py_RETURN_NONE
33 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
34 #endif
36 /* Include tdb headers */
37 #include <tdb2.h>
39 typedef struct {
40 PyObject_HEAD
41 struct tdb_context *ctx;
42 bool closed;
43 } PyTdbObject;
45 staticforward PyTypeObject PyTdb;
47 static void PyErr_SetTDBError(enum TDB_ERROR e)
49 PyErr_SetObject(PyExc_RuntimeError,
50 Py_BuildValue("(i,s)", e, tdb_errorstr(e)));
53 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
55 TDB_DATA ret;
56 ret.dptr = (unsigned char *)PyString_AsString(data);
57 ret.dsize = PyString_Size(data);
58 return ret;
61 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
63 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
64 data.dsize);
65 free(data.dptr);
66 return ret;
69 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret) \
70 if (ret != TDB_SUCCESS) { \
71 PyErr_SetTDBError(ret); \
72 return NULL; \
75 static void stderr_log(struct tdb_context *tdb,
76 enum tdb_log_level level,
77 const char *message,
78 void *data)
80 fprintf(stderr, "%s:%s\n", tdb_name(tdb), message);
83 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
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 };
92 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siii", (char **)kwnames, &name, &tdb_flags, &flags, &mode))
93 return NULL;
95 if (name == NULL) {
96 tdb_flags |= TDB_INTERNAL;
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;
108 ret = PyObject_New(PyTdbObject, &PyTdb);
109 if (!ret) {
110 tdb_close(ctx);
111 return NULL;
114 ret->ctx = ctx;
115 ret->closed = false;
116 return (PyObject *)ret;
119 static PyObject *obj_transaction_cancel(PyTdbObject *self)
121 tdb_transaction_cancel(self->ctx);
122 Py_RETURN_NONE;
125 static PyObject *obj_transaction_commit(PyTdbObject *self)
127 enum TDB_ERROR ret = tdb_transaction_commit(self->ctx);
128 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
129 Py_RETURN_NONE;
132 static PyObject *obj_transaction_prepare_commit(PyTdbObject *self)
134 enum TDB_ERROR ret = tdb_transaction_prepare_commit(self->ctx);
135 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
136 Py_RETURN_NONE;
139 static PyObject *obj_transaction_start(PyTdbObject *self)
141 enum TDB_ERROR ret = tdb_transaction_start(self->ctx);
142 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
143 Py_RETURN_NONE;
146 static PyObject *obj_lockall(PyTdbObject *self)
148 enum TDB_ERROR ret = tdb_lockall(self->ctx);
149 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
150 Py_RETURN_NONE;
153 static PyObject *obj_unlockall(PyTdbObject *self)
155 tdb_unlockall(self->ctx);
156 Py_RETURN_NONE;
159 static PyObject *obj_lockall_read(PyTdbObject *self)
161 enum TDB_ERROR ret = tdb_lockall_read(self->ctx);
162 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
163 Py_RETURN_NONE;
166 static PyObject *obj_unlockall_read(PyTdbObject *self)
168 tdb_unlockall_read(self->ctx);
169 Py_RETURN_NONE;
172 static PyObject *obj_close(PyTdbObject *self)
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;
183 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
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;
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);
199 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
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;
207 key = PyString_AsTDB_DATA(py_key);
208 data = PyString_AsTDB_DATA(py_data);
210 ret = tdb_append(self->ctx, key, data);
211 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
212 Py_RETURN_NONE;
215 static PyObject *obj_firstkey(PyTdbObject *self)
217 enum TDB_ERROR ret;
218 TDB_DATA key;
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);
225 return PyString_FromTDB_DATA(key);
228 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
230 TDB_DATA key;
231 PyObject *py_key;
232 enum TDB_ERROR ret;
233 if (!PyArg_ParseTuple(args, "O", &py_key))
234 return NULL;
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);
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);
246 return PyString_FromTDB_DATA(key);
249 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
251 TDB_DATA key;
252 PyObject *py_key;
253 enum TDB_ERROR ret;
254 if (!PyArg_ParseTuple(args, "O", &py_key))
255 return NULL;
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;
263 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
265 TDB_DATA key;
266 enum TDB_ERROR ret;
267 PyObject *py_key;
268 if (!PyArg_ParseTuple(args, "O", &py_key))
269 return NULL;
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;
279 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
281 TDB_DATA key, value;
282 enum TDB_ERROR ret;
283 int flag = TDB_REPLACE;
284 PyObject *py_key, *py_value;
286 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
287 return NULL;
289 key = PyString_AsTDB_DATA(py_key);
290 value = PyString_AsTDB_DATA(py_value);
292 ret = tdb_store(self->ctx, key, value, flag);
293 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
294 Py_RETURN_NONE;
297 static PyObject *obj_add_flag(PyTdbObject *self, PyObject *args)
299 unsigned flag;
301 if (!PyArg_ParseTuple(args, "I", &flag))
302 return NULL;
304 tdb_add_flag(self->ctx, flag);
305 Py_RETURN_NONE;
308 static PyObject *obj_remove_flag(PyTdbObject *self, PyObject *args)
310 unsigned flag;
312 if (!PyArg_ParseTuple(args, "I", &flag))
313 return NULL;
315 tdb_remove_flag(self->ctx, flag);
316 Py_RETURN_NONE;
319 typedef struct {
320 PyObject_HEAD
321 TDB_DATA current;
322 bool end;
323 PyTdbObject *iteratee;
324 } PyTdbIteratorObject;
326 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
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;
342 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
344 Py_DECREF(self->iteratee);
345 PyObject_Del(self);
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,
357 static PyObject *tdb_object_iter(PyTdbObject *self)
359 PyTdbIteratorObject *ret;
360 enum TDB_ERROR e;
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;
372 ret->iteratee = self;
373 Py_INCREF(self);
374 return (PyObject *)ret;
377 static PyObject *obj_clear(PyTdbObject *self)
379 enum TDB_ERROR ret = tdb_wipe_all(self->ctx);
380 PyErr_TDB_ERROR_IS_ERR_RAISE(ret);
381 Py_RETURN_NONE;
384 static PyObject *obj_enable_seqnum(PyTdbObject *self)
386 tdb_add_flag(self->ctx, TDB_SEQNUM);
387 Py_RETURN_NONE;
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 }
432 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
434 return PyInt_FromLong(tdb_get_flags(self->ctx));
437 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
439 return PyString_FromString(tdb_name(self->ctx));
442 static PyObject *obj_get_seqnum(PyTdbObject *self, void *closure)
444 return PyInt_FromLong(tdb_get_seqnum(self->ctx));
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 }
455 static PyObject *tdb_object_repr(PyTdbObject *self)
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));
464 static void tdb_object_dealloc(PyTdbObject *self)
466 if (!self->closed)
467 tdb_close(self->ctx);
468 self->ob_type->tp_free(self);
471 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
473 TDB_DATA tkey, val;
474 enum TDB_ERROR ret;
476 if (!PyString_Check(key)) {
477 PyErr_SetString(PyExc_TypeError, "Expected string as key");
478 return NULL;
481 tkey.dptr = (unsigned char *)PyString_AsString(key);
482 tkey.dsize = PyString_Size(key);
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);
494 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
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;
503 tkey = PyString_AsTDB_DATA(key);
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;
513 tval = PyString_AsTDB_DATA(value);
515 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
518 if (ret != TDB_SUCCESS) {
519 PyErr_SetTDBError(ret);
520 return -1;
523 return ret;
526 static PyMappingMethods tdb_object_mapping = {
527 .mp_subscript = (binaryfunc)obj_getitem,
528 .mp_ass_subscript = (objobjargproc)obj_setitem,
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,
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 }
550 void inittdb(void);
551 void inittdb(void)
553 PyObject *m;
555 if (PyType_Ready(&PyTdb) < 0)
556 return;
558 if (PyType_Ready(&PyTdbIterator) < 0)
559 return;
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;
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));
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));
578 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
580 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
582 Py_INCREF(&PyTdb);
583 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
585 Py_INCREF(&PyTdbIterator);