~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/dbm/dbm.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
 
 * Copyright (c) 1990, 1993
9
 
 *      Margo Seltzer.  All rights reserved.
10
 
 */
11
 
/*
12
 
 * Copyright (c) 1990, 1993
13
 
 *      The Regents of the University of California.  All rights reserved.
14
 
 *
15
 
 * This code is derived from software contributed to Berkeley by
16
 
 * Margo Seltzer.
17
 
 *
18
 
 * Redistribution and use in source and binary forms, with or without
19
 
 * modification, are permitted provided that the following conditions
20
 
 * are met:
21
 
 * 1. Redistributions of source code must retain the above copyright
22
 
 *    notice, this list of conditions and the following disclaimer.
23
 
 * 2. Redistributions in binary form must reproduce the above copyright
24
 
 *    notice, this list of conditions and the following disclaimer in the
25
 
 *    documentation and/or other materials provided with the distribution.
26
 
 * 3. Neither the name of the University nor the names of its contributors
27
 
 *    may be used to endorse or promote products derived from this software
28
 
 *    without specific prior written permission.
29
 
 *
30
 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34
 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35
 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36
 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37
 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39
 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
 
 * SUCH DAMAGE.
41
 
 */
42
 
 
43
 
#include "db_config.h"
44
 
 
45
 
#ifndef lint
46
 
static const char revid[] = "$Id$";
47
 
#endif /* not lint */
48
 
 
49
 
#ifndef NO_SYSTEM_INCLUDES
50
 
#include <sys/types.h>
51
 
 
52
 
#include <fcntl.h>
53
 
#include <string.h>
54
 
#endif
55
 
 
56
 
#define DB_DBM_HSEARCH  1
57
 
#include "db_int.h"
58
 
 
59
 
/*
60
 
 *
61
 
 * This package provides dbm and ndbm compatible interfaces to DB.
62
 
 *
63
 
 * EXTERN: #if DB_DBM_HSEARCH != 0
64
 
 *
65
 
 * EXTERN: int   __db_ndbm_clearerr __P((DBM *));
66
 
 * EXTERN: void  __db_ndbm_close __P((DBM *));
67
 
 * EXTERN: int   __db_ndbm_delete __P((DBM *, datum));
68
 
 * EXTERN: int   __db_ndbm_dirfno __P((DBM *));
69
 
 * EXTERN: int   __db_ndbm_error __P((DBM *));
70
 
 * EXTERN: datum __db_ndbm_fetch __P((DBM *, datum));
71
 
 * EXTERN: datum __db_ndbm_firstkey __P((DBM *));
72
 
 * EXTERN: datum __db_ndbm_nextkey __P((DBM *));
73
 
 * EXTERN: DBM  *__db_ndbm_open __P((const char *, int, int));
74
 
 * EXTERN: int   __db_ndbm_pagfno __P((DBM *));
75
 
 * EXTERN: int   __db_ndbm_rdonly __P((DBM *));
76
 
 * EXTERN: int   __db_ndbm_store __P((DBM *, datum, datum, int));
77
 
 *
78
 
 * EXTERN: int   __db_dbm_close __P((void));
79
 
 * EXTERN: int   __db_dbm_dbrdonly __P((void));
80
 
 * EXTERN: int   __db_dbm_delete __P((datum));
81
 
 * EXTERN: int   __db_dbm_dirf __P((void));
82
 
 * EXTERN: datum __db_dbm_fetch __P((datum));
83
 
 * EXTERN: datum __db_dbm_firstkey __P((void));
84
 
 * EXTERN: int   __db_dbm_init __P((char *));
85
 
 * EXTERN: datum __db_dbm_nextkey __P((datum));
86
 
 * EXTERN: int   __db_dbm_pagf __P((void));
87
 
 * EXTERN: int   __db_dbm_store __P((datum, datum));
88
 
 *
89
 
 * EXTERN: #endif
90
 
 */
91
 
 
92
 
/*
93
 
 * The DBM routines, which call the NDBM routines.
94
 
 */
95
 
static DBM *__cur_db;
96
 
 
97
 
static void __db_no_open __P((void));
98
 
 
99
 
int
100
 
__db_dbm_init(file)
101
 
        char *file;
102
 
{
103
 
        if (__cur_db != NULL)
104
 
                (void)dbm_close(__cur_db);
105
 
        if ((__cur_db =
106
 
            dbm_open(file, O_CREAT | O_RDWR, __db_omode("rw----"))) != NULL)
107
 
                return (0);
108
 
        if ((__cur_db = dbm_open(file, O_RDONLY, 0)) != NULL)
109
 
                return (0);
110
 
        return (-1);
111
 
}
112
 
 
113
 
int
114
 
__db_dbm_close()
115
 
{
116
 
        if (__cur_db != NULL) {
117
 
                dbm_close(__cur_db);
118
 
                __cur_db = NULL;
119
 
        }
120
 
        return (0);
121
 
}
122
 
 
123
 
datum
124
 
__db_dbm_fetch(key)
125
 
        datum key;
126
 
{
127
 
        datum item;
128
 
 
129
 
        if (__cur_db == NULL) {
130
 
                __db_no_open();
131
 
                item.dptr = NULL;
132
 
                item.dsize = 0;
133
 
                return (item);
134
 
        }
135
 
        return (dbm_fetch(__cur_db, key));
136
 
}
137
 
 
138
 
datum
139
 
__db_dbm_firstkey()
140
 
{
141
 
        datum item;
142
 
 
143
 
        if (__cur_db == NULL) {
144
 
                __db_no_open();
145
 
                item.dptr = NULL;
146
 
                item.dsize = 0;
147
 
                return (item);
148
 
        }
149
 
        return (dbm_firstkey(__cur_db));
150
 
}
151
 
 
152
 
datum
153
 
__db_dbm_nextkey(key)
154
 
        datum key;
155
 
{
156
 
        datum item;
157
 
 
158
 
        COMPQUIET(key.dsize, 0);
159
 
 
160
 
        if (__cur_db == NULL) {
161
 
                __db_no_open();
162
 
                item.dptr = NULL;
163
 
                item.dsize = 0;
164
 
                return (item);
165
 
        }
166
 
        return (dbm_nextkey(__cur_db));
167
 
}
168
 
 
169
 
int
170
 
__db_dbm_delete(key)
171
 
        datum key;
172
 
{
173
 
        if (__cur_db == NULL) {
174
 
                __db_no_open();
175
 
                return (-1);
176
 
        }
177
 
        return (dbm_delete(__cur_db, key));
178
 
}
179
 
 
180
 
int
181
 
__db_dbm_store(key, dat)
182
 
        datum key, dat;
183
 
{
184
 
        if (__cur_db == NULL) {
185
 
                __db_no_open();
186
 
                return (-1);
187
 
        }
188
 
        return (dbm_store(__cur_db, key, dat, DBM_REPLACE));
189
 
}
190
 
 
191
 
static void
192
 
__db_no_open()
193
 
{
194
 
        (void)fprintf(stderr, "dbm: no open database.\n");
195
 
}
196
 
 
197
 
/*
198
 
 * This package provides dbm and ndbm compatible interfaces to DB.
199
 
 *
200
 
 * The NDBM routines, which call the DB routines.
201
 
 */
202
 
/*
203
 
 * Returns:
204
 
 *      *DBM on success
205
 
 *       NULL on failure
206
 
 */
207
 
DBM *
208
 
__db_ndbm_open(file, oflags, mode)
209
 
        const char *file;
210
 
        int oflags, mode;
211
 
{
212
 
        DB *dbp;
213
 
        DBC *dbc;
214
 
        int ret;
215
 
        char path[MAXPATHLEN];
216
 
 
217
 
        /*
218
 
         * !!!
219
 
         * Don't use sprintf(3)/snprintf(3) -- the former is dangerous, and
220
 
         * the latter isn't standard, and we're manipulating strings handed
221
 
         * us by the application.
222
 
         */
223
 
        if (strlen(file) + strlen(DBM_SUFFIX) + 1 > sizeof(path)) {
224
 
                __os_set_errno(ENAMETOOLONG);
225
 
                return (NULL);
226
 
        }
227
 
        (void)strcpy(path, file);
228
 
        (void)strcat(path, DBM_SUFFIX);
229
 
        if ((ret = db_create(&dbp, NULL, 0)) != 0) {
230
 
                __os_set_errno(ret);
231
 
                return (NULL);
232
 
        }
233
 
 
234
 
        /*
235
 
         * !!!
236
 
         * The historic ndbm library corrected for opening O_WRONLY.
237
 
         */
238
 
        if (oflags & O_WRONLY) {
239
 
                oflags &= ~O_WRONLY;
240
 
                oflags |= O_RDWR;
241
 
        }
242
 
 
243
 
        if ((ret = dbp->set_pagesize(dbp, 4096)) != 0 ||
244
 
            (ret = dbp->set_h_ffactor(dbp, 40)) != 0 ||
245
 
            (ret = dbp->set_h_nelem(dbp, 1)) != 0 ||
246
 
            (ret = (*dbp->open)(dbp, NULL,
247
 
            path, NULL, DB_HASH, __db_oflags(oflags), mode)) != 0) {
248
 
                __os_set_errno(ret);
249
 
                return (NULL);
250
 
        }
251
 
 
252
 
        if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0) {
253
 
                (void)dbp->close(dbp, 0);
254
 
                __os_set_errno(ret);
255
 
                return (NULL);
256
 
        }
257
 
 
258
 
        return ((DBM *)dbc);
259
 
}
260
 
 
261
 
/*
262
 
 * Returns:
263
 
 *      Nothing.
264
 
 */
265
 
void
266
 
__db_ndbm_close(dbm)
267
 
        DBM *dbm;
268
 
{
269
 
        DBC *dbc;
270
 
 
271
 
        dbc = (DBC *)dbm;
272
 
 
273
 
        (void)dbc->dbp->close(dbc->dbp, 0);
274
 
}
275
 
 
276
 
/*
277
 
 * Returns:
278
 
 *      DATUM on success
279
 
 *      NULL on failure
280
 
 */
281
 
datum
282
 
__db_ndbm_fetch(dbm, key)
283
 
        DBM *dbm;
284
 
        datum key;
285
 
{
286
 
        DBC *dbc;
287
 
        DBT _key, _data;
288
 
        datum data;
289
 
        int ret;
290
 
 
291
 
        dbc = (DBC *)dbm;
292
 
 
293
 
        memset(&_key, 0, sizeof(DBT));
294
 
        memset(&_data, 0, sizeof(DBT));
295
 
        _key.size = key.dsize;
296
 
        _key.data = key.dptr;
297
 
 
298
 
        /*
299
 
         * Note that we can't simply use the dbc we have to do a c_get/SET,
300
 
         * because that cursor is the one used for sequential iteration and
301
 
         * it has to remain stable in the face of intervening gets and puts.
302
 
         */
303
 
        if ((ret = dbc->dbp->get(dbc->dbp, NULL, &_key, &_data, 0)) == 0) {
304
 
                data.dptr = _data.data;
305
 
                data.dsize = _data.size;
306
 
        } else {
307
 
                data.dptr = NULL;
308
 
                data.dsize = 0;
309
 
                if (ret == DB_NOTFOUND)
310
 
                        __os_set_errno(ENOENT);
311
 
                else {
312
 
                        __os_set_errno(ret);
313
 
                        F_SET(dbc->dbp, DB_AM_DBM_ERROR);
314
 
                }
315
 
        }
316
 
        return (data);
317
 
}
318
 
 
319
 
/*
320
 
 * Returns:
321
 
 *      DATUM on success
322
 
 *      NULL on failure
323
 
 */
324
 
datum
325
 
__db_ndbm_firstkey(dbm)
326
 
        DBM *dbm;
327
 
{
328
 
        DBC *dbc;
329
 
        DBT _key, _data;
330
 
        datum key;
331
 
        int ret;
332
 
 
333
 
        dbc = (DBC *)dbm;
334
 
 
335
 
        memset(&_key, 0, sizeof(DBT));
336
 
        memset(&_data, 0, sizeof(DBT));
337
 
 
338
 
        if ((ret = dbc->c_get(dbc, &_key, &_data, DB_FIRST)) == 0) {
339
 
                key.dptr = _key.data;
340
 
                key.dsize = _key.size;
341
 
        } else {
342
 
                key.dptr = NULL;
343
 
                key.dsize = 0;
344
 
                if (ret == DB_NOTFOUND)
345
 
                        __os_set_errno(ENOENT);
346
 
                else {
347
 
                        __os_set_errno(ret);
348
 
                        F_SET(dbc->dbp, DB_AM_DBM_ERROR);
349
 
                }
350
 
        }
351
 
        return (key);
352
 
}
353
 
 
354
 
/*
355
 
 * Returns:
356
 
 *      DATUM on success
357
 
 *      NULL on failure
358
 
 */
359
 
datum
360
 
__db_ndbm_nextkey(dbm)
361
 
        DBM *dbm;
362
 
{
363
 
        DBC *dbc;
364
 
        DBT _key, _data;
365
 
        datum key;
366
 
        int ret;
367
 
 
368
 
        dbc = (DBC *)dbm;
369
 
 
370
 
        memset(&_key, 0, sizeof(DBT));
371
 
        memset(&_data, 0, sizeof(DBT));
372
 
 
373
 
        if ((ret = dbc->c_get(dbc, &_key, &_data, DB_NEXT)) == 0) {
374
 
                key.dptr = _key.data;
375
 
                key.dsize = _key.size;
376
 
        } else {
377
 
                key.dptr = NULL;
378
 
                key.dsize = 0;
379
 
                if (ret == DB_NOTFOUND)
380
 
                        __os_set_errno(ENOENT);
381
 
                else {
382
 
                        __os_set_errno(ret);
383
 
                        F_SET(dbc->dbp, DB_AM_DBM_ERROR);
384
 
                }
385
 
        }
386
 
        return (key);
387
 
}
388
 
 
389
 
/*
390
 
 * Returns:
391
 
 *       0 on success
392
 
 *      <0 failure
393
 
 */
394
 
int
395
 
__db_ndbm_delete(dbm, key)
396
 
        DBM *dbm;
397
 
        datum key;
398
 
{
399
 
        DBC *dbc;
400
 
        DBT _key;
401
 
        int ret;
402
 
 
403
 
        dbc = (DBC *)dbm;
404
 
 
405
 
        memset(&_key, 0, sizeof(DBT));
406
 
        _key.data = key.dptr;
407
 
        _key.size = key.dsize;
408
 
 
409
 
        if ((ret = dbc->dbp->del(dbc->dbp, NULL, &_key, 0)) == 0)
410
 
                return (0);
411
 
 
412
 
        if (ret == DB_NOTFOUND)
413
 
                __os_set_errno(ENOENT);
414
 
        else {
415
 
                __os_set_errno(ret);
416
 
                F_SET(dbc->dbp, DB_AM_DBM_ERROR);
417
 
        }
418
 
        return (-1);
419
 
}
420
 
 
421
 
/*
422
 
 * Returns:
423
 
 *       0 on success
424
 
 *      <0 failure
425
 
 *       1 if DBM_INSERT and entry exists
426
 
 */
427
 
int
428
 
__db_ndbm_store(dbm, key, data, flags)
429
 
        DBM *dbm;
430
 
        datum key, data;
431
 
        int flags;
432
 
{
433
 
        DBC *dbc;
434
 
        DBT _key, _data;
435
 
        int ret;
436
 
 
437
 
        dbc = (DBC *)dbm;
438
 
 
439
 
        memset(&_key, 0, sizeof(DBT));
440
 
        _key.data = key.dptr;
441
 
        _key.size = key.dsize;
442
 
 
443
 
        memset(&_data, 0, sizeof(DBT));
444
 
        _data.data = data.dptr;
445
 
        _data.size = data.dsize;
446
 
 
447
 
        if ((ret = dbc->dbp->put(dbc->dbp, NULL,
448
 
            &_key, &_data, flags == DBM_INSERT ? DB_NOOVERWRITE : 0)) == 0)
449
 
                return (0);
450
 
 
451
 
        if (ret == DB_KEYEXIST)
452
 
                return (1);
453
 
 
454
 
        __os_set_errno(ret);
455
 
        F_SET(dbc->dbp, DB_AM_DBM_ERROR);
456
 
        return (-1);
457
 
}
458
 
 
459
 
int
460
 
__db_ndbm_error(dbm)
461
 
        DBM *dbm;
462
 
{
463
 
        DBC *dbc;
464
 
 
465
 
        dbc = (DBC *)dbm;
466
 
 
467
 
        return (F_ISSET(dbc->dbp, DB_AM_DBM_ERROR));
468
 
}
469
 
 
470
 
int
471
 
__db_ndbm_clearerr(dbm)
472
 
        DBM *dbm;
473
 
{
474
 
        DBC *dbc;
475
 
 
476
 
        dbc = (DBC *)dbm;
477
 
 
478
 
        F_CLR(dbc->dbp, DB_AM_DBM_ERROR);
479
 
        return (0);
480
 
}
481
 
 
482
 
/*
483
 
 * Returns:
484
 
 *      1 if read-only
485
 
 *      0 if not read-only
486
 
 */
487
 
int
488
 
__db_ndbm_rdonly(dbm)
489
 
        DBM *dbm;
490
 
{
491
 
        DBC *dbc;
492
 
 
493
 
        dbc = (DBC *)dbm;
494
 
 
495
 
        return (F_ISSET(dbc->dbp, DB_AM_RDONLY) ? 1 : 0);
496
 
}
497
 
 
498
 
/*
499
 
 * XXX
500
 
 * We only have a single file descriptor that we can return, not two.  Return
501
 
 * the same one for both files.  Hopefully, the user is using it for locking
502
 
 * and picked one to use at random.
503
 
 */
504
 
int
505
 
__db_ndbm_dirfno(dbm)
506
 
        DBM *dbm;
507
 
{
508
 
        return (dbm_pagfno(dbm));
509
 
}
510
 
 
511
 
int
512
 
__db_ndbm_pagfno(dbm)
513
 
        DBM *dbm;
514
 
{
515
 
        DBC *dbc;
516
 
        int fd;
517
 
 
518
 
        dbc = (DBC *)dbm;
519
 
 
520
 
        (void)dbc->dbp->fd(dbc->dbp, &fd);
521
 
        return (fd);
522
 
}