1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
/***********************************************************
Copyright (c) 2000, BeOpen.com.
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
All rights reserved.
See the file "Misc/COPYRIGHT" for information on usage and
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
******************************************************************/
/* Berkeley DB interface.
Author: Michael McLay
Hacked: Guido van Rossum
Btree and Recno additions plus sequence methods: David Ely
XXX To do:
- provide interface to the B-tree and record libraries too
- provide a way to access the various hash functions
- support more open flags
The windows port of the Berkeley DB code is hard to find on the web:
www.nightmare.com/software.html
*/
#include "Python.h"
#ifdef WITH_THREAD
#include "pythread.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_DB_185_H
#include <db_185.h>
#else
#include <db.h>
#endif
/* Please don't include internal header files of the Berkeley db package
(it messes up the info required in the Setup file) */
typedef struct {
PyObject_HEAD
DB *di_bsddb;
int di_size; /* -1 means recompute */
#ifdef WITH_THREAD
PyThread_type_lock di_lock;
#endif
} bsddbobject;
staticforward PyTypeObject Bsddbtype;
#define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype)
#define check_bsddbobject_open(v) if ((v)->di_bsddb == NULL) \
{ PyErr_SetString(BsddbError, "BSDDB object has already been closed"); \
return NULL; }
static PyObject *BsddbError;
static PyObject *
newdbhashobject(char *file, int flags, int mode,
int bsize, int ffactor, int nelem, int cachesize, int hash, int lorder)
{
bsddbobject *dp;
HASHINFO info;
if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
return NULL;
info.bsize = bsize;
info.ffactor = ffactor;
info.nelem = nelem;
info.cachesize = cachesize;
info.hash = NULL; /* XXX should derive from hash argument */
info.lorder = lorder;
#ifdef O_BINARY
flags |= O_BINARY;
#endif
Py_BEGIN_ALLOW_THREADS
dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info);
Py_END_ALLOW_THREADS
if (dp->di_bsddb == NULL) {
PyErr_SetFromErrno(BsddbError);
#ifdef WITH_THREAD
dp->di_lock = NULL;
#endif
Py_DECREF(dp);
return NULL;
}
dp->di_size = -1;
#ifdef WITH_THREAD
dp->di_lock = PyThread_allocate_lock();
if (dp->di_lock == NULL) {
PyErr_SetString(BsddbError, "can't allocate lock");
Py_DECREF(dp);
return NULL;
}
#endif
return (PyObject *)dp;
}
static PyObject *
newdbbtobject(char *file, int flags, int mode,
int btflags, int cachesize, int maxkeypage, int minkeypage, int psize, int lorder)
{
bsddbobject *dp;
BTREEINFO info;
if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
return NULL;
info.flags = btflags;
info.cachesize = cachesize;
info.maxkeypage = maxkeypage;
info.minkeypage = minkeypage;
info.psize = psize;
info.lorder = lorder;
info.compare = 0; /* Use default comparison functions, for now..*/
info.prefix = 0;
#ifdef O_BINARY
flags |= O_BINARY;
#endif
Py_BEGIN_ALLOW_THREADS
dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info);
Py_END_ALLOW_THREADS
if (dp->di_bsddb == NULL) {
PyErr_SetFromErrno(BsddbError);
#ifdef WITH_THREAD
dp->di_lock = NULL;
#endif
Py_DECREF(dp);
return NULL;
}
dp->di_size = -1;
#ifdef WITH_THREAD
dp->di_lock = PyThread_allocate_lock();
if (dp->di_lock == NULL) {
PyErr_SetString(BsddbError, "can't allocate lock");
Py_DECREF(dp);
return NULL;
}
#endif
return (PyObject *)dp;
}
static PyObject *
newdbrnobject(char *file, int flags, int mode,
int rnflags, int cachesize, int psize, int lorder, size_t reclen, u_char bval, char *bfname)
{
bsddbobject *dp;
RECNOINFO info;
if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
return NULL;
info.flags = rnflags;
info.cachesize = cachesize;
info.psize = psize;
info.lorder = lorder;
info.reclen = reclen;
info.bval = bval;
info.bfname = bfname;
#ifdef O_BINARY
flags |= O_BINARY;
#endif
Py_BEGIN_ALLOW_THREADS
dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info);
Py_END_ALLOW_THREADS
if (dp->di_bsddb == NULL) {
PyErr_SetFromErrno(BsddbError);
#ifdef WITH_THREAD
dp->di_lock = NULL;
#endif
Py_DECREF(dp);
return NULL;
}
dp->di_size = -1;
#ifdef WITH_THREAD
dp->di_lock = PyThread_allocate_lock();
if (dp->di_lock == NULL) {
PyErr_SetString(BsddbError, "can't allocate lock");
Py_DECREF(dp);
return NULL;
}
#endif
return (PyObject *)dp;
}
static void
bsddb_dealloc(bsddbobject *dp)
{
#ifdef WITH_THREAD
if (dp->di_lock) {
PyThread_acquire_lock(dp->di_lock, 0);
PyThread_release_lock(dp->di_lock);
PyThread_free_lock(dp->di_lock);
dp->di_lock = NULL;
}
#endif
if (dp->di_bsddb != NULL) {
int status;
Py_BEGIN_ALLOW_THREADS
status = (dp->di_bsddb->close)(dp->di_bsddb);
Py_END_ALLOW_THREADS
if (status != 0)
fprintf(stderr,
"Python bsddb: close errno %d in dealloc\n",
errno);
}
PyObject_Del(dp);
}
#ifdef WITH_THREAD
#define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock(_dp->di_lock,1);
#define BSDDB_END_SAVE(_dp) PyThread_release_lock(_dp->di_lock); Py_END_ALLOW_THREADS
#else
#define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS
#define BSDDB_END_SAVE(_dp) Py_END_ALLOW_THREADS
#endif
static int
bsddb_length(bsddbobject *dp)
{
if (dp->di_bsddb == NULL) {
PyErr_SetString(BsddbError, "BSDDB object has already been closed");
return -1;
}
if (dp->di_size < 0) {
DBT krec, drec;
int status;
int size = 0;
BSDDB_BGN_SAVE(dp)
for (status = (dp->di_bsddb->seq)(dp->di_bsddb,
&krec, &drec,R_FIRST);
status == 0;
status = (dp->di_bsddb->seq)(dp->di_bsddb,
&krec, &drec, R_NEXT))
size++;
BSDDB_END_SAVE(dp)
if (status < 0) {
PyErr_SetFromErrno(BsddbError);
return -1;
}
dp->di_size = size;
}
return dp->di_size;
}
static PyObject *
bsddb_subscript(bsddbobject *dp, PyObject *key)
{
int status;
DBT krec, drec;
char *data,buf[4096];
int size;
PyObject *result;
if (!PyArg_Parse(key, "s#", &data, &size))
return NULL;
check_bsddbobject_open(dp);
krec.data = data;
krec.size = size;
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
if (status == 0) {
if (drec.size > sizeof(buf)) data = malloc(drec.size);
else data = buf;
memcpy(data,drec.data,drec.size);
}
BSDDB_END_SAVE(dp)
if (status != 0) {
if (status < 0)
PyErr_SetFromErrno(BsddbError);
else
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
}
result = PyString_FromStringAndSize(data, (int)drec.size);
if (data != buf) free(data);
return result;
}
static int
bsddb_ass_sub(bsddbobject *dp, PyObject *key, PyObject *value)
{
int status;
DBT krec, drec;
char *data;
int size;
if (!PyArg_Parse(key, "s#", &data, &size)) {
PyErr_SetString(PyExc_TypeError,
"bsddb key type must be string");
return -1;
}
if (dp->di_bsddb == NULL) {
PyErr_SetString(BsddbError, "BSDDB object has already been closed");
return -1;
}
krec.data = data;
krec.size = size;
dp->di_size = -1;
if (value == NULL) {
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0);
BSDDB_END_SAVE(dp)
}
else {
if (!PyArg_Parse(value, "s#", &data, &size)) {
PyErr_SetString(PyExc_TypeError,
"bsddb value type must be string");
return -1;
}
drec.data = data;
drec.size = size;
#if 0
/* For RECNO, put fails with 'No space left on device'
after a few short records are added?? Looks fine
to this point... linked with 1.85 on Solaris Intel
Roger E. Masse 1/16/97
*/
printf("before put data: '%s', size: %d\n",
drec.data, drec.size);
printf("before put key= '%s', size= %d\n",
krec.data, krec.size);
#endif
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0);
BSDDB_END_SAVE(dp)
}
if (status != 0) {
if (status < 0)
PyErr_SetFromErrno(BsddbError);
else
PyErr_SetObject(PyExc_KeyError, key);
return -1;
}
return 0;
}
static PyMappingMethods bsddb_as_mapping = {
(inquiry)bsddb_length, /*mp_length*/
(binaryfunc)bsddb_subscript, /*mp_subscript*/
(objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/
};
static PyObject *
bsddb_close(bsddbobject *dp, PyObject *args)
{
if (!PyArg_NoArgs(args))
return NULL;
if (dp->di_bsddb != NULL) {
int status;
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->close)(dp->di_bsddb);
BSDDB_END_SAVE(dp)
if (status != 0) {
dp->di_bsddb = NULL;
PyErr_SetFromErrno(BsddbError);
return NULL;
}
}
dp->di_bsddb = NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
bsddb_keys(bsddbobject *dp, PyObject *args)
{
PyObject *list, *item;
DBT krec, drec;
char *data=NULL,buf[4096];
int status;
int err;
if (!PyArg_NoArgs(args))
return NULL;
check_bsddbobject_open(dp);
list = PyList_New(0);
if (list == NULL)
return NULL;
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST);
if (status == 0) {
if (krec.size > sizeof(buf)) data = malloc(krec.size);
else data = buf;
memcpy(data,krec.data,krec.size);
}
BSDDB_END_SAVE(dp)
while (status == 0) {
item = PyString_FromStringAndSize(data, (int)krec.size);
if (data != buf) free(data);
if (item == NULL) {
Py_DECREF(list);
return NULL;
}
err = PyList_Append(list, item);
Py_DECREF(item);
if (err != 0) {
Py_DECREF(list);
return NULL;
}
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT);
if (status == 0) {
if (krec.size > sizeof(buf)) data = malloc(krec.size);
else data = buf;
memcpy(data,krec.data,krec.size);
}
BSDDB_END_SAVE(dp)
}
if (status < 0) {
PyErr_SetFromErrno(BsddbError);
Py_DECREF(list);
return NULL;
}
if (dp->di_size < 0)
dp->di_size = PyList_Size(list); /* We just did the work */
return list;
}
static PyObject *
bsddb_has_key(bsddbobject *dp, PyObject *args)
{
DBT krec, drec;
int status;
char *data;
int size;
if (!PyArg_Parse(args, "s#", &data, &size))
return NULL;
check_bsddbobject_open(dp);
krec.data = data;
krec.size = size;
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
BSDDB_END_SAVE(dp)
if (status < 0) {
PyErr_SetFromErrno(BsddbError);
return NULL;
}
return PyInt_FromLong(status == 0);
}
static PyObject *
bsddb_set_location(bsddbobject *dp, PyObject *key)
{
int status;
DBT krec, drec;
char *data,buf[4096];
int size;
PyObject *result;
if (!PyArg_Parse(key, "s#", &data, &size))
return NULL;
check_bsddbobject_open(dp);
krec.data = data;
krec.size = size;
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR);
if (status == 0) {
if (drec.size > sizeof(buf)) data = malloc(drec.size);
else data = buf;
memcpy(data,drec.data,drec.size);
}
BSDDB_END_SAVE(dp)
if (status != 0) {
if (status < 0)
PyErr_SetFromErrno(BsddbError);
else
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
}
result = Py_BuildValue("s#s#", krec.data, krec.size, data, drec.size);
if (data != buf) free(data);
return result;
}
static PyObject *
bsddb_seq(bsddbobject *dp, PyObject *args, int sequence_request)
{
int status;
DBT krec, drec;
char *kdata=NULL,kbuf[4096];
char *ddata=NULL,dbuf[4096];
PyObject *result;
if (!PyArg_NoArgs(args))
return NULL;
check_bsddbobject_open(dp);
krec.data = 0;
krec.size = 0;
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec,
&drec, sequence_request);
if (status == 0) {
if (krec.size > sizeof(kbuf)) kdata = malloc(krec.size);
else kdata = kbuf;
memcpy(kdata,krec.data,krec.size);
if (drec.size > sizeof(dbuf)) ddata = malloc(drec.size);
else ddata = dbuf;
memcpy(ddata,drec.data,drec.size);
}
BSDDB_END_SAVE(dp)
if (status != 0) {
if (status < 0)
PyErr_SetFromErrno(BsddbError);
else
PyErr_SetObject(PyExc_KeyError, args);
return NULL;
}
result = Py_BuildValue("s#s#", kdata, krec.size, ddata, drec.size);
if (kdata != kbuf) free(kdata);
if (ddata != dbuf) free(ddata);
return result;
}
static PyObject *
bsddb_next(bsddbobject *dp, PyObject *key)
{
return bsddb_seq(dp, key, R_NEXT);
}
static PyObject *
bsddb_previous(bsddbobject *dp, PyObject *key)
{
return bsddb_seq(dp, key, R_PREV);
}
static PyObject *
bsddb_first(bsddbobject *dp, PyObject *key)
{
return bsddb_seq(dp, key, R_FIRST);
}
static PyObject *
bsddb_last(bsddbobject *dp, PyObject *key)
{
return bsddb_seq(dp, key, R_LAST);
}
static PyObject *
bsddb_sync(bsddbobject *dp, PyObject *args)
{
int status;
if (!PyArg_NoArgs(args))
return NULL;
check_bsddbobject_open(dp);
BSDDB_BGN_SAVE(dp)
status = (dp->di_bsddb->sync)(dp->di_bsddb, 0);
BSDDB_END_SAVE(dp)
if (status != 0) {
PyErr_SetFromErrno(BsddbError);
return NULL;
}
return PyInt_FromLong(status = 0);
}
static PyMethodDef bsddb_methods[] = {
{"close", (PyCFunction)bsddb_close},
{"keys", (PyCFunction)bsddb_keys},
{"has_key", (PyCFunction)bsddb_has_key},
{"set_location", (PyCFunction)bsddb_set_location},
{"next", (PyCFunction)bsddb_next},
{"previous", (PyCFunction)bsddb_previous},
{"first", (PyCFunction)bsddb_first},
{"last", (PyCFunction)bsddb_last},
{"sync", (PyCFunction)bsddb_sync},
{NULL, NULL} /* sentinel */
};
static PyObject *
bsddb_getattr(PyObject *dp, char *name)
{
return Py_FindMethod(bsddb_methods, dp, name);
}
static PyTypeObject Bsddbtype = {
PyObject_HEAD_INIT(NULL)
0,
"bsddb",
sizeof(bsddbobject),
0,
(destructor)bsddb_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)bsddb_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
&bsddb_as_mapping, /*tp_as_mapping*/
};
static PyObject *
bsdhashopen(PyObject *self, PyObject *args)
{
char *file;
char *flag = NULL;
int flags = O_RDONLY;
int mode = 0666;
int bsize = 0;
int ffactor = 0;
int nelem = 0;
int cachesize = 0;
int hash = 0; /* XXX currently ignored */
int lorder = 0;
if (!PyArg_ParseTuple(args, "s|siiiiiii:hashopen",
&file, &flag, &mode,
&bsize, &ffactor, &nelem, &cachesize,
&hash, &lorder))
return NULL;
if (flag != NULL) {
/* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
if (flag[0] == 'r')
flags = O_RDONLY;
else if (flag[0] == 'w')
flags = O_RDWR;
else if (flag[0] == 'c')
flags = O_RDWR|O_CREAT;
else if (flag[0] == 'n')
flags = O_RDWR|O_CREAT|O_TRUNC;
else {
PyErr_SetString(BsddbError,
"Flag should begin with 'r', 'w', 'c' or 'n'");
return NULL;
}
if (flag[1] == 'l') {
#if defined(O_EXLOCK) && defined(O_SHLOCK)
if (flag[0] == 'r')
flags |= O_SHLOCK;
else
flags |= O_EXLOCK;
#else
PyErr_SetString(BsddbError,
"locking not supported on this platform");
return NULL;
#endif
}
}
return newdbhashobject(file, flags, mode,
bsize, ffactor, nelem, cachesize, hash, lorder);
}
static PyObject *
bsdbtopen(PyObject *self, PyObject *args)
{
char *file;
char *flag = NULL;
int flags = O_RDONLY;
int mode = 0666;
int cachesize = 0;
int maxkeypage = 0;
int minkeypage = 0;
int btflags = 0;
unsigned int psize = 0;
int lorder = 0;
if (!PyArg_ParseTuple(args, "s|siiiiiii:btopen",
&file, &flag, &mode,
&btflags, &cachesize, &maxkeypage, &minkeypage,
&psize, &lorder))
return NULL;
if (flag != NULL) {
/* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
if (flag[0] == 'r')
flags = O_RDONLY;
else if (flag[0] == 'w')
flags = O_RDWR;
else if (flag[0] == 'c')
flags = O_RDWR|O_CREAT;
else if (flag[0] == 'n')
flags = O_RDWR|O_CREAT|O_TRUNC;
else {
PyErr_SetString(BsddbError,
"Flag should begin with 'r', 'w', 'c' or 'n'");
return NULL;
}
if (flag[1] == 'l') {
#if defined(O_EXLOCK) && defined(O_SHLOCK)
if (flag[0] == 'r')
flags |= O_SHLOCK;
else
flags |= O_EXLOCK;
#else
PyErr_SetString(BsddbError,
"locking not supported on this platform");
return NULL;
#endif
}
}
return newdbbtobject(file, flags, mode,
btflags, cachesize, maxkeypage, minkeypage,
psize, lorder);
}
static PyObject *
bsdrnopen(PyObject *self, PyObject *args)
{
char *file;
char *flag = NULL;
int flags = O_RDONLY;
int mode = 0666;
int cachesize = 0;
int rnflags = 0;
unsigned int psize = 0;
int lorder = 0;
size_t reclen = 0;
char *bval = "";
char *bfname = NULL;
if (!PyArg_ParseTuple(args, "s|siiiiiiss:rnopen",
&file, &flag, &mode,
&rnflags, &cachesize, &psize, &lorder,
&reclen, &bval, &bfname))
return NULL;
# if 0
printf("file: %s\n", file);
printf("flag: %s\n", flag);
printf("mode: %d\n", mode);
printf("rnflags: 0x%x\n", rnflags);
printf("cachesize: %d\n", cachesize);
printf("psize: %d\n", psize);
printf("lorder: %d\n", 0);
printf("reclen: %d\n", reclen);
printf("bval: %c\n", bval[0]);
printf("bfname %s\n", bfname);
#endif
if (flag != NULL) {
/* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
if (flag[0] == 'r')
flags = O_RDONLY;
else if (flag[0] == 'w')
flags = O_RDWR;
else if (flag[0] == 'c')
flags = O_RDWR|O_CREAT;
else if (flag[0] == 'n')
flags = O_RDWR|O_CREAT|O_TRUNC;
else {
PyErr_SetString(BsddbError,
"Flag should begin with 'r', 'w', 'c' or 'n'");
return NULL;
}
if (flag[1] == 'l') {
#if defined(O_EXLOCK) && defined(O_SHLOCK)
if (flag[0] == 'r')
flags |= O_SHLOCK;
else
flags |= O_EXLOCK;
#else
PyErr_SetString(BsddbError,
"locking not supported on this platform");
return NULL;
#endif
}
else if (flag[1] != '\0') {
PyErr_SetString(BsddbError,
"Flag char 2 should be 'l' or absent");
return NULL;
}
}
return newdbrnobject(file, flags, mode, rnflags, cachesize,
psize, lorder, reclen, bval[0], bfname);
}
static PyMethodDef bsddbmodule_methods[] = {
{"hashopen", (PyCFunction)bsdhashopen, 1},
{"btopen", (PyCFunction)bsdbtopen, 1},
{"rnopen", (PyCFunction)bsdrnopen, 1},
{0, 0},
};
DL_EXPORT(void)
initbsddb(void) {
PyObject *m, *d;
Bsddbtype.ob_type = &PyType_Type;
m = Py_InitModule("bsddb", bsddbmodule_methods);
d = PyModule_GetDict(m);
BsddbError = PyErr_NewException("bsddb.error", NULL, NULL);
if (BsddbError != NULL)
PyDict_SetItemString(d, "error", BsddbError);
}
|