pyntdb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. Unix SMB/CIFS implementation.
  3. Python interface to ntdb. Simply modified from tdb version.
  4. Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
  5. Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
  6. Copyright (C) 2011 Rusty Russell <rusty@rustcorp.com.au>
  7. ** NOTE! The following LGPL license applies to the ntdb
  8. ** library. This does NOT imply that all of Samba is released
  9. ** under the LGPL
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 3 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <Python.h>
  22. #include "replace.h"
  23. #include "system/filesys.h"
  24. /* Include ntdb headers */
  25. #include <ntdb.h>
  26. typedef struct {
  27. PyObject_HEAD
  28. struct ntdb_context *ctx;
  29. bool closed;
  30. } PyNtdbObject;
  31. static PyTypeObject PyNtdb;
  32. static void PyErr_SetTDBError(enum NTDB_ERROR e)
  33. {
  34. PyErr_SetObject(PyExc_RuntimeError,
  35. Py_BuildValue("(i,s)", e, ntdb_errorstr(e)));
  36. }
  37. static NTDB_DATA PyString_AsNtdb_Data(PyObject *data)
  38. {
  39. NTDB_DATA ret;
  40. ret.dptr = (unsigned char *)PyString_AsString(data);
  41. ret.dsize = PyString_Size(data);
  42. return ret;
  43. }
  44. static PyObject *PyString_FromNtdb_Data(NTDB_DATA data)
  45. {
  46. PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
  47. data.dsize);
  48. free(data.dptr);
  49. return ret;
  50. }
  51. #define PyErr_NTDB_ERROR_IS_ERR_RAISE(ret) \
  52. if (ret != NTDB_SUCCESS) { \
  53. PyErr_SetTDBError(ret); \
  54. return NULL; \
  55. }
  56. #define PyNtdb_CHECK_CLOSED(pyobj) \
  57. if (pyobj->closed) {\
  58. PyErr_SetObject(PyExc_RuntimeError, \
  59. Py_BuildValue("(i,s)", NTDB_ERR_EINVAL, "database is closed")); \
  60. return NULL; \
  61. }
  62. static void stderr_log(struct ntdb_context *ntdb,
  63. enum ntdb_log_level level,
  64. enum NTDB_ERROR ecode,
  65. const char *message,
  66. void *data)
  67. {
  68. fprintf(stderr, "%s:%s:%s\n",
  69. ntdb_name(ntdb), ntdb_errorstr(ecode), message);
  70. }
  71. static PyObject *py_ntdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  72. {
  73. char *name = NULL;
  74. int ntdb_flags = NTDB_DEFAULT, flags = O_RDWR, mode = 0600;
  75. struct ntdb_context *ctx;
  76. PyNtdbObject *ret;
  77. union ntdb_attribute logattr;
  78. const char *kwnames[] = { "name", "ntdb_flags", "flags", "mode", NULL };
  79. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siii", cast_const2(char **, kwnames), &name, &ntdb_flags, &flags, &mode))
  80. return NULL;
  81. if (name == NULL) {
  82. ntdb_flags |= NTDB_INTERNAL;
  83. name = "<internal>";
  84. }
  85. logattr.log.base.attr = NTDB_ATTRIBUTE_LOG;
  86. logattr.log.base.next = NULL;
  87. logattr.log.fn = stderr_log;
  88. ctx = ntdb_open(name, ntdb_flags, flags, mode, &logattr);
  89. if (ctx == NULL) {
  90. PyErr_SetFromErrno(PyExc_IOError);
  91. return NULL;
  92. }
  93. ret = PyObject_New(PyNtdbObject, &PyNtdb);
  94. if (!ret) {
  95. ntdb_close(ctx);
  96. return NULL;
  97. }
  98. ret->ctx = ctx;
  99. ret->closed = false;
  100. return (PyObject *)ret;
  101. }
  102. static PyObject *obj_transaction_cancel(PyNtdbObject *self)
  103. {
  104. PyNtdb_CHECK_CLOSED(self);
  105. ntdb_transaction_cancel(self->ctx);
  106. Py_RETURN_NONE;
  107. }
  108. static PyObject *obj_transaction_commit(PyNtdbObject *self)
  109. {
  110. enum NTDB_ERROR ret;
  111. PyNtdb_CHECK_CLOSED(self);
  112. ret = ntdb_transaction_commit(self->ctx);
  113. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  114. Py_RETURN_NONE;
  115. }
  116. static PyObject *obj_transaction_prepare_commit(PyNtdbObject *self)
  117. {
  118. enum NTDB_ERROR ret;
  119. PyNtdb_CHECK_CLOSED(self);
  120. ret = ntdb_transaction_prepare_commit(self->ctx);
  121. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  122. Py_RETURN_NONE;
  123. }
  124. static PyObject *obj_transaction_start(PyNtdbObject *self)
  125. {
  126. enum NTDB_ERROR ret;
  127. PyNtdb_CHECK_CLOSED(self);
  128. ret = ntdb_transaction_start(self->ctx);
  129. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  130. Py_RETURN_NONE;
  131. }
  132. static PyObject *obj_lockall(PyNtdbObject *self)
  133. {
  134. enum NTDB_ERROR ret;
  135. PyNtdb_CHECK_CLOSED(self);
  136. ret = ntdb_lockall(self->ctx);
  137. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  138. Py_RETURN_NONE;
  139. }
  140. static PyObject *obj_unlockall(PyNtdbObject *self)
  141. {
  142. PyNtdb_CHECK_CLOSED(self);
  143. ntdb_unlockall(self->ctx);
  144. Py_RETURN_NONE;
  145. }
  146. static PyObject *obj_lockall_read(PyNtdbObject *self)
  147. {
  148. enum NTDB_ERROR ret;
  149. PyNtdb_CHECK_CLOSED(self);
  150. ret = ntdb_lockall_read(self->ctx);
  151. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  152. Py_RETURN_NONE;
  153. }
  154. static PyObject *obj_unlockall_read(PyNtdbObject *self)
  155. {
  156. PyNtdb_CHECK_CLOSED(self);
  157. ntdb_unlockall_read(self->ctx);
  158. Py_RETURN_NONE;
  159. }
  160. static PyObject *obj_close(PyNtdbObject *self)
  161. {
  162. int ret;
  163. if (self->closed)
  164. Py_RETURN_NONE;
  165. ret = ntdb_close(self->ctx);
  166. self->closed = true;
  167. if (ret != 0) {
  168. PyErr_SetTDBError(NTDB_ERR_IO);
  169. return NULL;
  170. }
  171. Py_RETURN_NONE;
  172. }
  173. static PyObject *obj_get(PyNtdbObject *self, PyObject *args)
  174. {
  175. NTDB_DATA key, data;
  176. PyObject *py_key;
  177. enum NTDB_ERROR ret;
  178. PyNtdb_CHECK_CLOSED(self);
  179. if (!PyArg_ParseTuple(args, "O", &py_key))
  180. return NULL;
  181. key = PyString_AsNtdb_Data(py_key);
  182. ret = ntdb_fetch(self->ctx, key, &data);
  183. if (ret == NTDB_ERR_NOEXIST)
  184. Py_RETURN_NONE;
  185. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  186. return PyString_FromNtdb_Data(data);
  187. }
  188. static PyObject *obj_append(PyNtdbObject *self, PyObject *args)
  189. {
  190. NTDB_DATA key, data;
  191. PyObject *py_key, *py_data;
  192. enum NTDB_ERROR ret;
  193. PyNtdb_CHECK_CLOSED(self);
  194. if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
  195. return NULL;
  196. key = PyString_AsNtdb_Data(py_key);
  197. data = PyString_AsNtdb_Data(py_data);
  198. ret = ntdb_append(self->ctx, key, data);
  199. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  200. Py_RETURN_NONE;
  201. }
  202. static PyObject *obj_firstkey(PyNtdbObject *self)
  203. {
  204. enum NTDB_ERROR ret;
  205. NTDB_DATA key;
  206. PyNtdb_CHECK_CLOSED(self);
  207. ret = ntdb_firstkey(self->ctx, &key);
  208. if (ret == NTDB_ERR_NOEXIST)
  209. Py_RETURN_NONE;
  210. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  211. return PyString_FromNtdb_Data(key);
  212. }
  213. static PyObject *obj_nextkey(PyNtdbObject *self, PyObject *args)
  214. {
  215. NTDB_DATA key;
  216. PyObject *py_key;
  217. enum NTDB_ERROR ret;
  218. PyNtdb_CHECK_CLOSED(self);
  219. if (!PyArg_ParseTuple(args, "O", &py_key))
  220. return NULL;
  221. /* Malloc here, since ntdb_nextkey frees. */
  222. key.dsize = PyString_Size(py_key);
  223. key.dptr = malloc(key.dsize);
  224. memcpy(key.dptr, PyString_AsString(py_key), key.dsize);
  225. ret = ntdb_nextkey(self->ctx, &key);
  226. if (ret == NTDB_ERR_NOEXIST)
  227. Py_RETURN_NONE;
  228. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  229. return PyString_FromNtdb_Data(key);
  230. }
  231. static PyObject *obj_delete(PyNtdbObject *self, PyObject *args)
  232. {
  233. NTDB_DATA key;
  234. PyObject *py_key;
  235. enum NTDB_ERROR ret;
  236. PyNtdb_CHECK_CLOSED(self);
  237. if (!PyArg_ParseTuple(args, "O", &py_key))
  238. return NULL;
  239. key = PyString_AsNtdb_Data(py_key);
  240. ret = ntdb_delete(self->ctx, key);
  241. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  242. Py_RETURN_NONE;
  243. }
  244. static PyObject *obj_has_key(PyNtdbObject *self, PyObject *args)
  245. {
  246. NTDB_DATA key;
  247. PyObject *py_key;
  248. PyNtdb_CHECK_CLOSED(self);
  249. if (!PyArg_ParseTuple(args, "O", &py_key))
  250. return NULL;
  251. key = PyString_AsNtdb_Data(py_key);
  252. if (ntdb_exists(self->ctx, key))
  253. return Py_True;
  254. return Py_False;
  255. }
  256. static PyObject *obj_store(PyNtdbObject *self, PyObject *args)
  257. {
  258. NTDB_DATA key, value;
  259. enum NTDB_ERROR ret;
  260. int flag = NTDB_REPLACE;
  261. PyObject *py_key, *py_value;
  262. PyNtdb_CHECK_CLOSED(self);
  263. if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
  264. return NULL;
  265. key = PyString_AsNtdb_Data(py_key);
  266. value = PyString_AsNtdb_Data(py_value);
  267. ret = ntdb_store(self->ctx, key, value, flag);
  268. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  269. Py_RETURN_NONE;
  270. }
  271. static PyObject *obj_add_flag(PyNtdbObject *self, PyObject *args)
  272. {
  273. unsigned flag;
  274. PyNtdb_CHECK_CLOSED(self);
  275. if (!PyArg_ParseTuple(args, "I", &flag))
  276. return NULL;
  277. ntdb_add_flag(self->ctx, flag);
  278. Py_RETURN_NONE;
  279. }
  280. static PyObject *obj_remove_flag(PyNtdbObject *self, PyObject *args)
  281. {
  282. unsigned flag;
  283. PyNtdb_CHECK_CLOSED(self);
  284. if (!PyArg_ParseTuple(args, "I", &flag))
  285. return NULL;
  286. ntdb_remove_flag(self->ctx, flag);
  287. Py_RETURN_NONE;
  288. }
  289. typedef struct {
  290. PyObject_HEAD
  291. NTDB_DATA current;
  292. bool end;
  293. PyNtdbObject *iteratee;
  294. } PyNtdbIteratorObject;
  295. static PyObject *ntdb_iter_next(PyNtdbIteratorObject *self)
  296. {
  297. enum NTDB_ERROR e;
  298. PyObject *ret;
  299. if (self->end)
  300. return NULL;
  301. ret = PyString_FromStringAndSize((const char *)self->current.dptr,
  302. self->current.dsize);
  303. e = ntdb_nextkey(self->iteratee->ctx, &self->current);
  304. if (e == NTDB_ERR_NOEXIST)
  305. self->end = true;
  306. else
  307. PyErr_NTDB_ERROR_IS_ERR_RAISE(e);
  308. return ret;
  309. }
  310. static void ntdb_iter_dealloc(PyNtdbIteratorObject *self)
  311. {
  312. Py_DECREF(self->iteratee);
  313. PyObject_Del(self);
  314. }
  315. PyTypeObject PyNtdbIterator = {
  316. .tp_name = "Iterator",
  317. .tp_basicsize = sizeof(PyNtdbIteratorObject),
  318. .tp_iternext = (iternextfunc)ntdb_iter_next,
  319. .tp_dealloc = (destructor)ntdb_iter_dealloc,
  320. .tp_flags = Py_TPFLAGS_DEFAULT,
  321. .tp_iter = PyObject_SelfIter,
  322. };
  323. static PyObject *ntdb_object_iter(PyNtdbObject *self)
  324. {
  325. PyNtdbIteratorObject *ret;
  326. enum NTDB_ERROR e;
  327. PyNtdb_CHECK_CLOSED(self);
  328. ret = PyObject_New(PyNtdbIteratorObject, &PyNtdbIterator);
  329. if (!ret)
  330. return NULL;
  331. e = ntdb_firstkey(self->ctx, &ret->current);
  332. if (e == NTDB_ERR_NOEXIST) {
  333. ret->end = true;
  334. } else {
  335. PyErr_NTDB_ERROR_IS_ERR_RAISE(e);
  336. ret->end = false;
  337. }
  338. ret->iteratee = self;
  339. Py_INCREF(self);
  340. return (PyObject *)ret;
  341. }
  342. static PyObject *obj_clear(PyNtdbObject *self)
  343. {
  344. enum NTDB_ERROR ret;
  345. PyNtdb_CHECK_CLOSED(self);
  346. ret = ntdb_wipe_all(self->ctx);
  347. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  348. Py_RETURN_NONE;
  349. }
  350. static PyObject *obj_enable_seqnum(PyNtdbObject *self)
  351. {
  352. PyNtdb_CHECK_CLOSED(self);
  353. ntdb_add_flag(self->ctx, NTDB_SEQNUM);
  354. Py_RETURN_NONE;
  355. }
  356. static PyMethodDef ntdb_object_methods[] = {
  357. { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
  358. "S.transaction_cancel() -> None\n"
  359. "Cancel the currently active transaction." },
  360. { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
  361. "S.transaction_commit() -> None\n"
  362. "Commit the currently active transaction." },
  363. { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
  364. "S.transaction_prepare_commit() -> None\n"
  365. "Prepare to commit the currently active transaction" },
  366. { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
  367. "S.transaction_start() -> None\n"
  368. "Start a new transaction." },
  369. { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
  370. { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
  371. { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
  372. { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
  373. { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
  374. { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
  375. "Fetch a value." },
  376. { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
  377. "Append data to an existing key." },
  378. { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
  379. "Return the first key in this database." },
  380. { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
  381. "Return the next key in this database." },
  382. { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
  383. "Delete an entry." },
  384. { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
  385. "Check whether key exists in this database." },
  386. { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
  387. "Store data." },
  388. { "add_flag", (PyCFunction)obj_add_flag, METH_VARARGS, "S.add_flag(flag) -> None" },
  389. { "remove_flag", (PyCFunction)obj_remove_flag, METH_VARARGS, "S.remove_flag(flag) -> None" },
  390. { "iterkeys", (PyCFunction)ntdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
  391. { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
  392. "Wipe the entire database." },
  393. { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
  394. "S.enable_seqnum() -> None" },
  395. { NULL }
  396. };
  397. static PyObject *obj_get_flags(PyNtdbObject *self, void *closure)
  398. {
  399. PyNtdb_CHECK_CLOSED(self);
  400. return PyInt_FromLong(ntdb_get_flags(self->ctx));
  401. }
  402. static PyObject *obj_get_filename(PyNtdbObject *self, void *closure)
  403. {
  404. PyNtdb_CHECK_CLOSED(self);
  405. return PyString_FromString(ntdb_name(self->ctx));
  406. }
  407. static PyObject *obj_get_seqnum(PyNtdbObject *self, void *closure)
  408. {
  409. PyNtdb_CHECK_CLOSED(self);
  410. return PyInt_FromLong(ntdb_get_seqnum(self->ctx));
  411. }
  412. static PyGetSetDef ntdb_object_getsetters[] = {
  413. { cast_const(char *, "flags"), (getter)obj_get_flags, NULL, NULL },
  414. { cast_const(char *, "filename"), (getter)obj_get_filename, NULL,
  415. cast_const(char *, "The filename of this NTDB file.")},
  416. { cast_const(char *, "seqnum"), (getter)obj_get_seqnum, NULL, NULL },
  417. { NULL }
  418. };
  419. static PyObject *ntdb_object_repr(PyNtdbObject *self)
  420. {
  421. if (ntdb_get_flags(self->ctx) & NTDB_INTERNAL) {
  422. return PyString_FromString("Ntdb(<internal>)");
  423. } else {
  424. return PyString_FromFormat("Ntdb('%s')", ntdb_name(self->ctx));
  425. }
  426. }
  427. static void ntdb_object_dealloc(PyNtdbObject *self)
  428. {
  429. if (!self->closed)
  430. ntdb_close(self->ctx);
  431. self->ob_type->tp_free(self);
  432. }
  433. static PyObject *obj_getitem(PyNtdbObject *self, PyObject *key)
  434. {
  435. NTDB_DATA tkey, val;
  436. enum NTDB_ERROR ret;
  437. PyNtdb_CHECK_CLOSED(self);
  438. if (!PyString_Check(key)) {
  439. PyErr_SetString(PyExc_TypeError, "Expected string as key");
  440. return NULL;
  441. }
  442. tkey.dptr = (unsigned char *)PyString_AsString(key);
  443. tkey.dsize = PyString_Size(key);
  444. ret = ntdb_fetch(self->ctx, tkey, &val);
  445. if (ret == NTDB_ERR_NOEXIST) {
  446. PyErr_SetString(PyExc_KeyError, "No such NTDB entry");
  447. return NULL;
  448. } else {
  449. PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
  450. return PyString_FromNtdb_Data(val);
  451. }
  452. }
  453. static int obj_setitem(PyNtdbObject *self, PyObject *key, PyObject *value)
  454. {
  455. NTDB_DATA tkey, tval;
  456. enum NTDB_ERROR ret;
  457. if (self->closed) {
  458. PyErr_SetObject(PyExc_RuntimeError,
  459. Py_BuildValue("(i,s)", NTDB_ERR_EINVAL, "database is closed"));
  460. return -1;
  461. }
  462. if (!PyString_Check(key)) {
  463. PyErr_SetString(PyExc_TypeError, "Expected string as key");
  464. return -1;
  465. }
  466. tkey = PyString_AsNtdb_Data(key);
  467. if (value == NULL) {
  468. ret = ntdb_delete(self->ctx, tkey);
  469. } else {
  470. if (!PyString_Check(value)) {
  471. PyErr_SetString(PyExc_TypeError, "Expected string as value");
  472. return -1;
  473. }
  474. tval = PyString_AsNtdb_Data(value);
  475. ret = ntdb_store(self->ctx, tkey, tval, NTDB_REPLACE);
  476. }
  477. if (ret != NTDB_SUCCESS) {
  478. PyErr_SetTDBError(ret);
  479. return -1;
  480. }
  481. return ret;
  482. }
  483. static PyMappingMethods ntdb_object_mapping = {
  484. .mp_subscript = (binaryfunc)obj_getitem,
  485. .mp_ass_subscript = (objobjargproc)obj_setitem,
  486. };
  487. static PyTypeObject PyNtdb = {
  488. .tp_name = "ntdb.Ntdb",
  489. .tp_basicsize = sizeof(PyNtdbObject),
  490. .tp_methods = ntdb_object_methods,
  491. .tp_getset = ntdb_object_getsetters,
  492. .tp_new = py_ntdb_open,
  493. .tp_doc = "A NTDB file",
  494. .tp_repr = (reprfunc)ntdb_object_repr,
  495. .tp_dealloc = (destructor)ntdb_object_dealloc,
  496. .tp_as_mapping = &ntdb_object_mapping,
  497. .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
  498. .tp_iter = (getiterfunc)ntdb_object_iter,
  499. };
  500. static PyMethodDef ntdb_methods[] = {
  501. { "open", (PyCFunction)py_ntdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, ntdb_flags=NTDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
  502. "Open a NTDB file." },
  503. { NULL }
  504. };
  505. void initntdb(void);
  506. void initntdb(void)
  507. {
  508. PyObject *m;
  509. if (PyType_Ready(&PyNtdb) < 0)
  510. return;
  511. if (PyType_Ready(&PyNtdbIterator) < 0)
  512. return;
  513. m = Py_InitModule3("ntdb", ntdb_methods, "NTDB is a simple key-value database similar to GDBM that supports multiple writers.");
  514. if (m == NULL)
  515. return;
  516. PyModule_AddObject(m, "REPLACE", PyInt_FromLong(NTDB_REPLACE));
  517. PyModule_AddObject(m, "INSERT", PyInt_FromLong(NTDB_INSERT));
  518. PyModule_AddObject(m, "MODIFY", PyInt_FromLong(NTDB_MODIFY));
  519. PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(NTDB_DEFAULT));
  520. PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(NTDB_INTERNAL));
  521. PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(NTDB_NOLOCK));
  522. PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(NTDB_NOMMAP));
  523. PyModule_AddObject(m, "CONVERT", PyInt_FromLong(NTDB_CONVERT));
  524. PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(NTDB_NOSYNC));
  525. PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(NTDB_SEQNUM));
  526. PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(NTDB_ALLOW_NESTING));
  527. PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
  528. PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
  529. Py_INCREF(&PyNtdb);
  530. PyModule_AddObject(m, "Ntdb", (PyObject *)&PyNtdb);
  531. Py_INCREF(&PyNtdbIterator);
  532. }