2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2009
4 * All Rights Reserved. See COPYING.
9 #endif /* HAVE_CONFIG_H */
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif /* HAVE_SYS_TYPES_H */
18 #include <sys/types.h>
20 #include <sys/cdefs.h>
22 #include <atalk/logger.h>
28 #define DB_ERRLOGFILE "db_errlog"
30 static char *old_dbfiles[] = {"cnid.db", NULL};
33 static int upgrade_required(const DBD *dbd)
41 if ( ! dbd->db_filename)
46 if ((cwd = open(".", O_RDONLY)) < 0) {
47 LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
51 /* chdir to db_envhome */
52 if ((chdir(dbd->db_envhome)) != 0) {
53 LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));
58 for (i = 0; old_dbfiles[i] != NULL; i++) {
59 if ( !(stat(old_dbfiles[i], &st) < 0) ) {
63 if (errno != ENOENT) {
64 LOG(log_error, logtype_cnid, "cnid_open: Checking %s gave %s", old_dbfiles[i], strerror(errno));
71 if ((fchdir(cwd)) != 0) {
72 LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));
77 return (ret < 0 ? ret : found);
81 static int dbif_openlog(DBD *dbd)
86 if ( ! dbd->db_filename)
91 if ((cwd = open(".", O_RDONLY)) < 0) {
92 LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
96 /* chdir to db_envhome */
97 if ((chdir(dbd->db_envhome)) != 0) {
98 LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));
103 if ((dbd->db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
104 LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));
106 if (dbd->db_errlog != NULL) {
107 dbd->db_env->set_errfile(dbd->db_env, dbd->db_errlog);
108 dbd->db_env->set_msgfile(dbd->db_env, dbd->db_errlog);
113 if ((fchdir(cwd)) != 0) {
114 LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));
122 /* --------------- */
123 static int dbif_logautorem(DBD *dbd)
127 char **logfiles = NULL;
130 if ( ! dbd->db_filename)
135 if ((cwd = open(".", O_RDONLY)) < 0) {
136 LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
140 /* chdir to db_envhome */
141 if ((chdir(dbd->db_envhome)) != 0) {
142 LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));
147 if ((ret = dbd->db_env->log_archive(dbd->db_env, &logfiles, 0)) != 0) {
148 LOG(log_error, logtype_cnid, "error getting list of stale logfiles: %s",
150 dbd->db_env->close(dbd->db_env, 0);
156 if (logfiles != NULL) {
157 for (file = logfiles; *file != NULL; file++) {
158 if (unlink(*file) < 0)
159 LOG(log_warning, logtype_cnid, "Error removing stale logfile %s: %s", *file, strerror(errno));
166 if ((fchdir(cwd)) != 0) {
167 LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));
175 /* --------------- */
176 int dbif_stamp(DBD *dbd, void *buffer, int size)
185 if ((cwd = open(".", O_RDONLY)) < 0) {
186 LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
190 /* chdir to db_envhome */
191 if ((chdir(dbd->db_envhome)) != 0) {
192 LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));
196 if ((rc = stat(dbd->db_table[DBIF_CNID].name, &st)) < 0) {
197 LOG(log_error, logtype_cnid, "error stating database %s: %s", dbd->db_table[DBIF_CNID].name, db_strerror(errno));
200 memset(buffer, 0, size);
201 memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
203 if ((fchdir(cwd)) != 0) {
204 LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));
211 /* --------------- */
212 DBD *dbif_init(const char *envhome, const char *filename)
216 if ( NULL == (dbd = calloc(sizeof(DBD), 1)) )
219 /* filename == NULL means in memory db */
224 dbd->db_envhome = strdup(envhome);
225 if (NULL == dbd->db_envhome) {
230 dbd->db_filename = strdup(filename);
231 if (NULL == dbd->db_filename) {
232 free(dbd->db_envhome);
238 dbd->db_table[DBIF_CNID].name = "cnid2.db";
239 dbd->db_table[DBIF_IDX_DEVINO].name = "devino.db";
240 dbd->db_table[DBIF_IDX_DIDNAME].name = "didname.db";
242 dbd->db_table[DBIF_CNID].type = DB_BTREE;
243 dbd->db_table[DBIF_IDX_DEVINO].type = DB_BTREE;
244 dbd->db_table[DBIF_IDX_DIDNAME].type = DB_BTREE;
246 dbd->db_table[DBIF_CNID].openflags = DB_CREATE;
247 dbd->db_table[DBIF_IDX_DEVINO].openflags = DB_CREATE;
248 dbd->db_table[DBIF_IDX_DIDNAME].openflags = DB_CREATE;
254 We must open the db_env with an absolute pathname, as `dbd` keeps chdir'ing, which
255 breaks e.g. bdb logfile-rotation with relative pathnames.
256 But still we use relative paths with upgrade_required() and the DB_ERRLOGFILE
257 in order to avoid creating absolute paths by copying. Both have no problem with
260 int dbif_env_open(DBD *dbd, struct db_param *dbp, uint32_t dbenv_oflags)
264 /* Refuse to do anything if this is an old version of the CNID database */
265 if (upgrade_required(dbd)) {
266 LOG(log_error, logtype_cnid, "Found version 1 of the CNID database. Please upgrade to version 2");
270 if ((ret = db_env_create(&dbd->db_env, 0))) {
271 LOG(log_error, logtype_cnid, "error creating DB environment: %s",
277 dbd->db_param = *dbp;
279 if ((dbif_openlog(dbd)) != 0)
282 if (dbenv_oflags & DB_RECOVER) {
284 LOG(log_debug, logtype_cnid, "Running recovery");
286 dbd->db_env->set_verbose(dbd->db_env, DB_VERB_RECOVERY, 1);
287 /* Open the database for recovery using DB_PRIVATE option which is faster */
288 if ((ret = dbd->db_env->open(dbd->db_env, dbd->db_envhome, dbenv_oflags | DB_PRIVATE, 0))) {
289 LOG(log_error, logtype_cnid, "error opening DB environment: %s",
291 dbd->db_env->close(dbd->db_env, 0);
295 dbenv_oflags = (dbenv_oflags & ~DB_RECOVER);
297 if (dbd->db_errlog != NULL)
298 fflush(dbd->db_errlog);
300 if ((ret = dbd->db_env->close(dbd->db_env, 0))) {
301 LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s",
306 dbd->db_errlog = NULL;
308 if ((ret = db_env_create(&dbd->db_env, 0))) {
309 LOG(log_error, logtype_cnid, "error creating DB environment after recovery: %s",
315 if ((dbif_openlog(dbd)) != 0)
318 LOG(log_debug, logtype_cnid, "Finished recovery.");
321 if ((ret = dbd->db_env->set_cachesize(dbd->db_env, 0, 1024 * dbp->cachesize, 0))) {
322 LOG(log_error, logtype_cnid, "error setting DB environment cachesize to %i: %s",
323 dbp->cachesize, db_strerror(ret));
324 dbd->db_env->close(dbd->db_env, 0);
329 if ((ret = dbd->db_env->open(dbd->db_env, dbd->db_envhome, dbenv_oflags, 0))) {
330 LOG(log_error, logtype_cnid, "error opening DB environment after recovery: %s",
332 dbd->db_env->close(dbd->db_env, 0);
337 if ((ret = dbd->db_env->set_flags(dbd->db_env, DB_AUTO_COMMIT, 1))) {
338 LOG(log_error, logtype_cnid, "error setting DB_AUTO_COMMIT flag: %s",
340 dbd->db_env->close(dbd->db_env, 0);
345 if (dbp->logfile_autoremove) {
346 if ((dbif_logautorem(dbd)) != 0)
349 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 7)
350 if ((ret = dbd->db_env->log_set_config(dbd->db_env, DB_LOG_AUTO_REMOVE, 1))) {
351 LOG(log_error, logtype_cnid, "error setting DB_LOG_AUTO_REMOVE flag: %s",
353 dbd->db_env->close(dbd->db_env, 0);
358 if ((ret = dbd->db_env->set_flags(dbd->db_env, DB_LOG_AUTOREMOVE, 1))) {
359 LOG(log_error, logtype_cnid, "error setting DB_LOG_AUTOREMOVE flag: %s",
361 dbd->db_env->close(dbd->db_env, 0);
371 /* --------------- */
372 int dbif_open(DBD *dbd, struct db_param *dbp, int reindex)
379 /* Try to upgrade if it's a normal on-disk database */
380 if (dbd->db_envhome) {
382 if ((cwd = open(".", O_RDONLY)) < 0) {
383 LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
387 /* chdir to db_envhome. makes it easier checking for old db files and creating db_errlog file */
388 if ((chdir(dbd->db_envhome)) != 0) {
389 LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));
393 if ((stat(dbd->db_filename, &st)) == 0) {
394 LOG(log_debug, logtype_cnid, "See if we can upgrade the CNID database...");
395 if ((ret = db_create(&upgrade_db, dbd->db_env, 0))) {
396 LOG(log_error, logtype_cnid, "error creating handle for database: %s", db_strerror(ret));
399 if ((ret = upgrade_db->upgrade(upgrade_db, dbd->db_filename, 0))) {
400 LOG(log_error, logtype_cnid, "error upgarding database: %s", db_strerror(ret));
403 if ((ret = upgrade_db->close(upgrade_db, 0))) {
404 LOG(log_error, logtype_cnid, "error closing database: %s", db_strerror(ret));
407 if ((ret = dbd->db_env->txn_checkpoint(dbd->db_env, 0, 0, DB_FORCE))) {
408 LOG(log_error, logtype_cnid, "error forcing checkpoint: %s", db_strerror(ret));
411 LOG(log_debug, logtype_cnid, "Finished CNID database upgrade check");
414 if ((fchdir(cwd)) != 0) {
415 LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));
420 /* Now open databases ... */
421 for (i = 0; i != DBIF_DB_CNT; i++) {
422 if ((ret = db_create(&dbd->db_table[i].db, dbd->db_env, 0))) {
423 LOG(log_error, logtype_cnid, "error creating handle for database %s: %s",
424 dbd->db_table[i].name, db_strerror(ret));
428 if (dbd->db_table[i].flags) {
429 if ((ret = dbd->db_table[i].db->set_flags(dbd->db_table[i].db,
430 dbd->db_table[i].flags))) {
431 LOG(log_error, logtype_cnid, "error setting flags for database %s: %s",
432 dbd->db_table[i].name, db_strerror(ret));
437 if ( ! dbd->db_env) { /* In memory db */
438 if ((ret = dbd->db_table[i].db->set_cachesize(dbd->db_table[i].db,
441 4)) /* split in 4 memory chunks */
443 LOG(log_error, logtype_cnid, "error setting cachesize %u KB for database %s: %s",
444 dbp->cachesize / 1024, dbd->db_table[i].name, db_strerror(ret));
449 if (dbd->db_table[i].db->open(dbd->db_table[i].db,
452 dbd->db_table[i].name,
453 dbd->db_table[i].type,
454 dbd->db_table[i].openflags,
456 LOG(log_error, logtype_cnid, "Cant open database");
460 if (reindex && i > 0) {
461 LOG(log_info, logtype_cnid, "Truncating CNID index.");
462 if ((ret = dbd->db_table[i].db->truncate(dbd->db_table[i].db, NULL, &count, 0))) {
463 LOG(log_error, logtype_cnid, "error truncating database %s: %s",
464 dbd->db_table[i].name, db_strerror(ret));
470 /* TODO: Implement CNID DB versioning info on new databases. */
472 /* Associate the secondary with the primary. */
474 LOG(log_info, logtype_cnid, "Reindexing did/name index...");
475 if ((ret = dbd->db_table[0].db->associate(dbd->db_table[DBIF_CNID].db,
477 dbd->db_table[DBIF_IDX_DIDNAME].db,
479 (reindex) ? DB_CREATE : 0))
481 LOG(log_error, logtype_cnid, "Failed to associate didname database: %s",db_strerror(ret));
485 LOG(log_info, logtype_cnid, "... done.");
488 LOG(log_info, logtype_cnid, "Reindexing dev/ino index...");
489 if ((ret = dbd->db_table[0].db->associate(dbd->db_table[0].db,
491 dbd->db_table[DBIF_IDX_DEVINO].db,
493 (reindex) ? DB_CREATE : 0))
495 LOG(log_error, logtype_cnid, "Failed to associate devino database: %s",db_strerror(ret));
499 LOG(log_info, logtype_cnid, "... done.");
504 /* ------------------------ */
505 static int dbif_closedb(DBD *dbd)
511 for (i = DBIF_DB_CNT -1; i >= 0; i--) {
512 if (dbd->db_table[i].db != NULL && (ret = dbd->db_table[i].db->close(dbd->db_table[i].db, 0))) {
513 LOG(log_error, logtype_cnid, "error closing database %s: %s", dbd->db_table[i].name, db_strerror(ret));
522 /* ------------------------ */
523 int dbif_close(DBD *dbd)
528 if (dbif_closedb(dbd))
531 if (dbd->db_env != NULL && (ret = dbd->db_env->close(dbd->db_env, 0))) {
532 LOG(log_error, logtype_cnid, "error closing DB environment: %s", db_strerror(ret));
535 if (dbd->db_errlog != NULL && fclose(dbd->db_errlog) == EOF) {
536 LOG(log_error, logtype_cnid, "error closing DB logfile: %s", strerror(errno));
540 free(dbd->db_filename);
550 In order to support silent database upgrades:
551 destroy env at cnid_dbd shutdown.
553 int dbif_prep_upgrade(const char *path)
558 LOG(log_debug, logtype_cnid, "Reopening BerkeleyDB environment");
560 if (NULL == (dbd = dbif_init(path, "cnid2.db")))
563 /* Get db_env handle */
564 if ((ret = db_env_create(&dbd->db_env, 0))) {
565 LOG(log_error, logtype_cnid, "error creating DB environment: %s", db_strerror(ret));
570 if ((dbif_openlog(dbd)) != 0)
573 /* Open environment with recovery */
574 if ((ret = dbd->db_env->open(dbd->db_env,
576 DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | DB_RECOVER | DB_PRIVATE,
578 LOG(log_error, logtype_cnid, "error opening DB environment: %s",
580 dbd->db_env->close(dbd->db_env, 0);
585 if (dbd->db_errlog != NULL)
586 fflush(dbd->db_errlog);
588 /* Remove logfiles */
589 if ((ret = dbd->db_env->log_archive(dbd->db_env, NULL, DB_ARCH_REMOVE))) {
590 LOG(log_error, logtype_cnid, "error removing transaction logfiles: %s", db_strerror(ret));
594 if ((ret = dbd->db_env->close(dbd->db_env, 0))) {
595 LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s", db_strerror(ret));
600 LOG(log_debug, logtype_cnid, "BerkeleyDB environment recovery done.");
602 /* Get a new db_env handle and then remove environment */
603 if ((ret = db_env_create(&dbd->db_env, 0))) {
604 LOG(log_error, logtype_cnid, "error acquiring db_end handle: %s", db_strerror(ret));
608 if ((ret = dbd->db_env->remove(dbd->db_env, dbd->db_envhome, 0))) {
609 LOG(log_error, logtype_cnid, "error removing BerkeleyDB environment: %s", db_strerror(ret));
613 LOG(log_debug, logtype_cnid, "Removed BerkeleyDB environment.");
619 * The following three functions are wrappers for DB->get(), DB->put() and DB->del().
620 * All three return -1 on error. dbif_get()/dbif_del return 1 if the key was found and 0
621 * otherwise. dbif_put() returns 0 if key/val was successfully updated and 1 if
622 * the DB_NOOVERWRITE flag was specified and the key already exists.
624 * All return codes other than DB_NOTFOUND and DB_KEYEXIST from the DB->()
625 * functions are not expected and therefore error conditions.
628 int dbif_get(DBD *dbd, const int dbi, DBT *key, DBT *val, u_int32_t flags)
632 ret = dbd->db_table[dbi].db->get(dbd->db_table[dbi].db,
638 if (ret == DB_NOTFOUND)
641 LOG(log_error, logtype_cnid, "error retrieving value from %s: %s",
642 dbd->db_table[dbi].name, db_strerror(ret));
648 /* search by secondary return primary */
649 int dbif_pget(DBD *dbd, const int dbi, DBT *key, DBT *pkey, DBT *val, u_int32_t flags)
653 ret = dbd->db_table[dbi].db->pget(dbd->db_table[dbi].db,
660 if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD) {
664 LOG(log_error, logtype_cnid, "error retrieving value from %s: %s",
665 dbd->db_table[dbi].name, db_strerror(ret));
671 /* -------------------------- */
672 int dbif_put(DBD *dbd, const int dbi, DBT *key, DBT *val, u_int32_t flags)
676 if (dbif_txn_begin(dbd) < 0) {
677 LOG(log_error, logtype_cnid, "error setting key/value in %s", dbd->db_table[dbi].name);
681 ret = dbd->db_table[dbi].db->put(dbd->db_table[dbi].db,
689 if ((flags & DB_NOOVERWRITE) && ret == DB_KEYEXIST) {
692 LOG(log_error, logtype_cnid, "error setting key/value in %s: %s",
693 dbd->db_table[dbi].name, db_strerror(ret));
700 int dbif_del(DBD *dbd, const int dbi, DBT *key, u_int32_t flags)
704 /* For cooperation with the dbd utility and its usage of a cursor */
706 dbd->db_cur->close(dbd->db_cur);
710 if (dbif_txn_begin(dbd) < 0) {
711 LOG(log_error, logtype_cnid, "error deleting key/value from %s", dbd->db_table[dbi].name);
715 ret = dbd->db_table[dbi].db->del(dbd->db_table[dbi].db,
720 if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD)
723 LOG(log_error, logtype_cnid, "error deleting key/value from %s: %s",
724 dbd->db_table[dbi].name, db_strerror(ret));
730 int dbif_txn_begin(DBD *dbd)
734 /* If we already have an active txn, just return */
738 /* If our DBD has no env, just return (-> in memory db) */
739 if (dbd->db_env == NULL)
742 ret = dbd->db_env->txn_begin(dbd->db_env, NULL, &dbd->db_txn, 0);
745 LOG(log_error, logtype_cnid, "error starting transaction: %s", db_strerror(ret));
751 int dbif_txn_commit(DBD *dbd)
758 /* If our DBD has no env, just return (-> in memory db) */
759 if (dbd->db_env == NULL)
762 ret = dbd->db_txn->commit(dbd->db_txn, 0);
766 LOG(log_error, logtype_cnid, "error committing transaction: %s", db_strerror(ret));
772 int dbif_txn_abort(DBD *dbd)
779 /* If our DBD has no env, just return (-> in memory db) */
780 if (dbd->db_env == NULL)
783 ret = dbd->db_txn->abort(dbd->db_txn);
787 LOG(log_error, logtype_cnid, "error aborting transaction: %s", db_strerror(ret));
794 ret = 2 -> commit txn regardless of db_param.txn_frequency
795 ret = 1 -> commit txn if db_param.txn_frequency
796 ret = 0 -> abort txn db_param.txn_frequency -> exit!
797 anything else -> exit!
799 db_param of the db environment might specify txn_frequency > 1 in which case
800 we only close a txn every txn_frequency time. the `dbd` command uses this for the
801 temp rebuild db, cnid_dbd keeps it at 0. For increasing cnid_dbd throughput this
802 should be tuned and testes as well.
804 void dbif_txn_close(DBD *dbd, int ret)
807 if (dbif_txn_abort(dbd) < 0) {
808 LOG( log_error, logtype_cnid, "Fatal error aborting transaction. Exiting!");
811 } else if (ret == 1 || ret == 2) {
813 if (ret != 2 && dbd->db_param.txn_frequency > 1) {
815 if ((count % dbd->db_param.txn_frequency) != 0)
818 ret = dbif_txn_commit(dbd);
820 LOG( log_error, logtype_cnid, "Fatal error committing transaction. Exiting!");
827 int dbif_txn_checkpoint(DBD *dbd, u_int32_t kbyte, u_int32_t min, u_int32_t flags)
830 ret = dbd->db_env->txn_checkpoint(dbd->db_env, kbyte, min, flags);
832 LOG(log_error, logtype_cnid, "error checkpointing transaction susystem: %s", db_strerror(ret));
838 int dbif_count(DBD *dbd, const int dbi, u_int32_t *count)
842 DB *db = dbd->db_table[dbi].db;
844 ret = db->stat(db, NULL, &sp, 0);
847 LOG(log_error, logtype_cnid, "error getting stat infotmation on database: %s", db_strerror(ret));
851 *count = sp->bt_ndata;
857 int dbif_copy_rootinfokey(DBD *srcdbd, DBD *destdbd)
862 memset(&key, 0, sizeof(key));
863 memset(&data, 0, sizeof(data));
865 key.data = ROOTINFO_KEY;
866 key.size = ROOTINFO_KEYLEN;
868 if ((rc = dbif_get(srcdbd, DBIF_CNID, &key, &data, 0)) <= 0) {
869 LOG(log_error, logtype_cnid, "dbif_copy_rootinfokey: Error getting rootinfo record");
873 memset(&key, 0, sizeof(key));
874 key.data = ROOTINFO_KEY;
875 key.size = ROOTINFO_KEYLEN;
877 if ((rc = dbif_put(destdbd, DBIF_CNID, &key, &data, 0))) {
878 LOG(log_error, logtype_cnid, "dbif_copy_rootinfokey: Error writing rootinfo key");
885 int dbif_dump(DBD *dbd, int dumpindexes)
888 uint32_t max = 0, count = 0, cnid, type, did, lastid;
892 DBT key = { 0 }, data = { 0 };
893 DB *db = dbd->db_table[DBIF_CNID].db;
894 char *typestring[2] = {"f", "d"};
897 printf("CNID database dump:\n");
899 rc = db->cursor(db, NULL, &cur, 0);
901 LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
905 cur->c_get(cur, &key, &data, DB_FIRST);
907 /* Parse and print data */
908 memcpy(&cnid, key.data, 4);
913 /* Rootinfo node ? */
915 memcpy(&stamp, (char *)data.data + 4, sizeof(time_t));
916 memcpy(&lastid, (char *)data.data + 20, sizeof(cnid_t));
917 lastid = ntohl(lastid);
918 strftime(timebuf, sizeof(timebuf), "%b %d %Y %H:%M:%S", localtime(&stamp));
919 printf("dbd stamp: 0x%08x (%s), next free CNID: %u\n", (unsigned int)stamp, timebuf, lastid + 1);
922 memcpy(&dev, (char *)data.data + CNID_DEV_OFS, 8);
925 memcpy(&ino, (char *)data.data + CNID_INO_OFS, 8);
928 memcpy(&type, (char *)data.data + CNID_TYPE_OFS, 4);
931 memcpy(&did, (char *)data.data + CNID_DID_OFS, 4);
935 printf("id: %10u, did: %10u, type: %s, dev: 0x%llx, ino: 0x%016llx, name: %s\n",
936 cnid, did, typestring[type],
937 (long long unsigned int)dev, (long long unsigned int)ino,
938 (char *)data.data + CNID_NAME_OFS);
942 rc = cur->c_get(cur, &key, &data, DB_NEXT);
945 if (rc != DB_NOTFOUND) {
946 LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
950 rc = cur->c_close(cur);
952 LOG(log_error, logtype_cnid, "Couldn't close cursor: %s", db_strerror(rc));
955 printf("%u CNIDs in database. Max CNID: %u.\n", count, max);
957 /* Dump indexes too ? */
959 /* DBIF_IDX_DEVINO */
960 printf("\ndev/inode index:\n");
962 db = dbd->db_table[DBIF_IDX_DEVINO].db;
963 rc = db->cursor(db, NULL, &cur, 0);
965 LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
969 cur->c_get(cur, &key, &data, DB_FIRST);
971 /* Parse and print data */
974 memcpy(&cnid, data.data, CNID_LEN);
980 memcpy(&dev, key.data, CNID_DEV_LEN);
983 memcpy(&ino, (char *)key.data + CNID_DEV_LEN, CNID_INO_LEN);
986 printf("id: %10u <== dev: 0x%llx, ino: 0x%016llx\n",
987 cnid, (unsigned long long int)dev, (unsigned long long int)ino);
990 rc = cur->c_get(cur, &key, &data, DB_NEXT);
992 if (rc != DB_NOTFOUND) {
993 LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
997 rc = cur->c_close(cur);
999 LOG(log_error, logtype_cnid, "Couldn't close cursor: %s", db_strerror(rc));
1002 printf("%u items\n", count);
1004 /* Now dump DBIF_IDX_DIDNAME */
1005 printf("\ndid/name index:\n");
1007 db = dbd->db_table[DBIF_IDX_DIDNAME].db;
1008 rc = db->cursor(db, NULL, &cur, 0);
1010 LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
1014 cur->c_get(cur, &key, &data, DB_FIRST);
1016 /* Parse and print data */
1019 memcpy(&cnid, data.data, CNID_LEN);
1025 memcpy(&did, key.data, CNID_LEN);
1028 printf("id: %10u <== did: %10u, name: %s\n", cnid, did, (char *)key.data + CNID_LEN);
1031 rc = cur->c_get(cur, &key, &data, DB_NEXT);
1033 if (rc != DB_NOTFOUND) {
1034 LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
1038 rc = cur->c_close(cur);
1040 LOG(log_error, logtype_cnid, "Couldn't close cursor: %s", db_strerror(rc));
1043 printf("%u items\n", count);
1050 Iterates over dbd, returning cnids.
1051 Uses in-value of cnid to seek to that cnid, then gets next and return that in cnid.
1052 If close=1, close cursor.
1053 Return -1 on error, 0 on EOD (end-of-database), 1 if returning cnid.
1055 int dbif_idwalk(DBD *dbd, cnid_t *cnid, int close)
1061 static DBT key = { 0 }, data = { 0 };
1062 DB *db = dbd->db_table[DBIF_CNID].db;
1066 dbd->db_cur->close(dbd->db_cur);
1072 /* An dbif_del will have closed our cursor too */
1073 if ( ! dbd->db_cur ) {
1074 if ((rc = db->cursor(db, NULL, &dbd->db_cur, 0)) != 0) {
1075 LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(rc));
1078 flag = DB_SET_RANGE; /* This will seek to next cnid after the one just deleted */
1081 key.size = sizeof(cnid_t);
1085 if ((rc = dbd->db_cur->get(dbd->db_cur, &key, &data, flag)) == 0) {
1086 memcpy(cnid, key.data, sizeof(cnid_t));
1087 *cnid = ntohl(*cnid);
1091 if (rc != DB_NOTFOUND) {
1092 LOG(log_error, logtype_cnid, "Error iterating over btree: %s", db_strerror(rc));
1093 dbd->db_cur->close(dbd->db_cur);
1099 dbd->db_cur->close(dbd->db_cur);