1ba3da9e726995e23f11bf14d2812f9b58763136
[platform/upstream/rpm.git] / rpmdb / db3.c
1 /** \ingroup db3
2  * \file rpmdb/db3.c
3  */
4
5 static int _debug = 1;  /* XXX if < 0 debugging, > 0 unusual error returns */
6
7 #include "system.h"
8
9 #if defined(HAVE_FTOK) && defined(HAVE_SYS_IPC_H)
10 #include <sys/ipc.h>
11 #endif
12
13 #include <db3/db.h>
14
15 #include <rpmlib.h>
16 #include <rpmmacro.h>
17 #include <rpmurl.h>     /* XXX urlPath proto */
18
19 #include "rpmdb.h"
20
21 #include "debug.h"
22
23 /*@access rpmdb@*/
24 /*@access dbiIndex@*/
25 /*@access dbiIndexSet@*/
26
27 /** \ingroup dbi
28  * Hash database statistics.
29  */
30 /*@-fielduse@*/
31 struct dbiHStats_s {
32     unsigned int hash_magic;    /*!< hash database magic number. */
33     unsigned int hash_version;  /*!< version of the hash database. */
34     unsigned int hash_nkeys;    /*!< no. of unique keys in the database. */
35     unsigned int hash_ndata;    /*!< no. of key/data pairs in the database. */
36     unsigned int hash_pagesize; /*!< db page (and bucket) size, in bytes. */
37     unsigned int hash_nelem;    /*!< estimated size of the hash table. */
38     unsigned int hash_ffactor;  /*!< no. of items per bucket. */
39     unsigned int hash_buckets;  /*!< no. of hash buckets. */
40     unsigned int hash_free;     /*!< no. of pages on the free list. */
41     unsigned int hash_bfree;    /*!< no. of bytes free on bucket pages. */
42     unsigned int hash_bigpages; /*!< no. of big key/data pages. */
43     unsigned int hash_big_bfree;/*!< no. of bytes free on big item pages. */
44     unsigned int hash_overflows;/*!< no. of overflow pages. */
45     unsigned int hash_ovfl_free;/*!< no. of bytes free on overflow pages. */
46     unsigned int hash_dup;      /*!< no. of duplicate pages. */
47     unsigned int hash_dup_free; /*!< no. bytes free on duplicate pages. */
48 };
49
50 /** \ingroup dbi
51  * B-tree database statistics.
52  */
53 struct dbiBStats_s {
54     unsigned int bt_magic;      /*!< btree database magic. */
55     unsigned int bt_version;    /*!< version of the btree database. */
56     unsigned int bt_nkeys;      /*!< no. of unique keys in the database. */
57     unsigned int bt_ndata;      /*!< no. of key/data pairs in the database. */
58     unsigned int bt_pagesize;   /*!< database page size, in bytes. */
59     unsigned int bt_minkey;     /*!< minimum keys per page. */
60     unsigned int bt_re_len;     /*!< length of fixed-length records. */
61     unsigned int bt_re_pad;     /*!< padding byte for fixed-length records. */
62     unsigned int bt_levels;     /*!< no. of levels in the database. */
63     unsigned int bt_int_pg;     /*!< no. of database internal pages. */
64     unsigned int bt_leaf_pg;    /*!< no. of database leaf pages. */
65     unsigned int bt_dup_pg;     /*!< no. of database duplicate pages. */
66     unsigned int bt_over_pg;    /*!< no. of database overflow pages. */
67     unsigned int bt_free;       /*!< no. of pages on the free list. */
68     unsigned int bt_int_pgfree; /*!< no. of bytes free in internal pages. */
69     unsigned int bt_leaf_pgfree;/*!< no. of bytes free in leaf pages. */
70     unsigned int bt_dup_pgfree; /*!< no. of bytes free in duplicate pages. */
71     unsigned int bt_over_pgfree;/*!< no. of bytes free in overflow pages. */
72 };
73 /*@=fielduse@*/
74
75 static int cvtdberr(dbiIndex dbi, const char * msg, int error, int printit)
76         /*@modifies fileSystem @*/
77 {
78     int rc = 0;
79
80     rc = error;
81
82     if (printit && rc) {
83         if (msg)
84             rpmError(RPMERR_DBERR, _("db%d error(%d) from %s: %s\n"),
85                 dbi->dbi_api, rc, msg, db_strerror(error));
86         else
87             rpmError(RPMERR_DBERR, _("db%d error(%d): %s\n"),
88                 dbi->dbi_api, rc, db_strerror(error));
89     }
90
91     return rc;
92 }
93
94 static int db_fini(dbiIndex dbi, const char * dbhome,
95                 /*@null@*/ const char * dbfile,
96                 /*@unused@*/ /*@null@*/ const char * dbsubfile)
97         /*@modifies dbi, fileSystem @*/
98 {
99     rpmdb rpmdb = dbi->dbi_rpmdb;
100     DB_ENV * dbenv = dbi->dbi_dbenv;
101     int rc;
102
103     if (dbenv == NULL) {
104         dbi->dbi_dbenv = NULL;
105         return 0;
106     }
107
108     rc = dbenv->close(dbenv, 0);
109     rc = cvtdberr(dbi, "dbenv->close", rc, _debug);
110
111     if (dbfile)
112         rpmMessage(RPMMESS_DEBUG, _("closed   db environment %s/%s\n"),
113                         dbhome, dbfile);
114
115     if (rpmdb->db_remove_env || dbi->dbi_tear_down) {
116         int xx;
117
118         xx = db_env_create(&dbenv, 0);
119         xx = cvtdberr(dbi, "db_env_create", rc, _debug);
120 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0
121         xx = dbenv->remove(dbenv, dbhome, 0);
122 #else
123         xx = dbenv->remove(dbenv, dbhome, NULL, 0);
124 #endif
125         xx = cvtdberr(dbi, "dbenv->remove", rc, _debug);
126
127         if (dbfile)
128             rpmMessage(RPMMESS_DEBUG, _("removed  db environment %s/%s\n"),
129                         dbhome, dbfile);
130
131     }
132     dbi->dbi_dbenv = NULL;
133     return rc;
134 }
135
136 static int db3_fsync_disable(/*@unused@*/ int fd)
137         /*@*/
138 {
139     return 0;
140 }
141
142 static int db_init(dbiIndex dbi, const char * dbhome,
143                 /*@null@*/ const char * dbfile,
144                 /*@unused@*/ /*@null@*/ const char * dbsubfile,
145                 /*@out@*/ DB_ENV ** dbenvp)
146         /*@modifies dbi, *dbenvp, fileSystem @*/
147 {
148     rpmdb rpmdb = dbi->dbi_rpmdb;
149     DB_ENV *dbenv = NULL;
150     int eflags;
151     int rc;
152
153     if (dbenvp == NULL)
154         return 1;
155
156     /* XXX HACK */
157     /*@-assignexpose@*/
158     if (rpmdb->db_errfile == NULL)
159         rpmdb->db_errfile = stderr;
160     /*@=assignexpose@*/
161
162     eflags = (dbi->dbi_oeflags | dbi->dbi_eflags);
163     if (eflags & DB_JOINENV) eflags &= DB_JOINENV;
164
165     if (dbfile)
166         rpmMessage(RPMMESS_DEBUG, _("opening  db environment %s/%s %s\n"),
167                 dbhome, dbfile, prDbiOpenFlags(eflags, 1));
168
169     /* XXX Can't do RPC w/o host. */
170     if (dbi->dbi_host == NULL)
171         dbi->dbi_ecflags &= ~DB_CLIENT;
172
173     /* XXX Set a default shm_key. */
174     if ((dbi->dbi_eflags & DB_SYSTEM_MEM) && dbi->dbi_shmkey == 0) {
175 #if defined(HAVE_FTOK)
176         dbi->dbi_shmkey = ftok(dbhome, 0);
177 #else
178         dbi->dbi_shmkey = 0x44631380;
179 #endif
180     }
181
182     rc = db_env_create(&dbenv, dbi->dbi_ecflags);
183     rc = cvtdberr(dbi, "db_env_create", rc, _debug);
184     if (rc)
185         goto errxit;
186
187     if (dbenv == NULL)
188         return 1;
189
190   { int xx;
191     dbenv->set_errcall(dbenv, rpmdb->db_errcall);
192     dbenv->set_errfile(dbenv, rpmdb->db_errfile);
193     dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
194  /* dbenv->set_paniccall(???) */
195     (void) dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
196                 (dbi->dbi_verbose & DB_VERB_CHKPOINT));
197     (void) dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
198                 (dbi->dbi_verbose & DB_VERB_DEADLOCK));
199     (void) dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
200                 (dbi->dbi_verbose & DB_VERB_RECOVERY));
201     (void) dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
202                 (dbi->dbi_verbose & DB_VERB_WAITSFOR));
203  /* dbenv->set_lg_max(???) */
204  /* dbenv->set_lk_conflicts(???) */
205  /* dbenv->set_lk_detect(???) */
206  /* dbenv->set_lk_max(???) */
207     xx = dbenv->set_mp_mmapsize(dbenv, dbi->dbi_mp_mmapsize);
208     xx = cvtdberr(dbi, "dbenv->set_mp_mmapsize", xx, _debug);
209     xx = dbenv->set_cachesize(dbenv, 0, dbi->dbi_mp_size, 0);
210     xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug);
211  /* dbenv->set_tx_max(???) */
212  /* dbenv->set_tx_recover(???) */
213     if (dbi->dbi_no_fsync) {
214 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0
215         xx = db_env_set_func_fsync(db3_fsync_disable);
216 #else
217         xx = dbenv->set_func_fsync(dbenv, db3_fsync_disable);
218 #endif
219         xx = cvtdberr(dbi, "db_env_set_func_fsync", xx, _debug);
220     }
221
222 /* XXX 3.3.4 change. */
223 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3
224     if ((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) {
225         xx = dbenv->set_rpc_server(dbenv, NULL, dbi->dbi_host,
226                 dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
227         xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug);
228     }
229 #else
230     if ((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) {
231         xx = dbenv->set_server(dbenv, dbi->dbi_host,
232                 dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
233         xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug);
234     }
235 #endif
236     if (dbi->dbi_shmkey) {
237         xx = dbenv->set_shm_key(dbenv, dbi->dbi_shmkey);
238         xx = cvtdberr(dbi, "dbenv->set_shm_key", xx, _debug);
239     }
240     if (dbi->dbi_tmpdir) {
241         const char * root;
242         const char * tmpdir;
243
244         root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
245         if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
246             root = NULL;
247         tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
248         xx = dbenv->set_tmp_dir(dbenv, tmpdir);
249         xx = cvtdberr(dbi, "dbenv->set_tmp_dir", rc, _debug);
250         tmpdir = _free(tmpdir);
251     }
252   }
253
254 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0
255     rc = dbenv->open(dbenv, dbhome, eflags, dbi->dbi_perms);
256 #else
257     rc = dbenv->open(dbenv, dbhome, NULL, eflags, dbi->dbi_perms);
258 #endif
259     rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
260     if (rc)
261         goto errxit;
262
263     *dbenvp = dbenv;
264
265     return 0;
266
267 errxit:
268     if (dbenv) {
269         int xx;
270         xx = dbenv->close(dbenv, 0);
271         xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
272     }
273     return rc;
274 }
275
276 static int db3sync(dbiIndex dbi, unsigned int flags)
277         /*@modifies fileSystem @*/
278 {
279     DB * db = dbi->dbi_db;
280     int rc = 0;
281     int _printit;
282
283     if (db != NULL)
284         rc = db->sync(db, flags);
285     /* XXX DB_INCOMPLETE is returned occaisionally with multiple access. */
286     _printit = (rc == DB_INCOMPLETE ? 0 : _debug);
287     rc = cvtdberr(dbi, "db->sync", rc, _printit);
288     return rc;
289 }
290
291 static int db3c_del(dbiIndex dbi, DBC * dbcursor, u_int32_t flags)
292         /*@modifies fileSystem @*/
293 {
294     int rc;
295
296     rc = dbcursor->c_del(dbcursor, flags);
297     rc = cvtdberr(dbi, "dbcursor->c_del", rc, _debug);
298     return rc;
299 }
300
301 /*@unused@*/ static int db3c_dup(dbiIndex dbi, DBC * dbcursor, DBC ** dbcp,
302                 u_int32_t flags)
303         /*@modifies *dbcp, fileSystem @*/
304 {
305     int rc;
306
307     if (dbcp) *dbcp = NULL;
308     rc = dbcursor->c_dup(dbcursor, dbcp, flags);
309     rc = cvtdberr(dbi, "dbcursor->c_dup", rc, _debug);
310     return rc;
311 }
312
313 static int db3c_get(dbiIndex dbi, DBC * dbcursor,
314                 DBT * key, DBT * data, u_int32_t flags)
315         /*@modifies fileSystem @*/
316 {
317     int _printit;
318     int rc;
319     int rmw;
320
321 #ifdef  NOTYET
322     if ((dbi->dbi_eflags & DB_INIT_CDB) && !(dbi->dbi_oflags & DB_RDONLY))
323         rmw = DB_RMW;
324     else
325 #endif
326         rmw = 0;
327
328     rc = dbcursor->c_get(dbcursor, key, data, rmw | flags);
329
330     /* XXX DB_NOTFOUND can be returned */
331     _printit = (rc == DB_NOTFOUND ? 0 : _debug);
332     rc = cvtdberr(dbi, "dbcursor->c_get", rc, _printit);
333     return rc;
334 }
335
336 static int db3c_put(dbiIndex dbi, DBC * dbcursor,
337                 DBT * key, DBT * data, u_int32_t flags)
338         /*@modifies fileSystem @*/
339 {
340     int rc;
341
342     rc = dbcursor->c_put(dbcursor, key, data, flags);
343
344     rc = cvtdberr(dbi, "dbcursor->c_put", rc, _debug);
345     return rc;
346 }
347
348 static inline int db3c_close(dbiIndex dbi, /*@only@*/ /*@null@*/ DBC * dbcursor)
349         /*@modifies fileSystem @*/
350 {
351     int rc;
352
353     if (dbcursor == NULL) return -2;
354
355     rc = dbcursor->c_close(dbcursor);
356     rc = cvtdberr(dbi, "dbcursor->c_close", rc, _debug);
357     return rc;
358 }
359
360 static inline int db3c_open(dbiIndex dbi, /*@null@*/ /*@out@*/ DBC ** dbcp,
361                 int dbiflags)
362         /*@modifies *dbcp, fileSystem @*/
363 {
364     DB * db = dbi->dbi_db;
365     DB_TXN * txnid = NULL;
366     int flags;
367     int rc;
368
369     if (db == NULL) return -2;
370     if ((dbiflags & DBI_WRITECURSOR) &&
371         (dbi->dbi_eflags & DB_INIT_CDB) && !(dbi->dbi_oflags & DB_RDONLY))
372     {
373         flags = DB_WRITECURSOR;
374     } else
375         flags = 0;
376     if (dbcp) *dbcp = NULL;
377     rc = db->cursor(db, txnid, dbcp, flags);
378     rc = cvtdberr(dbi, "db3c_open", rc, _debug);
379
380     return rc;
381 }
382
383 static int db3cclose(dbiIndex dbi, /*@only@*/ /*@null@*/ DBC * dbcursor,
384                 unsigned int flags)
385         /*@modifies dbi, fileSystem @*/
386 {
387     int rc = 0;
388
389     /* XXX per-iterator cursors */
390     if (flags & DBI_ITERATOR)
391         return db3c_close(dbi, dbcursor);
392
393     if (!dbi->dbi_use_cursors)
394         return 0;
395
396     if (dbcursor == NULL)
397         dbcursor = dbi->dbi_rmw;
398     if (dbcursor) {
399         if (dbcursor == dbi->dbi_rmw)
400             dbi->dbi_rmw = NULL;
401         rc = db3c_close(dbi, dbcursor);
402     }
403     /*@-usereleased -compdef@*/ return rc; /*@=usereleased =compdef@*/
404 }
405
406 static int db3copen(dbiIndex dbi,
407                 /*@null@*/ /*@out@*/ DBC ** dbcp, unsigned int flags)
408         /*@modifies dbi, *dbcp, fileSystem @*/
409 {
410     DBC * dbcursor;
411     int rc = 0;
412
413     /* XXX per-iterator cursors */
414     if (flags & DBI_ITERATOR)
415         return db3c_open(dbi, dbcp, flags);
416
417     if (!dbi->dbi_use_cursors) {
418         if (dbcp) *dbcp = NULL;
419         return 0;
420     }
421
422     if ((dbcursor = dbi->dbi_rmw) == NULL) {
423         if ((rc = db3c_open(dbi, &dbcursor, flags)) == 0)
424             dbi->dbi_rmw = dbcursor;
425     }
426
427     if (dbcp)
428         /*@-onlytrans@*/ *dbcp = dbi->dbi_rmw; /*@=onlytrans@*/
429
430     return rc;
431 }
432
433 static int db3cput(dbiIndex dbi, DBC * dbcursor,
434                 const void * keyp, size_t keylen,
435                 const void * datap, size_t datalen,
436                 /*@unused@*/ unsigned int flags)
437         /*@modifies fileSystem @*/
438 {
439     DB * db = dbi->dbi_db;
440     DB_TXN * txnid = NULL;
441     DBT key, data;
442     int rc;
443
444     memset(&key, 0, sizeof(key));
445     memset(&data, 0, sizeof(data));
446     key.data = (void *)keyp;
447     key.size = keylen;
448     data.data = (void *)datap;
449     data.size = datalen;
450
451     if (dbcursor == NULL) {
452         if (db == NULL) return -2;
453         rc = db->put(db, txnid, &key, &data, 0);
454         rc = cvtdberr(dbi, "db->put", rc, _debug);
455     } else {
456
457         rc = db3c_put(dbi, dbcursor, &key, &data, DB_KEYLAST);
458
459     }
460
461     return rc;
462 }
463
464 static int db3cdel(dbiIndex dbi, DBC * dbcursor,
465                 const void * keyp, size_t keylen,
466                 /*@unused@*/ unsigned int flags)
467         /*@modifies fileSystem @*/
468 {
469     DB * db = dbi->dbi_db;
470     DB_TXN * txnid = NULL;
471     DBT key, data;
472     int rc;
473
474     memset(&key, 0, sizeof(key));
475     memset(&data, 0, sizeof(data));
476
477     key.data = (void *)keyp;
478     key.size = keylen;
479
480     if (dbcursor == NULL) {
481         if (db == NULL) return -2;
482         rc = db->del(db, txnid, &key, 0);
483         rc = cvtdberr(dbi, "db->del", rc, _debug);
484     } else {
485
486         rc = db3c_get(dbi, dbcursor, &key, &data, DB_SET);
487
488         if (rc == 0) {
489             /* XXX TODO: loop over duplicates */
490             rc = db3c_del(dbi, dbcursor, 0);
491         }
492
493     }
494
495     return rc;
496 }
497
498 static int db3cget(dbiIndex dbi, DBC * dbcursor,
499                 /*@null@*/ void ** keyp, /*@null@*/ size_t * keylen,
500                 /*@null@*/ void ** datap, /*@null@*/ size_t * datalen,
501                 /*@unused@*/ unsigned int flags)
502         /*@modifies *keyp, *keylen, *datap, *datalen, fileSystem @*/
503 {
504     DB * db = dbi->dbi_db;
505     DB_TXN * txnid = NULL;
506     DBT key, data;
507     int rc;
508
509     memset(&key, 0, sizeof(key));
510     memset(&data, 0, sizeof(data));
511     /*@-unqualifiedtrans@*/
512     if (keyp)           key.data = *keyp;
513     if (keylen)         key.size = *keylen;
514     if (datap)          data.data = *datap;
515     if (datalen)        data.size = *datalen;
516     /*@=unqualifiedtrans@*/
517
518     if (dbcursor == NULL) {
519         int _printit;
520         /*@-compmempass@*/
521         if (db == NULL) return -2;
522         /*@=compmempass@*/
523         rc = db->get(db, txnid, &key, &data, 0);
524         /* XXX DB_NOTFOUND can be returned */
525         _printit = (rc == DB_NOTFOUND ? 0 : _debug);
526         rc = cvtdberr(dbi, "db->get", rc, _printit);
527     } else {
528
529         /* XXX db3 does DB_FIRST on uninitialized cursor */
530         rc = db3c_get(dbi, dbcursor, &key, &data,
531                 key.data == NULL ? DB_NEXT : DB_SET);
532
533     }
534
535     if (rc == 0) {
536         /*@-onlytrans@*/
537         if (keyp)       *keyp = key.data;
538         if (keylen)     *keylen = key.size;
539         if (datap)      *datap = data.data;
540         if (datalen)    *datalen = data.size;
541         /*@=onlytrans@*/
542     }
543
544     /*@-compmempass -nullstate@*/
545     return rc;
546     /*@=compmempass =nullstate@*/
547 }
548
549 static int db3ccount(dbiIndex dbi, DBC * dbcursor,
550                 /*@null@*/ /*@out@*/ unsigned int * countp,
551                 /*@unused@*/ unsigned int flags)
552         /*@modifies *countp, fileSystem @*/
553 {
554     db_recno_t count = 0;
555     int rc = 0;
556
557     flags = 0;
558     rc = dbcursor->c_count(dbcursor, &count, flags);
559     rc = cvtdberr(dbi, "dbcursor->c_count", rc, _debug);
560     if (rc) return rc;
561     if (countp) *countp = count;
562
563     return rc;
564 }
565
566 static int db3byteswapped(dbiIndex dbi) /*@*/
567 {
568     DB * db = dbi->dbi_db;
569     int rc = 0;
570
571     if (db != NULL) {
572 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3 && DB_VERSION_PATCH == 11
573         int isswapped = 0;
574         rc = db->get_byteswapped(db, &isswapped);
575         if (rc == 0)
576             rc = isswapped;
577 #else
578         rc = db->get_byteswapped(db);
579 #endif
580     }
581
582     return rc;
583 }
584
585 static int db3stat(dbiIndex dbi, unsigned int flags)
586         /*@modifies dbi, fileSystem @*/
587 {
588     DB * db = dbi->dbi_db;
589     int rc = 0;
590
591     if (db == NULL) return -2;
592 #if defined(DB_FAST_STAT)
593     if (flags)
594         flags = DB_FAST_STAT;
595     else
596 #endif
597         flags = 0;
598     dbi->dbi_stats = _free(dbi->dbi_stats);
599 /* XXX 3.3.4 change. */
600 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3
601     rc = db->stat(db, &dbi->dbi_stats, flags);
602 #else
603     rc = db->stat(db, &dbi->dbi_stats, NULL, flags);
604 #endif
605     rc = cvtdberr(dbi, "db->stat", rc, _debug);
606     return rc;
607 }
608
609 /** @todo Add/use per-rpmdb verify_on_close. */
610 static int db3close(/*@only@*/ dbiIndex dbi, /*@unused@*/ unsigned int flags)
611         /*@modifies dbi, fileSystem @*/
612 {
613     rpmdb rpmdb = dbi->dbi_rpmdb;
614     const char * urlfn = NULL;
615     const char * root;
616     const char * home;
617     const char * dbhome;
618     const char * dbfile;
619     const char * dbsubfile;
620     DB * db = dbi->dbi_db;
621     int rc = 0, xx;
622
623     flags = 0;  /* XXX unused */
624
625     /*
626      * Get the prefix/root component and directory path.
627      */
628     root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
629     if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
630         root = NULL;
631     home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);
632
633     /*
634      * Either the root or directory components may be a URL. Concatenate,
635      * convert the URL to a path, and add the name of the file.
636      */
637     urlfn = rpmGenPath(root, home, NULL);
638     (void) urlPath(urlfn, &dbhome);
639     if (dbi->dbi_temporary) {
640         dbfile = NULL;
641         dbsubfile = NULL;
642     } else {
643 #ifdef  HACK
644         dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
645         dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile : tagName(dbi->dbi_rpmtag));
646 #else
647         dbfile = (dbi->dbi_file ? dbi->dbi_file : tagName(dbi->dbi_rpmtag));
648         dbsubfile = NULL;
649 #endif
650     }
651
652     if (dbi->dbi_rmw)
653         rc = db3cclose(dbi, NULL, 0);
654
655     if (db) {
656         rc = db->close(db, 0);
657         rc = cvtdberr(dbi, "db->close", rc, _debug);
658         db = dbi->dbi_db = NULL;
659
660         rpmMessage(RPMMESS_DEBUG, _("closed   db index       %s/%s\n"),
661                 dbhome, (dbfile ? dbfile : tagName(dbi->dbi_rpmtag)));
662
663     }
664
665     dbi->dbi_dbinfo = _free(dbi->dbi_dbinfo);
666
667     if (dbi->dbi_use_dbenv) {
668         /*@-nullstate@*/
669         xx = db_fini(dbi, (dbhome ? dbhome : ""), dbfile, dbsubfile);
670         /*@=nullstate@*/
671     }
672
673     if (dbi->dbi_verify_on_close && !dbi->dbi_temporary) {
674         DB_ENV * dbenv = NULL;
675
676         rc = db_env_create(&dbenv, 0);
677         rc = cvtdberr(dbi, "db_env_create", rc, _debug);
678         if (rc || dbenv == NULL) goto exit;
679
680         if (dbi->dbi_tmpdir) {
681             const char * tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
682             rc = dbenv->set_tmp_dir(dbenv, tmpdir);
683             rc = cvtdberr(dbi, "dbenv->set_tmp_dir", rc, _debug);
684             tmpdir = _free(tmpdir);
685             if (rc) goto exit;
686         }
687             
688         rc = dbenv->open(dbenv, dbhome,
689             DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
690         rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
691         if (rc) goto exit;
692
693         rc = db_create(&db, dbenv, 0);
694         rc = cvtdberr(dbi, "db_create", rc, _debug);
695
696         if (db != NULL) {
697                 const char * dbf = rpmGetPath(dbhome, "/", dbfile, NULL);
698
699                 rc = db->verify(db, dbf, NULL, NULL, flags);
700                 rc = cvtdberr(dbi, "db->verify", rc, _debug);
701
702                 rpmMessage(RPMMESS_DEBUG, _("verified db index       %s/%s\n"),
703                         (dbhome ? dbhome : ""),
704                         (dbfile ? dbfile : tagName(dbi->dbi_rpmtag)));
705
706                 xx = db->close(db, 0);
707                 xx = cvtdberr(dbi, "db->close", xx, _debug);
708                 db = NULL;
709                 if (rc == 0 && xx) rc = xx;
710
711                 dbf = _free(dbf);
712         }
713         xx = dbenv->close(dbenv, 0);
714         xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
715         if (rc == 0 && xx) rc = xx;
716     }
717
718 exit:
719     dbi->dbi_db = NULL;
720
721     urlfn = _free(urlfn);
722
723     dbi = db3Free(dbi);
724
725     return rc;
726 }
727
728 static int db3open(/*@keep@*/ rpmdb rpmdb, int rpmtag, dbiIndex * dbip)
729         /*@modifies *dbip, fileSystem @*/
730 {
731     /*@-nestedextern@*/
732     extern struct _dbiVec db3vec;
733     /*@=nestedextern@*/
734     const char * urlfn = NULL;
735     const char * root;
736     const char * home;
737     const char * dbhome;
738     const char * dbfile;
739     const char * dbsubfile;
740     dbiIndex dbi = NULL;
741     int rc = 0;
742     int xx;
743
744     DB * db = NULL;
745     DB_ENV * dbenv = NULL;
746     DB_TXN * txnid = NULL;
747     u_int32_t oflags;
748     int _printit;
749
750     if (dbip)
751         *dbip = NULL;
752
753     /*
754      * Parse db configuration parameters.
755      */
756     if ((dbi = db3New(rpmdb, rpmtag)) == NULL)
757         /*@-nullstate@*/
758         return 1;
759         /*@=nullstate@*/
760     dbi->dbi_api = DB_VERSION_MAJOR;
761
762     /*
763      * Get the prefix/root component and directory path.
764      */
765     root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
766     if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
767         root = NULL;
768     home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);
769
770     /*
771      * Either the root or directory components may be a URL. Concatenate,
772      * convert the URL to a path, and add the name of the file.
773      */
774     urlfn = rpmGenPath(root, home, NULL);
775     (void) urlPath(urlfn, &dbhome);
776     if (dbi->dbi_temporary) {
777         dbfile = NULL;
778         dbsubfile = NULL;
779     } else {
780 #ifdef  HACK
781         dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
782         dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile : tagName(dbi->dbi_rpmtag));
783 #else
784         dbfile = (dbi->dbi_file ? dbi->dbi_file : tagName(dbi->dbi_rpmtag));
785         dbsubfile = NULL;
786 #endif
787     }
788
789     oflags = (dbi->dbi_oeflags | dbi->dbi_oflags);
790     oflags &= ~DB_TRUNCATE;     /* XXX this is dangerous */
791
792 #if 0   /* XXX rpmdb: illegal flag combination specified to DB->open */
793     if ( dbi->dbi_mode & O_EXCL) oflags |= DB_EXCL;
794 #endif
795
796     /*
797      * Map open mode flags onto configured database/environment flags.
798      */
799     if (dbi->dbi_temporary) {
800         oflags |= DB_CREATE;
801         dbi->dbi_oeflags |= DB_CREATE;
802         oflags &= ~DB_RDONLY;
803         dbi->dbi_oflags &= ~DB_RDONLY;
804     } else {
805         if (!(dbi->dbi_mode & (O_RDWR|O_WRONLY))) oflags |= DB_RDONLY;
806         if (dbi->dbi_mode & O_CREAT) {
807             oflags |= DB_CREATE;
808             dbi->dbi_oeflags |= DB_CREATE;
809         }
810 #ifdef  DANGEROUS
811         if ( dbi->dbi_mode & O_TRUNC) oflags |= DB_TRUNCATE;
812 #endif
813     }
814
815     /*
816      * Avoid incompatible DB_CREATE/DB_RDONLY flags on DBENV->open.
817      */
818     if (dbi->dbi_use_dbenv) {
819         if (access(dbhome, W_OK) == -1) {
820
821             /* dbhome is unwritable, don't attempt DB_CREATE on DB->open ... */
822             oflags &= ~DB_CREATE;
823
824             /* ... but DBENV->open might still need DB_CREATE ... */
825             if (dbi->dbi_eflags & DB_PRIVATE) {
826                 dbi->dbi_eflags &= ~DB_JOINENV;
827             } else {
828                 dbi->dbi_eflags |= DB_JOINENV;
829                 dbi->dbi_oeflags &= ~DB_CREATE;
830                 dbi->dbi_oeflags &= ~DB_THREAD;
831                 /* ... but, unless DB_PRIVATE is used, skip DBENV. */
832                 dbi->dbi_use_dbenv = 0;
833             }
834
835             /* ... DB_RDONLY maps dphome perms across files ...  */
836             if (dbi->dbi_temporary) {
837                 oflags |= DB_CREATE;
838                 dbi->dbi_oeflags |= DB_CREATE;
839                 oflags &= ~DB_RDONLY;
840                 dbi->dbi_oflags &= ~DB_RDONLY;
841             } else {
842                 oflags |= DB_RDONLY;
843                 /* ... and DB_WRITECURSOR won't be needed ...  */
844                 dbi->dbi_oflags |= DB_RDONLY;
845             }
846
847         } else {        /* dbhome is writable, check for persistent dbenv. */
848             const char * dbf = rpmGetPath(dbhome, "/__db.001", NULL);
849
850             if (access(dbf, F_OK) == -1) {
851                 /* ... non-existent (or unwritable) DBENV, will create ... */
852                 dbi->dbi_oeflags |= DB_CREATE;
853                 dbi->dbi_eflags &= ~DB_JOINENV;
854             } else {
855                 /* ... pre-existent (or bogus) DBENV, will join ... */
856                 dbi->dbi_oeflags &= ~DB_CREATE;
857                 dbi->dbi_eflags |= DB_JOINENV;
858             }
859             dbf = _free(dbf);
860         }
861     }
862
863     /*
864      * Avoid incompatible DB_CREATE/DB_RDONLY flags on DB->open.
865      */
866     if ((oflags & DB_CREATE) && (oflags & DB_RDONLY)) {
867         /* dbhome is writable, and DB->open flags may conflict. */
868         const char * dbfn = (dbfile ? dbfile : tagName(dbi->dbi_rpmtag));
869         const char * dbf = rpmGetPath(dbhome, "/", dbfn, NULL);
870
871         if (access(dbf, F_OK) == -1) {
872             /* File does not exist, DB->open might create ... */
873             oflags &= ~DB_RDONLY;
874         } else {
875             /* File exists, DB->open need not create ... */
876             oflags &= ~DB_CREATE;
877         }
878
879         /* Only writers need DB_WRITECURSOR ... */
880         if (!(oflags & DB_RDONLY) && access(dbf, W_OK) == 0) {
881             dbi->dbi_oflags &= ~DB_RDONLY;
882         } else {
883             dbi->dbi_oflags |= DB_RDONLY;
884         }
885         dbf = _free(dbf);
886     }
887
888     /*
889      * Turn off verify-on-close if opening read-only.
890      */
891     if (oflags & DB_RDONLY)
892         dbi->dbi_verify_on_close = 0;
893
894     dbi->dbi_dbinfo = NULL;
895
896     if (dbi->dbi_use_dbenv)
897         rc = db_init(dbi, dbhome, dbfile, dbsubfile, &dbenv);
898
899     rpmMessage(RPMMESS_DEBUG, _("opening  db index       %s/%s %s mode=0x%x\n"),
900                 dbhome, (dbfile ? dbfile : tagName(dbi->dbi_rpmtag)),
901                 prDbiOpenFlags(oflags, 0), dbi->dbi_mode);
902
903     if (rc == 0) {
904         static int _lockdbfd = 0;
905
906         rc = db_create(&db, dbenv, dbi->dbi_cflags);
907         rc = cvtdberr(dbi, "db_create", rc, _debug);
908         if (rc == 0 && db != NULL) {
909             if (rc == 0 && dbi->dbi_lorder) {
910                 rc = db->set_lorder(db, dbi->dbi_lorder);
911                 rc = cvtdberr(dbi, "db->set_lorder", rc, _debug);
912             }
913             if (rc == 0 && dbi->dbi_cachesize) {
914                 rc = db->set_cachesize(db, 0, dbi->dbi_cachesize, 0);
915                 rc = cvtdberr(dbi, "db->set_cachesize", rc, _debug);
916             }
917             if (rc == 0 && dbi->dbi_pagesize) {
918                 rc = db->set_pagesize(db, dbi->dbi_pagesize);
919                 rc = cvtdberr(dbi, "db->set_pagesize", rc, _debug);
920             }
921 /* XXX 3.3.4 change. */
922 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3
923             if (rc == 0 &&
924                         rpmdb->db_malloc && rpmdb->db_realloc && rpmdb->db_free)
925             {
926                 rc = db->set_alloc(db,
927                         rpmdb->db_malloc, rpmdb->db_realloc, rpmdb->db_free);
928                 rc = cvtdberr(dbi, "db->set_alloc", rc, _debug);
929             }
930 #else
931             if (rc == 0 && rpmdb->db_malloc) {
932                 rc = db->set_malloc(db, rpmdb->db_malloc);
933                 rc = cvtdberr(dbi, "db->set_malloc", rc, _debug);
934             }
935 #endif
936             if (rc == 0 && oflags & DB_CREATE) {
937                 switch(dbi->dbi_type) {
938                 default:
939                 case DB_HASH:
940                     if (dbi->dbi_h_ffactor) {
941                         rc = db->set_h_ffactor(db, dbi->dbi_h_ffactor);
942                         rc = cvtdberr(dbi, "db->set_h_ffactor", rc, _debug);
943                         if (rc) break;
944                     }
945                     if (dbi->dbi_h_nelem) {
946                         rc = db->set_h_nelem(db, dbi->dbi_h_nelem);
947                         rc = cvtdberr(dbi, "db->set_h_nelem", rc, _debug);
948                         if (rc) break;
949                     }
950                     if (dbi->dbi_h_flags) {
951                         rc = db->set_flags(db, dbi->dbi_h_flags);
952                         rc = cvtdberr(dbi, "db->set_h_flags", rc, _debug);
953                         if (rc) break;
954                     }
955 /* XXX db-3.2.9 has added a DB arg to the call. */
956 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR > 2
957                     if (dbi->dbi_h_hash_fcn) {
958                         rc = db->set_h_hash(db, dbi->dbi_h_hash_fcn);
959                         rc = cvtdberr(dbi, "db->set_h_hash", rc, _debug);
960                         if (rc) break;
961                     }
962                     if (dbi->dbi_h_dup_compare_fcn) {
963                         rc = db->set_dup_compare(db, dbi->dbi_h_dup_compare_fcn);
964                         rc = cvtdberr(dbi, "db->set_dup_compare", rc, _debug);
965                         if (rc) break;
966                     }
967 #endif
968                     break;
969                 case DB_BTREE:
970                     if (dbi->dbi_bt_flags) {
971                         rc = db->set_flags(db, dbi->dbi_bt_flags);
972                         rc = cvtdberr(dbi, "db->set_bt_flags", rc, _debug);
973                         if (rc) break;
974                     }
975                     if (dbi->dbi_bt_minkey) {
976                         rc = db->set_bt_minkey(db, dbi->dbi_bt_minkey);
977                         rc = cvtdberr(dbi, "db->set_bt_minkey", rc, _debug);
978                         if (rc) break;
979                     }
980 /* XXX db-3.2.9 has added a DB arg to the call. */
981 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR > 2
982                     if (dbi->dbi_bt_compare_fcn) {
983                         rc = db->set_bt_compare(db, dbi->dbi_bt_compare_fcn);
984                         rc = cvtdberr(dbi, "db->set_bt_compare", rc, _debug);
985                         if (rc) break;
986                     }
987                     if (dbi->dbi_bt_dup_compare_fcn) {
988                         rc = db->set_dup_compare(db, dbi->dbi_bt_dup_compare_fcn);
989                         rc = cvtdberr(dbi, "db->set_dup_compare", rc, _debug);
990                         if (rc) break;
991                     }
992                     if (dbi->dbi_bt_prefix_fcn) {
993                         rc = db->set_bt_prefix(db, dbi->dbi_bt_prefix_fcn);
994                         rc = cvtdberr(dbi, "db->set_bt_prefix", rc, _debug);
995                         if (rc) break;
996                     }
997 #endif
998                     break;
999                 case DB_RECNO:
1000                     if (dbi->dbi_re_delim) {
1001                         rc = db->set_re_delim(db, dbi->dbi_re_delim);
1002                         rc = cvtdberr(dbi, "db->set_re_selim", rc, _debug);
1003                         if (rc) break;
1004                     }
1005                     if (dbi->dbi_re_len) {
1006                         rc = db->set_re_len(db, dbi->dbi_re_len);
1007                         rc = cvtdberr(dbi, "db->set_re_len", rc, _debug);
1008                         if (rc) break;
1009                     }
1010                     if (dbi->dbi_re_pad) {
1011                         rc = db->set_re_pad(db, dbi->dbi_re_pad);
1012                         rc = cvtdberr(dbi, "db->set_re_pad", rc, _debug);
1013                         if (rc) break;
1014                     }
1015                     if (dbi->dbi_re_source) {
1016                         rc = db->set_re_source(db, dbi->dbi_re_source);
1017                         rc = cvtdberr(dbi, "db->set_re_source", rc, _debug);
1018                         if (rc) break;
1019                     }
1020                     break;
1021                 case DB_QUEUE:
1022                     if (dbi->dbi_q_extentsize) {
1023                         rc = db->set_q_extentsize(db, dbi->dbi_q_extentsize);
1024                         rc = cvtdberr(dbi, "db->set_q_extentsize", rc, _debug);
1025                         if (rc) break;
1026                     }
1027                     break;
1028                 }
1029             }
1030             dbi->dbi_dbinfo = NULL;
1031
1032             if (rc == 0) {
1033                 const char * dbfullpath;
1034                 const char * dbpath;
1035                 char * t;
1036                 int nb;
1037
1038                 nb = strlen(dbhome);
1039                 if (dbfile)     nb += 1 + strlen(dbfile);
1040                 dbfullpath = t = alloca(nb + 1);
1041
1042                 t = stpcpy(t, dbhome);
1043                 if (dbfile)
1044                     t = stpcpy( stpcpy( t, "/"), dbfile);
1045                 dbpath = (!dbi->dbi_use_dbenv && !dbi->dbi_temporary)
1046                         ? dbfullpath : dbfile;
1047
1048                 rc = db->open(db, dbpath, dbsubfile,
1049                     dbi->dbi_type, oflags, dbi->dbi_perms);
1050
1051                 if (rc == 0 && dbi->dbi_type == DB_UNKNOWN) {
1052 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3 && DB_VERSION_PATCH == 11
1053                     DBTYPE dbi_type = DB_UNKNOWN;
1054                     xx = db->get_type(db, &dbi_type);
1055                     if (xx == 0)
1056                         dbi->dbi_type = dbi_type;
1057 #else
1058                     dbi->dbi_type = db->get_type(db);
1059 #endif
1060                 }
1061             }
1062
1063             /* XXX return rc == errno without printing */
1064             _printit = (rc > 0 ? 0 : _debug);
1065             xx = cvtdberr(dbi, "db->open", rc, _printit);
1066
1067             if (rc == 0 && dbi->dbi_use_dbenv
1068             && (dbi->dbi_eflags & DB_INIT_CDB) && dbi->dbi_get_rmw_cursor)
1069             {
1070                 DBC * dbcursor = NULL;
1071                 xx = db->cursor(db, txnid, &dbcursor,
1072                         ((oflags & DB_RDONLY) ? 0 : DB_WRITECURSOR));
1073                 xx = cvtdberr(dbi, "db->cursor", xx, _debug);
1074                 dbi->dbi_rmw = dbcursor;
1075             } else
1076                 dbi->dbi_rmw = NULL;
1077
1078             /*
1079              * Lock a file using fcntl(2). Traditionally this is Packages,
1080              * the file used * to store metadata of installed header(s),
1081              * as Packages is always opened, and should be opened first,
1082              * for any rpmdb access.
1083              *
1084              * If no DBENV is used, then access is protected with a
1085              * shared/exclusive locking scheme, as always.
1086              *
1087              * With a DBENV, the fcntl(2) lock is necessary only to keep
1088              * the riff-raff from playing where they don't belong, as
1089              * the DBENV should provide it's own locking scheme. So try to
1090              * acquire a lock, but permit failures, as some other
1091              * DBENV player may already have acquired the lock.
1092              */
1093             if (rc == 0 && dbi->dbi_lockdbfd &&
1094                 (!dbi->dbi_use_dbenv || _lockdbfd++ == 0))
1095             {
1096                 int fdno = -1;
1097
1098                 if (!(db->fd(db, &fdno) == 0 && fdno >= 0)) {
1099                     rc = 1;
1100                 } else {
1101                     struct flock l;
1102                     memset(&l, 0, sizeof(l));
1103                     l.l_whence = 0;
1104                     l.l_start = 0;
1105                     l.l_len = 0;
1106                     l.l_type = (dbi->dbi_mode & (O_RDWR|O_WRONLY))
1107                                 ? F_WRLCK : F_RDLCK;
1108                     l.l_pid = 0;
1109
1110                     rc = fcntl(fdno, F_SETLK, (void *) &l);
1111                     if (rc) {
1112                         /* Warning only if using CDB locking. */
1113                         rc = ((dbi->dbi_use_dbenv &&
1114                                 (dbi->dbi_eflags & DB_INIT_CDB))
1115                             ? 0 : 1);
1116                         rpmError( (rc ? RPMERR_FLOCK : RPMWARN_FLOCK),
1117                                 _("cannot get %s lock on %s/%s\n"),
1118                                 ((dbi->dbi_mode & (O_RDWR|O_WRONLY))
1119                                         ? _("exclusive") : _("shared")),
1120                                 dbhome, (dbfile ? dbfile : ""));
1121                     } else if (dbfile) {
1122                         rpmMessage(RPMMESS_DEBUG,
1123                                 _("locked   db index       %s/%s\n"),
1124                                 dbhome, dbfile);
1125                     }
1126                 }
1127             }
1128         }
1129     }
1130
1131     dbi->dbi_db = db;
1132     dbi->dbi_dbenv = dbenv;
1133
1134     if (rc == 0 && dbi->dbi_db != NULL && dbip != NULL) {
1135         dbi->dbi_vec = &db3vec;
1136         *dbip = dbi;
1137     } else {
1138         dbi->dbi_verify_on_close = 0;
1139         (void) db3close(dbi, 0);
1140     }
1141
1142     urlfn = _free(urlfn);
1143
1144     /*@-nullstate@*/
1145     return rc;
1146     /*@=nullstate@*/
1147 }
1148
1149 /** \ingroup db3
1150  */
1151 /*@-exportheadervar@*/
1152 struct _dbiVec db3vec = {
1153     DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH,
1154     db3open, db3close, db3sync, db3copen, db3cclose, db3cdel, db3cget, db3cput,
1155     db3ccount, db3byteswapped, db3stat
1156 };
1157 /*@=exportheadervar@*/