File: dbif.c

package info (click to toggle)
netatalk 3.1.12~ds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,756 kB
  • sloc: ansic: 104,976; sh: 8,247; xml: 7,394; perl: 1,936; makefile: 1,430; python: 1,342; yacc: 309; lex: 49
file content (1327 lines) | stat: -rw-r--r-- 40,325 bytes parent folder | download | duplicates (2)
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
/*
 * Copyright (C) Joerg Lenneis 2003
 * Copyright (C) Frank Lahm 2009
 * All Rights Reserved.  See COPYING.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <arpa/inet.h>

#include <db.h>

#include <atalk/logger.h>
#include <atalk/util.h>
#include <atalk/errchk.h>
#include <atalk/cnid.h>

#include "db_param.h"
#include "dbif.h"
#include "pack.h"

#define DB_ERRLOGFILE "db_errlog"

/*!
 * Get the db stamp which is the st_ctime of the file "cnid2.db" and store it in buffer
 */
static int dbif_stamp(DBD *dbd, void *buffer, int size)
{
    EC_INIT;
    struct stat st;
    int cwd = -1;

    if (size < 8)
        EC_FAIL;

    /* Remember cwd */
    if ((cwd = open(".", O_RDONLY)) < 0) {
        LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
        EC_FAIL;
    }

    /* chdir to db_envhome */
    if ((chdir(dbd->db_envhome)) != 0) {
        LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));        
        EC_FAIL;
    }

    if (stat(dbd->db_table[DBIF_CNID].name, &st) < 0) {
        LOG(log_error, logtype_cnid, "error stating database %s: %s", dbd->db_table[DBIF_CNID].name, db_strerror(errno));
        EC_FAIL;
    }

    LOG(log_maxdebug, logtype_cnid,"stamp: %s", asctime(localtime(&st.st_ctime)));

    memset(buffer, 0, size);
    memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));

EC_CLEANUP:
    if (cwd != -1) {
        if (fchdir(cwd) != 0) {
            LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
            EC_STATUS(-1);
        }
        close(cwd);
    }
    EC_EXIT;
}

/*!
 * Inititialize rootinfo key (which has CNID 0 as key)
 *
 * This also "stamps" the database, which means storing st.st_ctime of the
 * "cnid2.db" file in the rootinfo data at the DEV offset
 *
 * @param dbd      (rw) database handle
 * @param version  (r)  database version number
 *
 * @returns -1 on error, 0 on success
 */
static int dbif_init_rootinfo(DBD *dbd, int version)
{
    DBT key, data;
    uint32_t uint32;
    char buf[ROOTINFO_DATALEN];

    LOG(log_debug, logtype_cnid, "Setting CNID database version to %u", version);


    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    key.data = ROOTINFO_KEY;
    key.size = ROOTINFO_KEYLEN;
    data.data = buf;
    data.size = ROOTINFO_DATALEN;

    memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);

    uint32 = htonl(CNID_START - 1);
    memcpy(buf + CNID_TYPE_OFS, &uint32, sizeof(uint32));

    uint32 = htonl(version);
    memcpy(buf + CNID_DID_OFS, &uint32, sizeof(uint32));

    if (dbif_stamp(dbd, buf + CNID_DEV_OFS, CNID_DEV_LEN) < 0)
        return -1;

    if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0)
        return -1;
    if (dbif_txn_commit(dbd) != 1) {
        LOG(log_error, logtype_cnid, "dbif_init_rootinfo: can't commit txn");
        return -1;
    }

    return 0;
}

/*!
 * Return CNID database version number
 *
 * Returns version in *version
 *
 * @returns -1 on error, 0 if theres no rootinfo key yet, 1 if *version is returned
 */
static int dbif_getversion(DBD *dbd, uint32_t *version)
{
    DBT key, data;
    int ret;

    LOG(log_maxdebug, logtype_cnid, "dbif_getversion: reading version info");

    *version = -1;
    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    key.data = ROOTINFO_KEY;
    key.size = ROOTINFO_KEYLEN;

    switch (dbif_get(dbd, DBIF_CNID, &key, &data, 0)) {
    case 1: /* found */
        memcpy(version, (char *)data.data + CNID_DID_OFS, sizeof(uint32_t));
        *version = ntohl(*version);
        LOG(log_debug, logtype_cnid, "CNID database version %u", *version);
        ret = 1;
        break;
    case 0: /* not found */
        LOG(log_debug, logtype_cnid, "dbif_getversion: no version info found");
        ret = 0;
        break;
    default:
        LOG(log_error, logtype_cnid, "dbif_getversion: database error");
        ret = -1;
        break;
    }

    return ret;
}

/*!
 * Set CNID database version number
 *
 * Initializes rootinfo key as neccessary
 * @returns -1 on error, 0 on success
 */
static int dbif_setversion(DBD *dbd, uint32_t version)
{
    int ret;
    DBT key, data;
    uint32_t v;

    LOG(log_debug, logtype_cnid, "Setting CNID database version to %u", version);

    v = version;
    v = htonl(v);

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    key.data = ROOTINFO_KEY;
    key.size = ROOTINFO_KEYLEN;

    if ((ret = dbif_get(dbd, DBIF_CNID, &key, &data, 0)) == -1)
        return -1;
    if (ret == 0) {
        /* No rootinfo key yet, init it */
        if (dbif_init_rootinfo(dbd, CNID_VERSION) != 0)
            return -1;
        /* Now try again */
        if (dbif_get(dbd, DBIF_CNID, &key, &data, 0) == -1)
            return -1;
    }
    memcpy((char *)data.data + CNID_DID_OFS, &v, sizeof(v));
    data.size = ROOTINFO_DATALEN;
    if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0)
        return -1;

    return 0;
}

/*!
 * Upgrade CNID database versions, initialize rootinfo key as as necessary in dbif_setversion()
 *
 * For now this does nothing, as upgrading from ver. 0 to 1 is done in dbif_open
 */
#define UNINTIALIZED_DB UINT32_MAX
static int dbif_upgrade(DBD *dbd)
{
    uint32_t version = CNID_VERSION_UNINTIALIZED_DB;

    if (dbif_getversion(dbd, &version) == -1)
        return -1;
    if (version == CNID_VERSION_UNINTIALIZED_DB) {
        version = CNID_VERSION;
        if (dbif_setversion(dbd, CNID_VERSION) != 0)
            return -1;
    }

    /* 
     * Do upgrade stuff ...
     */

    /* Write current version to database */
    if (version != CNID_VERSION) {
        if (dbif_setversion(dbd, CNID_VERSION) != 0)
            return -1;
    }

    LOG(log_debug, logtype_cnid, "Finished CNID database version upgrade check");

    return 0;
}

/* --------------- */
static int dbif_openlog(DBD *dbd)
{
    int ret = 0;
    int cwd = -1;

    if ( ! dbd->db_filename)
        /* in memory db */
        return 0;

    /* Remember cwd */
    if ((cwd = open(".", O_RDONLY)) < 0) {
        LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
        return -1;
    }

    /* chdir to db_envhome */
    if ((chdir(dbd->db_envhome)) != 0) {
        LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));        
        ret = -1;
        goto exit;
    }

    if ((dbd->db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
        LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));

    if (dbd->db_errlog != NULL) {
        dbd->db_env->set_errfile(dbd->db_env, dbd->db_errlog);
        dbd->db_env->set_msgfile(dbd->db_env, dbd->db_errlog);
    }

exit:
    if (cwd != -1) {
        if ((fchdir(cwd)) != 0) {
            LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
            ret = -1;
        }
        close(cwd);
    }
    return ret;
}

/* --------------- */
static int dbif_logautorem(DBD *dbd)
{
    int ret = 0;
    int cwd = -1;
    char **logfiles = NULL;
    char **file;

    if ( ! dbd->db_filename)
        /* in memory db */
        return 0;

    /* Remember cwd */
    if ((cwd = open(".", O_RDONLY)) < 0) {
        LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
        return -1;
    }

    /* chdir to db_envhome */
    if ((chdir(dbd->db_envhome)) != 0) {
        LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));        
        ret = -1;
        goto exit;
    }

    if ((ret = dbd->db_env->log_archive(dbd->db_env, &logfiles, 0)) != 0) {
        LOG(log_error, logtype_cnid, "error getting list of stale logfiles: %s",
            db_strerror(ret));
        dbd->db_env->close(dbd->db_env, 0);
        dbd->db_env = NULL;
        ret = -1;
        goto exit;
    }

    if (logfiles != NULL) {
        for (file = logfiles; *file != NULL; file++) {
            if (unlink(*file) < 0)
                LOG(log_warning, logtype_cnid, "Error removing stale logfile %s: %s", *file, strerror(errno));
        }
        free(logfiles);
    }

exit:
    if (cwd != -1) {
        if ((fchdir(cwd)) != 0) {
            LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
            ret = -1;
        }
        close(cwd);
    }
    return ret;
}

/* --------------- */
DBD *dbif_init(const char *envhome, const char *filename)
{
    DBD *dbd;

    if ( NULL == (dbd = calloc(sizeof(DBD), 1)) )
        return NULL;

    /* filename == NULL means in memory db */
    if (filename) {
        if (! envhome)
            return NULL;

        dbd->db_envhome = strdup(envhome);
        if (NULL == dbd->db_envhome) {
            free(dbd);
            return NULL;
        }

        dbd->db_filename = strdup(filename);
        if (NULL == dbd->db_filename) {
            free(dbd->db_envhome);
            free(dbd);
            return NULL;
        }
    }
    
    dbd->db_table[DBIF_CNID].name        = "cnid2.db";
    dbd->db_table[DBIF_IDX_DEVINO].name  = "devino.db";
    dbd->db_table[DBIF_IDX_DIDNAME].name = "didname.db";
    dbd->db_table[DBIF_IDX_NAME].name    = "name.db";

    dbd->db_table[DBIF_CNID].type        = DB_BTREE;
    dbd->db_table[DBIF_IDX_DEVINO].type  = DB_BTREE;
    dbd->db_table[DBIF_IDX_DIDNAME].type = DB_BTREE;
    dbd->db_table[DBIF_IDX_NAME].type    = DB_BTREE;

    dbd->db_table[DBIF_CNID].openflags        = DB_CREATE;
    dbd->db_table[DBIF_IDX_DEVINO].openflags  = DB_CREATE;
    dbd->db_table[DBIF_IDX_DIDNAME].openflags = DB_CREATE;
    dbd->db_table[DBIF_IDX_NAME].openflags    = DB_CREATE;

    dbd->db_table[DBIF_IDX_NAME].flags = DB_DUPSORT;

    return dbd;
}

/* 
   We must open the db_env with an absolute pathname, as `dbd` keeps chdir'ing, which
   breaks e.g. bdb logfile-rotation with relative pathnames.
   But still we use relative paths with DB_ERRLOGFILE
   in order to avoid creating absolute paths by copying. Both have no problem with
   a relative path.
*/
int dbif_env_open(DBD *dbd, struct db_param *dbp, uint32_t dbenv_oflags)
{
    int ret;

    if ((ret = db_env_create(&dbd->db_env, 0))) {
        LOG(log_error, logtype_cnid, "error creating DB environment: %s",
            db_strerror(ret));
        dbd->db_env = NULL;
        return -1;
    }

    dbd->db_param = *dbp;

    if ((dbif_openlog(dbd)) != 0)
        return -1;

    if (dbenv_oflags & DB_RECOVER) {

        LOG(log_debug, logtype_cnid, "Running recovery");

        dbd->db_env->set_verbose(dbd->db_env, DB_VERB_RECOVERY, 1);
        /* Open the database for recovery using DB_PRIVATE option which is faster */
        if ((ret = dbd->db_env->open(dbd->db_env, dbd->db_envhome, dbenv_oflags | DB_PRIVATE, 0))) {
            LOG(log_error, logtype_cnid, "error opening DB environment: %s",
                db_strerror(ret));
            dbd->db_env->close(dbd->db_env, 0);
            dbd->db_env = NULL;
            return -1;
        }
        dbenv_oflags = (dbenv_oflags & ~DB_RECOVER);

        if (dbd->db_errlog != NULL)
            fflush(dbd->db_errlog);

        if ((ret = dbd->db_env->close(dbd->db_env, 0))) {
            LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s",
                db_strerror(ret));
            dbd->db_env = NULL;
            return -1;
        }
        dbd->db_errlog = NULL;        

        if ((ret = db_env_create(&dbd->db_env, 0))) {
            LOG(log_error, logtype_cnid, "error creating DB environment after recovery: %s",
                db_strerror(ret));
            dbd->db_env = NULL;
            return -1;
        }

        if ((dbif_openlog(dbd)) != 0)
            return -1;

        LOG(log_debug, logtype_cnid, "Finished recovery.");
    }

    if ((ret = dbd->db_env->set_cachesize(dbd->db_env, 0, 1024 * dbp->cachesize, 0))) {
        LOG(log_error, logtype_cnid, "error setting DB environment cachesize to %i: %s",
            dbp->cachesize, db_strerror(ret));
        dbd->db_env->close(dbd->db_env, 0);
        dbd->db_env = NULL;
        return -1;
    }

    if ((ret = dbd->db_env->set_lk_max_locks(dbd->db_env, dbp->maxlocks))) {
        LOG(log_error, logtype_cnid, "error setting DB environment maxlocks to %i: %s",
            10000, db_strerror(ret));
        dbd->db_env->close(dbd->db_env, 0);
        dbd->db_env = NULL;
        return -1;
    }

    if ((ret = dbd->db_env->set_lk_max_objects(dbd->db_env, dbp->maxlockobjs))) {
        LOG(log_error, logtype_cnid, "error setting DB environment max lockobjects to %i: %s",
            10000, db_strerror(ret));
        dbd->db_env->close(dbd->db_env, 0);
        dbd->db_env = NULL;
        return -1;
    }

    if ((ret = dbd->db_env->open(dbd->db_env, dbd->db_envhome, dbenv_oflags, 0))) {
        LOG(log_error, logtype_cnid, "error opening DB environment after recovery: %s",
            db_strerror(ret));
        dbd->db_env->close(dbd->db_env, 0);
        dbd->db_env = NULL;
        return -1;
    }

    if ((ret = dbd->db_env->set_flags(dbd->db_env, DB_AUTO_COMMIT, 1))) {
        LOG(log_error, logtype_cnid, "error setting DB_AUTO_COMMIT flag: %s",
            db_strerror(ret));
        dbd->db_env->close(dbd->db_env, 0);
        dbd->db_env = NULL;
        return -1;
    }

    if (dbp->logfile_autoremove) {
        if ((dbif_logautorem(dbd)) != 0)
            return -1;

#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 7)
        if ((ret = dbd->db_env->log_set_config(dbd->db_env, DB_LOG_AUTO_REMOVE, 1))) {
            LOG(log_error, logtype_cnid, "error setting DB_LOG_AUTO_REMOVE flag: %s",
            db_strerror(ret));
            dbd->db_env->close(dbd->db_env, 0);
            dbd->db_env = NULL;
            return -1;
        }
#else
        if ((ret = dbd->db_env->set_flags(dbd->db_env, DB_LOG_AUTOREMOVE, 1))) {
            LOG(log_error, logtype_cnid, "error setting DB_LOG_AUTOREMOVE flag: %s",
                db_strerror(ret));
            dbd->db_env->close(dbd->db_env, 0);
            dbd->db_env = NULL;
            return -1;
        }
#endif
    }

    return 0;
}

/* --------------- */
int dbif_open(DBD *dbd, struct db_param *dbp, int reindex)
{
    int ret, i, cwd;
    u_int32_t count;
    struct stat st;
    DB *upgrade_db;

    /* Try to upgrade if it's a normal on-disk database */
    if (dbd->db_envhome) {
        /* Remember cwd */
        if ((cwd = open(".", O_RDONLY)) < 0) {
            LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
            return -1;
        }
        
        /* chdir to db_envhome. makes it easier checking for old db files and creating db_errlog file  */
        if ((chdir(dbd->db_envhome)) != 0) {
            LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));        
            return -1;
        }
        
        if ((stat(dbd->db_filename, &st)) == 0) {
            LOG(log_debug, logtype_cnid, "See if we can upgrade the CNID database...");
            if ((ret = db_create(&upgrade_db, dbd->db_env, 0))) {
                LOG(log_error, logtype_cnid, "error creating handle for database: %s", db_strerror(ret));
                return -1;
            }
            if ((ret = upgrade_db->upgrade(upgrade_db, dbd->db_filename, 0))) {
                LOG(log_error, logtype_cnid, "error upgarding database: %s", db_strerror(ret));
                return -1;
            }
            if ((ret = upgrade_db->close(upgrade_db, 0))) {
                LOG(log_error, logtype_cnid, "error closing database: %s", db_strerror(ret));
                return -1;
            }
            if ((ret = dbd->db_env->txn_checkpoint(dbd->db_env, 0, 0, DB_FORCE))) {
                LOG(log_error, logtype_cnid, "error forcing checkpoint: %s", db_strerror(ret));
                return -1;
            }
            LOG(log_debug, logtype_cnid, "Finished BerkeleyBD upgrade check");
        }
        
        if ((fchdir(cwd)) != 0) {
            LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
            return -1;
        }
    }

    /* Now open databases ... */
    for (i = 0; i != DBIF_DB_CNT; i++) {
        if ((ret = db_create(&dbd->db_table[i].db, dbd->db_env, 0))) {
            LOG(log_error, logtype_cnid, "error creating handle for database %s: %s",
                dbd->db_table[i].name, db_strerror(ret));
            return -1;
        }

        if (dbd->db_table[i].flags) {
            if ((ret = dbd->db_table[i].db->set_flags(dbd->db_table[i].db,
                                                      dbd->db_table[i].flags))) {
                LOG(log_error, logtype_cnid, "error setting flags for database %s: %s",
                    dbd->db_table[i].name, db_strerror(ret));
                return -1;
            }
        }

        if ( ! dbd->db_env) {   /* In memory db */
            if ((ret = dbd->db_table[i].db->set_cachesize(dbd->db_table[i].db,
                                                          0,
                                                          dbp->cachesize,
                                                          4)) /* split in 4 memory chunks */
                < 0)  {
                LOG(log_error, logtype_cnid, "error setting cachesize %u KB for database %s: %s",
                    dbp->cachesize / 1024, dbd->db_table[i].name, db_strerror(ret));
                return -1;
            }
        }

        if (dbd->db_table[i].db->open(dbd->db_table[i].db,
                                      dbd->db_txn,
                                      dbd->db_filename,
                                      dbd->db_table[i].name,
                                      dbd->db_table[i].type,
                                      dbd->db_table[i].openflags,
                                      0664) < 0) {
            LOG(log_error, logtype_cnid, "Can't open database");
            return -1;
        }

        if (reindex && i > 0) {
            LOG(log_info, logtype_cnid, "Truncating CNID index.");
            if ((ret = dbd->db_table[i].db->truncate(dbd->db_table[i].db, NULL, &count, 0))) {
                LOG(log_error, logtype_cnid, "error truncating database %s: %s",
                    dbd->db_table[i].name, db_strerror(ret));
                return -1;
            }
        }
    }

    /* TODO: Implement CNID DB versioning info on new databases. */

    /* Associate the secondary with the primary. */
    if (reindex)
        LOG(log_info, logtype_cnid, "Reindexing did/name index...");
    if ((ret = dbd->db_table[0].db->associate(dbd->db_table[DBIF_CNID].db,
                                              dbd->db_txn,
                                              dbd->db_table[DBIF_IDX_DIDNAME].db, 
                                              didname,
                                              (reindex) ? DB_CREATE : 0))
         != 0) {
        LOG(log_error, logtype_cnid, "Failed to associate didname database: %s",db_strerror(ret));
        return -1;
    }
    if (reindex)
        LOG(log_info, logtype_cnid, "... done.");

    if (reindex)
        LOG(log_info, logtype_cnid, "Reindexing dev/ino index...");
    if ((ret = dbd->db_table[0].db->associate(dbd->db_table[0].db, 
                                              dbd->db_txn,
                                              dbd->db_table[DBIF_IDX_DEVINO].db, 
                                              devino,
                                              (reindex) ? DB_CREATE : 0))
        != 0) {
        LOG(log_error, logtype_cnid, "Failed to associate devino database: %s",db_strerror(ret));
        return -1;
    }
    if (reindex)
        LOG(log_info, logtype_cnid, "... done.");

    if (reindex)
        LOG(log_info, logtype_cnid, "Reindexing name index...");

    /*
     * Upgrading from version 0 to 1 requires adding the name index below which
     * must be done by specifying the DB_CREATE flag
     */
    uint32_t version = CNID_VERSION;
    if (dbd->db_envhome && !reindex) {
        if (dbif_getversion(dbd, &version) == -1)
            return -1;
    }

    if ((ret = dbd->db_table[0].db->associate(dbd->db_table[0].db, 
                                              dbd->db_txn,
                                              dbd->db_table[DBIF_IDX_NAME].db, 
                                              idxname,
                                              (reindex
                                               || 
                                               ((CNID_VERSION == CNID_VERSION_1) && (version == CNID_VERSION_0)))
                                              ? DB_CREATE : 0)) != 0) {
        LOG(log_error, logtype_cnid, "Failed to associate name index: %s", db_strerror(ret));
        return -1;
    }
    if (reindex)
        LOG(log_info, logtype_cnid, "... done.");

    if ((dbd->db_envhome) && ((ret = dbif_upgrade(dbd)) != 0)) {
        LOG(log_error, logtype_cnid, "Error upgrading CNID database to version %d", CNID_VERSION);
        return -1;
    }
    
    return 0;
}

/* ------------------------ */
static int dbif_closedb(DBD *dbd)
{
    int i;
    int ret;
    int err = 0;

    for (i = DBIF_DB_CNT -1; i >= 0; i--) {
        if (dbd->db_table[i].db != NULL && (ret = dbd->db_table[i].db->close(dbd->db_table[i].db, 0))) {
            LOG(log_error, logtype_cnid, "error closing database %s: %s", dbd->db_table[i].name, db_strerror(ret));
            err++;
        }
    }
    if (err)
        return -1;
    return 0;
}

/* ------------------------ */
int dbif_close(DBD *dbd)
{
    int ret;
    int err = 0;

    if (dbif_closedb(dbd))
        err++;

    if (dbd->db_env != NULL && (ret = dbd->db_env->close(dbd->db_env, 0))) {
        LOG(log_error, logtype_cnid, "error closing DB environment: %s", db_strerror(ret));
        err++;
    }
    if (dbd->db_errlog != NULL && fclose(dbd->db_errlog) == EOF) {
        LOG(log_error, logtype_cnid, "error closing DB logfile: %s", strerror(errno));
        err++;
    }

    free(dbd->db_filename);
    free(dbd);
    dbd = NULL;

    if (err)
        return -1;
    return 0;
}

/* 
   In order to support silent database upgrades:
   destroy env at cnid_dbd shutdown.
 */
int dbif_env_remove(const char *path)
{
    int ret;
    DBD *dbd;

    LOG(log_debug, logtype_cnid, "Trying to remove BerkeleyDB environment");

    if (NULL == (dbd = dbif_init(path, "cnid2.db")))
        return -1;

    /* Get db_env handle */
    if ((ret = db_env_create(&dbd->db_env, 0))) {
        LOG(log_error, logtype_cnid, "error creating DB environment: %s", db_strerror(ret));
        dbd->db_env = NULL;
        return -1;
    }

    if ((dbif_openlog(dbd)) != 0)
        return -1;

    /* Open environment with recovery */
    if ((ret = dbd->db_env->open(dbd->db_env, 
                                 dbd->db_envhome,
                                 DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | DB_RECOVER | DB_PRIVATE,
                                 0))) {
        LOG(log_error, logtype_cnid, "error opening DB environment: %s",
            db_strerror(ret));
        dbd->db_env->close(dbd->db_env, 0);
        dbd->db_env = NULL;
        return -1;
    }

    if (dbd->db_errlog != NULL)
        fflush(dbd->db_errlog);

    /* Remove logfiles */
    if ((ret = dbd->db_env->log_archive(dbd->db_env, NULL, DB_ARCH_REMOVE))) {
         LOG(log_error, logtype_cnid, "error removing transaction logfiles: %s", db_strerror(ret));
         return -1;
    }

    if ((ret = dbd->db_env->close(dbd->db_env, 0))) {
        LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s", db_strerror(ret));
        dbd->db_env = NULL;
        return -1;
    }

    LOG(log_debug, logtype_cnid, "BerkeleyDB environment recovery done.");

    /* Get a new db_env handle and then remove environment */
    if ((ret = db_env_create(&dbd->db_env, 0))) {
        LOG(log_error, logtype_cnid, "error acquiring db_end handle: %s", db_strerror(ret));
        dbd->db_env = NULL;
        return -1;
    }
    if ((ret = dbd->db_env->remove(dbd->db_env, dbd->db_envhome, 0))) {
        LOG(log_error, logtype_cnid, "error removing BerkeleyDB environment: %s", db_strerror(ret));
        return -1;
    }

    LOG(log_debug, logtype_cnid, "Removed BerkeleyDB environment.");

    return 0;
}

/*
 *  The following three functions are wrappers for DB->get(), DB->put() and DB->del().
 *  All three return -1 on error. dbif_get()/dbif_del return 1 if the key was found and 0
 *  otherwise. dbif_put() returns 0 if key/val was successfully updated and 1 if
 *  the DB_NOOVERWRITE flag was specified and the key already exists.
 *
 *  All return codes other than DB_NOTFOUND and DB_KEYEXIST from the DB->()
 *  functions are not expected and therefore error conditions.
 */

int dbif_get(DBD *dbd, const int dbi, DBT *key, DBT *val, u_int32_t flags)
{
    int ret;

    ret = dbd->db_table[dbi].db->get(dbd->db_table[dbi].db,
                                     dbd->db_txn,
                                     key,
                                     val,
                                     flags);

    if (ret == DB_NOTFOUND)
        return 0;
    if (ret) {
        LOG(log_error, logtype_cnid, "error retrieving value from %s: %s",
            dbd->db_table[dbi].name, db_strerror(ret));
        return -1;
    } else
        return 1;
}

/* search by secondary return primary */
int dbif_pget(DBD *dbd, const int dbi, DBT *key, DBT *pkey, DBT *val, u_int32_t flags)
{
    int ret;

    ret = dbd->db_table[dbi].db->pget(dbd->db_table[dbi].db,
                                      dbd->db_txn,
                                      key,
                                      pkey,
                                      val,
                                      flags);

    if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD) {
        return 0;
    }
    if (ret) {
        LOG(log_error, logtype_cnid, "error retrieving value from %s: %s",
            dbd->db_table[dbi].name, db_strerror(ret));
        return -1;
   } else
        return 1;
}

/* -------------------------- */
int dbif_put(DBD *dbd, const int dbi, DBT *key, DBT *val, u_int32_t flags)
{
    int ret;

    if (dbif_txn_begin(dbd) < 0) {
        LOG(log_error, logtype_cnid, "error setting key/value in %s", dbd->db_table[dbi].name);
        return -1;
    }

    ret = dbd->db_table[dbi].db->put(dbd->db_table[dbi].db,
                                     dbd->db_txn,
                                     key,
                                     val,
                                     flags);

    
    if (ret) {
        if ((flags & DB_NOOVERWRITE) && ret == DB_KEYEXIST) {
            return 1;
        } else {
            LOG(log_error, logtype_cnid, "error setting key/value in %s: %s",
                dbd->db_table[dbi].name, db_strerror(ret));
            return -1;
        }
    } else
        return 0;
}

int dbif_del(DBD *dbd, const int dbi, DBT *key, u_int32_t flags)
{
    int ret;

    /* For cooperation with the dbd utility and its usage of a cursor */
    if (dbd->db_cur) {
        dbd->db_cur->close(dbd->db_cur);
        dbd->db_cur = NULL;
    }    

    if (dbif_txn_begin(dbd) < 0) {
        LOG(log_error, logtype_cnid, "error deleting key/value from %s", dbd->db_table[dbi].name);
        return -1;
    }

    ret = dbd->db_table[dbi].db->del(dbd->db_table[dbi].db,
                                     dbd->db_txn,
                                     key,
                                     flags);
    
    if (ret == DB_NOTFOUND) {
        LOG(log_debug, logtype_cnid, "key not found");
        return 0;
    }
    if (ret) {
        LOG(log_error, logtype_cnid, "error deleting key/value from %s: %s",
            dbd->db_table[dbi].name, db_strerror(ret));
        return -1;
    } else
        return 1;
}

/*!
 * Search the database by name
 *
 * @param resbuf    (w) buffer for search results CNIDs, maxsize is assumed to be
 *                      DBD_MAX_SRCH_RSLTS * sizefof(cnid_t)
 *
 * @returns -1 on error, 0 when nothing found, else the number of matches
 */
int dbif_search(DBD *dbd, DBT *key, char *resbuf)
{
    int ret = 0;
    int count = 0;
    DBC *cursorp = NULL;
    DBT pkey, data;
    char *cnids = resbuf;
    cnid_t cnid;
    char *namebkp = key->data;
    int namelenbkp = key->size;

    memset(&pkey, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    /* Get a cursor */
    ret = dbd->db_table[DBIF_IDX_NAME].db->cursor(dbd->db_table[DBIF_IDX_NAME].db,
                                                  NULL,
                                                  &cursorp,
                                                  0);
    if (ret != 0) {
        LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(ret));
        ret = -1;
        goto exit;
    }

    ret = cursorp->pget(cursorp, key, &pkey, &data, DB_SET_RANGE);
    while (count < DBD_MAX_SRCH_RSLTS && ret != DB_NOTFOUND) {
        if (!((namelenbkp <= key->size) && (strncmp(namebkp, key->data, namelenbkp) == 0)))
            break;
        count++;
        memcpy(cnids, pkey.data, sizeof(cnid_t));
        memcpy(&cnid, pkey.data, sizeof(cnid_t));
        cnids += sizeof(cnid_t);
        LOG(log_debug, logtype_cnid, "match: CNID %" PRIu32, ntohl(cnid));

        ret = cursorp->pget(cursorp, key, &pkey, &data, DB_NEXT);
    }

    ret = count;

exit:
    if (cursorp != NULL)
        cursorp->close(cursorp);
    return ret;
}

int dbif_txn_begin(DBD *dbd)
{
    int ret;

    /* If we already have an active txn, just return */
    if (dbd->db_txn)
        return 0;

    /* If our DBD has no env, just return (-> in memory db) */
    if (dbd->db_env == NULL)
        return 0;

    ret = dbd->db_env->txn_begin(dbd->db_env, NULL, &dbd->db_txn, 0);

    if (ret) {
        LOG(log_error, logtype_cnid, "error starting transaction: %s", db_strerror(ret));
        return -1;
    } else
        return 0;
}

int dbif_txn_commit(DBD *dbd)
{
    int ret;

    if (! dbd->db_txn)
        return 0;

    /* If our DBD has no env, just return (-> in memory db) */
    if (dbd->db_env == NULL)
        return 0;

    ret = dbd->db_txn->commit(dbd->db_txn, 0);
    dbd->db_txn = NULL;
    
    if (ret) {
        LOG(log_error, logtype_cnid, "error committing transaction: %s", db_strerror(ret));
        return -1;
    } else
        return 1;
}

int dbif_txn_abort(DBD *dbd)
{
    int ret;

    if (! dbd->db_txn)
        return 0;

    /* If our DBD has no env, just return (-> in memory db) */
    if (dbd->db_env == NULL)
        return 0;

    ret = dbd->db_txn->abort(dbd->db_txn);
    dbd->db_txn = NULL;
    
    if (ret) {
        LOG(log_error, logtype_cnid, "error aborting transaction: %s", db_strerror(ret));
        return -1;
    } else
        return 0;
}

/* 
   ret = 1 -> commit txn if db_param.txn_frequency
   ret = 0 -> abort txn db_param.txn_frequency -> exit!
   anything else -> exit!

   @returns 0 on success (abort or commit), -1 on error
*/
int dbif_txn_close(DBD *dbd, int ret)
{
    if (ret == 0) {
        if (dbif_txn_abort(dbd) < 0) {
            LOG( log_error, logtype_cnid, "Fatal error aborting transaction. Exiting!");
            return -1;
        }
    } else if (ret == 1) {
        ret = dbif_txn_commit(dbd);
        if (  ret < 0) {
            LOG( log_error, logtype_cnid, "Fatal error committing transaction. Exiting!");
            return -1;
        }
    } else {
        return -1;
    }

    return 0;
}

int dbif_txn_checkpoint(DBD *dbd, u_int32_t kbyte, u_int32_t min, u_int32_t flags)
{
    int ret;
    ret = dbd->db_env->txn_checkpoint(dbd->db_env, kbyte, min, flags);
    if (ret) {
        LOG(log_error, logtype_cnid, "error checkpointing transaction susystem: %s", db_strerror(ret));
        return -1;
    } else
        return 0;
}

int dbif_count(DBD *dbd, const int dbi, u_int32_t *count)
{
    int ret;
    DB_BTREE_STAT *sp;
    DB *db = dbd->db_table[dbi].db;

    ret = db->stat(db, NULL, &sp, 0);

    if (ret) {
        LOG(log_error, logtype_cnid, "error getting stat infotmation on database: %s", db_strerror(ret));
        return -1;
    }

    *count = sp->bt_ndata;
    free(sp);

    return 0;
}

int dbif_copy_rootinfokey(DBD *srcdbd, DBD *destdbd)
{
    DBT key, data;
    int rc;

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));

    key.data = ROOTINFO_KEY;
    key.size = ROOTINFO_KEYLEN;

    if ((rc = dbif_get(srcdbd, DBIF_CNID, &key, &data, 0)) <= 0) {
        LOG(log_error, logtype_cnid, "dbif_copy_rootinfokey: Error getting rootinfo record");
        return -1;
    }

    memset(&key, 0, sizeof(key));
    key.data = ROOTINFO_KEY;
    key.size = ROOTINFO_KEYLEN;

    if ((rc = dbif_put(destdbd, DBIF_CNID, &key, &data, 0))) {
        LOG(log_error, logtype_cnid, "dbif_copy_rootinfokey: Error writing rootinfo key");
        return -1;
    }

    return 0;
}

int dbif_dump(DBD *dbd, int dumpindexes)
{
    int rc;
    uint32_t max = 0, count = 0, cnid, type, did, lastid, version;
    uint64_t dev, ino;
    time_t stamp;
    DBC *cur;
    DBT key = { 0 }, data = { 0 };
    DB *db = dbd->db_table[DBIF_CNID].db;
    char *typestring[2] = {"f", "d"};
    char timebuf[64];

    printf("CNID database dump:\n");

    rc = db->cursor(db, NULL, &cur, 0);
    if (rc) {
        LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
        return -1;
    }

    cur->c_get(cur, &key, &data, DB_FIRST);
    while (rc == 0) {
        /* Parse and print data */
        memcpy(&cnid, key.data, 4);
        cnid = ntohl(cnid);
        if (cnid > max)
            max = cnid;

        /* Rootinfo node ? */
        if (cnid == 0) {
            memcpy(&stamp, (char *)data.data + CNID_DEV_OFS, sizeof(time_t));
            memcpy(&lastid, (char *)data.data + CNID_TYPE_OFS, CNID_TYPE_LEN);
            lastid = ntohl(lastid);
            memcpy(&version, (char *)data.data + CNID_DID_OFS, CNID_DID_LEN);
            version = ntohl(version);

            strftime(timebuf, sizeof(timebuf), "%b %d %Y %H:%M:%S", localtime(&stamp));
            printf("CNID db version: %u, dbd stamp: 0x%08x (%s), next free CNID: %u\n",
                   version, (unsigned int)stamp, timebuf, lastid + 1);
        } else {
            /* dev */
            memcpy(&dev, (char *)data.data + CNID_DEV_OFS, 8);
            dev = ntoh64(dev);
            /* ino */
            memcpy(&ino, (char *)data.data + CNID_INO_OFS, 8);
            ino = ntoh64(ino);
            /* type */
            memcpy(&type, (char *)data.data + CNID_TYPE_OFS, 4);
            type = ntohl(type);
            /* did */
            memcpy(&did, (char *)data.data + CNID_DID_OFS, 4);
            did = ntohl(did);

            count++;
            printf("id: %10u, did: %10u, type: %s, dev: 0x%llx, ino: 0x%016llx, name: %s\n", 
                   cnid, did, typestring[type],
                   (long long unsigned int)dev, (long long unsigned int)ino, 
                   (char *)data.data + CNID_NAME_OFS);

        }

        rc = cur->c_get(cur, &key, &data, DB_NEXT);
    }

    if (rc != DB_NOTFOUND) {
        LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
        return -1;
    }

    rc = cur->c_close(cur);
    if (rc) {
        LOG(log_error, logtype_cnid, "Couldn't close cursor: %s", db_strerror(rc));
        return -1;
    }
    printf("%u CNIDs in database. Max CNID: %u.\n", count, max);

    /* Dump indexes too ? */
    if (dumpindexes) {
        /* DBIF_IDX_DEVINO */
        printf("\ndev/inode index:\n");
        count = 0;
        db = dbd->db_table[DBIF_IDX_DEVINO].db;
        rc = db->cursor(db, NULL, &cur, 0);
        if (rc) {
            LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
            return -1;
        }
        
        cur->c_get(cur, &key, &data, DB_FIRST);
        while (rc == 0) {
            /* Parse and print data */

            /* cnid */
            memcpy(&cnid, data.data, CNID_LEN);
            cnid = ntohl(cnid);
            if (cnid == 0) {
                /* Rootinfo node */
            } else {
                /* dev */
                memcpy(&dev, key.data, CNID_DEV_LEN);
                dev = ntoh64(dev);
                /* ino */
                memcpy(&ino, (char *)key.data + CNID_DEV_LEN, CNID_INO_LEN);
                ino = ntoh64(ino);
                
                printf("id: %10u <== dev: 0x%llx, ino: 0x%016llx\n", 
                       cnid, (unsigned long long int)dev, (unsigned long long int)ino);
                count++;
            }
            rc = cur->c_get(cur, &key, &data, DB_NEXT);
        }
        if (rc != DB_NOTFOUND) {
            LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
            return -1;
        }
        
        rc = cur->c_close(cur);
        if (rc) {
            LOG(log_error, logtype_cnid, "Couldn't close cursor: %s", db_strerror(rc));
            return -1;
        }
        printf("%u items\n", count);

        /* Now dump DBIF_IDX_DIDNAME */
        printf("\ndid/name index:\n");
        count = 0;
        db = dbd->db_table[DBIF_IDX_DIDNAME].db;
        rc = db->cursor(db, NULL, &cur, 0);
        if (rc) {
            LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
            return -1;
        }
        
        cur->c_get(cur, &key, &data, DB_FIRST);
        while (rc == 0) {
            /* Parse and print data */

            /* cnid */
            memcpy(&cnid, data.data, CNID_LEN);
            cnid = ntohl(cnid);
            if (cnid == 0) {
                /* Rootinfo node */
            } else {
                /* did */
                memcpy(&did, key.data, CNID_LEN);
                did = ntohl(did);

                printf("id: %10u <== did: %10u, name: %s\n", cnid, did, (char *)key.data + CNID_LEN);
                count++;
            }
            rc = cur->c_get(cur, &key, &data, DB_NEXT);
        }
        if (rc != DB_NOTFOUND) {
            LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
            return -1;
        }
        
        rc = cur->c_close(cur);
        if (rc) {
            LOG(log_error, logtype_cnid, "Couldn't close cursor: %s", db_strerror(rc));
            return -1;
        }
        printf("%u items\n", count);
    }

    return 0;
}

/* 
   Iterates over dbd, returning cnids.
   Uses in-value of cnid to seek to that cnid, then gets next and return that in cnid.
   If close=1, close cursor.
   Return -1 on error, 0 on EOD (end-of-database), 1 if returning cnid.
*/
int dbif_idwalk(DBD *dbd, cnid_t *cnid, int close)
{
    int rc;
    int flag;
    cnid_t id;

    static DBT key = { 0 }, data = { 0 };
    DB *db = dbd->db_table[DBIF_CNID].db;

    if (close) {
        if (dbd->db_cur) {
            dbd->db_cur->close(dbd->db_cur);
            dbd->db_cur = NULL;
        }
        return 0;
    }

    /* An dbif_del will have closed our cursor too */
    if ( ! dbd->db_cur ) {
        if ((rc = db->cursor(db, NULL, &dbd->db_cur, 0)) != 0) {
            LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
            return -1;
        }
        flag = DB_SET_RANGE;    /* This will seek to next cnid after the one just deleted */
        id = htonl(*cnid);
        key.data = &id;
        key.size = sizeof(cnid_t);
    } else
        flag = DB_NEXT;

    if ((rc = dbd->db_cur->get(dbd->db_cur, &key, &data, flag)) == 0) {
        memcpy(cnid, key.data, sizeof(cnid_t));
        *cnid = ntohl(*cnid);
        return 1;
    }

    if (rc != DB_NOTFOUND) {
        LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
        dbd->db_cur->close(dbd->db_cur);
        dbd->db_cur = NULL;
        return -1;
    }

    if (dbd->db_cur) {
        dbd->db_cur->close(dbd->db_cur);
        dbd->db_cur = NULL;
    }    

    return 0;
}