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
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/>.
29 #include "system/filesys.h"
31 /* Include tdb headers */
40 staticforward PyTypeObject PyTdb
;
42 static void PyErr_SetTDBError(TDB_CONTEXT
*tdb
)
44 PyErr_SetObject(PyExc_RuntimeError
,
45 Py_BuildValue("(i,s)", tdb_error(tdb
), tdb_errorstr(tdb
)));
48 static TDB_DATA
PyString_AsTDB_DATA(PyObject
*data
)
51 ret
.dptr
= (unsigned char *)PyString_AsString(data
);
52 ret
.dsize
= PyString_Size(data
);
56 static PyObject
*PyString_FromTDB_DATA(TDB_DATA data
)
58 if (data
.dptr
== NULL
&& data
.dsize
== 0) {
61 PyObject
*ret
= PyString_FromStringAndSize((const char *)data
.dptr
,
68 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
70 PyErr_SetTDBError(tdb); \
74 #define PyErr_TDB_RAISE_IF_CLOSED(self) \
76 PyErr_SetObject(PyExc_RuntimeError, \
77 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
81 #define PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self) \
83 PyErr_SetObject(PyExc_RuntimeError, \
84 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
88 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
91 int hash_size
= 0, tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
94 const char *_kwnames
[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL
};
95 char **kwnames
= discard_const_p(char *, _kwnames
);
97 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siiii", kwnames
, &name
, &hash_size
, &tdb_flags
, &flags
, &mode
))
101 tdb_flags
|= TDB_INTERNAL
;
104 ctx
= tdb_open(name
, hash_size
, tdb_flags
, flags
, mode
);
106 PyErr_SetFromErrno(PyExc_IOError
);
110 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
118 return (PyObject
*)ret
;
121 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
)
125 PyErr_TDB_RAISE_IF_CLOSED(self
);
127 ret
= tdb_transaction_cancel(self
->ctx
);
128 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
132 static PyObject
*obj_transaction_commit(PyTdbObject
*self
)
135 PyErr_TDB_RAISE_IF_CLOSED(self
);
136 ret
= tdb_transaction_commit(self
->ctx
);
137 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
141 static PyObject
*obj_transaction_prepare_commit(PyTdbObject
*self
)
144 PyErr_TDB_RAISE_IF_CLOSED(self
);
145 ret
= tdb_transaction_prepare_commit(self
->ctx
);
146 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
150 static PyObject
*obj_transaction_start(PyTdbObject
*self
)
153 PyErr_TDB_RAISE_IF_CLOSED(self
);
154 ret
= tdb_transaction_start(self
->ctx
);
155 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
159 static PyObject
*obj_reopen(PyTdbObject
*self
)
162 PyErr_TDB_RAISE_IF_CLOSED(self
);
163 ret
= tdb_reopen(self
->ctx
);
166 PyErr_SetObject(PyExc_RuntimeError
,
167 Py_BuildValue("(i,s)",
169 "Failed to reopen database"));
175 static PyObject
*obj_lockall(PyTdbObject
*self
)
178 PyErr_TDB_RAISE_IF_CLOSED(self
);
179 ret
= tdb_lockall(self
->ctx
);
180 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
184 static PyObject
*obj_unlockall(PyTdbObject
*self
)
187 PyErr_TDB_RAISE_IF_CLOSED(self
);
188 ret
= tdb_unlockall(self
->ctx
);
189 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
193 static PyObject
*obj_lockall_read(PyTdbObject
*self
)
196 PyErr_TDB_RAISE_IF_CLOSED(self
);
197 ret
= tdb_lockall_read(self
->ctx
);
198 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
202 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
204 int ret
= tdb_unlockall_read(self
->ctx
);
205 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
209 static PyObject
*obj_close(PyTdbObject
*self
)
214 ret
= tdb_close(self
->ctx
);
217 PyErr_SetObject(PyExc_RuntimeError
,
218 Py_BuildValue("(i,s)",
220 "Failed to close database"));
226 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
231 PyErr_TDB_RAISE_IF_CLOSED(self
);
233 if (!PyArg_ParseTuple(args
, "O", &py_key
))
236 key
= PyString_AsTDB_DATA(py_key
);
240 return PyString_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
243 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
246 PyObject
*py_key
, *py_data
;
249 PyErr_TDB_RAISE_IF_CLOSED(self
);
251 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
254 key
= PyString_AsTDB_DATA(py_key
);
257 data
= PyString_AsTDB_DATA(py_data
);
261 ret
= tdb_append(self
->ctx
, key
, data
);
262 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
266 static PyObject
*obj_firstkey(PyTdbObject
*self
)
268 PyErr_TDB_RAISE_IF_CLOSED(self
);
270 return PyString_FromTDB_DATA(tdb_firstkey(self
->ctx
));
273 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
277 PyErr_TDB_RAISE_IF_CLOSED(self
);
279 if (!PyArg_ParseTuple(args
, "O", &py_key
))
282 key
= PyString_AsTDB_DATA(py_key
);
286 return PyString_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
289 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
294 PyErr_TDB_RAISE_IF_CLOSED(self
);
296 if (!PyArg_ParseTuple(args
, "O", &py_key
))
299 key
= PyString_AsTDB_DATA(py_key
);
302 ret
= tdb_delete(self
->ctx
, key
);
303 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
307 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
312 PyErr_TDB_RAISE_IF_CLOSED(self
);
314 if (!PyArg_ParseTuple(args
, "O", &py_key
))
317 key
= PyString_AsTDB_DATA(py_key
);
320 ret
= tdb_exists(self
->ctx
, key
);
321 if (ret
!= TDB_ERR_NOEXIST
) {
322 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
325 return (ret
== TDB_ERR_NOEXIST
)?Py_False
:Py_True
;
328 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
332 int flag
= TDB_REPLACE
;
333 PyObject
*py_key
, *py_value
;
335 PyErr_TDB_RAISE_IF_CLOSED(self
);
337 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
340 key
= PyString_AsTDB_DATA(py_key
);
343 value
= PyString_AsTDB_DATA(py_value
);
347 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
348 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
352 static PyObject
*obj_add_flags(PyTdbObject
*self
, PyObject
*args
)
356 PyErr_TDB_RAISE_IF_CLOSED(self
);
358 if (!PyArg_ParseTuple(args
, "I", &flags
))
361 tdb_add_flags(self
->ctx
, flags
);
365 static PyObject
*obj_remove_flags(PyTdbObject
*self
, PyObject
*args
)
369 PyErr_TDB_RAISE_IF_CLOSED(self
);
371 if (!PyArg_ParseTuple(args
, "I", &flags
))
374 tdb_remove_flags(self
->ctx
, flags
);
381 PyTdbObject
*iteratee
;
382 } PyTdbIteratorObject
;
384 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
388 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
390 current
= self
->current
;
391 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
392 ret
= PyString_FromTDB_DATA(current
);
396 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
398 Py_DECREF(self
->iteratee
);
402 PyTypeObject PyTdbIterator
= {
403 .tp_name
= "Iterator",
404 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
405 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
406 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
407 .tp_flags
= Py_TPFLAGS_DEFAULT
,
408 .tp_iter
= PyObject_SelfIter
,
411 static PyObject
*tdb_object_iter(PyTdbObject
*self
)
413 PyTdbIteratorObject
*ret
;
415 PyErr_TDB_RAISE_IF_CLOSED(self
);
417 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
420 ret
->current
= tdb_firstkey(self
->ctx
);
421 ret
->iteratee
= self
;
423 return (PyObject
*)ret
;
426 static PyObject
*obj_clear(PyTdbObject
*self
)
429 PyErr_TDB_RAISE_IF_CLOSED(self
);
430 ret
= tdb_wipe_all(self
->ctx
);
431 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
435 static PyObject
*obj_repack(PyTdbObject
*self
)
438 PyErr_TDB_RAISE_IF_CLOSED(self
);
439 ret
= tdb_repack(self
->ctx
);
440 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
444 static PyObject
*obj_enable_seqnum(PyTdbObject
*self
)
446 PyErr_TDB_RAISE_IF_CLOSED(self
);
447 tdb_enable_seqnum(self
->ctx
);
451 static PyObject
*obj_increment_seqnum_nonblock(PyTdbObject
*self
)
453 PyErr_TDB_RAISE_IF_CLOSED(self
);
454 tdb_increment_seqnum_nonblock(self
->ctx
);
458 static PyMethodDef tdb_object_methods
[] = {
459 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
460 "S.transaction_cancel() -> None\n"
461 "Cancel the currently active transaction." },
462 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
463 "S.transaction_commit() -> None\n"
464 "Commit the currently active transaction." },
465 { "transaction_prepare_commit", (PyCFunction
)obj_transaction_prepare_commit
, METH_NOARGS
,
466 "S.transaction_prepare_commit() -> None\n"
467 "Prepare to commit the currently active transaction" },
468 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
469 "S.transaction_start() -> None\n"
470 "Start a new transaction." },
471 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
472 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
473 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
474 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
475 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
476 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
477 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.get(key) -> value\n"
479 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
480 "Append data to an existing key." },
481 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
482 "Return the first key in this database." },
483 { "nextkey", (PyCFunction
)obj_nextkey
, METH_NOARGS
, "S.nextkey(key) -> data\n"
484 "Return the next key in this database." },
485 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
486 "Delete an entry." },
487 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
488 "Check whether key exists in this database." },
489 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
491 { "add_flags", (PyCFunction
)obj_add_flags
, METH_VARARGS
, "S.add_flags(flags) -> None" },
492 { "remove_flags", (PyCFunction
)obj_remove_flags
, METH_VARARGS
, "S.remove_flags(flags) -> None" },
493 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
494 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
495 "Wipe the entire database." },
496 { "repack", (PyCFunction
)obj_repack
, METH_NOARGS
, "S.repack() -> None\n"
497 "Repack the entire database." },
498 { "enable_seqnum", (PyCFunction
)obj_enable_seqnum
, METH_NOARGS
,
499 "S.enable_seqnum() -> None" },
500 { "increment_seqnum_nonblock", (PyCFunction
)obj_increment_seqnum_nonblock
, METH_NOARGS
,
501 "S.increment_seqnum_nonblock() -> None" },
505 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
507 PyErr_TDB_RAISE_IF_CLOSED(self
);
508 return PyInt_FromLong(tdb_hash_size(self
->ctx
));
511 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
513 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
514 if (!PyInt_Check(max_dead
))
516 tdb_set_max_dead(self
->ctx
, PyInt_AsLong(max_dead
));
520 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
522 PyErr_TDB_RAISE_IF_CLOSED(self
);
523 return PyInt_FromLong(tdb_map_size(self
->ctx
));
526 static PyObject
*obj_get_freelist_size(PyTdbObject
*self
, void *closure
)
528 PyErr_TDB_RAISE_IF_CLOSED(self
);
529 return PyInt_FromLong(tdb_freelist_size(self
->ctx
));
532 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
534 PyErr_TDB_RAISE_IF_CLOSED(self
);
535 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
538 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
540 PyErr_TDB_RAISE_IF_CLOSED(self
);
541 return PyString_FromString(tdb_name(self
->ctx
));
544 static PyObject
*obj_get_seqnum(PyTdbObject
*self
, void *closure
)
546 PyErr_TDB_RAISE_IF_CLOSED(self
);
547 return PyInt_FromLong(tdb_get_seqnum(self
->ctx
));
551 static PyGetSetDef tdb_object_getsetters
[] = {
552 { discard_const_p(char, "hash_size"),
553 (getter
)obj_get_hash_size
, NULL
, NULL
},
554 { discard_const_p(char, "map_size"),
555 (getter
)obj_get_map_size
, NULL
, NULL
},
556 { discard_const_p(char, "freelist_size"),
557 (getter
)obj_get_freelist_size
, NULL
, NULL
},
558 { discard_const_p(char, "flags"),
559 (getter
)obj_get_flags
, NULL
, NULL
},
560 { discard_const_p(char, "max_dead"),
561 NULL
, (setter
)obj_set_max_dead
, NULL
},
562 { discard_const_p(char, "filename"),
563 (getter
)obj_get_filename
, NULL
,
564 discard_const_p(char, "The filename of this TDB file.") },
565 { discard_const_p(char, "seqnum"),
566 (getter
)obj_get_seqnum
, NULL
, NULL
},
570 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
572 PyErr_TDB_RAISE_IF_CLOSED(self
);
573 if (tdb_get_flags(self
->ctx
) & TDB_INTERNAL
) {
574 return PyString_FromString("Tdb(<internal>)");
576 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
580 static void tdb_object_dealloc(PyTdbObject
*self
)
583 tdb_close(self
->ctx
);
584 self
->ob_type
->tp_free(self
);
587 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
590 PyErr_TDB_RAISE_IF_CLOSED(self
);
591 if (!PyString_Check(key
)) {
592 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
596 tkey
.dptr
= (unsigned char *)PyString_AsString(key
);
597 tkey
.dsize
= PyString_Size(key
);
599 val
= tdb_fetch(self
->ctx
, tkey
);
600 if (val
.dptr
== NULL
) {
601 PyErr_SetString(PyExc_KeyError
, "No such TDB entry");
604 return PyString_FromTDB_DATA(val
);
608 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
612 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
613 if (!PyString_Check(key
)) {
614 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
618 tkey
= PyString_AsTDB_DATA(key
);
621 ret
= tdb_delete(self
->ctx
, tkey
);
623 if (!PyString_Check(value
)) {
624 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
628 tval
= PyString_AsTDB_DATA(value
);
630 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
634 PyErr_SetTDBError(self
->ctx
);
641 static PyMappingMethods tdb_object_mapping
= {
642 .mp_subscript
= (binaryfunc
)obj_getitem
,
643 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
645 static PyTypeObject PyTdb
= {
646 .tp_name
= "tdb.Tdb",
647 .tp_basicsize
= sizeof(PyTdbObject
),
648 .tp_methods
= tdb_object_methods
,
649 .tp_getset
= tdb_object_getsetters
,
650 .tp_new
= py_tdb_open
,
651 .tp_doc
= "A TDB file",
652 .tp_repr
= (reprfunc
)tdb_object_repr
,
653 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
654 .tp_as_mapping
= &tdb_object_mapping
,
655 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
656 .tp_iter
= (getiterfunc
)tdb_object_iter
,
659 static PyMethodDef tdb_methods
[] = {
660 { "open", (PyCFunction
)py_tdb_open
, METH_VARARGS
|METH_KEYWORDS
, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
661 "Open a TDB file." },
670 if (PyType_Ready(&PyTdb
) < 0)
673 if (PyType_Ready(&PyTdbIterator
) < 0)
676 m
= Py_InitModule3("tdb", tdb_methods
,
677 "simple key-value database that supports multiple writers.");
681 PyModule_AddObject(m
, "REPLACE", PyInt_FromLong(TDB_REPLACE
));
682 PyModule_AddObject(m
, "INSERT", PyInt_FromLong(TDB_INSERT
));
683 PyModule_AddObject(m
, "MODIFY", PyInt_FromLong(TDB_MODIFY
));
685 PyModule_AddObject(m
, "DEFAULT", PyInt_FromLong(TDB_DEFAULT
));
686 PyModule_AddObject(m
, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST
));
687 PyModule_AddObject(m
, "INTERNAL", PyInt_FromLong(TDB_INTERNAL
));
688 PyModule_AddObject(m
, "NOLOCK", PyInt_FromLong(TDB_NOLOCK
));
689 PyModule_AddObject(m
, "NOMMAP", PyInt_FromLong(TDB_NOMMAP
));
690 PyModule_AddObject(m
, "CONVERT", PyInt_FromLong(TDB_CONVERT
));
691 PyModule_AddObject(m
, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN
));
692 PyModule_AddObject(m
, "NOSYNC", PyInt_FromLong(TDB_NOSYNC
));
693 PyModule_AddObject(m
, "SEQNUM", PyInt_FromLong(TDB_SEQNUM
));
694 PyModule_AddObject(m
, "VOLATILE", PyInt_FromLong(TDB_VOLATILE
));
695 PyModule_AddObject(m
, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING
));
696 PyModule_AddObject(m
, "DISALLOW_NESTING", PyInt_FromLong(TDB_DISALLOW_NESTING
));
697 PyModule_AddObject(m
, "INCOMPATIBLE_HASH", PyInt_FromLong(TDB_INCOMPATIBLE_HASH
));
699 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
701 PyModule_AddObject(m
, "__version__", PyString_FromString(PACKAGE_VERSION
));
704 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
706 Py_INCREF(&PyTdbIterator
);