[987] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 |
|
---|
| 4 | Python interface to tdb.
|
---|
| 5 |
|
---|
| 6 | Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
|
---|
| 7 | Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
|
---|
| 8 |
|
---|
| 9 | ** NOTE! The following LGPL license applies to the tdb
|
---|
| 10 | ** library. This does NOT imply that all of Samba is released
|
---|
| 11 | ** under the LGPL
|
---|
| 12 |
|
---|
| 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.
|
---|
| 17 |
|
---|
| 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.
|
---|
| 22 |
|
---|
| 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/>.
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | #include <Python.h>
|
---|
| 28 | #include "replace.h"
|
---|
| 29 | #include "system/filesys.h"
|
---|
| 30 |
|
---|
| 31 | #ifndef Py_RETURN_NONE
|
---|
| 32 | #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | /* Include tdb headers */
|
---|
| 36 | #include <tdb.h>
|
---|
| 37 |
|
---|
| 38 | typedef struct {
|
---|
| 39 | PyObject_HEAD
|
---|
| 40 | TDB_CONTEXT *ctx;
|
---|
| 41 | bool closed;
|
---|
| 42 | } PyTdbObject;
|
---|
| 43 |
|
---|
| 44 | staticforward PyTypeObject PyTdb;
|
---|
| 45 |
|
---|
| 46 | static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
|
---|
| 47 | {
|
---|
| 48 | PyErr_SetObject(PyExc_RuntimeError,
|
---|
| 49 | Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
|
---|
| 53 | {
|
---|
| 54 | TDB_DATA ret;
|
---|
| 55 | ret.dptr = (unsigned char *)PyString_AsString(data);
|
---|
| 56 | ret.dsize = PyString_Size(data);
|
---|
| 57 | return ret;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
|
---|
| 61 | {
|
---|
| 62 | if (data.dptr == NULL && data.dsize == 0) {
|
---|
| 63 | Py_RETURN_NONE;
|
---|
| 64 | } else {
|
---|
| 65 | PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
|
---|
| 66 | data.dsize);
|
---|
| 67 | free(data.dptr);
|
---|
| 68 | return ret;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
|
---|
| 73 | if (ret != 0) { \
|
---|
| 74 | PyErr_SetTDBError(tdb); \
|
---|
| 75 | return NULL; \
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
---|
| 79 | {
|
---|
| 80 | char *name = NULL;
|
---|
| 81 | int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
|
---|
| 82 | TDB_CONTEXT *ctx;
|
---|
| 83 | PyTdbObject *ret;
|
---|
| 84 | const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
|
---|
| 85 |
|
---|
| 86 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
|
---|
| 87 | return NULL;
|
---|
| 88 |
|
---|
| 89 | if (name == NULL) {
|
---|
| 90 | tdb_flags |= TDB_INTERNAL;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
|
---|
| 94 | if (ctx == NULL) {
|
---|
| 95 | PyErr_SetFromErrno(PyExc_IOError);
|
---|
| 96 | return NULL;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | ret = PyObject_New(PyTdbObject, &PyTdb);
|
---|
| 100 | if (!ret) {
|
---|
| 101 | tdb_close(ctx);
|
---|
| 102 | return NULL;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | ret->ctx = ctx;
|
---|
| 106 | ret->closed = false;
|
---|
| 107 | return (PyObject *)ret;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | static PyObject *obj_transaction_cancel(PyTdbObject *self)
|
---|
| 111 | {
|
---|
| 112 | int ret = tdb_transaction_cancel(self->ctx);
|
---|
| 113 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 114 | Py_RETURN_NONE;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | static PyObject *obj_transaction_commit(PyTdbObject *self)
|
---|
| 118 | {
|
---|
| 119 | int ret = tdb_transaction_commit(self->ctx);
|
---|
| 120 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 121 | Py_RETURN_NONE;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static PyObject *obj_transaction_prepare_commit(PyTdbObject *self)
|
---|
| 125 | {
|
---|
| 126 | int ret = tdb_transaction_prepare_commit(self->ctx);
|
---|
| 127 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 128 | Py_RETURN_NONE;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static PyObject *obj_transaction_start(PyTdbObject *self)
|
---|
| 132 | {
|
---|
| 133 | int ret = tdb_transaction_start(self->ctx);
|
---|
| 134 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 135 | Py_RETURN_NONE;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | static PyObject *obj_reopen(PyTdbObject *self)
|
---|
| 139 | {
|
---|
| 140 | int ret = tdb_reopen(self->ctx);
|
---|
| 141 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 142 | Py_RETURN_NONE;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | static PyObject *obj_lockall(PyTdbObject *self)
|
---|
| 146 | {
|
---|
| 147 | int ret = tdb_lockall(self->ctx);
|
---|
| 148 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 149 | Py_RETURN_NONE;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | static PyObject *obj_unlockall(PyTdbObject *self)
|
---|
| 153 | {
|
---|
| 154 | int ret = tdb_unlockall(self->ctx);
|
---|
| 155 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 156 | Py_RETURN_NONE;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | static PyObject *obj_lockall_read(PyTdbObject *self)
|
---|
| 160 | {
|
---|
| 161 | int ret = tdb_lockall_read(self->ctx);
|
---|
| 162 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 163 | Py_RETURN_NONE;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | static PyObject *obj_unlockall_read(PyTdbObject *self)
|
---|
| 167 | {
|
---|
| 168 | int ret = tdb_unlockall_read(self->ctx);
|
---|
| 169 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 170 | Py_RETURN_NONE;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | static PyObject *obj_close(PyTdbObject *self)
|
---|
| 174 | {
|
---|
| 175 | int ret;
|
---|
| 176 | if (self->closed)
|
---|
| 177 | Py_RETURN_NONE;
|
---|
| 178 | ret = tdb_close(self->ctx);
|
---|
| 179 | self->closed = true;
|
---|
| 180 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 181 | Py_RETURN_NONE;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | static PyObject *obj_get(PyTdbObject *self, PyObject *args)
|
---|
| 185 | {
|
---|
| 186 | TDB_DATA key;
|
---|
| 187 | PyObject *py_key;
|
---|
| 188 | if (!PyArg_ParseTuple(args, "O", &py_key))
|
---|
| 189 | return NULL;
|
---|
| 190 |
|
---|
| 191 | key = PyString_AsTDB_DATA(py_key);
|
---|
| 192 |
|
---|
| 193 | return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | static PyObject *obj_append(PyTdbObject *self, PyObject *args)
|
---|
| 197 | {
|
---|
| 198 | TDB_DATA key, data;
|
---|
| 199 | PyObject *py_key, *py_data;
|
---|
| 200 | int ret;
|
---|
| 201 | if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
|
---|
| 202 | return NULL;
|
---|
| 203 |
|
---|
| 204 | key = PyString_AsTDB_DATA(py_key);
|
---|
| 205 | data = PyString_AsTDB_DATA(py_data);
|
---|
| 206 |
|
---|
| 207 | ret = tdb_append(self->ctx, key, data);
|
---|
| 208 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 209 | Py_RETURN_NONE;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | static PyObject *obj_firstkey(PyTdbObject *self)
|
---|
| 213 | {
|
---|
| 214 | return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
|
---|
| 218 | {
|
---|
| 219 | TDB_DATA key;
|
---|
| 220 | PyObject *py_key;
|
---|
| 221 | if (!PyArg_ParseTuple(args, "O", &py_key))
|
---|
| 222 | return NULL;
|
---|
| 223 |
|
---|
| 224 | key = PyString_AsTDB_DATA(py_key);
|
---|
| 225 |
|
---|
| 226 | return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
|
---|
| 230 | {
|
---|
| 231 | TDB_DATA key;
|
---|
| 232 | PyObject *py_key;
|
---|
| 233 | int ret;
|
---|
| 234 | if (!PyArg_ParseTuple(args, "O", &py_key))
|
---|
| 235 | return NULL;
|
---|
| 236 |
|
---|
| 237 | key = PyString_AsTDB_DATA(py_key);
|
---|
| 238 | ret = tdb_delete(self->ctx, key);
|
---|
| 239 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 240 | Py_RETURN_NONE;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
|
---|
| 244 | {
|
---|
| 245 | TDB_DATA key;
|
---|
| 246 | int ret;
|
---|
| 247 | PyObject *py_key;
|
---|
| 248 | if (!PyArg_ParseTuple(args, "O", &py_key))
|
---|
| 249 | return NULL;
|
---|
| 250 |
|
---|
| 251 | key = PyString_AsTDB_DATA(py_key);
|
---|
| 252 | ret = tdb_exists(self->ctx, key);
|
---|
| 253 | if (ret != TDB_ERR_NOEXIST) {
|
---|
| 254 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | static PyObject *obj_store(PyTdbObject *self, PyObject *args)
|
---|
| 261 | {
|
---|
| 262 | TDB_DATA key, value;
|
---|
| 263 | int ret;
|
---|
| 264 | int flag = TDB_REPLACE;
|
---|
| 265 | PyObject *py_key, *py_value;
|
---|
| 266 |
|
---|
| 267 | if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
|
---|
| 268 | return NULL;
|
---|
| 269 |
|
---|
| 270 | key = PyString_AsTDB_DATA(py_key);
|
---|
| 271 | value = PyString_AsTDB_DATA(py_value);
|
---|
| 272 |
|
---|
| 273 | ret = tdb_store(self->ctx, key, value, flag);
|
---|
| 274 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 275 | Py_RETURN_NONE;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | static PyObject *obj_add_flags(PyTdbObject *self, PyObject *args)
|
---|
| 279 | {
|
---|
| 280 | unsigned flags;
|
---|
| 281 |
|
---|
| 282 | if (!PyArg_ParseTuple(args, "I", &flags))
|
---|
| 283 | return NULL;
|
---|
| 284 |
|
---|
| 285 | tdb_add_flags(self->ctx, flags);
|
---|
| 286 | Py_RETURN_NONE;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | static PyObject *obj_remove_flags(PyTdbObject *self, PyObject *args)
|
---|
| 290 | {
|
---|
| 291 | unsigned flags;
|
---|
| 292 |
|
---|
| 293 | if (!PyArg_ParseTuple(args, "I", &flags))
|
---|
| 294 | return NULL;
|
---|
| 295 |
|
---|
| 296 | tdb_remove_flags(self->ctx, flags);
|
---|
| 297 | Py_RETURN_NONE;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | typedef struct {
|
---|
| 301 | PyObject_HEAD
|
---|
| 302 | TDB_DATA current;
|
---|
| 303 | PyTdbObject *iteratee;
|
---|
| 304 | } PyTdbIteratorObject;
|
---|
| 305 |
|
---|
| 306 | static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
|
---|
| 307 | {
|
---|
| 308 | TDB_DATA current;
|
---|
| 309 | PyObject *ret;
|
---|
| 310 | if (self->current.dptr == NULL && self->current.dsize == 0)
|
---|
| 311 | return NULL;
|
---|
| 312 | current = self->current;
|
---|
| 313 | self->current = tdb_nextkey(self->iteratee->ctx, self->current);
|
---|
| 314 | ret = PyString_FromTDB_DATA(current);
|
---|
| 315 | return ret;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | static void tdb_iter_dealloc(PyTdbIteratorObject *self)
|
---|
| 319 | {
|
---|
| 320 | Py_DECREF(self->iteratee);
|
---|
| 321 | PyObject_Del(self);
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | PyTypeObject PyTdbIterator = {
|
---|
| 325 | .tp_name = "Iterator",
|
---|
| 326 | .tp_basicsize = sizeof(PyTdbIteratorObject),
|
---|
| 327 | .tp_iternext = (iternextfunc)tdb_iter_next,
|
---|
| 328 | .tp_dealloc = (destructor)tdb_iter_dealloc,
|
---|
| 329 | .tp_flags = Py_TPFLAGS_DEFAULT,
|
---|
| 330 | .tp_iter = PyObject_SelfIter,
|
---|
| 331 | };
|
---|
| 332 |
|
---|
| 333 | static PyObject *tdb_object_iter(PyTdbObject *self)
|
---|
| 334 | {
|
---|
| 335 | PyTdbIteratorObject *ret;
|
---|
| 336 |
|
---|
| 337 | ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
|
---|
| 338 | if (!ret)
|
---|
| 339 | return NULL;
|
---|
| 340 | ret->current = tdb_firstkey(self->ctx);
|
---|
| 341 | ret->iteratee = self;
|
---|
| 342 | Py_INCREF(self);
|
---|
| 343 | return (PyObject *)ret;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | static PyObject *obj_clear(PyTdbObject *self)
|
---|
| 347 | {
|
---|
| 348 | int ret = tdb_wipe_all(self->ctx);
|
---|
| 349 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 350 | Py_RETURN_NONE;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | static PyObject *obj_repack(PyTdbObject *self)
|
---|
| 354 | {
|
---|
| 355 | int ret = tdb_repack(self->ctx);
|
---|
| 356 | PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
|
---|
| 357 | Py_RETURN_NONE;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | static PyObject *obj_enable_seqnum(PyTdbObject *self)
|
---|
| 361 | {
|
---|
| 362 | tdb_enable_seqnum(self->ctx);
|
---|
| 363 | Py_RETURN_NONE;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | static PyObject *obj_increment_seqnum_nonblock(PyTdbObject *self)
|
---|
| 367 | {
|
---|
| 368 | tdb_increment_seqnum_nonblock(self->ctx);
|
---|
| 369 | Py_RETURN_NONE;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | static PyMethodDef tdb_object_methods[] = {
|
---|
| 373 | { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
|
---|
| 374 | "S.transaction_cancel() -> None\n"
|
---|
| 375 | "Cancel the currently active transaction." },
|
---|
| 376 | { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
|
---|
| 377 | "S.transaction_commit() -> None\n"
|
---|
| 378 | "Commit the currently active transaction." },
|
---|
| 379 | { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
|
---|
| 380 | "S.transaction_prepare_commit() -> None\n"
|
---|
| 381 | "Prepare to commit the currently active transaction" },
|
---|
| 382 | { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
|
---|
| 383 | "S.transaction_start() -> None\n"
|
---|
| 384 | "Start a new transaction." },
|
---|
| 385 | { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
|
---|
| 386 | { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
|
---|
| 387 | { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
|
---|
| 388 | { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
|
---|
| 389 | { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
|
---|
| 390 | { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
|
---|
| 391 | { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
|
---|
| 392 | "Fetch a value." },
|
---|
| 393 | { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
|
---|
| 394 | "Append data to an existing key." },
|
---|
| 395 | { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
|
---|
| 396 | "Return the first key in this database." },
|
---|
| 397 | { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
|
---|
| 398 | "Return the next key in this database." },
|
---|
| 399 | { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
|
---|
| 400 | "Delete an entry." },
|
---|
| 401 | { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
|
---|
| 402 | "Check whether key exists in this database." },
|
---|
| 403 | { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
|
---|
| 404 | "Store data." },
|
---|
| 405 | { "add_flags", (PyCFunction)obj_add_flags, METH_VARARGS, "S.add_flags(flags) -> None" },
|
---|
| 406 | { "remove_flags", (PyCFunction)obj_remove_flags, METH_VARARGS, "S.remove_flags(flags) -> None" },
|
---|
| 407 | { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
|
---|
| 408 | { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
|
---|
| 409 | "Wipe the entire database." },
|
---|
| 410 | { "repack", (PyCFunction)obj_repack, METH_NOARGS, "S.repack() -> None\n"
|
---|
| 411 | "Repack the entire database." },
|
---|
| 412 | { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
|
---|
| 413 | "S.enable_seqnum() -> None" },
|
---|
| 414 | { "increment_seqnum_nonblock", (PyCFunction)obj_increment_seqnum_nonblock, METH_NOARGS,
|
---|
| 415 | "S.increment_seqnum_nonblock() -> None" },
|
---|
| 416 | { NULL }
|
---|
| 417 | };
|
---|
| 418 |
|
---|
| 419 | static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
|
---|
| 420 | {
|
---|
| 421 | return PyInt_FromLong(tdb_hash_size(self->ctx));
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
|
---|
| 425 | {
|
---|
| 426 | if (!PyInt_Check(max_dead))
|
---|
| 427 | return -1;
|
---|
| 428 | tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
|
---|
| 429 | return 0;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
|
---|
| 433 | {
|
---|
| 434 | return PyInt_FromLong(tdb_map_size(self->ctx));
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | static PyObject *obj_get_freelist_size(PyTdbObject *self, void *closure)
|
---|
| 438 | {
|
---|
| 439 | return PyInt_FromLong(tdb_freelist_size(self->ctx));
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
|
---|
| 443 | {
|
---|
| 444 | return PyInt_FromLong(tdb_get_flags(self->ctx));
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
|
---|
| 448 | {
|
---|
| 449 | return PyString_FromString(tdb_name(self->ctx));
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | static PyObject *obj_get_seqnum(PyTdbObject *self, void *closure)
|
---|
| 453 | {
|
---|
| 454 | return PyInt_FromLong(tdb_get_seqnum(self->ctx));
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 |
|
---|
| 458 | static PyGetSetDef tdb_object_getsetters[] = {
|
---|
| 459 | { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
|
---|
| 460 | { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
|
---|
| 461 | { (char *)"freelist_size", (getter)obj_get_freelist_size, NULL, NULL },
|
---|
| 462 | { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
|
---|
| 463 | { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
|
---|
| 464 | { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
|
---|
| 465 | { (char *)"seqnum", (getter)obj_get_seqnum, NULL, NULL },
|
---|
| 466 | { NULL }
|
---|
| 467 | };
|
---|
| 468 |
|
---|
| 469 | static PyObject *tdb_object_repr(PyTdbObject *self)
|
---|
| 470 | {
|
---|
| 471 | if (tdb_get_flags(self->ctx) & TDB_INTERNAL) {
|
---|
| 472 | return PyString_FromString("Tdb(<internal>)");
|
---|
| 473 | } else {
|
---|
| 474 | return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
|
---|
| 475 | }
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | static void tdb_object_dealloc(PyTdbObject *self)
|
---|
| 479 | {
|
---|
| 480 | if (!self->closed)
|
---|
| 481 | tdb_close(self->ctx);
|
---|
| 482 | self->ob_type->tp_free(self);
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
|
---|
| 486 | {
|
---|
| 487 | TDB_DATA tkey, val;
|
---|
| 488 | if (!PyString_Check(key)) {
|
---|
| 489 | PyErr_SetString(PyExc_TypeError, "Expected string as key");
|
---|
| 490 | return NULL;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | tkey.dptr = (unsigned char *)PyString_AsString(key);
|
---|
| 494 | tkey.dsize = PyString_Size(key);
|
---|
| 495 |
|
---|
| 496 | val = tdb_fetch(self->ctx, tkey);
|
---|
| 497 | if (val.dptr == NULL) {
|
---|
| 498 | PyErr_SetString(PyExc_KeyError, "No such TDB entry");
|
---|
| 499 | return NULL;
|
---|
| 500 | } else {
|
---|
| 501 | return PyString_FromTDB_DATA(val);
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
|
---|
| 506 | {
|
---|
| 507 | TDB_DATA tkey, tval;
|
---|
| 508 | int ret;
|
---|
| 509 | if (!PyString_Check(key)) {
|
---|
| 510 | PyErr_SetString(PyExc_TypeError, "Expected string as key");
|
---|
| 511 | return -1;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | tkey = PyString_AsTDB_DATA(key);
|
---|
| 515 |
|
---|
| 516 | if (value == NULL) {
|
---|
| 517 | ret = tdb_delete(self->ctx, tkey);
|
---|
| 518 | } else {
|
---|
| 519 | if (!PyString_Check(value)) {
|
---|
| 520 | PyErr_SetString(PyExc_TypeError, "Expected string as value");
|
---|
| 521 | return -1;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | tval = PyString_AsTDB_DATA(value);
|
---|
| 525 |
|
---|
| 526 | ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | if (ret != 0) {
|
---|
| 530 | PyErr_SetTDBError(self->ctx);
|
---|
| 531 | return -1;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | return ret;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | static PyMappingMethods tdb_object_mapping = {
|
---|
| 538 | .mp_subscript = (binaryfunc)obj_getitem,
|
---|
| 539 | .mp_ass_subscript = (objobjargproc)obj_setitem,
|
---|
| 540 | };
|
---|
| 541 | static PyTypeObject PyTdb = {
|
---|
| 542 | .tp_name = "Tdb",
|
---|
| 543 | .tp_basicsize = sizeof(PyTdbObject),
|
---|
| 544 | .tp_methods = tdb_object_methods,
|
---|
| 545 | .tp_getset = tdb_object_getsetters,
|
---|
| 546 | .tp_new = py_tdb_open,
|
---|
| 547 | .tp_doc = "A TDB file",
|
---|
| 548 | .tp_repr = (reprfunc)tdb_object_repr,
|
---|
| 549 | .tp_dealloc = (destructor)tdb_object_dealloc,
|
---|
| 550 | .tp_as_mapping = &tdb_object_mapping,
|
---|
| 551 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
|
---|
| 552 | .tp_iter = (getiterfunc)tdb_object_iter,
|
---|
| 553 | };
|
---|
| 554 |
|
---|
| 555 | static PyMethodDef tdb_methods[] = {
|
---|
| 556 | { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
|
---|
| 557 | "Open a TDB file." },
|
---|
| 558 | { NULL }
|
---|
| 559 | };
|
---|
| 560 |
|
---|
| 561 | void inittdb(void);
|
---|
| 562 | void inittdb(void)
|
---|
| 563 | {
|
---|
| 564 | PyObject *m;
|
---|
| 565 |
|
---|
| 566 | if (PyType_Ready(&PyTdb) < 0)
|
---|
| 567 | return;
|
---|
| 568 |
|
---|
| 569 | if (PyType_Ready(&PyTdbIterator) < 0)
|
---|
| 570 | return;
|
---|
| 571 |
|
---|
| 572 | m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
|
---|
| 573 | if (m == NULL)
|
---|
| 574 | return;
|
---|
| 575 |
|
---|
| 576 | PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
|
---|
| 577 | PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
|
---|
| 578 | PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
|
---|
| 579 |
|
---|
| 580 | PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
|
---|
| 581 | PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
|
---|
| 582 | PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
|
---|
| 583 | PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
|
---|
| 584 | PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
|
---|
| 585 | PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
|
---|
| 586 | PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
|
---|
| 587 | PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(TDB_NOSYNC));
|
---|
| 588 | PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(TDB_SEQNUM));
|
---|
| 589 | PyModule_AddObject(m, "VOLATILE", PyInt_FromLong(TDB_VOLATILE));
|
---|
| 590 | PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING));
|
---|
| 591 | PyModule_AddObject(m, "DISALLOW_NESTING", PyInt_FromLong(TDB_DISALLOW_NESTING));
|
---|
| 592 | PyModule_AddObject(m, "INCOMPATIBLE_HASH", PyInt_FromLong(TDB_INCOMPATIBLE_HASH));
|
---|
| 593 |
|
---|
| 594 | PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
|
---|
| 595 |
|
---|
| 596 | PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
|
---|
| 597 |
|
---|
| 598 | Py_INCREF(&PyTdb);
|
---|
| 599 | PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
|
---|
| 600 |
|
---|
| 601 | Py_INCREF(&PyTdbIterator);
|
---|
| 602 | }
|
---|