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 #ifndef Py_RETURN_NONE
32 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
35 /* Include tdb headers */
44 staticforward 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
)
55 ret
.dptr
= (unsigned char *)PyString_AsString(data
);
56 ret
.dsize
= PyString_Size(data
);
60 static PyObject
*PyString_FromTDB_DATA(TDB_DATA data
)
62 if (data
.dptr
== NULL
&& data
.dsize
== 0) {
65 PyObject
*ret
= PyString_FromStringAndSize((const char *)data
.dptr
,
72 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
74 PyErr_SetTDBError(tdb); \
78 #define PyErr_TDB_RAISE_IF_CLOSED(self) \
80 PyErr_SetObject(PyExc_RuntimeError, \
81 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
85 #define PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self) \
87 PyErr_SetObject(PyExc_RuntimeError, \
88 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
92 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
95 int hash_size
= 0, tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
98 const char *kwnames
[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL
};
100 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siiii", (char **)kwnames
, &name
, &hash_size
, &tdb_flags
, &flags
, &mode
))
104 tdb_flags
|= TDB_INTERNAL
;
107 ctx
= tdb_open(name
, hash_size
, tdb_flags
, flags
, mode
);
109 PyErr_SetFromErrno(PyExc_IOError
);
113 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
121 return (PyObject
*)ret
;
124 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
)
128 PyErr_TDB_RAISE_IF_CLOSED(self
);
130 ret
= tdb_transaction_cancel(self
->ctx
);
131 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
135 static PyObject
*obj_transaction_commit(PyTdbObject
*self
)
138 PyErr_TDB_RAISE_IF_CLOSED(self
);
139 ret
= tdb_transaction_commit(self
->ctx
);
140 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
144 static PyObject
*obj_transaction_prepare_commit(PyTdbObject
*self
)
147 PyErr_TDB_RAISE_IF_CLOSED(self
);
148 ret
= tdb_transaction_prepare_commit(self
->ctx
);
149 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
153 static PyObject
*obj_transaction_start(PyTdbObject
*self
)
156 PyErr_TDB_RAISE_IF_CLOSED(self
);
157 ret
= tdb_transaction_start(self
->ctx
);
158 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
162 static PyObject
*obj_reopen(PyTdbObject
*self
)
165 PyErr_TDB_RAISE_IF_CLOSED(self
);
166 ret
= tdb_reopen(self
->ctx
);
167 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
171 static PyObject
*obj_lockall(PyTdbObject
*self
)
174 PyErr_TDB_RAISE_IF_CLOSED(self
);
175 ret
= tdb_lockall(self
->ctx
);
176 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
180 static PyObject
*obj_unlockall(PyTdbObject
*self
)
183 PyErr_TDB_RAISE_IF_CLOSED(self
);
184 ret
= tdb_unlockall(self
->ctx
);
185 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
189 static PyObject
*obj_lockall_read(PyTdbObject
*self
)
192 PyErr_TDB_RAISE_IF_CLOSED(self
);
193 ret
= tdb_lockall_read(self
->ctx
);
194 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
198 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
200 int ret
= tdb_unlockall_read(self
->ctx
);
201 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
205 static PyObject
*obj_close(PyTdbObject
*self
)
210 ret
= tdb_close(self
->ctx
);
212 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
216 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
221 PyErr_TDB_RAISE_IF_CLOSED(self
);
223 if (!PyArg_ParseTuple(args
, "O", &py_key
))
226 key
= PyString_AsTDB_DATA(py_key
);
230 return PyString_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
233 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
236 PyObject
*py_key
, *py_data
;
239 PyErr_TDB_RAISE_IF_CLOSED(self
);
241 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
244 key
= PyString_AsTDB_DATA(py_key
);
247 data
= PyString_AsTDB_DATA(py_data
);
251 ret
= tdb_append(self
->ctx
, key
, data
);
252 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
256 static PyObject
*obj_firstkey(PyTdbObject
*self
)
258 PyErr_TDB_RAISE_IF_CLOSED(self
);
260 return PyString_FromTDB_DATA(tdb_firstkey(self
->ctx
));
263 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
267 PyErr_TDB_RAISE_IF_CLOSED(self
);
269 if (!PyArg_ParseTuple(args
, "O", &py_key
))
272 key
= PyString_AsTDB_DATA(py_key
);
276 return PyString_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
279 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
284 PyErr_TDB_RAISE_IF_CLOSED(self
);
286 if (!PyArg_ParseTuple(args
, "O", &py_key
))
289 key
= PyString_AsTDB_DATA(py_key
);
292 ret
= tdb_delete(self
->ctx
, key
);
293 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
297 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
302 PyErr_TDB_RAISE_IF_CLOSED(self
);
304 if (!PyArg_ParseTuple(args
, "O", &py_key
))
307 key
= PyString_AsTDB_DATA(py_key
);
310 ret
= tdb_exists(self
->ctx
, key
);
311 if (ret
!= TDB_ERR_NOEXIST
) {
312 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
315 return (ret
== TDB_ERR_NOEXIST
)?Py_False
:Py_True
;
318 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
322 int flag
= TDB_REPLACE
;
323 PyObject
*py_key
, *py_value
;
325 PyErr_TDB_RAISE_IF_CLOSED(self
);
327 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
330 key
= PyString_AsTDB_DATA(py_key
);
333 value
= PyString_AsTDB_DATA(py_value
);
337 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
338 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
342 static PyObject
*obj_add_flags(PyTdbObject
*self
, PyObject
*args
)
346 PyErr_TDB_RAISE_IF_CLOSED(self
);
348 if (!PyArg_ParseTuple(args
, "I", &flags
))
351 tdb_add_flags(self
->ctx
, flags
);
355 static PyObject
*obj_remove_flags(PyTdbObject
*self
, PyObject
*args
)
359 PyErr_TDB_RAISE_IF_CLOSED(self
);
361 if (!PyArg_ParseTuple(args
, "I", &flags
))
364 tdb_remove_flags(self
->ctx
, flags
);
371 PyTdbObject
*iteratee
;
372 } PyTdbIteratorObject
;
374 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
378 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
380 current
= self
->current
;
381 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
382 ret
= PyString_FromTDB_DATA(current
);
386 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
388 Py_DECREF(self
->iteratee
);
392 PyTypeObject PyTdbIterator
= {
393 .tp_name
= "Iterator",
394 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
395 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
396 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
397 .tp_flags
= Py_TPFLAGS_DEFAULT
,
398 .tp_iter
= PyObject_SelfIter
,
401 static PyObject
*tdb_object_iter(PyTdbObject
*self
)
403 PyTdbIteratorObject
*ret
;
405 PyErr_TDB_RAISE_IF_CLOSED(self
);
407 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
410 ret
->current
= tdb_firstkey(self
->ctx
);
411 ret
->iteratee
= self
;
413 return (PyObject
*)ret
;
416 static PyObject
*obj_clear(PyTdbObject
*self
)
419 PyErr_TDB_RAISE_IF_CLOSED(self
);
420 ret
= tdb_wipe_all(self
->ctx
);
421 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
425 static PyObject
*obj_repack(PyTdbObject
*self
)
428 PyErr_TDB_RAISE_IF_CLOSED(self
);
429 ret
= tdb_repack(self
->ctx
);
430 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
434 static PyObject
*obj_enable_seqnum(PyTdbObject
*self
)
436 PyErr_TDB_RAISE_IF_CLOSED(self
);
437 tdb_enable_seqnum(self
->ctx
);
441 static PyObject
*obj_increment_seqnum_nonblock(PyTdbObject
*self
)
443 PyErr_TDB_RAISE_IF_CLOSED(self
);
444 tdb_increment_seqnum_nonblock(self
->ctx
);
448 static PyMethodDef tdb_object_methods
[] = {
449 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
450 "S.transaction_cancel() -> None\n"
451 "Cancel the currently active transaction." },
452 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
453 "S.transaction_commit() -> None\n"
454 "Commit the currently active transaction." },
455 { "transaction_prepare_commit", (PyCFunction
)obj_transaction_prepare_commit
, METH_NOARGS
,
456 "S.transaction_prepare_commit() -> None\n"
457 "Prepare to commit the currently active transaction" },
458 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
459 "S.transaction_start() -> None\n"
460 "Start a new transaction." },
461 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
462 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
463 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
464 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
465 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
466 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
467 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.get(key) -> value\n"
469 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
470 "Append data to an existing key." },
471 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
472 "Return the first key in this database." },
473 { "nextkey", (PyCFunction
)obj_nextkey
, METH_NOARGS
, "S.nextkey(key) -> data\n"
474 "Return the next key in this database." },
475 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
476 "Delete an entry." },
477 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
478 "Check whether key exists in this database." },
479 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
481 { "add_flags", (PyCFunction
)obj_add_flags
, METH_VARARGS
, "S.add_flags(flags) -> None" },
482 { "remove_flags", (PyCFunction
)obj_remove_flags
, METH_VARARGS
, "S.remove_flags(flags) -> None" },
483 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
484 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
485 "Wipe the entire database." },
486 { "repack", (PyCFunction
)obj_repack
, METH_NOARGS
, "S.repack() -> None\n"
487 "Repack the entire database." },
488 { "enable_seqnum", (PyCFunction
)obj_enable_seqnum
, METH_NOARGS
,
489 "S.enable_seqnum() -> None" },
490 { "increment_seqnum_nonblock", (PyCFunction
)obj_increment_seqnum_nonblock
, METH_NOARGS
,
491 "S.increment_seqnum_nonblock() -> None" },
495 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
497 PyErr_TDB_RAISE_IF_CLOSED(self
);
498 return PyInt_FromLong(tdb_hash_size(self
->ctx
));
501 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
503 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
504 if (!PyInt_Check(max_dead
))
506 tdb_set_max_dead(self
->ctx
, PyInt_AsLong(max_dead
));
510 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
512 PyErr_TDB_RAISE_IF_CLOSED(self
);
513 return PyInt_FromLong(tdb_map_size(self
->ctx
));
516 static PyObject
*obj_get_freelist_size(PyTdbObject
*self
, void *closure
)
518 PyErr_TDB_RAISE_IF_CLOSED(self
);
519 return PyInt_FromLong(tdb_freelist_size(self
->ctx
));
522 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
524 PyErr_TDB_RAISE_IF_CLOSED(self
);
525 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
528 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
530 PyErr_TDB_RAISE_IF_CLOSED(self
);
531 return PyString_FromString(tdb_name(self
->ctx
));
534 static PyObject
*obj_get_seqnum(PyTdbObject
*self
, void *closure
)
536 PyErr_TDB_RAISE_IF_CLOSED(self
);
537 return PyInt_FromLong(tdb_get_seqnum(self
->ctx
));
541 static PyGetSetDef tdb_object_getsetters
[] = {
542 { (char *)"hash_size", (getter
)obj_get_hash_size
, NULL
, NULL
},
543 { (char *)"map_size", (getter
)obj_get_map_size
, NULL
, NULL
},
544 { (char *)"freelist_size", (getter
)obj_get_freelist_size
, NULL
, NULL
},
545 { (char *)"flags", (getter
)obj_get_flags
, NULL
, NULL
},
546 { (char *)"max_dead", NULL
, (setter
)obj_set_max_dead
, NULL
},
547 { (char *)"filename", (getter
)obj_get_filename
, NULL
, (char *)"The filename of this TDB file."},
548 { (char *)"seqnum", (getter
)obj_get_seqnum
, NULL
, NULL
},
552 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
554 PyErr_TDB_RAISE_IF_CLOSED(self
);
555 if (tdb_get_flags(self
->ctx
) & TDB_INTERNAL
) {
556 return PyString_FromString("Tdb(<internal>)");
558 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
562 static void tdb_object_dealloc(PyTdbObject
*self
)
565 tdb_close(self
->ctx
);
566 self
->ob_type
->tp_free(self
);
569 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
572 PyErr_TDB_RAISE_IF_CLOSED(self
);
573 if (!PyString_Check(key
)) {
574 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
578 tkey
.dptr
= (unsigned char *)PyString_AsString(key
);
579 tkey
.dsize
= PyString_Size(key
);
581 val
= tdb_fetch(self
->ctx
, tkey
);
582 if (val
.dptr
== NULL
) {
583 PyErr_SetString(PyExc_KeyError
, "No such TDB entry");
586 return PyString_FromTDB_DATA(val
);
590 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
594 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
595 if (!PyString_Check(key
)) {
596 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
600 tkey
= PyString_AsTDB_DATA(key
);
603 ret
= tdb_delete(self
->ctx
, tkey
);
605 if (!PyString_Check(value
)) {
606 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
610 tval
= PyString_AsTDB_DATA(value
);
612 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
616 PyErr_SetTDBError(self
->ctx
);
623 static PyMappingMethods tdb_object_mapping
= {
624 .mp_subscript
= (binaryfunc
)obj_getitem
,
625 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
627 static PyTypeObject PyTdb
= {
628 .tp_name
= "tdb.Tdb",
629 .tp_basicsize
= sizeof(PyTdbObject
),
630 .tp_methods
= tdb_object_methods
,
631 .tp_getset
= tdb_object_getsetters
,
632 .tp_new
= py_tdb_open
,
633 .tp_doc
= "A TDB file",
634 .tp_repr
= (reprfunc
)tdb_object_repr
,
635 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
636 .tp_as_mapping
= &tdb_object_mapping
,
637 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
638 .tp_iter
= (getiterfunc
)tdb_object_iter
,
641 static PyMethodDef tdb_methods
[] = {
642 { "open", (PyCFunction
)py_tdb_open
, METH_VARARGS
|METH_KEYWORDS
, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
643 "Open a TDB file." },
652 if (PyType_Ready(&PyTdb
) < 0)
655 if (PyType_Ready(&PyTdbIterator
) < 0)
658 m
= Py_InitModule3("tdb", tdb_methods
,
659 "simple key-value database that supports multiple writers.");
663 PyModule_AddObject(m
, "REPLACE", PyInt_FromLong(TDB_REPLACE
));
664 PyModule_AddObject(m
, "INSERT", PyInt_FromLong(TDB_INSERT
));
665 PyModule_AddObject(m
, "MODIFY", PyInt_FromLong(TDB_MODIFY
));
667 PyModule_AddObject(m
, "DEFAULT", PyInt_FromLong(TDB_DEFAULT
));
668 PyModule_AddObject(m
, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST
));
669 PyModule_AddObject(m
, "INTERNAL", PyInt_FromLong(TDB_INTERNAL
));
670 PyModule_AddObject(m
, "NOLOCK", PyInt_FromLong(TDB_NOLOCK
));
671 PyModule_AddObject(m
, "NOMMAP", PyInt_FromLong(TDB_NOMMAP
));
672 PyModule_AddObject(m
, "CONVERT", PyInt_FromLong(TDB_CONVERT
));
673 PyModule_AddObject(m
, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN
));
674 PyModule_AddObject(m
, "NOSYNC", PyInt_FromLong(TDB_NOSYNC
));
675 PyModule_AddObject(m
, "SEQNUM", PyInt_FromLong(TDB_SEQNUM
));
676 PyModule_AddObject(m
, "VOLATILE", PyInt_FromLong(TDB_VOLATILE
));
677 PyModule_AddObject(m
, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING
));
678 PyModule_AddObject(m
, "DISALLOW_NESTING", PyInt_FromLong(TDB_DISALLOW_NESTING
));
679 PyModule_AddObject(m
, "INCOMPATIBLE_HASH", PyInt_FromLong(TDB_INCOMPATIBLE_HASH
));
681 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
683 PyModule_AddObject(m
, "__version__", PyString_FromString(PACKAGE_VERSION
));
686 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
688 Py_INCREF(&PyTdbIterator
);