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/>.
28 #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 PyAPI_DATA(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 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
81 int hash_size
= 0, tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
84 const char *kwnames
[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL
};
86 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|iiii", (char **)kwnames
, &name
, &hash_size
, &tdb_flags
, &flags
, &mode
))
89 ctx
= tdb_open(name
, hash_size
, tdb_flags
, flags
, mode
);
91 PyErr_SetFromErrno(PyExc_IOError
);
95 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
98 return (PyObject
*)ret
;
101 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
)
103 int ret
= tdb_transaction_cancel(self
->ctx
);
104 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
108 static PyObject
*obj_transaction_commit(PyTdbObject
*self
)
110 int ret
= tdb_transaction_commit(self
->ctx
);
111 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
115 static PyObject
*obj_transaction_start(PyTdbObject
*self
)
117 int ret
= tdb_transaction_start(self
->ctx
);
118 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
122 static PyObject
*obj_reopen(PyTdbObject
*self
)
124 int ret
= tdb_reopen(self
->ctx
);
125 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
129 static PyObject
*obj_lockall(PyTdbObject
*self
)
131 int ret
= tdb_lockall(self
->ctx
);
132 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
136 static PyObject
*obj_unlockall(PyTdbObject
*self
)
138 int ret
= tdb_unlockall(self
->ctx
);
139 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
143 static PyObject
*obj_lockall_read(PyTdbObject
*self
)
145 int ret
= tdb_lockall_read(self
->ctx
);
146 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
150 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
152 int ret
= tdb_unlockall_read(self
->ctx
);
153 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
157 static PyObject
*obj_close(PyTdbObject
*self
)
162 ret
= tdb_close(self
->ctx
);
164 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
168 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
172 if (!PyArg_ParseTuple(args
, "O", &py_key
))
175 key
= PyString_AsTDB_DATA(py_key
);
177 return PyString_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
180 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
183 PyObject
*py_key
, *py_data
;
185 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
188 key
= PyString_AsTDB_DATA(py_key
);
189 data
= PyString_AsTDB_DATA(py_data
);
191 ret
= tdb_append(self
->ctx
, key
, data
);
192 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
196 static PyObject
*obj_firstkey(PyTdbObject
*self
)
198 return PyString_FromTDB_DATA(tdb_firstkey(self
->ctx
));
201 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
205 if (!PyArg_ParseTuple(args
, "O", &py_key
))
208 key
= PyString_AsTDB_DATA(py_key
);
210 return PyString_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
213 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
218 if (!PyArg_ParseTuple(args
, "O", &py_key
))
221 key
= PyString_AsTDB_DATA(py_key
);
222 ret
= tdb_delete(self
->ctx
, key
);
223 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
227 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
232 if (!PyArg_ParseTuple(args
, "O", &py_key
))
235 key
= PyString_AsTDB_DATA(py_key
);
236 ret
= tdb_exists(self
->ctx
, key
);
237 if (ret
!= TDB_ERR_NOEXIST
) {
238 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
241 return (ret
== TDB_ERR_NOEXIST
)?Py_False
:Py_True
;
244 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
248 int flag
= TDB_REPLACE
;
249 PyObject
*py_key
, *py_value
;
251 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
254 key
= PyString_AsTDB_DATA(py_key
);
255 value
= PyString_AsTDB_DATA(py_value
);
257 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
258 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
266 PyTdbObject
*iteratee
;
267 } PyTdbIteratorObject
;
269 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
273 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
275 current
= self
->current
;
276 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
277 ret
= PyString_FromTDB_DATA(current
);
281 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
283 Py_DECREF(self
->iteratee
);
287 PyTypeObject PyTdbIterator
= {
288 .tp_name
= "Iterator",
289 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
290 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
291 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
292 .tp_flags
= Py_TPFLAGS_DEFAULT
,
293 .tp_iter
= PyObject_SelfIter
,
296 static PyObject
*tdb_object_iter(PyTdbObject
*self
)
298 PyTdbIteratorObject
*ret
;
300 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
301 ret
->current
= tdb_firstkey(self
->ctx
);
302 ret
->iteratee
= self
;
304 return (PyObject
*)ret
;
307 static PyObject
*obj_clear(PyTdbObject
*self
)
309 int ret
= tdb_wipe_all(self
->ctx
);
310 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
314 static PyMethodDef tdb_object_methods
[] = {
315 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
316 "S.transaction_cancel() -> None\n"
317 "Cancel the currently active transaction." },
318 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
319 "S.transaction_commit() -> None\n"
320 "Commit the currently active transaction." },
321 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
322 "S.transaction_start() -> None\n"
323 "Start a new transaction." },
324 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
325 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
326 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
327 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
328 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
329 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
330 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.get(key) -> value\n"
332 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
333 "Append data to an existing key." },
334 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
335 "Return the first key in this database." },
336 { "nextkey", (PyCFunction
)obj_nextkey
, METH_NOARGS
, "S.nextkey(key) -> data\n"
337 "Return the next key in this database." },
338 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
339 "Delete an entry." },
340 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
341 "Check whether key exists in this database." },
342 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
344 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
345 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
346 "Wipe the entire database." },
350 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
352 return PyInt_FromLong(tdb_hash_size(self
->ctx
));
355 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
357 if (!PyInt_Check(max_dead
))
359 tdb_set_max_dead(self
->ctx
, PyInt_AsLong(max_dead
));
363 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
365 return PyInt_FromLong(tdb_map_size(self
->ctx
));
368 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
370 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
373 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
375 return PyString_FromString(tdb_name(self
->ctx
));
378 static PyGetSetDef tdb_object_getsetters
[] = {
379 { (char *)"hash_size", (getter
)obj_get_hash_size
, NULL
, NULL
},
380 { (char *)"map_size", (getter
)obj_get_map_size
, NULL
, NULL
},
381 { (char *)"flags", (getter
)obj_get_flags
, NULL
, NULL
},
382 { (char *)"max_dead", NULL
, (setter
)obj_set_max_dead
, NULL
},
383 { (char *)"filename", (getter
)obj_get_filename
, NULL
, (char *)"The filename of this TDB file."},
387 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
389 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
392 static void tdb_object_dealloc(PyTdbObject
*self
)
395 tdb_close(self
->ctx
);
399 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
402 if (!PyString_Check(key
)) {
403 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
407 tkey
.dptr
= (unsigned char *)PyString_AsString(key
);
408 tkey
.dsize
= PyString_Size(key
);
410 val
= tdb_fetch(self
->ctx
, tkey
);
411 if (val
.dptr
== NULL
) {
412 PyErr_SetString(PyExc_KeyError
, "No such TDB entry");
415 return PyString_FromTDB_DATA(val
);
419 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
423 if (!PyString_Check(key
)) {
424 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
428 tkey
= PyString_AsTDB_DATA(key
);
431 ret
= tdb_delete(self
->ctx
, tkey
);
433 if (!PyString_Check(value
)) {
434 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
438 tval
= PyString_AsTDB_DATA(value
);
440 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
444 PyErr_SetTDBError(self
->ctx
);
451 static PyMappingMethods tdb_object_mapping
= {
452 .mp_subscript
= (binaryfunc
)obj_getitem
,
453 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
455 PyTypeObject PyTdb
= {
457 .tp_basicsize
= sizeof(PyTdbObject
),
458 .tp_methods
= tdb_object_methods
,
459 .tp_getset
= tdb_object_getsetters
,
460 .tp_new
= py_tdb_open
,
461 .tp_doc
= "A TDB file",
462 .tp_repr
= (reprfunc
)tdb_object_repr
,
463 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
464 .tp_as_mapping
= &tdb_object_mapping
,
465 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
466 .tp_iter
= (getiterfunc
)tdb_object_iter
,
469 static PyMethodDef tdb_methods
[] = {
470 { "open", (PyCFunction
)py_tdb_open
, METH_VARARGS
|METH_KEYWORDS
, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
471 "Open a TDB file." },
479 if (PyType_Ready(&PyTdb
) < 0)
482 if (PyType_Ready(&PyTdbIterator
) < 0)
485 m
= Py_InitModule3("tdb", tdb_methods
, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
489 PyModule_AddObject(m
, "REPLACE", PyInt_FromLong(TDB_REPLACE
));
490 PyModule_AddObject(m
, "INSERT", PyInt_FromLong(TDB_INSERT
));
491 PyModule_AddObject(m
, "MODIFY", PyInt_FromLong(TDB_MODIFY
));
493 PyModule_AddObject(m
, "DEFAULT", PyInt_FromLong(TDB_DEFAULT
));
494 PyModule_AddObject(m
, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST
));
495 PyModule_AddObject(m
, "INTERNAL", PyInt_FromLong(TDB_INTERNAL
));
496 PyModule_AddObject(m
, "NOLOCK", PyInt_FromLong(TDB_NOLOCK
));
497 PyModule_AddObject(m
, "NOMMAP", PyInt_FromLong(TDB_NOMMAP
));
498 PyModule_AddObject(m
, "CONVERT", PyInt_FromLong(TDB_CONVERT
));
499 PyModule_AddObject(m
, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN
));
500 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
503 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
505 Py_INCREF(&PyTdbIterator
);