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
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 tdb headers */
41 struct tdb_context
*ctx
;
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
)
56 ret
.dptr
= (unsigned char *)PyString_AsString(data
);
57 ret
.dsize
= PyString_Size(data
);
61 static PyObject
*PyString_FromTDB_DATA(TDB_DATA data
)
63 PyObject
*ret
= PyString_FromStringAndSize((const char *)data
.dptr
,
69 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret) \
70 if (ret != TDB_SUCCESS) { \
71 PyErr_SetTDBError(ret); \
75 static void stderr_log(struct tdb_context
*tdb
,
76 enum tdb_log_level level
,
80 fprintf(stderr
, "%s:%s\n", tdb_name(tdb
), message
);
83 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
86 int tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
87 struct tdb_context
*ctx
;
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
))
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
);
104 PyErr_SetFromErrno(PyExc_IOError
);
108 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
116 return (PyObject
*)ret
;
119 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
)
121 tdb_transaction_cancel(self
->ctx
);
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
);
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
);
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
);
146 static PyObject
*obj_lockall(PyTdbObject
*self
)
148 enum TDB_ERROR ret
= tdb_lockall(self
->ctx
);
149 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
153 static PyObject
*obj_unlockall(PyTdbObject
*self
)
155 tdb_unlockall(self
->ctx
);
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
);
166 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
168 tdb_unlockall_read(self
->ctx
);
172 static PyObject
*obj_close(PyTdbObject
*self
)
177 ret
= tdb_close(self
->ctx
);
179 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
183 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
188 if (!PyArg_ParseTuple(args
, "O", &py_key
))
191 key
= PyString_AsTDB_DATA(py_key
);
192 ret
= tdb_fetch(self
->ctx
, key
, &data
);
193 if (ret
== TDB_ERR_NOEXIST
)
195 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
196 return PyString_FromTDB_DATA(data
);
199 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
202 PyObject
*py_key
, *py_data
;
204 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
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
);
215 static PyObject
*obj_firstkey(PyTdbObject
*self
)
220 ret
= tdb_firstkey(self
->ctx
, &key
);
221 if (ret
== TDB_ERR_NOEXIST
)
223 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
225 return PyString_FromTDB_DATA(key
);
228 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
233 if (!PyArg_ParseTuple(args
, "O", &py_key
))
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
)
244 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
246 return PyString_FromTDB_DATA(key
);
249 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
254 if (!PyArg_ParseTuple(args
, "O", &py_key
))
257 key
= PyString_AsTDB_DATA(py_key
);
258 ret
= tdb_delete(self
->ctx
, key
);
259 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
263 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
268 if (!PyArg_ParseTuple(args
, "O", &py_key
))
271 key
= PyString_AsTDB_DATA(py_key
);
272 ret
= tdb_exists(self
->ctx
, key
);
273 if (ret
== TDB_ERR_NOEXIST
)
275 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
279 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
283 int flag
= TDB_REPLACE
;
284 PyObject
*py_key
, *py_value
;
286 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
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
);
297 static PyObject
*obj_add_flag(PyTdbObject
*self
, PyObject
*args
)
301 if (!PyArg_ParseTuple(args
, "I", &flag
))
304 tdb_add_flag(self
->ctx
, flag
);
308 static PyObject
*obj_remove_flag(PyTdbObject
*self
, PyObject
*args
)
312 if (!PyArg_ParseTuple(args
, "I", &flag
))
315 tdb_remove_flag(self
->ctx
, flag
);
323 PyTdbObject
*iteratee
;
324 } PyTdbIteratorObject
;
326 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
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
)
338 PyErr_TDB_ERROR_IS_ERR_RAISE(e
);
342 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
344 Py_DECREF(self
->iteratee
);
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
;
362 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
365 e
= tdb_firstkey(self
->ctx
, &ret
->current
);
366 if (e
== TDB_ERR_NOEXIST
) {
369 PyErr_TDB_ERROR_IS_ERR_RAISE(e
);
372 ret
->iteratee
= 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
);
384 static PyObject
*obj_enable_seqnum(PyTdbObject
*self
)
386 tdb_add_flag(self
->ctx
, TDB_SEQNUM
);
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"
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"
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" },
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
},
455 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
457 if (tdb_get_flags(self
->ctx
) & TDB_INTERNAL
) {
458 return PyString_FromString("Tdb(<internal>)");
460 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
464 static void tdb_object_dealloc(PyTdbObject
*self
)
467 tdb_close(self
->ctx
);
468 self
->ob_type
->tp_free(self
);
471 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
476 if (!PyString_Check(key
)) {
477 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
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");
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
)
498 if (!PyString_Check(key
)) {
499 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
503 tkey
= PyString_AsTDB_DATA(key
);
506 ret
= tdb_delete(self
->ctx
, tkey
);
508 if (!PyString_Check(value
)) {
509 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
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
);
526 static PyMappingMethods tdb_object_mapping
= {
527 .mp_subscript
= (binaryfunc
)obj_getitem
,
528 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
530 static PyTypeObject PyTdb
= {
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." },
555 if (PyType_Ready(&PyTdb
) < 0)
558 if (PyType_Ready(&PyTdbIterator
) < 0)
561 m
= Py_InitModule3("tdb", tdb_methods
, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
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
));
583 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
585 Py_INCREF(&PyTdbIterator
);