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/>.
32 /* Include tdb headers */
45 PyAPI_DATA(PyTypeObject
) PyTdb
;
47 static void PyErr_SetTDBError(TDB_CONTEXT
*tdb
)
49 PyErr_SetObject(PyExc_RuntimeError
,
50 Py_BuildValue("(i,s)", tdb_error(tdb
), tdb_errorstr(tdb
)));
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 if (data
.dptr
== NULL
&& data
.dsize
== 0) {
66 PyObject
*ret
= PyString_FromStringAndSize((const char *)data
.dptr
,
73 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
75 PyErr_SetTDBError(tdb); \
79 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
82 int hash_size
= 0, tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
85 const char *kwnames
[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL
};
87 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|iiii", (char **)kwnames
, &name
, &hash_size
, &tdb_flags
, &flags
, &mode
))
90 ctx
= tdb_open(name
, hash_size
, tdb_flags
, flags
, mode
);
92 PyErr_SetFromErrno(PyExc_IOError
);
96 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
99 return (PyObject
*)ret
;
102 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
)
104 int ret
= tdb_transaction_cancel(self
->ctx
);
105 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
109 static PyObject
*obj_transaction_commit(PyTdbObject
*self
)
111 int ret
= tdb_transaction_commit(self
->ctx
);
112 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
116 static PyObject
*obj_transaction_recover(PyTdbObject
*self
)
118 int ret
= tdb_transaction_recover(self
->ctx
);
119 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
123 static PyObject
*obj_transaction_start(PyTdbObject
*self
)
125 int ret
= tdb_transaction_start(self
->ctx
);
126 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
130 static PyObject
*obj_reopen(PyTdbObject
*self
)
132 int ret
= tdb_reopen(self
->ctx
);
133 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
137 static PyObject
*obj_lockall(PyTdbObject
*self
)
139 int ret
= tdb_lockall(self
->ctx
);
140 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
144 static PyObject
*obj_unlockall(PyTdbObject
*self
)
146 int ret
= tdb_unlockall(self
->ctx
);
147 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
151 static PyObject
*obj_lockall_read(PyTdbObject
*self
)
153 int ret
= tdb_lockall_read(self
->ctx
);
154 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
158 static PyObject
*obj_unlockall_read(PyTdbObject
*self
)
160 int ret
= tdb_unlockall_read(self
->ctx
);
161 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
165 static PyObject
*obj_close(PyTdbObject
*self
)
170 ret
= tdb_close(self
->ctx
);
172 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
176 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
180 if (!PyArg_ParseTuple(args
, "O", &py_key
))
183 key
= PyString_AsTDB_DATA(py_key
);
185 return PyString_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
188 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
191 PyObject
*py_key
, *py_data
;
193 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
196 key
= PyString_AsTDB_DATA(py_key
);
197 data
= PyString_AsTDB_DATA(py_data
);
199 ret
= tdb_append(self
->ctx
, key
, data
);
200 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
204 static PyObject
*obj_firstkey(PyTdbObject
*self
)
206 return PyString_FromTDB_DATA(tdb_firstkey(self
->ctx
));
209 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
213 if (!PyArg_ParseTuple(args
, "O", &py_key
))
216 key
= PyString_AsTDB_DATA(py_key
);
218 return PyString_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
221 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
226 if (!PyArg_ParseTuple(args
, "O", &py_key
))
229 key
= PyString_AsTDB_DATA(py_key
);
230 ret
= tdb_delete(self
->ctx
, key
);
231 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
235 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
240 if (!PyArg_ParseTuple(args
, "O", &py_key
))
243 key
= PyString_AsTDB_DATA(py_key
);
244 ret
= tdb_exists(self
->ctx
, key
);
245 if (ret
!= TDB_ERR_NOEXIST
) {
246 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
249 return (ret
== TDB_ERR_NOEXIST
)?Py_False
:Py_True
;
252 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
256 int flag
= TDB_REPLACE
;
257 PyObject
*py_key
, *py_value
;
259 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
262 key
= PyString_AsTDB_DATA(py_key
);
263 value
= PyString_AsTDB_DATA(py_value
);
265 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
266 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
274 PyTdbObject
*iteratee
;
275 } PyTdbIteratorObject
;
277 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
281 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
283 current
= self
->current
;
284 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
285 ret
= PyString_FromTDB_DATA(current
);
289 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
291 Py_DECREF(self
->iteratee
);
295 PyTypeObject PyTdbIterator
= {
296 .tp_name
= "Iterator",
297 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
298 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
299 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
300 .tp_flags
= Py_TPFLAGS_DEFAULT
,
301 .tp_iter
= PyObject_SelfIter
,
304 static PyObject
*tdb_object_iter(PyTdbObject
*self
)
306 PyTdbIteratorObject
*ret
;
308 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
309 ret
->current
= tdb_firstkey(self
->ctx
);
310 ret
->iteratee
= self
;
312 return (PyObject
*)ret
;
315 static PyObject
*obj_clear(PyTdbObject
*self
)
317 int ret
= tdb_wipe_all(self
->ctx
);
318 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
322 static PyMethodDef tdb_object_methods
[] = {
323 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
324 "S.transaction_cancel() -> None\n"
325 "Cancel the currently active transaction." },
326 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
327 "S.transaction_commit() -> None\n"
328 "Commit the currently active transaction." },
329 { "transaction_recover", (PyCFunction
)obj_transaction_recover
, METH_NOARGS
,
330 "S.transaction_recover() -> None\n"
331 "Recover the currently active transaction." },
332 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
333 "S.transaction_start() -> None\n"
334 "Start a new transaction." },
335 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
336 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
337 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
338 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
339 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
340 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
341 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.fetch(key) -> value\n"
343 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
344 "Append data to an existing key." },
345 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
346 "Return the first key in this database." },
347 { "nextkey", (PyCFunction
)obj_nextkey
, METH_NOARGS
, "S.nextkey(key) -> data\n"
348 "Return the next key in this database." },
349 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
350 "Delete an entry." },
351 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
352 "Check whether key exists in this database." },
353 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
355 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
356 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
357 "Wipe the entire database." },
361 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
363 return PyInt_FromLong(tdb_hash_size(self
->ctx
));
366 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
368 if (!PyInt_Check(max_dead
))
370 tdb_set_max_dead(self
->ctx
, PyInt_AsLong(max_dead
));
374 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
376 return PyInt_FromLong(tdb_map_size(self
->ctx
));
379 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
381 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
384 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
386 return PyString_FromString(tdb_name(self
->ctx
));
389 static PyGetSetDef tdb_object_getsetters
[] = {
390 { (char *)"hash_size", (getter
)obj_get_hash_size
, NULL
, NULL
},
391 { (char *)"map_size", (getter
)obj_get_map_size
, NULL
, NULL
},
392 { (char *)"flags", (getter
)obj_get_flags
, NULL
, NULL
},
393 { (char *)"max_dead", NULL
, (setter
)obj_set_max_dead
, NULL
},
394 { (char *)"filename", (getter
)obj_get_filename
, NULL
, (char *)"The filename of this TDB file."},
398 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
400 return PyString_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
403 static void tdb_object_dealloc(PyTdbObject
*self
)
406 tdb_close(self
->ctx
);
410 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
413 if (!PyString_Check(key
)) {
414 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
418 tkey
.dptr
= (unsigned char *)PyString_AsString(key
);
419 tkey
.dsize
= PyString_Size(key
);
421 val
= tdb_fetch(self
->ctx
, tkey
);
422 if (val
.dptr
== NULL
) {
423 PyErr_SetString(PyExc_KeyError
, "No such TDB entry");
426 return PyString_FromTDB_DATA(val
);
430 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
434 if (!PyString_Check(key
)) {
435 PyErr_SetString(PyExc_TypeError
, "Expected string as key");
439 tkey
= PyString_AsTDB_DATA(key
);
442 ret
= tdb_delete(self
->ctx
, tkey
);
444 if (!PyString_Check(value
)) {
445 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
449 tval
= PyString_AsTDB_DATA(value
);
451 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
455 PyErr_SetTDBError(self
->ctx
);
462 static PyMappingMethods tdb_object_mapping
= {
463 .mp_subscript
= (binaryfunc
)obj_getitem
,
464 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
466 PyTypeObject PyTdb
= {
468 .tp_basicsize
= sizeof(PyTdbObject
),
469 .tp_methods
= tdb_object_methods
,
470 .tp_getset
= tdb_object_getsetters
,
471 .tp_new
= py_tdb_open
,
472 .tp_doc
= "A TDB file",
473 .tp_repr
= (reprfunc
)tdb_object_repr
,
474 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
475 .tp_as_mapping
= &tdb_object_mapping
,
476 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
477 .tp_iter
= (getiterfunc
)tdb_object_iter
,
480 static PyMethodDef tdb_methods
[] = {
481 { "open", (PyCFunction
)py_tdb_open
, METH_VARARGS
|METH_KEYWORDS
, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
482 "Open a TDB file." },
490 if (PyType_Ready(&PyTdb
) < 0)
493 if (PyType_Ready(&PyTdbIterator
) < 0)
496 m
= Py_InitModule3("tdb", tdb_methods
, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
500 PyModule_AddObject(m
, "REPLACE", PyInt_FromLong(TDB_REPLACE
));
501 PyModule_AddObject(m
, "INSERT", PyInt_FromLong(TDB_INSERT
));
502 PyModule_AddObject(m
, "MODIFY", PyInt_FromLong(TDB_MODIFY
));
504 PyModule_AddObject(m
, "DEFAULT", PyInt_FromLong(TDB_DEFAULT
));
505 PyModule_AddObject(m
, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST
));
506 PyModule_AddObject(m
, "INTERNAL", PyInt_FromLong(TDB_INTERNAL
));
507 PyModule_AddObject(m
, "NOLOCK", PyInt_FromLong(TDB_NOLOCK
));
508 PyModule_AddObject(m
, "NOMMAP", PyInt_FromLong(TDB_NOMMAP
));
509 PyModule_AddObject(m
, "CONVERT", PyInt_FromLong(TDB_CONVERT
));
510 PyModule_AddObject(m
, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN
));
511 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
514 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
516 Py_INCREF(&PyTdbIterator
);