/*
- * $Id: cnid_add.c,v 1.14 2001-10-22 03:40:18 jmarcus Exp $
+ * $Id: cnid_add.c,v 1.14.2.1 2001-12-03 05:05:45 jmarcus Exp $
*
* Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT.
/* add an entry to the CNID databases. we do this as a transaction
* to prevent messiness. */
static int add_cnid(CNID_private *db, DB_TXN *ptid, DBT *key, DBT *data) {
- DBT altkey, altdata;
- DB_TXN *tid;
- /* We create rc here because using errno is bad. Why? Well, if you
- * use errno once, then call another function which resets it, you're
- * screwed. */
- int rc;
+ DBT altkey, altdata;
+ DB_TXN *tid;
+ /* We create rc here because using errno is bad. Why? Well, if you
+ * use errno once, then call another function which resets it, you're
+ * screwed. */
+ int rc;
- memset(&altkey, 0, sizeof(altkey));
- memset(&altdata, 0, sizeof(altdata));
+ memset(&altkey, 0, sizeof(altkey));
+ memset(&altdata, 0, sizeof(altdata));
retry:
- if ((rc = txn_begin(db->dbenv, ptid, &tid, 0)) != 0) {
- return rc;
- }
-
-
- /* main database */
- if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
- int ret;
- if ((ret = txn_abort(tid)) != 0) {
- return ret;
- }
- if (rc == DB_LOCK_DEADLOCK) {
- goto retry;
- }
-
- return rc;
- }
-
-
- /* dev/ino database */
- altkey.data = data->data;
- altkey.size = CNID_DEVINO_LEN;
- altdata.data = key->data;
- altdata.size = key->size;
- if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
- int ret;
- if ((ret = txn_abort(tid)) != 0) {
- return ret;
- }
- if (rc == DB_LOCK_DEADLOCK) {
- goto retry;
- }
-
- return rc;
- }
-
-
- /* did/name database */
- altkey.data = (char *) data->data + CNID_DEVINO_LEN;
- altkey.size = data->size - CNID_DEVINO_LEN;
- if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
- int ret;
- if ((ret = txn_abort(tid)) != 0) {
- return ret;
- }
- if (rc == DB_LOCK_DEADLOCK) {
- goto retry;
- }
-
- return rc;
- }
-
- if ((rc = txn_commit(tid, 0)) != 0) {
- syslog(LOG_ERR, "add_cnid: Failed to commit transaction: %s",
- db_strerror(rc));
- return rc;
- }
- return 0;
+ if ((rc = txn_begin(db->dbenv, ptid, &tid, 0)) != 0) {
+ return rc;
+ }
+
+
+ /* main database */
+ if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
+ int ret;
+ if ((ret = txn_abort(tid)) != 0) {
+ return ret;
+ }
+ if (rc == DB_LOCK_DEADLOCK) {
+ goto retry;
+ }
+
+ return rc;
+ }
+
+
+ /* dev/ino database */
+ altkey.data = data->data;
+ altkey.size = CNID_DEVINO_LEN;
+ altdata.data = key->data;
+ altdata.size = key->size;
+ if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
+ int ret;
+ if ((ret = txn_abort(tid)) != 0) {
+ return ret;
+ }
+ if (rc == DB_LOCK_DEADLOCK) {
+ goto retry;
+ }
+
+ return rc;
+ }
+
+
+ /* did/name database */
+ altkey.data = (char *) data->data + CNID_DEVINO_LEN;
+ altkey.size = data->size - CNID_DEVINO_LEN;
+ if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
+ int ret;
+ if ((ret = txn_abort(tid)) != 0) {
+ return ret;
+ }
+ if (rc == DB_LOCK_DEADLOCK) {
+ goto retry;
+ }
+
+ return rc;
+ }
+
+ if ((rc = txn_commit(tid, 0)) != 0) {
+ syslog(LOG_ERR, "add_cnid: Failed to commit transaction: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ return 0;
}
cnid_t cnid_add(void *CNID, const struct stat *st,
- const cnid_t did, const char *name, const int len,
- cnid_t hint)
+ const cnid_t did, const char *name, const int len,
+ cnid_t hint)
{
- CNID_private *db;
- DBT key, data, rootinfo_key, rootinfo_data;
- DB_TXN *tid;
- cnid_t id, save;
- int rc;
-
- if (!(db = CNID) || !st || !name) {
- return 0;
- }
-
- /* Do a lookup. */
- id = cnid_lookup(db, st, did, name, len);
- /* ... Return id if it is valid, or if Rootinfo is read-only. */
- if (id || (db->flags & CNIDFLAG_DB_RO)) {
+ CNID_private *db;
+ DBT key, data, rootinfo_key, rootinfo_data;
+ DB_TXN *tid;
+ cnid_t id, save;
+ int rc;
+
+ if (!(db = CNID) || !st || !name) {
+ return 0;
+ }
+
+ /* Do a lookup. */
+ id = cnid_lookup(db, st, did, name, len);
+ /* ... Return id if it is valid, or if Rootinfo is read-only. */
+ if (id || (db->flags & CNIDFLAG_DB_RO)) {
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_add: Looked up did %u, name %s as %u",
- ntohl(did), name, ntohl(id));
+ syslog(LOG_INFO, "cnid_add: Looked up did %u, name %s as %u",
+ ntohl(did), name, ntohl(id));
#endif
- return id;
- }
-
- /* Initialize our DBT data structures. */
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
-
- /* Just tickle hint, and the key will change (gotta love pointers). */
- key.data = &hint;
- key.size = sizeof(hint);
-
- if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
- syslog(LOG_ERR, "cnid_add: Path name is too long");
- goto cleanup_err;
- }
-
- data.size = CNID_HEADER_LEN + len + 1;
-
- /* Start off with the hint. It should be in network byte order.
- * We need to make sure that somebody doesn't add in restricted
- * cnid's to the database. */
- if (ntohl(hint) >= CNID_START) {
- /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
- rc = add_cnid(db, NULL, &key, &data);
- switch (rc) {
- case DB_KEYEXIST: /* Need to use RootInfo after all. */
- break;
- default:
- syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
- ntohl(hint), db_strerror(rc));
- goto cleanup_err;
- case 0:
+ return id;
+ }
+
+ /* Initialize our DBT data structures. */
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ /* Just tickle hint, and the key will change (gotta love pointers). */
+ key.data = &hint;
+ key.size = sizeof(hint);
+
+ if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
+ syslog(LOG_ERR, "cnid_add: Path name is too long");
+ goto cleanup_err;
+ }
+
+ data.size = CNID_HEADER_LEN + len + 1;
+
+ /* Start off with the hint. It should be in network byte order.
+ * We need to make sure that somebody doesn't add in restricted
+ * cnid's to the database. */
+ if (ntohl(hint) >= CNID_START) {
+ /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
+ rc = add_cnid(db, NULL, &key, &data);
+ switch (rc) {
+ case DB_KEYEXIST: /* Need to use RootInfo after all. */
+ break;
+ default:
+ syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
+ ntohl(hint), db_strerror(rc));
+ goto cleanup_err;
+ case 0:
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_add: Used hint for did %u, name %s as %u",
- ntohl(did), name, ntohl(hint));
+ syslog(LOG_INFO, "cnid_add: Used hint for did %u, name %s as %u",
+ ntohl(did), name, ntohl(hint));
#endif
- return hint;
- }
- }
-
- memset(&rootinfo_key, 0, sizeof(rootinfo_key));
- memset(&rootinfo_data, 0, sizeof(rootinfo_data));
- rootinfo_key.data = ROOTINFO_KEY;
- rootinfo_key.size = ROOTINFO_KEYLEN;
+ return hint;
+ }
+ }
+
+ memset(&rootinfo_key, 0, sizeof(rootinfo_key));
+ memset(&rootinfo_data, 0, sizeof(rootinfo_data));
+ rootinfo_key.data = ROOTINFO_KEY;
+ rootinfo_key.size = ROOTINFO_KEYLEN;
retry:
- if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
- syslog(LOG_ERR, "cnid_add: Failed to begin transaction: %s",
- db_strerror(rc));
- goto cleanup_err;
- }
-
- /* Get the key. */
- switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
- &rootinfo_data, DB_RMW)) {
- case DB_LOCK_DEADLOCK:
- if ((rc = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
- goto cleanup_err;
- }
- goto retry;
- case 0:
- memcpy(&hint, rootinfo_data.data, sizeof(hint));
+ if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_add: Failed to begin transaction: %s",
+ db_strerror(rc));
+ goto cleanup_err;
+ }
+
+ /* Get the key. */
+ switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
+ &rootinfo_data, DB_RMW)) {
+ case DB_LOCK_DEADLOCK:
+ if ((rc = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
+ goto cleanup_err;
+ }
+ goto retry;
+ case 0:
+ memcpy(&hint, rootinfo_data.data, sizeof(hint));
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
+ syslog(LOG_INFO, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
#endif
- break;
- case DB_NOTFOUND:
- hint = htonl(CNID_START);
+ break;
+ case DB_NOTFOUND:
+ hint = htonl(CNID_START);
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_add: Using CNID_START for did %u, name %s",
- ntohl(did), name);
+ syslog(LOG_INFO, "cnid_add: Using CNID_START for did %u, name %s",
+ ntohl(did), name);
#endif
- break;
- default:
- syslog(LOG_ERR, "cnid_add: Unable to lookup rootinfo: %s",
- db_strerror(rc));
- goto cleanup_abort;
- }
-
- /* Search for a new id. We keep the first id around to check for
- * wrap-around. NOTE: I do it this way so that we can go back and
- * fill in holes. */
- save = id = ntohl(hint);
- while ((rc = add_cnid(db, tid, &key, &data)) != 0) {
- /* Don't use any special CNIDs. */
- if (++id < CNID_START) {
- id = CNID_START;
- }
-
- if ((rc != DB_KEYEXIST) || (save == id)) {
- syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
- ntohl(hint), db_strerror(rc));
- goto cleanup_abort;
- }
- hint = htonl(id);
- }
-
- rootinfo_data.data = &hint;
- rootinfo_data.size = sizeof(hint);
-
- switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
- case DB_LOCK_DEADLOCK:
- if ((rc = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
- goto cleanup_err;
- }
- goto retry;
- case 0:
- break;
- default:
- syslog(LOG_ERR, "cnid_add: Unable to update rootinfo: %s",
- db_strerror(rc));
- goto cleanup_abort;
- }
+ break;
+ default:
+ syslog(LOG_ERR, "cnid_add: Unable to lookup rootinfo: %s",
+ db_strerror(rc));
+ goto cleanup_abort;
+ }
+
+ /* Search for a new id. We keep the first id around to check for
+ * wrap-around. NOTE: I do it this way so that we can go back and
+ * fill in holes. */
+ save = id = ntohl(hint);
+ while ((rc = add_cnid(db, tid, &key, &data)) != 0) {
+ /* Don't use any special CNIDs. */
+ if (++id < CNID_START) {
+ id = CNID_START;
+ }
+
+ if ((rc != DB_KEYEXIST) || (save == id)) {
+ syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
+ ntohl(hint), db_strerror(rc));
+ goto cleanup_abort;
+ }
+ hint = htonl(id);
+ }
+
+ rootinfo_data.data = &hint;
+ rootinfo_data.size = sizeof(hint);
+
+ switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
+ case DB_LOCK_DEADLOCK:
+ if ((rc = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
+ goto cleanup_err;
+ }
+ goto retry;
+ case 0:
+ break;
+ default:
+ syslog(LOG_ERR, "cnid_add: Unable to update rootinfo: %s",
+ db_strerror(rc));
+ goto cleanup_abort;
+ }
cleanup_commit:
- /* The transaction finished, commit it. */
- if ((rc = txn_commit(tid, 0)) != 0) {
- syslog(LOG_ERR, "cnid_add: Unable to commit transaction: %s",
- db_strerror(rc));
- goto cleanup_err;
- }
+ /* The transaction finished, commit it. */
+ if ((rc = txn_commit(tid, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_add: Unable to commit transaction: %s",
+ db_strerror(rc));
+ goto cleanup_err;
+ }
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_add: Returned CNID for did %u, name %s as %u",
- ntohl(did), name, ntohl(hint));
+ syslog(LOG_INFO, "cnid_add: Returned CNID for did %u, name %s as %u",
+ ntohl(did), name, ntohl(hint));
#endif
- return hint;
+ return hint;
cleanup_abort:
- txn_abort(tid);
+ txn_abort(tid);
cleanup_err:
- return 0;
+ return 0;
}
#endif /* CNID_DB */
/*
- * $Id: cnid_close.c,v 1.12 2001-10-21 08:33:33 jmarcus Exp $
+ * $Id: cnid_close.c,v 1.12.2.1 2001-12-03 05:05:46 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
#include "cnid_private.h"
void cnid_close(void *CNID) {
- CNID_private *db;
- int rc;
+ CNID_private *db;
+ int rc;
- if (!(db = CNID)) {
- return;
- }
+ if (!(db = CNID)) {
+ return;
+ }
- /* Flush the transaction log and delete the log file if we can. */
- if ((db->lockfd > -1) && ((db->flags & CNIDFLAG_DB_RO) == 0)) {
- struct flock lock;
- char wd[MAXPATHLEN + 1];
+ /* Flush the transaction log and delete the log file if we can. */
+ if ((db->lockfd > -1) && ((db->flags & CNIDFLAG_DB_RO) == 0)) {
+ struct flock lock;
+ char wd[MAXPATHLEN + 1];
- /* Save the current working directory so we can restore it
- * when we're done. */
- getcwd(wd, MAXPATHLEN);
+ /* Save the current working directory so we can restore it
+ * when we're done. */
+ getcwd(wd, MAXPATHLEN);
- lock.l_type = F_WRLCK;
- lock.l_whence = SEEK_SET;
- lock.l_start = lock.l_len = 0;
- if (fcntl(db->lockfd, F_SETLK, &lock) == 0) {
- char **list, **first;
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = lock.l_len = 0;
+ if (fcntl(db->lockfd, F_SETLK, &lock) == 0) {
+ char **list, **first;
- chdir(db->dbenv->db_log_dir ? db->dbenv->db_log_dir : db->dbenv->db_home);
+ chdir(db->dbenv->db_log_dir ? db->dbenv->db_log_dir : db->dbenv->db_home);
#if DB_VERSION_MINOR > 2
- if ((rc = log_archive(db->dbenv, &list, 0)) != 0) {
+ if ((rc = log_archive(db->dbenv, &list, 0)) != 0) {
#else /* DB_VERSION_MINOR < 2 */
- if ((rc = log_archive(db->dbenv, &list, 0, NULL)) != 0) {
+ if ((rc = log_archive(db->dbenv, &list, 0, NULL)) != 0) {
#endif /* DB_VERSION_MINOR */
- syslog(LOG_ERR, "cnid_close: Unable to archive logfiles: %s",
- db_strerror(rc));
- }
+ syslog(LOG_ERR, "cnid_close: Unable to archive logfiles: %s",
+ db_strerror(rc));
+ }
- if (list != NULL) {
- for (first = list; *list != NULL; ++list) {
- if ((rc = remove(*list)) != 0) {
- syslog(LOG_INFO, "cnid_close: failed to remove %s: %s",
- *list, strerror(rc));
- }
- }
- free(first);
- }
- chdir(wd);
- }
- }
+ if (list != NULL) {
+ for (first = list; *list != NULL; ++list) {
+ if ((rc = remove(*list)) != 0) {
+ syslog(LOG_INFO, "cnid_close: failed to remove %s: %s",
+ *list, strerror(rc));
+ }
+ }
+ free(first);
+ }
+ chdir(wd);
+ }
+ }
- db->db_didname->close(db->db_didname, 0);
- db->db_devino->close(db->db_devino, 0);
- db->db_cnid->close(db->db_cnid, 0);
- db->dbenv->close(db->dbenv, 0);
+ db->db_didname->close(db->db_didname, 0);
+ db->db_devino->close(db->db_devino, 0);
+ db->db_cnid->close(db->db_cnid, 0);
+ db->dbenv->close(db->dbenv, 0);
- if (db->lockfd > -1) {
- close(db->lockfd); /* This will also release any locks we have. */
- }
+ if (db->lockfd > -1) {
+ close(db->lockfd); /* This will also release any locks we have. */
+ }
- free(db);
+ free(db);
}
#endif /* CNID_DB */
/*
- * $Id: cnid_delete.c,v 1.9 2001-10-22 03:40:19 jmarcus Exp $
+ * $Id: cnid_delete.c,v 1.9.2.1 2001-12-03 05:05:47 jmarcus Exp $
*
* Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT.
#include "cnid_private.h"
int cnid_delete(void *CNID, const cnid_t id) {
- CNID_private *db;
- DBT key, data;
- DB_TXN *tid;
- int rc;
+ CNID_private *db;
+ DBT key, data;
+ DB_TXN *tid;
+ int rc;
- if (!(db = CNID) || !id || (db->flags & CNIDFLAG_DB_RO)) {
- return -1;
- }
+ if (!(db = CNID) || !id || (db->flags & CNIDFLAG_DB_RO)) {
+ return -1;
+ }
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
retry:
- if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
- syslog(LOG_ERR, "cnid_delete: Failed to begin transaction: %s",
- db_strerror(rc));
- return rc;
- }
+ if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: Failed to begin transaction: %s",
+ db_strerror(rc));
+ return rc;
+ }
- /* Get from ain CNID database. */
- key.data = (cnid_t *)&id;
- key.size = sizeof(id);
- if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
- int ret;
- if ((ret = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_delete: txn_abort: %s", db_strerror(ret));
- return ret;
- }
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- goto retry;
- case DB_NOTFOUND:
- syslog(LOG_INFO, "cnid_delete: CNID %u not in database",
- ntohl(id));
- return 0;
- default:
- syslog(LOG_ERR, "cnid_delete: Unable to delete entry: %s",
- db_strerror(rc));
- return rc;
- }
- }
+ /* Get from ain CNID database. */
+ key.data = (cnid_t *)&id;
+ key.size = sizeof(id);
+ if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
+ int ret;
+ if ((ret = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: txn_abort: %s", db_strerror(ret));
+ return ret;
+ }
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ goto retry;
+ case DB_NOTFOUND:
+ syslog(LOG_INFO, "cnid_delete: CNID %u not in database",
+ ntohl(id));
+ return 0;
+ default:
+ syslog(LOG_ERR, "cnid_delete: Unable to delete entry: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ }
- /* Now delete from dev/ino database. */
- key.data = data.data;
- key.size = CNID_DEVINO_LEN;
- if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- if ((rc = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
- db_strerror(rc));
- return rc;
- }
- goto retry;
- case DB_NOTFOUND:
- /* Quietly fall through if the entry isn't found. */
- break;
- default:
- if ((rc = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
- db_strerror(rc));
- return rc;
- }
- goto abort_err;
- }
- }
+ /* Now delete from dev/ino database. */
+ key.data = data.data;
+ key.size = CNID_DEVINO_LEN;
+ if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ if ((rc = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ goto retry;
+ case DB_NOTFOUND:
+ /* Quietly fall through if the entry isn't found. */
+ break;
+ default:
+ if ((rc = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ goto abort_err;
+ }
+ }
- /* Get data from the did/name database.
- * TODO Also handle did/macname, did/shortname, and did/longname. */
-
- key.data = (char *)data.data + CNID_DEVINO_LEN;
- key.size = data.size - CNID_DEVINO_LEN;
- if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- if ((rc = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
- db_strerror(rc));
- return rc;
- }
- goto retry;
- case DB_NOTFOUND:
- break;
- default:
- if ((rc = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
- db_strerror(rc));
- return rc;
- }
- goto abort_err;
- }
- }
+ /* Get data from the did/name database.
+ * TODO Also handle did/macname, did/shortname, and did/longname. */
- /* Now delete from the main CNID database. */
- key.data = (cnid_t *)&id;
- key.size = sizeof(id);
- if ((rc = db->db_cnid->del(db->db_cnid, tid, &key, 0))) {
- int ret;
- if ((ret = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_delete: txn_abort: %s", db_strerror(ret));
- return ret;
- }
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- goto retry;
- default:
- goto abort_err;
- }
- }
+ key.data = (char *)data.data + CNID_DEVINO_LEN;
+ key.size = data.size - CNID_DEVINO_LEN;
+ if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ if ((rc = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ goto retry;
+ case DB_NOTFOUND:
+ break;
+ default:
+ if ((rc = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: txn_abort: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ goto abort_err;
+ }
+ }
+
+ /* Now delete from the main CNID database. */
+ key.data = (cnid_t *)&id;
+ key.size = sizeof(id);
+ if ((rc = db->db_cnid->del(db->db_cnid, tid, &key, 0))) {
+ int ret;
+ if ((ret = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: txn_abort: %s", db_strerror(ret));
+ return ret;
+ }
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ goto retry;
+ default:
+ goto abort_err;
+ }
+ }
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_delete: Deleting CNID %u", ntohl(id));
+ syslog(LOG_INFO, "cnid_delete: Deleting CNID %u", ntohl(id));
#endif
- if ((rc = txn_commit(tid, 0)) != 0) {
- syslog(LOG_ERR, "cnid_delete: Failed to commit transaction: %s",
- db_strerror(rc));
- return rc;
- }
- return 0;
+ if ((rc = txn_commit(tid, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_delete: Failed to commit transaction: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ return 0;
abort_err:
- syslog(LOG_ERR, "cnid_delete: Unable to delete CNID %u: %s",
- ntohl(id), db_strerror(rc));
- return rc;
+ syslog(LOG_ERR, "cnid_delete: Unable to delete CNID %u: %s",
+ ntohl(id), db_strerror(rc));
+ return rc;
}
#endif /*CNID_DB */
/*
- * $Id: cnid_get.c,v 1.9 2001-10-21 08:33:33 jmarcus Exp $
+ * $Id: cnid_get.c,v 1.9.2.1 2001-12-03 05:05:47 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
/* Return CNID for a given did/name. */
cnid_t cnid_get(void *CNID, const cnid_t did, const char *name,
- const int len)
+ const int len)
{
- char start[CNID_DID_LEN + MAXPATHLEN + 1], *buf;
- CNID_private *db;
- DBT key, data;
- cnid_t id;
- int rc;
+ char start[CNID_DID_LEN + MAXPATHLEN + 1], *buf;
+ CNID_private *db;
+ DBT key, data;
+ cnid_t id;
+ int rc;
- if (!(db = CNID) || (len > MAXPATHLEN)) {
- return 0;
- }
+ if (!(db = CNID) || (len > MAXPATHLEN)) {
+ return 0;
+ }
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
- buf = start;
- memcpy(buf, &did, sizeof(did));
- buf += sizeof(did);
- memcpy(buf, name, len);
- *(buf + len) = '\0'; /* Make it a C-string. */
- key.data = start;
- key.size = CNID_DID_LEN + len + 1;
+ buf = start;
+ memcpy(buf, &did, sizeof(did));
+ buf += sizeof(did);
+ memcpy(buf, name, len);
+ *(buf + len) = '\0'; /* Make it a C-string. */
+ key.data = start;
+ key.size = CNID_DID_LEN + len + 1;
- while ((rc = db->db_didname->get(db->db_didname, NULL, &key, &data, 0))) {
- if (rc == DB_LOCK_DEADLOCK) {
- continue;
- }
+ while ((rc = db->db_didname->get(db->db_didname, NULL, &key, &data, 0))) {
+ if (rc == DB_LOCK_DEADLOCK) {
+ continue;
+ }
- if (rc != DB_NOTFOUND) {
- syslog(LOG_ERR, "cnid_get: Unable to get CNID %u, name %s: %s",
- ntohl(did), name, db_strerror(rc));
- }
+ if (rc != DB_NOTFOUND) {
+ syslog(LOG_ERR, "cnid_get: Unable to get CNID %u, name %s: %s",
+ ntohl(did), name, db_strerror(rc));
+ }
- return 0;
- }
+ return 0;
+ }
- memcpy(&id, data.data, sizeof(id));
+ memcpy(&id, data.data, sizeof(id));
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_get: Returning CNID for %u, name %s as %u",
- ntohl(did), name, ntohl(id));
+ syslog(LOG_INFO, "cnid_get: Returning CNID for %u, name %s as %u",
+ ntohl(did), name, ntohl(id));
#endif
- return id;
+ return id;
}
#endif /* CNID_DB */
/*
- * $Id: cnid_lookup.c,v 1.9 2001-10-21 08:33:33 jmarcus Exp $
+ * $Id: cnid_lookup.c,v 1.9.2.1 2001-12-03 05:05:48 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
/* This returns the CNID corresponding to a particular file. It will
* also fix up the various databases if there's a problem. */
cnid_t cnid_lookup(void *CNID, const struct stat *st, const cnid_t did,
- const char *name, const int len)
+ const char *name, const int len)
{
- char *buf;
- CNID_private *db;
- DBT key, devdata, diddata;
- int devino = 1, didname = 1;
- cnid_t id = 0;
- int rc;
-
- if (!(db = CNID) || !st || !name) {
- return 0;
- }
-
- /* Do a little checkpointing if necessary. I stuck it here as cnid_lookup
- * gets called when we do directory lookups. Only do this if we're using
- * a read-write database. */
- if ((db->flags & CNIDFLAG_DB_RO) == 0) {
+ char *buf;
+ CNID_private *db;
+ DBT key, devdata, diddata;
+ int devino = 1, didname = 1;
+ cnid_t id = 0;
+ int rc;
+
+ if (!(db = CNID) || !st || !name) {
+ return 0;
+ }
+
+ /* Do a little checkpointing if necessary. I stuck it here as cnid_lookup
+ * gets called when we do directory lookups. Only do this if we're using
+ * a read-write database. */
+ if ((db->flags & CNIDFLAG_DB_RO) == 0) {
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_lookup: Running database checkpoint");
+ syslog(LOG_INFO, "cnid_lookup: Running database checkpoint");
#endif
- switch (rc = txn_checkpoint(db->dbenv, LOGFILEMAX, CHECKTIMEMAX, 0)) {
- case 0:
- case DB_INCOMPLETE:
- break;
- default:
- syslog(LOG_ERR, "cnid_lookup: txn_checkpoint: %s",
- db_strerror(rc));
- return 0;
- }
- }
-
- if ((buf = make_cnid_data(st, did, name, len)) == NULL) {
- syslog(LOG_ERR, "cnid_lookup: Pathname is too long");
- return 0;
- }
-
- memset(&key, 0, sizeof(key));
- memset(&devdata, 0, sizeof(devdata));
- memset(&diddata, 0, sizeof(diddata));
-
- /* Look for a CNID. We have two options: dev/ino or did/name. If we
- * only get a match in one of them, that means a file has moved. */
- key.data = buf;
- key.size = CNID_DEVINO_LEN;
- while ((rc = db->db_devino->get(db->db_devino, NULL, &key, &devdata, 0))) {
- if (rc == DB_LOCK_DEADLOCK) {
- continue;
- }
-
- if (rc == DB_NOTFOUND) {
- devino = 0;
- break;
- }
-
- syslog(LOG_ERR, "cnid_lookup: Unable to get CNID dev %u, ino %u: %s",
- st->st_dev, st->st_ino, db_strerror(rc));
- return 0;
- }
-
- /* did/name now */
- key.data = buf + CNID_DEVINO_LEN;
- key.size = CNID_DID_LEN + len + 1;
- while ((rc = db->db_didname->get(db->db_didname, NULL, &key, &diddata, 0))) {
- if (rc == DB_LOCK_DEADLOCK) {
- continue;
- }
-
- if (rc == DB_NOTFOUND) {
- didname = 0;
- break;
- }
-
- syslog(LOG_ERR, "cnid_lookup: Unable to get CNID %u, name %s: %s",
- ntohl(did), name, db_strerror(rc));
- return 0;
- }
-
- /* Set id. Honor did/name over dev/ino as dev/ino isn't necessarily
- * 1-1. */
- if (didname) {
- memcpy(&id, diddata.data, sizeof(id));
- }
- else if (devino) {
- memcpy(&id, devdata.data, sizeof(id));
- }
-
- /* Either entries are in both databases or neither of them. */
- if ((devino && didname) || !(devino || didname)) {
+ switch (rc = txn_checkpoint(db->dbenv, LOGFILEMAX, CHECKTIMEMAX, 0)) {
+ case 0:
+ case DB_INCOMPLETE:
+ break;
+ default:
+ syslog(LOG_ERR, "cnid_lookup: txn_checkpoint: %s",
+ db_strerror(rc));
+ return 0;
+ }
+ }
+
+ if ((buf = make_cnid_data(st, did, name, len)) == NULL) {
+ syslog(LOG_ERR, "cnid_lookup: Pathname is too long");
+ return 0;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&devdata, 0, sizeof(devdata));
+ memset(&diddata, 0, sizeof(diddata));
+
+ /* Look for a CNID. We have two options: dev/ino or did/name. If we
+ * only get a match in one of them, that means a file has moved. */
+ key.data = buf;
+ key.size = CNID_DEVINO_LEN;
+ while ((rc = db->db_devino->get(db->db_devino, NULL, &key, &devdata, 0))) {
+ if (rc == DB_LOCK_DEADLOCK) {
+ continue;
+ }
+
+ if (rc == DB_NOTFOUND) {
+ devino = 0;
+ break;
+ }
+
+ syslog(LOG_ERR, "cnid_lookup: Unable to get CNID dev %u, ino %u: %s",
+ st->st_dev, st->st_ino, db_strerror(rc));
+ return 0;
+ }
+
+ /* did/name now */
+ key.data = buf + CNID_DEVINO_LEN;
+ key.size = CNID_DID_LEN + len + 1;
+ while ((rc = db->db_didname->get(db->db_didname, NULL, &key, &diddata, 0))) {
+ if (rc == DB_LOCK_DEADLOCK) {
+ continue;
+ }
+
+ if (rc == DB_NOTFOUND) {
+ didname = 0;
+ break;
+ }
+
+ syslog(LOG_ERR, "cnid_lookup: Unable to get CNID %u, name %s: %s",
+ ntohl(did), name, db_strerror(rc));
+ return 0;
+ }
+
+ /* Set id. Honor did/name over dev/ino as dev/ino isn't necessarily
+ * 1-1. */
+ if (didname) {
+ memcpy(&id, diddata.data, sizeof(id));
+ }
+ else if (devino) {
+ memcpy(&id, devdata.data, sizeof(id));
+ }
+
+ /* Either entries are in both databases or neither of them. */
+ if ((devino && didname) || !(devino || didname)) {
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_lookup: Looked up did %u, name %s, as %u",
- ntohl(did), name, ntohl(id));
+ syslog(LOG_INFO, "cnid_lookup: Looked up did %u, name %s, as %u",
+ ntohl(did), name, ntohl(id));
#endif
- return id;
- }
+ return id;
+ }
- /* Fix up the database. */
- cnid_update(db, id, st, did, name, len);
+ /* Fix up the database. */
+ cnid_update(db, id, st, did, name, len);
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_lookup: Looked up did %u, name %s, as %u (needed update)", ntohl(did), name, ntohl(id));
+ syslog(LOG_INFO, "cnid_lookup: Looked up did %u, name %s, as %u (needed update)", ntohl(did), name, ntohl(id));
#endif
- return id;
+ return id;
}
#endif /* CNID_DB */
/*
- * $Id: cnid_nextid.c,v 1.6 2001-08-31 14:58:48 rufustfirefly Exp $
+ * $Id: cnid_nextid.c,v 1.6.2.1 2001-12-03 05:05:48 jmarcus Exp $
*/
#ifdef unused
* mapped. */
cnid_t cnid_nextid(void *CNID)
{
- CNID_private *db;
- cnid_t id;
+ CNID_private *db;
+ cnid_t id;
- if (!(db = CNID))
- return 0;
+ if (!(db = CNID))
+ return 0;
- memcpy(&id, ad_entry(&db->rootinfo, ADEID_DID), sizeof(id));
- return id;
+ memcpy(&id, ad_entry(&db->rootinfo, ADEID_DID), sizeof(id));
+ return id;
}
#endif /* CNID_DB */
#endif
/*
- * $Id: cnid_open.c,v 1.19 2001-11-05 16:06:32 jmarcus Exp $
+ * $Id: cnid_open.c,v 1.19.2.1 2001-12-03 05:05:48 jmarcus Exp $
*
* Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT.
#endif /* DB_VERSION_MINOR */
#define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
- * databases. */
+* databases. */
/* the first compare that's always done. */
static __inline__ int compare_did(const DBT *a, const DBT *b)
{
- u_int32_t dida, didb;
+ u_int32_t dida, didb;
- memcpy(&dida, a->data, sizeof(dida));
- memcpy(&didb, b->data, sizeof(didb));
- return dida - didb;
+ memcpy(&dida, a->data, sizeof(dida));
+ memcpy(&didb, b->data, sizeof(didb));
+ return dida - didb;
}
/* sort did's and then names. this is for unix paths.
static int compare_unix(const DBT *a, const DBT *b)
#endif /* DB_VERSION_MINOR */
{
- u_int8_t *sa, *sb;
- int len, ret;
+ u_int8_t *sa, *sb;
+ int len, ret;
- /* sort by did */
- if ((ret = compare_did(a, b)))
- return ret;
+ /* sort by did */
+ if ((ret = compare_did(a, b)))
+ return ret;
- sa = (u_int8_t *) a->data + 4; /* shift past did */
- sb = (u_int8_t *) b->data + 4;
- for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
- if ((ret = (*sa - *sb)))
- return ret; /* sort by lexical ordering */
+ sa = (u_int8_t *) a->data + 4; /* shift past did */
+ sb = (u_int8_t *) b->data + 4;
+ for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
+ if ((ret = (*sa - *sb)))
+ return ret; /* sort by lexical ordering */
- return a->size - b->size; /* sort by length */
+ return a->size - b->size; /* sort by length */
}
/* sort did's and then names. this is for macified paths (i.e.,
static int compare_mac(const DBT *a, const DBT *b)
#endif /* DB_VERSION_MINOR */
{
- u_int8_t *sa, *sb;
- int len, ret;
+ u_int8_t *sa, *sb;
+ int len, ret;
- /* sort by did */
- if ((ret = compare_did(a, b)))
- return ret;
+ /* sort by did */
+ if ((ret = compare_did(a, b)))
+ return ret;
- sa = (u_int8_t *) a->data + 4;
- sb = (u_int8_t *) b->data + 4;
- for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
- if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
- return ret; /* sort by lexical ordering */
+ sa = (u_int8_t *) a->data + 4;
+ sb = (u_int8_t *) b->data + 4;
+ for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
+ if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
+ return ret; /* sort by lexical ordering */
- return a->size - b->size; /* sort by length */
+ return a->size - b->size; /* sort by length */
}
static int have_lock = 0;
void *cnid_open(const char *dir) {
- struct stat st;
- struct flock lock;
- char path[MAXPATHLEN + 1];
- CNID_private *db;
- DBT key, data;
- DB_TXN *tid;
- u_int32_t DBEXTRAS = 0;
- int open_flag, len;
- int rc;
-
- if (!dir) {
- return NULL;
- }
-
- /* this checks .AppleDB */
- if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
- syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
- return NULL;
- }
-
- if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
- syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
- return NULL;
- }
-
- db->magic = CNID_DB_MAGIC;
-
- strcpy(path, dir);
- if (path[len - 1] != '/') {
- strcat(path, "/");
- len++;
- }
-
- lock.l_type = F_WRLCK;
- lock.l_whence = SEEK_SET;
-
- strcpy(path + len, DBHOME);
- if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
- syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed for %s", path);
- goto fail_adouble;
- }
-
- /* Search for a byte lock. This allows us to cleanup the log files
- * at cnid_close() in a clean fashion.
- *
- * NOTE: This won't work if multiple volumes for the same user refer
- * to the sahe directory. */
- strcat(path, DBLOCKFILE);
- if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
- lock.l_start = 0;
- lock.l_len = 1;
- while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
- if (++lock.l_start > MAXITER) {
- syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
- close(db->lockfd);
- db->lockfd = -1;
- break;
- }
- }
- }
- else {
- syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
- }
-
- if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
- /* We test to see if we have exclusive database access. If we do, we
- * will open the database with the DB_RECOVER flag.
- */
+ struct stat st;
+ struct flock lock;
+ char path[MAXPATHLEN + 1];
+ CNID_private *db;
+ DBT key, data;
+ DB_TXN *tid;
+ u_int32_t DBEXTRAS = 0;
+ int open_flag, len;
+ int rc;
+
+ if (!dir) {
+ return NULL;
+ }
+
+ /* this checks .AppleDB */
+ if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
+ syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
+ return NULL;
+ }
+
+ if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
+ syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
+ return NULL;
+ }
+
+ db->magic = CNID_DB_MAGIC;
+
+ strcpy(path, dir);
+ if (path[len - 1] != '/') {
+ strcat(path, "/");
+ len++;
+ }
+
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+
+ strcpy(path + len, DBHOME);
+ if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
+ syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed for %s", path);
+ goto fail_adouble;
+ }
+
+ /* Search for a byte lock. This allows us to cleanup the log files
+ * at cnid_close() in a clean fashion.
+ *
+ * NOTE: This won't work if multiple volumes for the same user refer
+ * to the sahe directory. */
+ strcat(path, DBLOCKFILE);
+ if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
+ lock.l_start = 0;
+ lock.l_len = 1;
+ while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
+ if (++lock.l_start > MAXITER) {
+ syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
+ close(db->lockfd);
+ db->lockfd = -1;
+ break;
+ }
+ }
+ }
+ else {
+ syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
+ }
+
+ if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
+ /* We test to see if we have exclusive database access. If we do, we
+ * will open the database with the DB_RECOVER flag.
+ */
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_open: Opening database environment %s with DB_RECOVER flag", path);
+ syslog(LOG_INFO, "cnid_open: Opening database environment %s with DB_RECOVER flag", path);
#endif
- DBEXTRAS |= DB_RECOVER;
- have_lock = 1;
- }
+ DBEXTRAS |= DB_RECOVER;
+ have_lock = 1;
+ }
- path[len + DBHOMELEN] = '\0';
- open_flag = DB_CREATE;
+ path[len + DBHOMELEN] = '\0';
+ open_flag = DB_CREATE;
- /* We need to be able to open the database environment with full
- * transaction, logging, and locking support if we ever hope to
- * be a true multi-acess file server. */
- if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
- goto fail_lock;
- }
+ /* We need to be able to open the database environment with full
+ * transaction, logging, and locking support if we ever hope to
+ * be a true multi-acess file server. */
+ if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
+ goto fail_lock;
+ }
- /* Setup internal deadlock detection. */
- if ((rc = db->dbenv->set_lk_detect(db->dbenv, DB_LOCK_DEFAULT)) != 0) {
- syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
- goto fail_lock;
- }
+ /* Setup internal deadlock detection. */
+ if ((rc = db->dbenv->set_lk_detect(db->dbenv, DB_LOCK_DEFAULT)) != 0) {
+ syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
+ goto fail_lock;
+ }
#if DB_VERSION_MINOR > 1
- /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
-/* if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
- syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
- goto fail_lock;
- }*/
+ /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
+ /* if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
+ syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
+ goto fail_lock;
+ }*/
#endif /* DB_VERSION_MINOR > 1 */
- /* Open the database environment. */
- if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
- if (rc == DB_RUNRECOVERY) {
- /* This is the mother of all errors. We _must_ fail here. */
- syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
- goto fail_lock;
- }
-
- /* We can't get a full transactional environment, so multi-access
- * is out of the question. Let's assume a read-only environment,
- * and trry to at least get a shared memory pool. */
- if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
- /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
- * open the environment with no flags. */
- if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
- syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
- path, db_strerror(rc));
- goto fail_lock;
- }
- }
- db->flags |= CNIDFLAG_DB_RO;
- open_flag = DB_RDONLY;
- syslog(LOG_INFO, "cnid_open: Obtained read-only database environment %s", path);
- }
-
- /* did/name reverse mapping. We use a BTree for this one. */
- if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
- db_strerror(rc));
- goto fail_appinit;
- }
-
- db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
- if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
- DB_BTREE, open_flag, 0666))) {
- syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
- db_strerror(rc));
- goto fail_appinit;
- }
-
- /* Check for version. This way we can update the database if we need
- * to change the format in any way. */
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
- key.data = DBVERSION_KEY;
- key.size = DBVERSION_KEYLEN;
+ /* Open the database environment. */
+ if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
+ if (rc == DB_RUNRECOVERY) {
+ /* This is the mother of all errors. We _must_ fail here. */
+ syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
+ goto fail_lock;
+ }
+
+ /* We can't get a full transactional environment, so multi-access
+ * is out of the question. Let's assume a read-only environment,
+ * and trry to at least get a shared memory pool. */
+ if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
+ /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
+ * open the environment with no flags. */
+ if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
+ syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
+ path, db_strerror(rc));
+ goto fail_lock;
+ }
+ }
+ db->flags |= CNIDFLAG_DB_RO;
+ open_flag = DB_RDONLY;
+ syslog(LOG_INFO, "cnid_open: Obtained read-only database environment %s", path);
+ }
+
+ /* did/name reverse mapping. We use a BTree for this one. */
+ if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
+ db_strerror(rc));
+ goto fail_appinit;
+ }
+
+ db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
+ if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
+ DB_BTREE, open_flag, 0666))) {
+ syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
+ db_strerror(rc));
+ goto fail_appinit;
+ }
+
+ /* Check for version. This way we can update the database if we need
+ * to change the format in any way. */
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+ key.data = DBVERSION_KEY;
+ key.size = DBVERSION_KEYLEN;
dbversion_retry:
- if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
-
- while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
- int ret;
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- if ((ret = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
- goto dbversion_retry;
- case DB_NOTFOUND:
- {
- u_int32_t version = htonl(DBVERSION);
-
- data.data = &version;
- data.size = sizeof(version);
- }
-
- if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
- DB_NOOVERWRITE))) {
- if (ret == DB_LOCK_DEADLOCK) {
- if ((ret = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_open: txn_abort: %s",
- db_strerror(ret));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
- goto dbversion_retry;
- }
- else if (ret == DB_RUNRECOVERY) {
- /* At this point, we don't care if the transaction aborts
- * successfully or not. */
- txn_abort(tid);
- syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
- db_strerror(ret));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
- }
- break; /* while loop */
- default:
- txn_abort(tid);
- syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
- }
-
- if ((rc = txn_commit(tid, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
-
- /* TODO In the future we might check for version number here. */
+ if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+
+ while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
+ int ret;
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ if ((ret = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+ goto dbversion_retry;
+ case DB_NOTFOUND:
+ {
+ u_int32_t version = htonl(DBVERSION);
+
+ data.data = &version;
+ data.size = sizeof(version);
+ }
+
+ if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
+ DB_NOOVERWRITE))) {
+ if (ret == DB_LOCK_DEADLOCK) {
+ if ((ret = txn_abort(tid)) != 0) {
+ syslog(LOG_ERR, "cnid_open: txn_abort: %s",
+ db_strerror(ret));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+ goto dbversion_retry;
+ }
+ else if (ret == DB_RUNRECOVERY) {
+ /* At this point, we don't care if the transaction aborts
+ * successfully or not. */
+ txn_abort(tid);
+ syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
+ db_strerror(ret));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+ }
+ break; /* while loop */
+ default:
+ txn_abort(tid);
+ syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+ }
+
+ if ((rc = txn_commit(tid, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+
+ /* TODO In the future we might check for version number here. */
#if 0
- memcpy(&version, data.data, sizeof(version));
- if (version != ntohl(DBVERSION)) {
- /* Do stuff here. */
- }
+ memcpy(&version, data.data, sizeof(version));
+ if (version != ntohl(DBVERSION)) {
+ /* Do stuff here. */
+ }
#endif /* 0 */
#ifdef EXTENDED_DB
- /* did/macname (31 character) mapping. Use a BTree for this one. */
- if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
-
- db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
- if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- goto fail_appinit;
- }
-
- /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
- if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- db->db_macname->close(db->db_macname, 0);
- goto fail_appinit;
- }
-
- db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
- if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to open did/shortname database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- db->db_macname->close(db->db_macname, 0);
- goto fail_appinit;
- }
-
- /* did/longname (Unicode) mapping. Use a BTree for this one. */
- if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to create did/longname database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- db->db_macname->close(db->db_macname, 0);
- db->db_shortname->close(db->db_shortname, 0);
- goto fail_appinit;
- }
-
- db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
- if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
- db->db_macname->close(db->db_macname, 0);
- db->db_shortname->close(db->db_shortname, 0);
- goto fail_appinit;
- }
+ /* did/macname (31 character) mapping. Use a BTree for this one. */
+ if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+
+ db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
+ if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+
+ /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
+ if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ goto fail_appinit;
+ }
+
+ db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
+ if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to open did/shortname database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ goto fail_appinit;
+ }
+
+ /* did/longname (Unicode) mapping. Use a BTree for this one. */
+ if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to create did/longname database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ db->db_shortname->close(db->db_shortname, 0);
+ goto fail_appinit;
+ }
+
+ db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
+ if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ db->db_shortname->close(db->db_shortname, 0);
+ goto fail_appinit;
+ }
#endif /* EXTENDED_DB */
- /* dev/ino reverse mapping. Use a hash for this one. */
- if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
+ /* dev/ino reverse mapping. Use a hash for this one. */
+ if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
#ifdef EXTENDED_DB
- db->db_macname->close(db->db_macname, 0);
- db->db_shortname->close(db->db_shortname, 0);
- db->db_longname->close(db->db_longname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ db->db_shortname->close(db->db_shortname, 0);
+ db->db_longname->close(db->db_longname, 0);
#endif /* EXTENDED_DB */
- goto fail_appinit;
- }
+ goto fail_appinit;
+ }
- if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
+ if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
#ifdef EXTENDED_DB
- db->db_macname->close(db->db_macname, 0);
- db->db_shortname->close(db->db_shortname, 0);
- db->db_longname->close(db->db_longname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ db->db_shortname->close(db->db_shortname, 0);
+ db->db_longname->close(db->db_longname, 0);
#endif /* EXTENDED_DB */
- goto fail_appinit;
- }
-
- /* Main CNID database. Use a hash for this one. */
- if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
+ goto fail_appinit;
+ }
+
+ /* Main CNID database. Use a hash for this one. */
+ if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
#ifdef EXTENDED_DB
- db->db_macname->close(db->db_macname, 0);
- db->db_shortname->close(db->db_shortname, 0);
- db->db_longname->close(db->db_longname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ db->db_shortname->close(db->db_shortname, 0);
+ db->db_longname->close(db->db_longname, 0);
#endif /* EXTENDED_DB */
- db->db_devino->close(db->db_devino, 0);
- goto fail_appinit;
- }
+ db->db_devino->close(db->db_devino, 0);
+ goto fail_appinit;
+ }
- if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
- syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
- db_strerror(rc));
- db->db_didname->close(db->db_didname, 0);
+ if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
+ syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
+ db_strerror(rc));
+ db->db_didname->close(db->db_didname, 0);
#ifdef EXTENDED_DB
- db->db_macname->close(db->db_macname, 0);
- db->db_shortname->close(db->db_shortname, 0);
- db->db_longname->close(db->db_longname, 0);
+ db->db_macname->close(db->db_macname, 0);
+ db->db_shortname->close(db->db_shortname, 0);
+ db->db_longname->close(db->db_longname, 0);
#endif /* EXTENDED_DB */
- goto fail_appinit;
- }
+ goto fail_appinit;
+ }
- return db;
+ return db;
fail_appinit:
- syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
- db->dbenv->close(db->dbenv, 0);
+ syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
+ db->dbenv->close(db->dbenv, 0);
fail_lock:
- if (db->lockfd > -1) {
- close(db->lockfd);
- }
+ if (db->lockfd > -1) {
+ close(db->lockfd);
+ }
fail_adouble:
fail_db:
- free(db);
- return NULL;
+ free(db);
+ return NULL;
}
#endif /* CNID_DB */
/*
- * $Id: cnid_private.h,v 1.3 2001-08-14 14:00:10 rufustfirefly Exp $
+ * $Id: cnid_private.h,v 1.3.2.1 2001-12-03 05:05:50 jmarcus Exp $
*/
#ifndef LIBATALK_CNID_PRIVATE_H
#define ROOTINFO_KEYLEN 12
typedef struct CNID_private {
- u_int32_t magic;
- DB *db_cnid;
- DB *db_didname;
- DB *db_devino;
+ u_int32_t magic;
+ DB *db_cnid;
+ DB *db_didname;
+ DB *db_devino;
#ifdef EXTENDED_DB
- DB *db_shortname;
- DB *db_macname;
- DB *db_longname;
+ DB *db_shortname;
+ DB *db_macname;
+ DB *db_longname;
#endif /* EXTENDED_DB */
- DB_ENV* dbenv;
+ DB_ENV* dbenv;
int lockfd, flags;
} CNID_private;
/* construct db_cnid data. NOTE: this is not re-entrant. */
static __inline__ char *make_cnid_data(const struct stat *st,
- const cnid_t did,
- const char *name, const int len)
+ const cnid_t did,
+ const char *name, const int len)
{
- static char start[CNID_HEADER_LEN + MAXPATHLEN + 1];
- char *buf = start;
- u_int32_t i;
-
- if (len > MAXPATHLEN)
- return NULL;
-
- i = htonl(st->st_dev);
- buf = memcpy(buf, &i, sizeof(i));
- i = htonl(st->st_ino);
- buf = memcpy(buf + sizeof(i), &i, sizeof(i));
- /* did is already in network byte order */
- buf = memcpy(buf + sizeof(i), &did, sizeof(did));
- buf = memcpy(buf + sizeof(did), name, len);
- *(buf + len) = '\0';
- buf += len + 1;
-
- return start;
+ static char start[CNID_HEADER_LEN + MAXPATHLEN + 1];
+ char *buf = start;
+ u_int32_t i;
+
+ if (len > MAXPATHLEN)
+ return NULL;
+
+ i = htonl(st->st_dev);
+ buf = memcpy(buf, &i, sizeof(i));
+ i = htonl(st->st_ino);
+ buf = memcpy(buf + sizeof(i), &i, sizeof(i));
+ /* did is already in network byte order */
+ buf = memcpy(buf + sizeof(i), &did, sizeof(did));
+ buf = memcpy(buf + sizeof(did), name, len);
+ *(buf + len) = '\0';
+ buf += len + 1;
+
+ return start;
}
#endif /* atalk/cnid/cnid_private.h */
/*
- * $Id: cnid_resolve.c,v 1.8 2001-10-21 08:33:33 jmarcus Exp $
+ * $Id: cnid_resolve.c,v 1.8.2.1 2001-12-03 05:05:51 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
/* Return the did/name pair corresponding to a CNID. */
char *cnid_resolve(void *CNID, cnid_t *id) {
- CNID_private *db;
- DBT key, data;
- int rc;
-
- if (!(db = CNID) || !id || !(*id)) {
- return NULL;
- }
-
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
-
- key.data = id;
- key.size = sizeof(cnid_t);
- while ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0))) {
- if (rc == DB_LOCK_DEADLOCK) {
- continue;
- }
-
- if (rc != DB_NOTFOUND) {
- syslog(LOG_ERR, "cnid_resolve: Unable to get did/name: %s",
- db_strerror(rc));
- }
-
- *id = 0;
- return NULL;
- }
-
- memcpy(id, (char *)data.data + CNID_DEVINO_LEN, sizeof(cnid_t));
+ CNID_private *db;
+ DBT key, data;
+ int rc;
+
+ if (!(db = CNID) || !id || !(*id)) {
+ return NULL;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = id;
+ key.size = sizeof(cnid_t);
+ while ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0))) {
+ if (rc == DB_LOCK_DEADLOCK) {
+ continue;
+ }
+
+ if (rc != DB_NOTFOUND) {
+ syslog(LOG_ERR, "cnid_resolve: Unable to get did/name: %s",
+ db_strerror(rc));
+ }
+
+ *id = 0;
+ return NULL;
+ }
+
+ memcpy(id, (char *)data.data + CNID_DEVINO_LEN, sizeof(cnid_t));
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_resolve: Returning id = %u, did/name = %s",
- ntohl(*id), (char *)data.data + CNID_HEADER_LEN);
+ syslog(LOG_INFO, "cnid_resolve: Returning id = %u, did/name = %s",
+ ntohl(*id), (char *)data.data + CNID_HEADER_LEN);
#endif
- return (char *)data.data + CNID_HEADER_LEN;
+ return (char *)data.data + CNID_HEADER_LEN;
}
#endif /* CNID_DB */
/*
- * $Id: cnid_update.c,v 1.12 2001-10-21 08:33:33 jmarcus Exp $
+ * $Id: cnid_update.c,v 1.12.2.1 2001-12-03 05:05:51 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
* handle the did/name data, there are a bunch of functions to get
* and set the various fields. */
int cnid_update(void *CNID, const cnid_t id, const struct stat *st,
- const cnid_t did, const char *name, const int len
- /*, const char *info, const int infolen*/)
+ const cnid_t did, const char *name, const int len
+ /*, const char *info, const int infolen*/)
{
- CNID_private *db;
- DBT key, data, altdata;
- DB_TXN *tid;
- int rc;
+ CNID_private *db;
+ DBT key, data, altdata;
+ DB_TXN *tid;
+ int rc;
- if (!(db = CNID) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO)) {
- return -1;
- }
+ if (!(db = CNID) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO)) {
+ return -1;
+ }
- memset(&key, 0, sizeof(key));
- memset(&altdata, 0, sizeof(altdata));
+ memset(&key, 0, sizeof(key));
+ memset(&altdata, 0, sizeof(altdata));
retry:
- if ((rc = txn_begin(db->dbenv, NULL, &tid, 0))) {
- syslog(LOG_ERR, "cnid_update: Failed to begin transaction: %s",
- db_strerror(rc));
- return rc;
- }
-
- /* Get the old info. */
- key.data = (cnid_t *)&id;
- key.size = sizeof(id);
- memset(&data, 0, sizeof(data));
- if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
- txn_abort(tid);
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- goto retry;
- default:
- goto update_err;
- }
- }
-
- /* Delete the old dev/ino mapping. */
- key.data = data.data;
- key.size = CNID_DEVINO_LEN;
- if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- txn_abort(tid);
- goto retry;
- case DB_NOTFOUND:
- break;
- default:
- txn_abort(tid);
- goto update_err;
- }
- }
-
- /* Delete the old did/name mapping. */
- key.data = (char *) data.data + CNID_DEVINO_LEN;
- key.size = data.size - CNID_DEVINO_LEN;
- if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- txn_abort(tid);
- goto retry;
- case DB_NOTFOUND:
- break;
- default:
- txn_abort(tid);
- goto update_err;
- }
- }
-
- /* Make a new entry. */
- data.data = make_cnid_data(st, did, name, len);
- data.size = CNID_HEADER_LEN + len + 1;
-
- /* Put in a new dev/ino mapping. */
- key.data = data.data;
- key.size = CNID_DEVINO_LEN;
- altdata.data = (cnid_t *) &id;
- altdata.size = sizeof(id);
- if ((rc = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
- txn_abort(tid);
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- goto retry;
- default:
- goto update_err;
- }
- }
-
- /* put in a new did/name mapping. */
- key.data = (char *) data.data + CNID_DEVINO_LEN;
- key.size = data.size - CNID_DEVINO_LEN;
- if ((rc = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
- txn_abort(tid);
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- goto retry;
- default:
- goto update_err;
- }
- }
-
- /* Update the old CNID with the new info. */
- key.data = (cnid_t *) &id;
- key.size = sizeof(id);
- if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
- txn_abort(tid);
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- goto retry;
- default:
- goto update_err;
- }
- }
-
- return txn_commit(tid, 0);
+ if ((rc = txn_begin(db->dbenv, NULL, &tid, 0))) {
+ syslog(LOG_ERR, "cnid_update: Failed to begin transaction: %s",
+ db_strerror(rc));
+ return rc;
+ }
+
+ /* Get the old info. */
+ key.data = (cnid_t *)&id;
+ key.size = sizeof(id);
+ memset(&data, 0, sizeof(data));
+ if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
+ txn_abort(tid);
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ goto retry;
+ default:
+ goto update_err;
+ }
+ }
+
+ /* Delete the old dev/ino mapping. */
+ key.data = data.data;
+ key.size = CNID_DEVINO_LEN;
+ if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ txn_abort(tid);
+ goto retry;
+ case DB_NOTFOUND:
+ break;
+ default:
+ txn_abort(tid);
+ goto update_err;
+ }
+ }
+
+ /* Delete the old did/name mapping. */
+ key.data = (char *) data.data + CNID_DEVINO_LEN;
+ key.size = data.size - CNID_DEVINO_LEN;
+ if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ txn_abort(tid);
+ goto retry;
+ case DB_NOTFOUND:
+ break;
+ default:
+ txn_abort(tid);
+ goto update_err;
+ }
+ }
+
+ /* Make a new entry. */
+ data.data = make_cnid_data(st, did, name, len);
+ data.size = CNID_HEADER_LEN + len + 1;
+
+ /* Put in a new dev/ino mapping. */
+ key.data = data.data;
+ key.size = CNID_DEVINO_LEN;
+ altdata.data = (cnid_t *) &id;
+ altdata.size = sizeof(id);
+ if ((rc = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
+ txn_abort(tid);
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ goto retry;
+ default:
+ goto update_err;
+ }
+ }
+
+ /* put in a new did/name mapping. */
+ key.data = (char *) data.data + CNID_DEVINO_LEN;
+ key.size = data.size - CNID_DEVINO_LEN;
+ if ((rc = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
+ txn_abort(tid);
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ goto retry;
+ default:
+ goto update_err;
+ }
+ }
+
+ /* Update the old CNID with the new info. */
+ key.data = (cnid_t *) &id;
+ key.size = sizeof(id);
+ if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
+ txn_abort(tid);
+ switch (rc) {
+ case DB_LOCK_DEADLOCK:
+ goto retry;
+ default:
+ goto update_err;
+ }
+ }
+
+ return txn_commit(tid, 0);
update_err:
- syslog(LOG_ERR, "cnid_update: Unable to update CNID %u: %s",
- ntohl(id), db_strerror(rc));
- return -1;
+ syslog(LOG_ERR, "cnid_update: Unable to update CNID %u: %s",
+ ntohl(id), db_strerror(rc));
+ return -1;
}
#endif /* CNID_DB */