s4-drs: Add DRSUAPI_DRS_NONGC_RO_REP bit to DRS_OPTIONS
[Samba/fernandojvsilva.git] / lib / tdb / pytdb.c
blob202dca1571e495496de2944e6c829e5b07b9b112
1 /*
2 Unix SMB/CIFS implementation.
4 Python interface to tdb.
6 Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
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
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.
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.
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/>.
27 #include "replace.h"
28 #include "system/filesys.h"
30 #include <Python.h>
31 #ifndef Py_RETURN_NONE
32 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
33 #endif
35 /* Include tdb headers */
36 #include <tdb.h>
38 typedef struct {
39 PyObject_HEAD
40 TDB_CONTEXT *ctx;
41 bool closed;
42 } PyTdbObject;
44 PyAPI_DATA(PyTypeObject) PyTdb;
46 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
48 PyErr_SetObject(PyExc_RuntimeError,
49 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
52 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
54 TDB_DATA ret;
55 ret.dptr = (unsigned char *)PyString_AsString(data);
56 ret.dsize = PyString_Size(data);
57 return ret;
60 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
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;
72 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
73 if (ret != 0) { \
74 PyErr_SetTDBError(tdb); \
75 return NULL; \
78 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
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 };
86 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
87 return NULL;
89 ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
90 if (ctx == NULL) {
91 PyErr_SetFromErrno(PyExc_IOError);
92 return NULL;
95 ret = PyObject_New(PyTdbObject, &PyTdb);
96 ret->ctx = ctx;
97 ret->closed = false;
98 return (PyObject *)ret;
101 static PyObject *obj_transaction_cancel(PyTdbObject *self)
103 int ret = tdb_transaction_cancel(self->ctx);
104 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
105 Py_RETURN_NONE;
108 static PyObject *obj_transaction_commit(PyTdbObject *self)
110 int ret = tdb_transaction_commit(self->ctx);
111 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
112 Py_RETURN_NONE;
115 static PyObject *obj_transaction_recover(PyTdbObject *self)
117 int ret = tdb_transaction_recover(self->ctx);
118 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
119 Py_RETURN_NONE;
122 static PyObject *obj_transaction_start(PyTdbObject *self)
124 int ret = tdb_transaction_start(self->ctx);
125 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
126 Py_RETURN_NONE;
129 static PyObject *obj_reopen(PyTdbObject *self)
131 int ret = tdb_reopen(self->ctx);
132 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
133 Py_RETURN_NONE;
136 static PyObject *obj_lockall(PyTdbObject *self)
138 int ret = tdb_lockall(self->ctx);
139 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
140 Py_RETURN_NONE;
143 static PyObject *obj_unlockall(PyTdbObject *self)
145 int ret = tdb_unlockall(self->ctx);
146 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
147 Py_RETURN_NONE;
150 static PyObject *obj_lockall_read(PyTdbObject *self)
152 int ret = tdb_lockall_read(self->ctx);
153 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
154 Py_RETURN_NONE;
157 static PyObject *obj_unlockall_read(PyTdbObject *self)
159 int ret = tdb_unlockall_read(self->ctx);
160 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
161 Py_RETURN_NONE;
164 static PyObject *obj_close(PyTdbObject *self)
166 int ret;
167 if (self->closed)
168 Py_RETURN_NONE;
169 ret = tdb_close(self->ctx);
170 self->closed = true;
171 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
172 Py_RETURN_NONE;
175 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
177 TDB_DATA key;
178 PyObject *py_key;
179 if (!PyArg_ParseTuple(args, "O", &py_key))
180 return NULL;
182 key = PyString_AsTDB_DATA(py_key);
184 return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
187 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
189 TDB_DATA key, data;
190 PyObject *py_key, *py_data;
191 int ret;
192 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
193 return NULL;
195 key = PyString_AsTDB_DATA(py_key);
196 data = PyString_AsTDB_DATA(py_data);
198 ret = tdb_append(self->ctx, key, data);
199 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
200 Py_RETURN_NONE;
203 static PyObject *obj_firstkey(PyTdbObject *self)
205 return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
208 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
210 TDB_DATA key;
211 PyObject *py_key;
212 if (!PyArg_ParseTuple(args, "O", &py_key))
213 return NULL;
215 key = PyString_AsTDB_DATA(py_key);
217 return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
220 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
222 TDB_DATA key;
223 PyObject *py_key;
224 int ret;
225 if (!PyArg_ParseTuple(args, "O", &py_key))
226 return NULL;
228 key = PyString_AsTDB_DATA(py_key);
229 ret = tdb_delete(self->ctx, key);
230 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
231 Py_RETURN_NONE;
234 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
236 TDB_DATA key;
237 int ret;
238 PyObject *py_key;
239 if (!PyArg_ParseTuple(args, "O", &py_key))
240 return NULL;
242 key = PyString_AsTDB_DATA(py_key);
243 ret = tdb_exists(self->ctx, key);
244 if (ret != TDB_ERR_NOEXIST) {
245 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
248 return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
251 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
253 TDB_DATA key, value;
254 int ret;
255 int flag = TDB_REPLACE;
256 PyObject *py_key, *py_value;
258 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
259 return NULL;
261 key = PyString_AsTDB_DATA(py_key);
262 value = PyString_AsTDB_DATA(py_value);
264 ret = tdb_store(self->ctx, key, value, flag);
265 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
266 Py_RETURN_NONE;
270 typedef struct {
271 PyObject_HEAD
272 TDB_DATA current;
273 PyTdbObject *iteratee;
274 } PyTdbIteratorObject;
276 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
278 TDB_DATA current;
279 PyObject *ret;
280 if (self->current.dptr == NULL && self->current.dsize == 0)
281 return NULL;
282 current = self->current;
283 self->current = tdb_nextkey(self->iteratee->ctx, self->current);
284 ret = PyString_FromTDB_DATA(current);
285 return ret;
288 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
290 Py_DECREF(self->iteratee);
291 PyObject_Del(self);
294 PyTypeObject PyTdbIterator = {
295 .tp_name = "Iterator",
296 .tp_basicsize = sizeof(PyTdbIteratorObject),
297 .tp_iternext = (iternextfunc)tdb_iter_next,
298 .tp_dealloc = (destructor)tdb_iter_dealloc,
299 .tp_flags = Py_TPFLAGS_DEFAULT,
300 .tp_iter = PyObject_SelfIter,
303 static PyObject *tdb_object_iter(PyTdbObject *self)
305 PyTdbIteratorObject *ret;
307 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
308 ret->current = tdb_firstkey(self->ctx);
309 ret->iteratee = self;
310 Py_INCREF(self);
311 return (PyObject *)ret;
314 static PyObject *obj_clear(PyTdbObject *self)
316 int ret = tdb_wipe_all(self->ctx);
317 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
318 Py_RETURN_NONE;
321 static PyMethodDef tdb_object_methods[] = {
322 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
323 "S.transaction_cancel() -> None\n"
324 "Cancel the currently active transaction." },
325 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
326 "S.transaction_commit() -> None\n"
327 "Commit the currently active transaction." },
328 { "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
329 "S.transaction_recover() -> None\n"
330 "Recover the currently active transaction." },
331 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
332 "S.transaction_start() -> None\n"
333 "Start a new transaction." },
334 { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
335 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
336 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
337 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
338 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
339 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
340 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
341 "Fetch a value." },
342 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
343 "Append data to an existing key." },
344 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
345 "Return the first key in this database." },
346 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
347 "Return the next key in this database." },
348 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
349 "Delete an entry." },
350 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
351 "Check whether key exists in this database." },
352 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
353 "Store data." },
354 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
355 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
356 "Wipe the entire database." },
357 { NULL }
360 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
362 return PyInt_FromLong(tdb_hash_size(self->ctx));
365 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
367 if (!PyInt_Check(max_dead))
368 return -1;
369 tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
370 return 0;
373 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
375 return PyInt_FromLong(tdb_map_size(self->ctx));
378 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
380 return PyInt_FromLong(tdb_get_flags(self->ctx));
383 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
385 return PyString_FromString(tdb_name(self->ctx));
388 static PyGetSetDef tdb_object_getsetters[] = {
389 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
390 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
391 { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
392 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
393 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
394 { NULL }
397 static PyObject *tdb_object_repr(PyTdbObject *self)
399 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
402 static void tdb_object_dealloc(PyTdbObject *self)
404 if (!self->closed)
405 tdb_close(self->ctx);
406 PyObject_Del(self);
409 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
411 TDB_DATA tkey, val;
412 if (!PyString_Check(key)) {
413 PyErr_SetString(PyExc_TypeError, "Expected string as key");
414 return NULL;
417 tkey.dptr = (unsigned char *)PyString_AsString(key);
418 tkey.dsize = PyString_Size(key);
420 val = tdb_fetch(self->ctx, tkey);
421 if (val.dptr == NULL) {
422 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
423 return NULL;
424 } else {
425 return PyString_FromTDB_DATA(val);
429 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
431 TDB_DATA tkey, tval;
432 int ret;
433 if (!PyString_Check(key)) {
434 PyErr_SetString(PyExc_TypeError, "Expected string as key");
435 return -1;
438 tkey = PyString_AsTDB_DATA(key);
440 if (value == NULL) {
441 ret = tdb_delete(self->ctx, tkey);
442 } else {
443 if (!PyString_Check(value)) {
444 PyErr_SetString(PyExc_TypeError, "Expected string as value");
445 return -1;
448 tval = PyString_AsTDB_DATA(value);
450 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
453 if (ret != 0) {
454 PyErr_SetTDBError(self->ctx);
455 return -1;
458 return ret;
461 static PyMappingMethods tdb_object_mapping = {
462 .mp_subscript = (binaryfunc)obj_getitem,
463 .mp_ass_subscript = (objobjargproc)obj_setitem,
465 PyTypeObject PyTdb = {
466 .tp_name = "Tdb",
467 .tp_basicsize = sizeof(PyTdbObject),
468 .tp_methods = tdb_object_methods,
469 .tp_getset = tdb_object_getsetters,
470 .tp_new = py_tdb_open,
471 .tp_doc = "A TDB file",
472 .tp_repr = (reprfunc)tdb_object_repr,
473 .tp_dealloc = (destructor)tdb_object_dealloc,
474 .tp_as_mapping = &tdb_object_mapping,
475 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
476 .tp_iter = (getiterfunc)tdb_object_iter,
479 static PyMethodDef tdb_methods[] = {
480 { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
481 "Open a TDB file." },
482 { NULL }
485 void inittdb(void)
487 PyObject *m;
489 if (PyType_Ready(&PyTdb) < 0)
490 return;
492 if (PyType_Ready(&PyTdbIterator) < 0)
493 return;
495 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
496 if (m == NULL)
497 return;
499 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
500 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
501 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
503 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
504 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
505 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
506 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
507 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
508 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
509 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
510 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
512 Py_INCREF(&PyTdb);
513 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
515 Py_INCREF(&PyTdbIterator);