~ubuntu-branches/ubuntu/natty/evolution-data-server/natty

« back to all changes in this revision

Viewing changes to libdb/db185/db185.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1996-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 */
7
 
 
8
 
#include "db_config.h"
9
 
 
10
 
#ifndef lint
11
 
static const char copyright[] =
12
 
    "Copyright (c) 1996-2002\nSleepycat Software Inc.  All rights reserved.\n";
13
 
static const char revid[] =
14
 
    "$Id$";
15
 
#endif
16
 
 
17
 
#ifndef NO_SYSTEM_INCLUDES
18
 
#include <sys/types.h>
19
 
 
20
 
#include <fcntl.h>
21
 
#include <string.h>
22
 
#include <unistd.h>
23
 
#endif
24
 
 
25
 
#include "db_int.h"
26
 
#include "db185_int.h"
27
 
 
28
 
static int      db185_close __P((DB185 *));
29
 
static int      db185_compare __P((DB *, const DBT *, const DBT *));
30
 
static int      db185_del __P((const DB185 *, const DBT185 *, u_int));
31
 
static int      db185_fd __P((const DB185 *));
32
 
static int      db185_get __P((const DB185 *, const DBT185 *, DBT185 *, u_int));
33
 
static u_int32_t
34
 
                db185_hash __P((DB *, const void *, u_int32_t));
35
 
static void     db185_openstderr __P((DB_FH *));
36
 
static size_t   db185_prefix __P((DB *, const DBT *, const DBT *));
37
 
static int      db185_put __P((const DB185 *, DBT185 *, const DBT185 *, u_int));
38
 
static int      db185_seq __P((const DB185 *, DBT185 *, DBT185 *, u_int));
39
 
static int      db185_sync __P((const DB185 *, u_int));
40
 
 
41
 
/*
42
 
 * EXTERN: #ifdef _DB185_INT_H_
43
 
 * EXTERN: DB185 *__db185_open
44
 
 * EXTERN:     __P((const char *, int, int, DBTYPE, const void *));
45
 
 * EXTERN: #else
46
 
 * EXTERN: DB *__db185_open
47
 
 * EXTERN:     __P((const char *, int, int, DBTYPE, const void *));
48
 
 * EXTERN: #endif
49
 
 */
50
 
DB185 *
51
 
__db185_open(file, oflags, mode, type, openinfo)
52
 
        const char *file;
53
 
        int oflags, mode;
54
 
        DBTYPE type;
55
 
        const void *openinfo;
56
 
{
57
 
        const BTREEINFO *bi;
58
 
        const HASHINFO *hi;
59
 
        const RECNOINFO *ri;
60
 
        DB *dbp;
61
 
        DB185 *db185p;
62
 
        DB_FH fh;
63
 
        size_t nw;
64
 
        int ret;
65
 
 
66
 
        dbp = NULL;
67
 
        db185p = NULL;
68
 
 
69
 
        if ((ret = db_create(&dbp, NULL, 0)) != 0)
70
 
                goto err;
71
 
 
72
 
        if ((ret = __os_calloc(NULL, 1, sizeof(DB185), &db185p)) != 0)
73
 
                goto err;
74
 
 
75
 
        /*
76
 
         * !!!
77
 
         * The DBTYPE enum wasn't initialized in DB 185, so it's off-by-one
78
 
         * from DB 2.0.
79
 
         */
80
 
        switch (type) {
81
 
        case 0:                                 /* DB_BTREE */
82
 
                type = DB_BTREE;
83
 
                if ((bi = openinfo) != NULL) {
84
 
                        if (bi->flags & ~R_DUP)
85
 
                                goto einval;
86
 
                        if (bi->flags & R_DUP)
87
 
                                (void)dbp->set_flags(dbp, DB_DUP);
88
 
                        if (bi->cachesize != 0)
89
 
                                (void)dbp->set_cachesize
90
 
                                    (dbp, 0, bi->cachesize, 0);
91
 
                        if (bi->minkeypage != 0)
92
 
                                (void)dbp->set_bt_minkey(dbp, bi->minkeypage);
93
 
                        if (bi->psize != 0)
94
 
                                (void)dbp->set_pagesize(dbp, bi->psize);
95
 
                        /*
96
 
                         * !!!
97
 
                         * Comparisons and prefix calls work because the DBT
98
 
                         * structures in 1.85 and 2.0 have the same initial
99
 
                         * fields.
100
 
                         */
101
 
                        if (bi->prefix != NULL) {
102
 
                                db185p->prefix = bi->prefix;
103
 
                                dbp->set_bt_prefix(dbp, db185_prefix);
104
 
                        }
105
 
                        if (bi->compare != NULL) {
106
 
                                db185p->compare = bi->compare;
107
 
                                dbp->set_bt_compare(dbp, db185_compare);
108
 
                        }
109
 
                        if (bi->lorder != 0)
110
 
                                dbp->set_lorder(dbp, bi->lorder);
111
 
                }
112
 
                break;
113
 
        case 1:                                 /* DB_HASH */
114
 
                type = DB_HASH;
115
 
                if ((hi = openinfo) != NULL) {
116
 
                        if (hi->bsize != 0)
117
 
                                (void)dbp->set_pagesize(dbp, hi->bsize);
118
 
                        if (hi->ffactor != 0)
119
 
                                (void)dbp->set_h_ffactor(dbp, hi->ffactor);
120
 
                        if (hi->nelem != 0)
121
 
                                (void)dbp->set_h_nelem(dbp, hi->nelem);
122
 
                        if (hi->cachesize != 0)
123
 
                                (void)dbp->set_cachesize
124
 
                                    (dbp, 0, hi->cachesize, 0);
125
 
                        if (hi->hash != NULL) {
126
 
                                db185p->hash = hi->hash;
127
 
                                (void)dbp->set_h_hash(dbp, db185_hash);
128
 
                        }
129
 
                        if (hi->lorder != 0)
130
 
                                dbp->set_lorder(dbp, hi->lorder);
131
 
                }
132
 
 
133
 
                break;
134
 
        case 2:                                 /* DB_RECNO */
135
 
                type = DB_RECNO;
136
 
 
137
 
                /* DB 1.85 did renumbering by default. */
138
 
                (void)dbp->set_flags(dbp, DB_RENUMBER);
139
 
 
140
 
                /*
141
 
                 * !!!
142
 
                 * The file name given to DB 1.85 recno is the name of the DB
143
 
                 * 2.0 backing file.  If the file doesn't exist, create it if
144
 
                 * the user has the O_CREAT flag set, DB 1.85 did it for you,
145
 
                 * and DB 2.0 doesn't.
146
 
                 *
147
 
                 * !!!
148
 
                 * Setting the file name to NULL specifies that we're creating
149
 
                 * a temporary backing file, in DB 2.X.  If we're opening the
150
 
                 * DB file read-only, change the flags to read-write, because
151
 
                 * temporary backing files cannot be opened read-only, and DB
152
 
                 * 2.X will return an error.  We are cheating here -- if the
153
 
                 * application does a put on the database, it will succeed --
154
 
                 * although that would be a stupid thing for the application
155
 
                 * to do.
156
 
                 *
157
 
                 * !!!
158
 
                 * Note, the file name in DB 1.85 was a const -- we don't do
159
 
                 * that in DB 2.0, so do that cast.
160
 
                 */
161
 
                if (file != NULL) {
162
 
                        if (oflags & O_CREAT && __os_exists(file, NULL) != 0)
163
 
                                if (__os_openhandle(NULL, file,
164
 
                                    oflags, mode, &fh) == 0)
165
 
                                        (void)__os_closehandle(NULL, &fh);
166
 
                        (void)dbp->set_re_source(dbp, file);
167
 
 
168
 
                        if (O_RDONLY)
169
 
                                oflags &= ~O_RDONLY;
170
 
                        oflags |= O_RDWR;
171
 
                        file = NULL;
172
 
                }
173
 
 
174
 
                if ((ri = openinfo) != NULL) {
175
 
                        /*
176
 
                         * !!!
177
 
                         * We can't support the bfname field.
178
 
                         */
179
 
#define BFMSG   "DB: DB 1.85's recno bfname field is not supported.\n"
180
 
                        if (ri->bfname != NULL) {
181
 
                                db185_openstderr(&fh);
182
 
                                (void)__os_write(NULL, &fh,
183
 
                                    BFMSG, sizeof(BFMSG) - 1, &nw);
184
 
                                goto einval;
185
 
                        }
186
 
 
187
 
                        if (ri->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
188
 
                                goto einval;
189
 
                        if (ri->flags & R_FIXEDLEN) {
190
 
                                if (ri->bval != 0)
191
 
                                        (void)dbp->set_re_pad(dbp, ri->bval);
192
 
                                if (ri->reclen != 0)
193
 
                                        (void)dbp->set_re_len(dbp, ri->reclen);
194
 
                        } else
195
 
                                if (ri->bval != 0)
196
 
                                        (void)dbp->set_re_delim(dbp, ri->bval);
197
 
 
198
 
                        /*
199
 
                         * !!!
200
 
                         * We ignore the R_NOKEY flag, but that's okay, it was
201
 
                         * only an optimization that was never implemented.
202
 
                         */
203
 
                        if (ri->flags & R_SNAPSHOT)
204
 
                                (void)dbp->set_flags(dbp, DB_SNAPSHOT);
205
 
 
206
 
                        if (ri->cachesize != 0)
207
 
                                (void)dbp->set_cachesize
208
 
                                    (dbp, 0, ri->cachesize, 0);
209
 
                        if (ri->psize != 0)
210
 
                                (void)dbp->set_pagesize(dbp, ri->psize);
211
 
                        if (ri->lorder != 0)
212
 
                                dbp->set_lorder(dbp, ri->lorder);
213
 
                }
214
 
                break;
215
 
        default:
216
 
                goto einval;
217
 
        }
218
 
 
219
 
        db185p->close = db185_close;
220
 
        db185p->del = db185_del;
221
 
        db185p->fd = db185_fd;
222
 
        db185p->get = db185_get;
223
 
        db185p->put = db185_put;
224
 
        db185p->seq = db185_seq;
225
 
        db185p->sync = db185_sync;
226
 
 
227
 
        /*
228
 
         * Store a reference so we can indirect from the DB 1.85 structure
229
 
         * to the underlying DB structure, and vice-versa.  This has to be
230
 
         * done BEFORE the DB::open method call because the hash callback
231
 
         * is exercised as part of hash database initialiation.
232
 
         */
233
 
        db185p->dbp = dbp;
234
 
        dbp->api_internal = db185p;
235
 
 
236
 
        /* Open the database. */
237
 
        if ((ret = dbp->open(dbp, NULL,
238
 
            file, NULL, type, __db_oflags(oflags), mode)) != 0)
239
 
                goto err;
240
 
 
241
 
        /* Create the cursor used for sequential ops. */
242
 
        if ((ret = dbp->cursor(dbp, NULL, &((DB185 *)db185p)->dbc, 0)) != 0)
243
 
                goto err;
244
 
 
245
 
        return (db185p);
246
 
 
247
 
err:    if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
248
 
einval:         ret = EINVAL;
249
 
        if (db185p != NULL)
250
 
                __os_free(NULL, db185p);
251
 
        if (dbp != NULL)
252
 
                (void)dbp->close(dbp, 0);
253
 
 
254
 
        __os_set_errno(ret);
255
 
        return (NULL);
256
 
}
257
 
 
258
 
static int
259
 
db185_close(db185p)
260
 
        DB185 *db185p;
261
 
{
262
 
        DB *dbp;
263
 
        int ret;
264
 
 
265
 
        dbp = db185p->dbp;
266
 
 
267
 
        ret = dbp->close(dbp, 0);
268
 
 
269
 
        __os_free(NULL, db185p);
270
 
 
271
 
        if (ret == 0)
272
 
                return (0);
273
 
 
274
 
        if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
275
 
                ret = EINVAL;
276
 
        __os_set_errno(ret);
277
 
        return (-1);
278
 
}
279
 
 
280
 
static int
281
 
db185_del(db185p, key185, flags)
282
 
        const DB185 *db185p;
283
 
        const DBT185 *key185;
284
 
        u_int flags;
285
 
{
286
 
        DB *dbp;
287
 
        DBT key;
288
 
        int ret;
289
 
 
290
 
        dbp = db185p->dbp;
291
 
 
292
 
        memset(&key, 0, sizeof(key));
293
 
        key.data = key185->data;
294
 
        key.size = key185->size;
295
 
 
296
 
        if (flags & ~R_CURSOR)
297
 
                goto einval;
298
 
        if (flags & R_CURSOR)
299
 
                ret = db185p->dbc->c_del(db185p->dbc, 0);
300
 
        else
301
 
                ret = dbp->del(dbp, NULL, &key, 0);
302
 
 
303
 
        switch (ret) {
304
 
        case 0:
305
 
                return (0);
306
 
        case DB_NOTFOUND:
307
 
                return (1);
308
 
        }
309
 
 
310
 
        if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
311
 
einval:         ret = EINVAL;
312
 
        __os_set_errno(ret);
313
 
        return (-1);
314
 
}
315
 
 
316
 
static int
317
 
db185_fd(db185p)
318
 
        const DB185 *db185p;
319
 
{
320
 
        DB *dbp;
321
 
        int fd, ret;
322
 
 
323
 
        dbp = db185p->dbp;
324
 
 
325
 
        if ((ret = dbp->fd(dbp, &fd)) == 0)
326
 
                return (fd);
327
 
 
328
 
        if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
329
 
                ret = EINVAL;
330
 
        __os_set_errno(ret);
331
 
        return (-1);
332
 
}
333
 
 
334
 
static int
335
 
db185_get(db185p, key185, data185, flags)
336
 
        const DB185 *db185p;
337
 
        const DBT185 *key185;
338
 
        DBT185 *data185;
339
 
        u_int flags;
340
 
{
341
 
        DB *dbp;
342
 
        DBT key, data;
343
 
        int ret;
344
 
 
345
 
        dbp = db185p->dbp;
346
 
 
347
 
        memset(&key, 0, sizeof(key));
348
 
        key.data = key185->data;
349
 
        key.size = key185->size;
350
 
        memset(&data, 0, sizeof(data));
351
 
        data.data = data185->data;
352
 
        data.size = data185->size;
353
 
 
354
 
        if (flags)
355
 
                goto einval;
356
 
 
357
 
        switch (ret = dbp->get(dbp, NULL, &key, &data, 0)) {
358
 
        case 0:
359
 
                data185->data = data.data;
360
 
                data185->size = data.size;
361
 
                return (0);
362
 
        case DB_NOTFOUND:
363
 
                return (1);
364
 
        }
365
 
 
366
 
        if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
367
 
einval:         ret = EINVAL;
368
 
        __os_set_errno(ret);
369
 
        return (-1);
370
 
}
371
 
 
372
 
static int
373
 
db185_put(db185p, key185, data185, flags)
374
 
        const DB185 *db185p;
375
 
        DBT185 *key185;
376
 
        const DBT185 *data185;
377
 
        u_int flags;
378
 
{
379
 
        DB *dbp;
380
 
        DBC *dbcp_put;
381
 
        DBT key, data;
382
 
        int ret, t_ret;
383
 
 
384
 
        dbp = db185p->dbp;
385
 
 
386
 
        memset(&key, 0, sizeof(key));
387
 
        key.data = key185->data;
388
 
        key.size = key185->size;
389
 
        memset(&data, 0, sizeof(data));
390
 
        data.data = data185->data;
391
 
        data.size = data185->size;
392
 
 
393
 
        switch (flags) {
394
 
        case 0:
395
 
                ret = dbp->put(dbp, NULL, &key, &data, 0);
396
 
                break;
397
 
        case R_CURSOR:
398
 
                ret = db185p->dbc->c_put(db185p->dbc, &key, &data, DB_CURRENT);
399
 
                break;
400
 
        case R_IAFTER:
401
 
        case R_IBEFORE:
402
 
                if (dbp->type != DB_RECNO)
403
 
                        goto einval;
404
 
 
405
 
                if ((ret = dbp->cursor(dbp, NULL, &dbcp_put, 0)) != 0)
406
 
                        break;
407
 
                if ((ret =
408
 
                    dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) == 0) {
409
 
                        memset(&data, 0, sizeof(data));
410
 
                        data.data = data185->data;
411
 
                        data.size = data185->size;
412
 
                        ret = dbcp_put->c_put(dbcp_put, &key, &data,
413
 
                            flags == R_IAFTER ? DB_AFTER : DB_BEFORE);
414
 
                }
415
 
                if ((t_ret = dbcp_put->c_close(dbcp_put)) != 0 && ret == 0)
416
 
                        ret = t_ret;
417
 
                break;
418
 
        case R_NOOVERWRITE:
419
 
                ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
420
 
                break;
421
 
        case R_SETCURSOR:
422
 
                if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
423
 
                        goto einval;
424
 
 
425
 
                if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0)
426
 
                        break;
427
 
                ret =
428
 
                    db185p->dbc->c_get(db185p->dbc, &key, &data, DB_SET_RANGE);
429
 
                break;
430
 
        default:
431
 
                goto einval;
432
 
        }
433
 
 
434
 
        switch (ret) {
435
 
        case 0:
436
 
                key185->data = key.data;
437
 
                key185->size = key.size;
438
 
                return (0);
439
 
        case DB_KEYEXIST:
440
 
                return (1);
441
 
        }
442
 
 
443
 
        if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
444
 
einval:         ret = EINVAL;
445
 
        __os_set_errno(ret);
446
 
        return (-1);
447
 
}
448
 
 
449
 
static int
450
 
db185_seq(db185p, key185, data185, flags)
451
 
        const DB185 *db185p;
452
 
        DBT185 *key185, *data185;
453
 
        u_int flags;
454
 
{
455
 
        DB *dbp;
456
 
        DBT key, data;
457
 
        int ret;
458
 
 
459
 
        dbp = db185p->dbp;
460
 
 
461
 
        memset(&key, 0, sizeof(key));
462
 
        key.data = key185->data;
463
 
        key.size = key185->size;
464
 
        memset(&data, 0, sizeof(data));
465
 
        data.data = data185->data;
466
 
        data.size = data185->size;
467
 
 
468
 
        switch (flags) {
469
 
        case R_CURSOR:
470
 
                flags = DB_SET_RANGE;
471
 
                break;
472
 
        case R_FIRST:
473
 
                flags = DB_FIRST;
474
 
                break;
475
 
        case R_LAST:
476
 
                if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
477
 
                        goto einval;
478
 
                flags = DB_LAST;
479
 
                break;
480
 
        case R_NEXT:
481
 
                flags = DB_NEXT;
482
 
                break;
483
 
        case R_PREV:
484
 
                if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
485
 
                        goto einval;
486
 
                flags = DB_PREV;
487
 
                break;
488
 
        default:
489
 
                goto einval;
490
 
        }
491
 
        switch (ret = db185p->dbc->c_get(db185p->dbc, &key, &data, flags)) {
492
 
        case 0:
493
 
                key185->data = key.data;
494
 
                key185->size = key.size;
495
 
                data185->data = data.data;
496
 
                data185->size = data.size;
497
 
                return (0);
498
 
        case DB_NOTFOUND:
499
 
                return (1);
500
 
        }
501
 
 
502
 
        if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
503
 
einval:         ret = EINVAL;
504
 
        __os_set_errno(ret);
505
 
        return (-1);
506
 
}
507
 
 
508
 
static int
509
 
db185_sync(db185p, flags)
510
 
        const DB185 *db185p;
511
 
        u_int flags;
512
 
{
513
 
        DB *dbp;
514
 
        DB_FH fh;
515
 
        size_t nw;
516
 
        int ret;
517
 
 
518
 
        dbp = db185p->dbp;
519
 
 
520
 
        switch (flags) {
521
 
        case 0:
522
 
                break;
523
 
        case R_RECNOSYNC:
524
 
                /*
525
 
                 * !!!
526
 
                 * We can't support the R_RECNOSYNC flag.
527
 
                 */
528
 
#define RSMSG   "DB: DB 1.85's R_RECNOSYNC sync flag is not supported.\n"
529
 
                db185_openstderr(&fh);
530
 
                (void)__os_write(NULL, &fh, RSMSG, sizeof(RSMSG) - 1, &nw);
531
 
                goto einval;
532
 
        default:
533
 
                goto einval;
534
 
        }
535
 
 
536
 
        if ((ret = dbp->sync(dbp, 0)) == 0)
537
 
                return (0);
538
 
 
539
 
        if (ret < 0)            /* DB 1.85 can't handle DB 2.0's errors. */
540
 
einval:         ret = EINVAL;
541
 
        __os_set_errno(ret);
542
 
        return (-1);
543
 
}
544
 
 
545
 
static void
546
 
db185_openstderr(fhp)
547
 
        DB_FH *fhp;
548
 
{
549
 
        /* Dummy up the results of an __os_openhandle() on stderr. */
550
 
        memset(fhp, 0, sizeof(*fhp));
551
 
        F_SET(fhp, DB_FH_VALID);
552
 
 
553
 
#ifndef STDERR_FILENO
554
 
#define STDERR_FILENO   2
555
 
#endif
556
 
        fhp->fd = STDERR_FILENO;
557
 
}
558
 
 
559
 
/*
560
 
 * db185_compare --
561
 
 *      Cutout routine to call the user's Btree comparison function.
562
 
 */
563
 
static int
564
 
db185_compare(dbp, a, b)
565
 
        DB *dbp;
566
 
        const DBT *a, *b;
567
 
{
568
 
        return (((DB185 *)dbp->api_internal)->compare(a, b));
569
 
}
570
 
 
571
 
/*
572
 
 * db185_prefix --
573
 
 *      Cutout routine to call the user's Btree prefix function.
574
 
 */
575
 
static size_t
576
 
db185_prefix(dbp, a, b)
577
 
        DB *dbp;
578
 
        const DBT *a, *b;
579
 
{
580
 
        return (((DB185 *)dbp->api_internal)->prefix(a, b));
581
 
}
582
 
 
583
 
/*
584
 
 * db185_hash --
585
 
 *      Cutout routine to call the user's hash function.
586
 
 */
587
 
static u_int32_t
588
 
db185_hash(dbp, key, len)
589
 
        DB *dbp;
590
 
        const void *key;
591
 
        u_int32_t len;
592
 
{
593
 
        return (((DB185 *)dbp->api_internal)->hash(key, (size_t)len));
594
 
}