2 Unix SMB/CIFS implementation.
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 #ifndef Py_RETURN_NONE
29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
36 /* Include tdb headers */
49 PyAPI_DATA(PyTypeObject
) PyTdb
;
51 static void PyErr_SetTDBError(TDB_CONTEXT
*tdb
)
53 PyErr_SetObject(PyExc_RuntimeError
,
54 Py_BuildValue("(i,s)", tdb_error(tdb
), tdb_errorstr(tdb
)));
57 static TDB_DATA
PyString_AsTDB_DATA(PyObject
*data
)
60 ret
.dptr
= (unsigned char *)PyString_AsString(data
);
61 ret
.dsize
= PyString_Size(data
);
65 static PyObject
*PyString_FromTDB_DATA(TDB_DATA data
)
67 if (data
.dptr
== NULL
&& data
.dsize
== 0) {
70 PyObject
*ret
= PyString_FromStringAndSize((const char *)data
.dptr
,
77 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
79 PyErr_SetTDBError(tdb); \
83 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
86 int hash_size
= 0, tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
89 const char *kwnames
[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL
};
91 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|iiii", (char **)kwnames
, &name
, &hash_size
, &tdb_flags
, &flags
, &mode
))
94 ctx
= tdb_open(name
, hash_size
, tdb_flags
, flags
, mode
);
96 PyErr_SetFromErrno(PyExc_IOError
);
100 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
103 return (PyObject
*)ret
;
106 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
)
108 int ret
= tdb_transaction_cancel(self
->ctx
);
109 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
113 static PyObject
*obj_transaction_commit(PyTdbObject
*self
)
115 int ret
= tdb_transaction_commit(self
->ctx
);
116 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
120 static PyObject
*obj_transaction_recover(PyTdbObject
*self
)
122 int ret
= tdb_transaction_recover(self
->ctx
);
123 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
127 static PyObject
*obj_transaction_start(PyTdbObject
*self
)
129 int ret
= tdb_transaction_start(self
->ctx
);
130 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
134 static PyObject
*obj_reopen(PyTdbObject
*self
)
136 int ret
= tdb_reopen(self
->ctx
);
137 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
141 static PyObject
*obj_lockall(PyTdbObject
*self
)
143 int ret
= tdb_lockall(self
->ctx
);
144 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
148 static PyObject
*obj_unlockall(PyTdbObject
*self
)
150 int ret
= tdb_unlockall(self
->ctx
);
151 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
155 static PyObject
*obj_lockall_read(PyTdbObject
*self
)
157 int ret
= tdb_lockall_read(self
->ctx
);
158 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
162 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
164 int ret
= tdb_unlockall_read(self
->ctx
);
165 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
169 static PyObject
*obj_close(PyTdbObject
*self
)
174 ret
= tdb_close(self
->ctx
);
176 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
180 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
184 if (!PyArg_ParseTuple(args
, "O", &py_key
))
187 key
= PyString_AsTDB_DATA(py_key
);
189 return PyString_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
192 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
195 PyObject
*py_key
, *py_data
;
197 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
200 key
= PyString_AsTDB_DATA(py_key
);
201 data
= PyString_AsTDB_DATA(py_data
);
203 ret
= tdb_append(self
->ctx
, key
, data
);
204 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
208 static PyObject
*obj_firstkey(PyTdbObject
*self
)
210 return PyString_FromTDB_DATA(tdb_firstkey(self
->ctx
));
213 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
217 if (!PyArg_ParseTuple(args
, "O", &py_key
))
220 key
= PyString_AsTDB_DATA(py_key
);
222 return PyString_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
225 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
230 if (!PyArg_ParseTuple(args
, "O", &py_key
))
233 key
= PyString_AsTDB_DATA(py_key
);
234 ret
= tdb_delete(self
->ctx
, key
);
235 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
239 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
244 if (!PyArg_ParseTuple(args
, "O", &py_key
))
247 key
= PyString_AsTDB_DATA(py_key
);
248 ret
= tdb_exists(self
->ctx
, key
);
249 if (ret
!= TDB_ERR_NOEXIST
) {
250 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
253 return (ret
== TDB_ERR_NOEXIST
)?Py_False
:Py_True
;
256 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
260 int flag
= TDB_REPLACE
;
261 PyObject
*py_key
, *py_value
;
263 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
266 key
= PyString_AsTDB_DATA(py_key
);
267 value
= PyString_AsTDB_DATA(py_value
);
269 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
270 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
278 PyTdbObject
*iteratee
;
279 } PyTdbIteratorObject
;
281 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
285 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
287 current
= self
->current
;
288 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
289 ret
= PyString_FromTDB_DATA(current
);
293 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
295 Py_DECREF(self
->iteratee
);
299 PyTypeObject PyTdbIterator
= {
300 .tp_name
= "Iterator",
301 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
302 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
303 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
304 .tp_flags
= Py_TPFLAGS_DEFAULT
,
305 .tp_iter
= PyObject_SelfIter
,
308 static PyObject
*tdb_object_iter(PyTdbObject
*self
)
310 PyTdbIteratorObject
*ret
;
312 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
313 ret
->current
= tdb_firstkey(self
->ctx
);
314 ret
->iteratee
= self
;
316 return (PyObject
*)ret
;
319 static PyObject
*obj_clear(PyTdbObject
*self
)
321 int ret
= tdb_wipe_all(self
->ctx
);
322 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
326 static PyMethodDef tdb_object_methods
[] = {
327 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
328 "S.transaction_cancel() -> None\n"
329 "Cancel the currently active transaction." },
330 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
331 "S.transaction_commit() -> None\n"
332 "Commit the currently active transaction." },
333 { "transaction_recover", (PyCFunction
)obj_transaction_recover
, METH_NOARGS
,
334 "S.transaction_recover() -> None\n"
335 "Recover the currently active transaction." },
336 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
337 "S.transaction_start() -> None\n"
338 "Start a new transaction." },
339 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
340 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
341 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
342 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
343 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
344 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
345 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.fetch(key) -> value\n"
347 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
348 "Append data to an existing key." },
349 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
350 "Return the first key in this database." },
351 { "nextkey", (PyCFunction
)obj_nextkey
, METH_NOARGS
, "S.nextkey(key) -> data\n"
352 "Return the next key in this database." },
353 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
354 "Delete an entry." },
355 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
356 "Check whether key exists in this database." },
357 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
359 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
360 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
361 "Wipe the entire database." },
365 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
367 return PyInt_FromLong(tdb_hash_size(self
->ctx
));
370 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
372 if (!PyInt_Check(max_dead
))
374 tdb_set_max_dead(self
->ctx
, PyInt_AsLong(max_dead
));
378 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
380 return PyInt_FromLong(tdb_map_size(self
->ctx
));
383 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
385 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
388 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
390 return PyString_FromString(tdb_name(self
->ctx
));
393 static PyGetSetDef tdb_object_getsetters
[] = {
394 { (char *)"hash_size", (getter
)obj_get_hash_size
, NULL
, NULL
},
395 { (char *)"map_size", (getter
)obj_get_map_size
, NULL
, NULL
},
396 { (char *)"flags", (getter
)obj_get_flags
, NULL
, NULL
},
397 { (char *)"max_dead", NULL
, (setter
)obj_set_max_dead
, NULL
},
398 { (char *)"filename", (getter
)obj_get_filename
, NULL
, (char *)"The filename of this TDB file."},
402 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
404 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
407 static void tdb_object_dealloc(PyTdbObject
*self
)
410 tdb_close(self
->ctx
);
414 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
417 if (!PyString_Check(key
)) {
418 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
422 tkey
.dptr
= (unsigned char *)PyString_AsString(key
);
423 tkey
.dsize
= PyString_Size(key
);
425 val
= tdb_fetch(self
->ctx
, tkey
);
426 if (val
.dptr
== NULL
) {
427 PyErr_SetString(PyExc_KeyError
, "No such TDB entry");
430 return PyString_FromTDB_DATA(val
);
434 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
438 if (!PyString_Check(key
)) {
439 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
443 tkey
= PyString_AsTDB_DATA(key
);
446 ret
= tdb_delete(self
->ctx
, tkey
);
448 if (!PyString_Check(value
)) {
449 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
453 tval
= PyString_AsTDB_DATA(value
);
455 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
459 PyErr_SetTDBError(self
->ctx
);
466 static PyMappingMethods tdb_object_mapping
= {
467 .mp_subscript
= (binaryfunc
)obj_getitem
,
468 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
470 PyTypeObject PyTdb
= {
472 .tp_basicsize
= sizeof(PyTdbObject
),
473 .tp_methods
= tdb_object_methods
,
474 .tp_getset
= tdb_object_getsetters
,
475 .tp_new
= py_tdb_open
,
476 .tp_doc
= "A TDB file",
477 .tp_repr
= (reprfunc
)tdb_object_repr
,
478 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
479 .tp_as_mapping
= &tdb_object_mapping
,
480 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
481 .tp_iter
= (getiterfunc
)tdb_object_iter
,
484 static PyMethodDef tdb_methods
[] = {
485 { "open", (PyCFunction
)py_tdb_open
, METH_VARARGS
|METH_KEYWORDS
, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
486 "Open a TDB file." },
494 if (PyType_Ready(&PyTdb
) < 0)
497 if (PyType_Ready(&PyTdbIterator
) < 0)
500 m
= Py_InitModule3("tdb", tdb_methods
, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
504 PyModule_AddObject(m
, "REPLACE", PyInt_FromLong(TDB_REPLACE
));
505 PyModule_AddObject(m
, "INSERT", PyInt_FromLong(TDB_INSERT
));
506 PyModule_AddObject(m
, "MODIFY", PyInt_FromLong(TDB_MODIFY
));
508 PyModule_AddObject(m
, "DEFAULT", PyInt_FromLong(TDB_DEFAULT
));
509 PyModule_AddObject(m
, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST
));
510 PyModule_AddObject(m
, "INTERNAL", PyInt_FromLong(TDB_INTERNAL
));
511 PyModule_AddObject(m
, "NOLOCK", PyInt_FromLong(TDB_NOLOCK
));
512 PyModule_AddObject(m
, "NOMMAP", PyInt_FromLong(TDB_NOMMAP
));
513 PyModule_AddObject(m
, "CONVERT", PyInt_FromLong(TDB_CONVERT
));
514 PyModule_AddObject(m
, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN
));
515 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
518 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
520 Py_INCREF(&PyTdbIterator
);