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
,
81 fprintf(stderr
, "%s:%s:%s\n",
82 tdb_name(tdb
), tdb_errorstr(ecode
), message
);
85 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
88 int tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
89 struct tdb_context
*ctx
;
91 union tdb_attribute logattr
;
92 const char *kwnames
[] = { "name", "tdb_flags", "flags", "mode", NULL
};
94 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siii", (char **)kwnames
, &name
, &tdb_flags
, &flags
, &mode
))
98 tdb_flags
|= TDB_INTERNAL
;
101 logattr
.log
.base
.attr
= TDB_ATTRIBUTE_LOG
;
102 logattr
.log
.base
.next
= NULL
;
103 logattr
.log
.fn
= stderr_log
;
104 ctx
= tdb_open(name
, tdb_flags
, flags
, mode
, &logattr
);
106 PyErr_SetFromErrno(PyExc_IOError
);
110 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
118 return (PyObject
*)ret
;
121 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
)
123 tdb_transaction_cancel(self
->ctx
);
127 static PyObject
*obj_transaction_commit(PyTdbObject
*self
)
129 enum TDB_ERROR ret
= tdb_transaction_commit(self
->ctx
);
130 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
134 static PyObject
*obj_transaction_prepare_commit(PyTdbObject
*self
)
136 enum TDB_ERROR ret
= tdb_transaction_prepare_commit(self
->ctx
);
137 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
141 static PyObject
*obj_transaction_start(PyTdbObject
*self
)
143 enum TDB_ERROR ret
= tdb_transaction_start(self
->ctx
);
144 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
148 static PyObject
*obj_lockall(PyTdbObject
*self
)
150 enum TDB_ERROR ret
= tdb_lockall(self
->ctx
);
151 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
155 static PyObject
*obj_unlockall(PyTdbObject
*self
)
157 tdb_unlockall(self
->ctx
);
161 static PyObject
*obj_lockall_read(PyTdbObject
*self
)
163 enum TDB_ERROR ret
= tdb_lockall_read(self
->ctx
);
164 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
168 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
170 tdb_unlockall_read(self
->ctx
);
174 static PyObject
*obj_close(PyTdbObject
*self
)
179 ret
= tdb_close(self
->ctx
);
182 PyErr_SetTDBError(TDB_ERR_IO
);
188 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
193 if (!PyArg_ParseTuple(args
, "O", &py_key
))
196 key
= PyString_AsTDB_DATA(py_key
);
197 ret
= tdb_fetch(self
->ctx
, key
, &data
);
198 if (ret
== TDB_ERR_NOEXIST
)
200 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
201 return PyString_FromTDB_DATA(data
);
204 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
207 PyObject
*py_key
, *py_data
;
209 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
212 key
= PyString_AsTDB_DATA(py_key
);
213 data
= PyString_AsTDB_DATA(py_data
);
215 ret
= tdb_append(self
->ctx
, key
, data
);
216 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
220 static PyObject
*obj_firstkey(PyTdbObject
*self
)
225 ret
= tdb_firstkey(self
->ctx
, &key
);
226 if (ret
== TDB_ERR_NOEXIST
)
228 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
230 return PyString_FromTDB_DATA(key
);
233 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
238 if (!PyArg_ParseTuple(args
, "O", &py_key
))
241 /* Malloc here, since tdb_nextkey frees. */
242 key
.dsize
= PyString_Size(py_key
);
243 key
.dptr
= malloc(key
.dsize
);
244 memcpy(key
.dptr
, PyString_AsString(py_key
), key
.dsize
);
246 ret
= tdb_nextkey(self
->ctx
, &key
);
247 if (ret
== TDB_ERR_NOEXIST
)
249 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
251 return PyString_FromTDB_DATA(key
);
254 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
259 if (!PyArg_ParseTuple(args
, "O", &py_key
))
262 key
= PyString_AsTDB_DATA(py_key
);
263 ret
= tdb_delete(self
->ctx
, key
);
264 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
268 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
272 if (!PyArg_ParseTuple(args
, "O", &py_key
))
275 key
= PyString_AsTDB_DATA(py_key
);
276 if (tdb_exists(self
->ctx
, key
))
278 if (tdb_error(self
->ctx
) != TDB_ERR_NOEXIST
)
279 PyErr_TDB_ERROR_IS_ERR_RAISE(tdb_error(self
->ctx
));
283 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
287 int flag
= TDB_REPLACE
;
288 PyObject
*py_key
, *py_value
;
290 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
293 key
= PyString_AsTDB_DATA(py_key
);
294 value
= PyString_AsTDB_DATA(py_value
);
296 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
297 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
301 static PyObject
*obj_add_flag(PyTdbObject
*self
, PyObject
*args
)
305 if (!PyArg_ParseTuple(args
, "I", &flag
))
308 tdb_add_flag(self
->ctx
, flag
);
312 static PyObject
*obj_remove_flag(PyTdbObject
*self
, PyObject
*args
)
316 if (!PyArg_ParseTuple(args
, "I", &flag
))
319 tdb_remove_flag(self
->ctx
, flag
);
327 PyTdbObject
*iteratee
;
328 } PyTdbIteratorObject
;
330 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
336 ret
= PyString_FromStringAndSize((const char *)self
->current
.dptr
,
337 self
->current
.dsize
);
338 e
= tdb_nextkey(self
->iteratee
->ctx
, &self
->current
);
339 if (e
== TDB_ERR_NOEXIST
)
342 PyErr_TDB_ERROR_IS_ERR_RAISE(e
);
346 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
348 Py_DECREF(self
->iteratee
);
352 PyTypeObject PyTdbIterator
= {
353 .tp_name
= "Iterator",
354 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
355 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
356 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
357 .tp_flags
= Py_TPFLAGS_DEFAULT
,
358 .tp_iter
= PyObject_SelfIter
,
361 static PyObject
*tdb_object_iter(PyTdbObject
*self
)
363 PyTdbIteratorObject
*ret
;
366 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
369 e
= tdb_firstkey(self
->ctx
, &ret
->current
);
370 if (e
== TDB_ERR_NOEXIST
) {
373 PyErr_TDB_ERROR_IS_ERR_RAISE(e
);
376 ret
->iteratee
= self
;
378 return (PyObject
*)ret
;
381 static PyObject
*obj_clear(PyTdbObject
*self
)
383 enum TDB_ERROR ret
= tdb_wipe_all(self
->ctx
);
384 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
388 static PyObject
*obj_enable_seqnum(PyTdbObject
*self
)
390 tdb_add_flag(self
->ctx
, TDB_SEQNUM
);
394 static PyMethodDef tdb_object_methods
[] = {
395 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
396 "S.transaction_cancel() -> None\n"
397 "Cancel the currently active transaction." },
398 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
399 "S.transaction_commit() -> None\n"
400 "Commit the currently active transaction." },
401 { "transaction_prepare_commit", (PyCFunction
)obj_transaction_prepare_commit
, METH_NOARGS
,
402 "S.transaction_prepare_commit() -> None\n"
403 "Prepare to commit the currently active transaction" },
404 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
405 "S.transaction_start() -> None\n"
406 "Start a new transaction." },
407 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
408 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
409 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
410 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
411 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
412 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.get(key) -> value\n"
414 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
415 "Append data to an existing key." },
416 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
417 "Return the first key in this database." },
418 { "nextkey", (PyCFunction
)obj_nextkey
, METH_NOARGS
, "S.nextkey(key) -> data\n"
419 "Return the next key in this database." },
420 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
421 "Delete an entry." },
422 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
423 "Check whether key exists in this database." },
424 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
426 { "add_flag", (PyCFunction
)obj_add_flag
, METH_VARARGS
, "S.add_flag(flag) -> None" },
427 { "remove_flag", (PyCFunction
)obj_remove_flag
, METH_VARARGS
, "S.remove_flag(flag) -> None" },
428 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
429 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
430 "Wipe the entire database." },
431 { "enable_seqnum", (PyCFunction
)obj_enable_seqnum
, METH_NOARGS
,
432 "S.enable_seqnum() -> None" },
436 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
438 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
441 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
443 return PyString_FromString(tdb_name(self
->ctx
));
446 static PyObject
*obj_get_seqnum(PyTdbObject
*self
, void *closure
)
448 return PyInt_FromLong(tdb_get_seqnum(self
->ctx
));
452 static PyGetSetDef tdb_object_getsetters
[] = {
453 { (char *)"flags", (getter
)obj_get_flags
, NULL
, NULL
},
454 { (char *)"filename", (getter
)obj_get_filename
, NULL
, (char *)"The filename of this TDB file."},
455 { (char *)"seqnum", (getter
)obj_get_seqnum
, NULL
, NULL
},
459 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
461 if (tdb_get_flags(self
->ctx
) & TDB_INTERNAL
) {
462 return PyString_FromString("Tdb(<internal>)");
464 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
468 static void tdb_object_dealloc(PyTdbObject
*self
)
471 tdb_close(self
->ctx
);
472 self
->ob_type
->tp_free(self
);
475 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
480 if (!PyString_Check(key
)) {
481 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
485 tkey
.dptr
= (unsigned char *)PyString_AsString(key
);
486 tkey
.dsize
= PyString_Size(key
);
488 ret
= tdb_fetch(self
->ctx
, tkey
, &val
);
489 if (ret
== TDB_ERR_NOEXIST
) {
490 PyErr_SetString(PyExc_KeyError
, "No such TDB entry");
493 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
);
494 return PyString_FromTDB_DATA(val
);
498 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
502 if (!PyString_Check(key
)) {
503 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
507 tkey
= PyString_AsTDB_DATA(key
);
510 ret
= tdb_delete(self
->ctx
, tkey
);
512 if (!PyString_Check(value
)) {
513 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
517 tval
= PyString_AsTDB_DATA(value
);
519 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
522 if (ret
!= TDB_SUCCESS
) {
523 PyErr_SetTDBError(ret
);
530 static PyMappingMethods tdb_object_mapping
= {
531 .mp_subscript
= (binaryfunc
)obj_getitem
,
532 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
534 static PyTypeObject PyTdb
= {
535 .tp_name
= "tdb.Tdb",
536 .tp_basicsize
= sizeof(PyTdbObject
),
537 .tp_methods
= tdb_object_methods
,
538 .tp_getset
= tdb_object_getsetters
,
539 .tp_new
= py_tdb_open
,
540 .tp_doc
= "A TDB file",
541 .tp_repr
= (reprfunc
)tdb_object_repr
,
542 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
543 .tp_as_mapping
= &tdb_object_mapping
,
544 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
545 .tp_iter
= (getiterfunc
)tdb_object_iter
,
548 static PyMethodDef tdb_methods
[] = {
549 { "open", (PyCFunction
)py_tdb_open
, METH_VARARGS
|METH_KEYWORDS
, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
550 "Open a TDB file." },
559 if (PyType_Ready(&PyTdb
) < 0)
562 if (PyType_Ready(&PyTdbIterator
) < 0)
565 m
= Py_InitModule3("tdb", tdb_methods
, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
569 PyModule_AddObject(m
, "REPLACE", PyInt_FromLong(TDB_REPLACE
));
570 PyModule_AddObject(m
, "INSERT", PyInt_FromLong(TDB_INSERT
));
571 PyModule_AddObject(m
, "MODIFY", PyInt_FromLong(TDB_MODIFY
));
573 PyModule_AddObject(m
, "DEFAULT", PyInt_FromLong(TDB_DEFAULT
));
574 PyModule_AddObject(m
, "INTERNAL", PyInt_FromLong(TDB_INTERNAL
));
575 PyModule_AddObject(m
, "NOLOCK", PyInt_FromLong(TDB_NOLOCK
));
576 PyModule_AddObject(m
, "NOMMAP", PyInt_FromLong(TDB_NOMMAP
));
577 PyModule_AddObject(m
, "CONVERT", PyInt_FromLong(TDB_CONVERT
));
578 PyModule_AddObject(m
, "NOSYNC", PyInt_FromLong(TDB_NOSYNC
));
579 PyModule_AddObject(m
, "SEQNUM", PyInt_FromLong(TDB_SEQNUM
));
580 PyModule_AddObject(m
, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING
));
582 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
584 PyModule_AddObject(m
, "__version__", PyString_FromString(PACKAGE_VERSION
));
587 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
589 Py_INCREF(&PyTdbIterator
);