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 #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_recover(PyTdbObject
*self
)
117 int ret
= tdb_transaction_recover(self
->ctx
);
118 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
122 static PyObject
*obj_transaction_start(PyTdbObject
*self
)
124 int ret
= tdb_transaction_start(self
->ctx
);
125 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
129 static PyObject
*obj_reopen(PyTdbObject
*self
)
131 int ret
= tdb_reopen(self
->ctx
);
132 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
136 static PyObject
*obj_lockall(PyTdbObject
*self
)
138 int ret
= tdb_lockall(self
->ctx
);
139 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
143 static PyObject
*obj_unlockall(PyTdbObject
*self
)
145 int ret
= tdb_unlockall(self
->ctx
);
146 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
150 static PyObject
*obj_lockall_read(PyTdbObject
*self
)
152 int ret
= tdb_lockall_read(self
->ctx
);
153 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
157 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
159 int ret
= tdb_unlockall_read(self
->ctx
);
160 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
164 static PyObject
*obj_close(PyTdbObject
*self
)
169 ret
= tdb_close(self
->ctx
);
171 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
175 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
179 if (!PyArg_ParseTuple(args
, "O", &py_key
))
182 key
= PyString_AsTDB_DATA(py_key
);
184 return PyString_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
187 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
190 PyObject
*py_key
, *py_data
;
192 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
195 key
= PyString_AsTDB_DATA(py_key
);
196 data
= PyString_AsTDB_DATA(py_data
);
198 ret
= tdb_append(self
->ctx
, key
, data
);
199 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
203 static PyObject
*obj_firstkey(PyTdbObject
*self
)
205 return PyString_FromTDB_DATA(tdb_firstkey(self
->ctx
));
208 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
212 if (!PyArg_ParseTuple(args
, "O", &py_key
))
215 key
= PyString_AsTDB_DATA(py_key
);
217 return PyString_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
220 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
225 if (!PyArg_ParseTuple(args
, "O", &py_key
))
228 key
= PyString_AsTDB_DATA(py_key
);
229 ret
= tdb_delete(self
->ctx
, key
);
230 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
234 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
239 if (!PyArg_ParseTuple(args
, "O", &py_key
))
242 key
= PyString_AsTDB_DATA(py_key
);
243 ret
= tdb_exists(self
->ctx
, key
);
244 if (ret
!= TDB_ERR_NOEXIST
) {
245 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
248 return (ret
== TDB_ERR_NOEXIST
)?Py_False
:Py_True
;
251 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
255 int flag
= TDB_REPLACE
;
256 PyObject
*py_key
, *py_value
;
258 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
261 key
= PyString_AsTDB_DATA(py_key
);
262 value
= PyString_AsTDB_DATA(py_value
);
264 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
265 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
273 PyTdbObject
*iteratee
;
274 } PyTdbIteratorObject
;
276 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
280 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
282 current
= self
->current
;
283 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
284 ret
= PyString_FromTDB_DATA(current
);
288 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
290 Py_DECREF(self
->iteratee
);
294 PyTypeObject PyTdbIterator
= {
295 .tp_name
= "Iterator",
296 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
297 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
298 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
299 .tp_flags
= Py_TPFLAGS_DEFAULT
,
300 .tp_iter
= PyObject_SelfIter
,
303 static PyObject
*tdb_object_iter(PyTdbObject
*self
)
305 PyTdbIteratorObject
*ret
;
307 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
308 ret
->current
= tdb_firstkey(self
->ctx
);
309 ret
->iteratee
= self
;
311 return (PyObject
*)ret
;
314 static PyObject
*obj_clear(PyTdbObject
*self
)
316 int ret
= tdb_wipe_all(self
->ctx
);
317 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
321 static PyMethodDef tdb_object_methods
[] = {
322 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
323 "S.transaction_cancel() -> None\n"
324 "Cancel the currently active transaction." },
325 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
326 "S.transaction_commit() -> None\n"
327 "Commit the currently active transaction." },
328 { "transaction_recover", (PyCFunction
)obj_transaction_recover
, METH_NOARGS
,
329 "S.transaction_recover() -> None\n"
330 "Recover the currently active transaction." },
331 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
332 "S.transaction_start() -> None\n"
333 "Start a new transaction." },
334 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
335 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
336 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
337 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
338 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
339 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
340 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.fetch(key) -> value\n"
342 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
343 "Append data to an existing key." },
344 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
345 "Return the first key in this database." },
346 { "nextkey", (PyCFunction
)obj_nextkey
, METH_NOARGS
, "S.nextkey(key) -> data\n"
347 "Return the next key in this database." },
348 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
349 "Delete an entry." },
350 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
351 "Check whether key exists in this database." },
352 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
354 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
355 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
356 "Wipe the entire database." },
360 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
362 return PyInt_FromLong(tdb_hash_size(self
->ctx
));
365 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
367 if (!PyInt_Check(max_dead
))
369 tdb_set_max_dead(self
->ctx
, PyInt_AsLong(max_dead
));
373 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
375 return PyInt_FromLong(tdb_map_size(self
->ctx
));
378 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
380 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
383 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
385 return PyString_FromString(tdb_name(self
->ctx
));
388 static PyGetSetDef tdb_object_getsetters
[] = {
389 { (char *)"hash_size", (getter
)obj_get_hash_size
, NULL
, NULL
},
390 { (char *)"map_size", (getter
)obj_get_map_size
, NULL
, NULL
},
391 { (char *)"flags", (getter
)obj_get_flags
, NULL
, NULL
},
392 { (char *)"max_dead", NULL
, (setter
)obj_set_max_dead
, NULL
},
393 { (char *)"filename", (getter
)obj_get_filename
, NULL
, (char *)"The filename of this TDB file."},
397 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
399 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
402 static void tdb_object_dealloc(PyTdbObject
*self
)
405 tdb_close(self
->ctx
);
409 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
412 if (!PyString_Check(key
)) {
413 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
417 tkey
.dptr
= (unsigned char *)PyString_AsString(key
);
418 tkey
.dsize
= PyString_Size(key
);
420 val
= tdb_fetch(self
->ctx
, tkey
);
421 if (val
.dptr
== NULL
) {
422 PyErr_SetString(PyExc_KeyError
, "No such TDB entry");
425 return PyString_FromTDB_DATA(val
);
429 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
433 if (!PyString_Check(key
)) {
434 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
438 tkey
= PyString_AsTDB_DATA(key
);
441 ret
= tdb_delete(self
->ctx
, tkey
);
443 if (!PyString_Check(value
)) {
444 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
448 tval
= PyString_AsTDB_DATA(value
);
450 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
454 PyErr_SetTDBError(self
->ctx
);
461 static PyMappingMethods tdb_object_mapping
= {
462 .mp_subscript
= (binaryfunc
)obj_getitem
,
463 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
465 PyTypeObject PyTdb
= {
467 .tp_basicsize
= sizeof(PyTdbObject
),
468 .tp_methods
= tdb_object_methods
,
469 .tp_getset
= tdb_object_getsetters
,
470 .tp_new
= py_tdb_open
,
471 .tp_doc
= "A TDB file",
472 .tp_repr
= (reprfunc
)tdb_object_repr
,
473 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
474 .tp_as_mapping
= &tdb_object_mapping
,
475 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
476 .tp_iter
= (getiterfunc
)tdb_object_iter
,
479 static PyMethodDef tdb_methods
[] = {
480 { "open", (PyCFunction
)py_tdb_open
, METH_VARARGS
|METH_KEYWORDS
, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
481 "Open a TDB file." },
489 if (PyType_Ready(&PyTdb
) < 0)
492 if (PyType_Ready(&PyTdbIterator
) < 0)
495 m
= Py_InitModule3("tdb", tdb_methods
, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
499 PyModule_AddObject(m
, "REPLACE", PyInt_FromLong(TDB_REPLACE
));
500 PyModule_AddObject(m
, "INSERT", PyInt_FromLong(TDB_INSERT
));
501 PyModule_AddObject(m
, "MODIFY", PyInt_FromLong(TDB_MODIFY
));
503 PyModule_AddObject(m
, "DEFAULT", PyInt_FromLong(TDB_DEFAULT
));
504 PyModule_AddObject(m
, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST
));
505 PyModule_AddObject(m
, "INTERNAL", PyInt_FromLong(TDB_INTERNAL
));
506 PyModule_AddObject(m
, "NOLOCK", PyInt_FromLong(TDB_NOLOCK
));
507 PyModule_AddObject(m
, "NOMMAP", PyInt_FromLong(TDB_NOMMAP
));
508 PyModule_AddObject(m
, "CONVERT", PyInt_FromLong(TDB_CONVERT
));
509 PyModule_AddObject(m
, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN
));
510 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
513 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
515 Py_INCREF(&PyTdbIterator
);