2 Unix SMB/CIFS implementation.
4 Python interface to ntdb. Simply modified from tdb 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 ntdb
11 ** library. This does NOT imply that all of Samba is released
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/>.
30 #include "system/filesys.h"
32 #ifndef Py_RETURN_NONE
33 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
36 /* Include ntdb headers */
41 struct ntdb_context *ctx;
45 staticforward PyTypeObject PyNtdb;
47 static void PyErr_SetTDBError(enum NTDB_ERROR e)
49 PyErr_SetObject(PyExc_RuntimeError,
50 Py_BuildValue("(i,s)", e, ntdb_errorstr(e)));
53 static NTDB_DATA PyString_AsNtdb_Data(PyObject *data)
56 ret.dptr = (unsigned char *)PyString_AsString(data);
57 ret.dsize = PyString_Size(data);
61 static PyObject *PyString_FromNtdb_Data(NTDB_DATA data)
63 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
69 #define PyErr_NTDB_ERROR_IS_ERR_RAISE(ret) \
70 if (ret != NTDB_SUCCESS) { \
71 PyErr_SetTDBError(ret); \
75 #define PyNtdb_CHECK_CLOSED(pyobj) \
77 PyErr_SetObject(PyExc_RuntimeError, \
78 Py_BuildValue("(i,s)", NTDB_ERR_EINVAL, "database is closed")); \
82 static void stderr_log(struct ntdb_context *ntdb,
83 enum ntdb_log_level level,
84 enum NTDB_ERROR ecode,
88 fprintf(stderr, "%s:%s:%s\n",
89 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
92 static PyObject *py_ntdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
95 int ntdb_flags = NTDB_DEFAULT, flags = O_RDWR, mode = 0600;
96 struct ntdb_context *ctx;
98 union ntdb_attribute logattr;
99 const char *kwnames[] = { "name", "ntdb_flags", "flags", "mode", NULL };
101 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siii", cast_const2(char **, kwnames), &name, &ntdb_flags, &flags, &mode))
105 ntdb_flags |= NTDB_INTERNAL;
109 logattr.log.base.attr = NTDB_ATTRIBUTE_LOG;
110 logattr.log.base.next = NULL;
111 logattr.log.fn = stderr_log;
112 ctx = ntdb_open(name, ntdb_flags, flags, mode, &logattr);
114 PyErr_SetFromErrno(PyExc_IOError);
118 ret = PyObject_New(PyNtdbObject, &PyNtdb);
126 return (PyObject *)ret;
129 static PyObject *obj_transaction_cancel(PyNtdbObject *self)
131 PyNtdb_CHECK_CLOSED(self);
132 ntdb_transaction_cancel(self->ctx);
136 static PyObject *obj_transaction_commit(PyNtdbObject *self)
139 PyNtdb_CHECK_CLOSED(self);
140 ret = ntdb_transaction_commit(self->ctx);
141 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
145 static PyObject *obj_transaction_prepare_commit(PyNtdbObject *self)
148 PyNtdb_CHECK_CLOSED(self);
149 ret = ntdb_transaction_prepare_commit(self->ctx);
150 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
154 static PyObject *obj_transaction_start(PyNtdbObject *self)
157 PyNtdb_CHECK_CLOSED(self);
158 ret = ntdb_transaction_start(self->ctx);
159 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
163 static PyObject *obj_lockall(PyNtdbObject *self)
166 PyNtdb_CHECK_CLOSED(self);
167 ret = ntdb_lockall(self->ctx);
168 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
172 static PyObject *obj_unlockall(PyNtdbObject *self)
174 PyNtdb_CHECK_CLOSED(self);
175 ntdb_unlockall(self->ctx);
179 static PyObject *obj_lockall_read(PyNtdbObject *self)
182 PyNtdb_CHECK_CLOSED(self);
183 ret = ntdb_lockall_read(self->ctx);
184 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
188 static PyObject *obj_unlockall_read(PyNtdbObject *self)
190 PyNtdb_CHECK_CLOSED(self);
191 ntdb_unlockall_read(self->ctx);
195 static PyObject *obj_close(PyNtdbObject *self)
200 ret = ntdb_close(self->ctx);
203 PyErr_SetTDBError(NTDB_ERR_IO);
209 static PyObject *obj_get(PyNtdbObject *self, PyObject *args)
215 PyNtdb_CHECK_CLOSED(self);
217 if (!PyArg_ParseTuple(args, "O", &py_key))
220 key = PyString_AsNtdb_Data(py_key);
221 ret = ntdb_fetch(self->ctx, key, &data);
222 if (ret == NTDB_ERR_NOEXIST)
224 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
225 return PyString_FromNtdb_Data(data);
228 static PyObject *obj_append(PyNtdbObject *self, PyObject *args)
231 PyObject *py_key, *py_data;
234 PyNtdb_CHECK_CLOSED(self);
236 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
239 key = PyString_AsNtdb_Data(py_key);
240 data = PyString_AsNtdb_Data(py_data);
242 ret = ntdb_append(self->ctx, key, data);
243 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
247 static PyObject *obj_firstkey(PyNtdbObject *self)
252 PyNtdb_CHECK_CLOSED(self);
254 ret = ntdb_firstkey(self->ctx, &key);
255 if (ret == NTDB_ERR_NOEXIST)
257 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
259 return PyString_FromNtdb_Data(key);
262 static PyObject *obj_nextkey(PyNtdbObject *self, PyObject *args)
268 PyNtdb_CHECK_CLOSED(self);
270 if (!PyArg_ParseTuple(args, "O", &py_key))
273 /* Malloc here, since ntdb_nextkey frees. */
274 key.dsize = PyString_Size(py_key);
275 key.dptr = malloc(key.dsize);
276 memcpy(key.dptr, PyString_AsString(py_key), key.dsize);
278 ret = ntdb_nextkey(self->ctx, &key);
279 if (ret == NTDB_ERR_NOEXIST)
281 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
283 return PyString_FromNtdb_Data(key);
286 static PyObject *obj_delete(PyNtdbObject *self, PyObject *args)
292 PyNtdb_CHECK_CLOSED(self);
294 if (!PyArg_ParseTuple(args, "O", &py_key))
297 key = PyString_AsNtdb_Data(py_key);
298 ret = ntdb_delete(self->ctx, key);
299 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
303 static PyObject *obj_has_key(PyNtdbObject *self, PyObject *args)
308 PyNtdb_CHECK_CLOSED(self);
310 if (!PyArg_ParseTuple(args, "O", &py_key))
313 key = PyString_AsNtdb_Data(py_key);
314 if (ntdb_exists(self->ctx, key))
319 static PyObject *obj_store(PyNtdbObject *self, PyObject *args)
321 NTDB_DATA key, value;
323 int flag = NTDB_REPLACE;
324 PyObject *py_key, *py_value;
325 PyNtdb_CHECK_CLOSED(self);
327 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
330 key = PyString_AsNtdb_Data(py_key);
331 value = PyString_AsNtdb_Data(py_value);
333 ret = ntdb_store(self->ctx, key, value, flag);
334 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
338 static PyObject *obj_add_flag(PyNtdbObject *self, PyObject *args)
341 PyNtdb_CHECK_CLOSED(self);
343 if (!PyArg_ParseTuple(args, "I", &flag))
346 ntdb_add_flag(self->ctx, flag);
350 static PyObject *obj_remove_flag(PyNtdbObject *self, PyObject *args)
354 PyNtdb_CHECK_CLOSED(self);
356 if (!PyArg_ParseTuple(args, "I", &flag))
359 ntdb_remove_flag(self->ctx, flag);
367 PyNtdbObject *iteratee;
368 } PyNtdbIteratorObject;
370 static PyObject *ntdb_iter_next(PyNtdbIteratorObject *self)
376 ret = PyString_FromStringAndSize((const char *)self->current.dptr,
377 self->current.dsize);
378 e = ntdb_nextkey(self->iteratee->ctx, &self->current);
379 if (e == NTDB_ERR_NOEXIST)
382 PyErr_NTDB_ERROR_IS_ERR_RAISE(e);
386 static void ntdb_iter_dealloc(PyNtdbIteratorObject *self)
388 Py_DECREF(self->iteratee);
392 PyTypeObject PyNtdbIterator = {
393 .tp_name = "Iterator",
394 .tp_basicsize = sizeof(PyNtdbIteratorObject),
395 .tp_iternext = (iternextfunc)ntdb_iter_next,
396 .tp_dealloc = (destructor)ntdb_iter_dealloc,
397 .tp_flags = Py_TPFLAGS_DEFAULT,
398 .tp_iter = PyObject_SelfIter,
401 static PyObject *ntdb_object_iter(PyNtdbObject *self)
403 PyNtdbIteratorObject *ret;
405 PyNtdb_CHECK_CLOSED(self);
407 ret = PyObject_New(PyNtdbIteratorObject, &PyNtdbIterator);
410 e = ntdb_firstkey(self->ctx, &ret->current);
411 if (e == NTDB_ERR_NOEXIST) {
414 PyErr_NTDB_ERROR_IS_ERR_RAISE(e);
417 ret->iteratee = self;
419 return (PyObject *)ret;
422 static PyObject *obj_clear(PyNtdbObject *self)
425 PyNtdb_CHECK_CLOSED(self);
426 ret = ntdb_wipe_all(self->ctx);
427 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
431 static PyObject *obj_enable_seqnum(PyNtdbObject *self)
433 PyNtdb_CHECK_CLOSED(self);
434 ntdb_add_flag(self->ctx, NTDB_SEQNUM);
438 static PyMethodDef ntdb_object_methods[] = {
439 { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
440 "S.transaction_cancel() -> None\n"
441 "Cancel the currently active transaction." },
442 { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
443 "S.transaction_commit() -> None\n"
444 "Commit the currently active transaction." },
445 { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
446 "S.transaction_prepare_commit() -> None\n"
447 "Prepare to commit the currently active transaction" },
448 { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
449 "S.transaction_start() -> None\n"
450 "Start a new transaction." },
451 { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
452 { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
453 { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
454 { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
455 { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
456 { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
458 { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
459 "Append data to an existing key." },
460 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
461 "Return the first key in this database." },
462 { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
463 "Return the next key in this database." },
464 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
465 "Delete an entry." },
466 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
467 "Check whether key exists in this database." },
468 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
470 { "add_flag", (PyCFunction)obj_add_flag, METH_VARARGS, "S.add_flag(flag) -> None" },
471 { "remove_flag", (PyCFunction)obj_remove_flag, METH_VARARGS, "S.remove_flag(flag) -> None" },
472 { "iterkeys", (PyCFunction)ntdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
473 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
474 "Wipe the entire database." },
475 { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
476 "S.enable_seqnum() -> None" },
480 static PyObject *obj_get_flags(PyNtdbObject *self, void *closure)
482 PyNtdb_CHECK_CLOSED(self);
483 return PyInt_FromLong(ntdb_get_flags(self->ctx));
486 static PyObject *obj_get_filename(PyNtdbObject *self, void *closure)
488 PyNtdb_CHECK_CLOSED(self);
489 return PyString_FromString(ntdb_name(self->ctx));
492 static PyObject *obj_get_seqnum(PyNtdbObject *self, void *closure)
494 PyNtdb_CHECK_CLOSED(self);
495 return PyInt_FromLong(ntdb_get_seqnum(self->ctx));
499 static PyGetSetDef ntdb_object_getsetters[] = {
500 { cast_const(char *, "flags"), (getter)obj_get_flags, NULL, NULL },
501 { cast_const(char *, "filename"), (getter)obj_get_filename, NULL,
502 cast_const(char *, "The filename of this NTDB file.")},
503 { cast_const(char *, "seqnum"), (getter)obj_get_seqnum, NULL, NULL },
507 static PyObject *ntdb_object_repr(PyNtdbObject *self)
509 if (ntdb_get_flags(self->ctx) & NTDB_INTERNAL) {
510 return PyString_FromString("Ntdb(<internal>)");
512 return PyString_FromFormat("Ntdb('%s')", ntdb_name(self->ctx));
516 static void ntdb_object_dealloc(PyNtdbObject *self)
519 ntdb_close(self->ctx);
520 self->ob_type->tp_free(self);
523 static PyObject *obj_getitem(PyNtdbObject *self, PyObject *key)
528 PyNtdb_CHECK_CLOSED(self);
530 if (!PyString_Check(key)) {
531 PyErr_SetString(PyExc_TypeError, "Expected string as key");
535 tkey.dptr = (unsigned char *)PyString_AsString(key);
536 tkey.dsize = PyString_Size(key);
538 ret = ntdb_fetch(self->ctx, tkey, &val);
539 if (ret == NTDB_ERR_NOEXIST) {
540 PyErr_SetString(PyExc_KeyError, "No such NTDB entry");
543 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
544 return PyString_FromNtdb_Data(val);
548 static int obj_setitem(PyNtdbObject *self, PyObject *key, PyObject *value)
550 NTDB_DATA tkey, tval;
553 PyErr_SetObject(PyExc_RuntimeError,
554 Py_BuildValue("(i,s)", NTDB_ERR_EINVAL, "database is closed"));
558 if (!PyString_Check(key)) {
559 PyErr_SetString(PyExc_TypeError, "Expected string as key");
563 tkey = PyString_AsNtdb_Data(key);
566 ret = ntdb_delete(self->ctx, tkey);
568 if (!PyString_Check(value)) {
569 PyErr_SetString(PyExc_TypeError, "Expected string as value");
573 tval = PyString_AsNtdb_Data(value);
575 ret = ntdb_store(self->ctx, tkey, tval, NTDB_REPLACE);
578 if (ret != NTDB_SUCCESS) {
579 PyErr_SetTDBError(ret);
586 static PyMappingMethods ntdb_object_mapping = {
587 .mp_subscript = (binaryfunc)obj_getitem,
588 .mp_ass_subscript = (objobjargproc)obj_setitem,
591 static PyTypeObject PyNtdb = {
592 .tp_name = "ntdb.Ntdb",
593 .tp_basicsize = sizeof(PyNtdbObject),
594 .tp_methods = ntdb_object_methods,
595 .tp_getset = ntdb_object_getsetters,
596 .tp_new = py_ntdb_open,
597 .tp_doc = "A NTDB file",
598 .tp_repr = (reprfunc)ntdb_object_repr,
599 .tp_dealloc = (destructor)ntdb_object_dealloc,
600 .tp_as_mapping = &ntdb_object_mapping,
601 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
602 .tp_iter = (getiterfunc)ntdb_object_iter,
605 static PyMethodDef ntdb_methods[] = {
606 { "open", (PyCFunction)py_ntdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, ntdb_flags=NTDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
607 "Open a NTDB file." },
616 if (PyType_Ready(&PyNtdb) < 0)
619 if (PyType_Ready(&PyNtdbIterator) < 0)
622 m = Py_InitModule3("ntdb", ntdb_methods, "NTDB is a simple key-value database similar to GDBM that supports multiple writers.");
626 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(NTDB_REPLACE));
627 PyModule_AddObject(m, "INSERT", PyInt_FromLong(NTDB_INSERT));
628 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(NTDB_MODIFY));
630 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(NTDB_DEFAULT));
631 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(NTDB_INTERNAL));
632 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(NTDB_NOLOCK));
633 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(NTDB_NOMMAP));
634 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(NTDB_CONVERT));
635 PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(NTDB_NOSYNC));
636 PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(NTDB_SEQNUM));
637 PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(NTDB_ALLOW_NESTING));
639 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
641 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
644 PyModule_AddObject(m, "Ntdb", (PyObject *)&PyNtdb);
646 Py_INCREF(&PyNtdbIterator);