/*
- * $Id: cnid_update.c,v 1.4 2001-08-15 02:16:25 srittau Exp $
+ * $Id: cnid_update.c,v 1.12.2.4 2002-02-09 20:29:02 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
+#ifdef CNID_DB
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include "cnid_private.h"
-
-/* cnid_update: takes the given cnid and updates the metadata. to
- handle the did/name data, there are a bunch of functions to get
- and set the various fields. */
+/* cnid_update: takes the given cnid and updates the metadata. To
+ * 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;
-
- if (!(db = CNID) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO))
- return -1;
+ 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;
+ }
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
- memset(&altdata, 0, sizeof(altdata));
+ memset(&key, 0, sizeof(key));
+ memset(&altdata, 0, sizeof(altdata));
- /* begin a transaction */
retry:
- if ((errno = txn_begin(db->dbenv, NULL, &tid, 0))) {
- return errno;
- }
-
- /* get the old info */
- key.data = (cnid_t *) &id;
- key.size = sizeof(id);
- if ((errno = db->db_cnid->get(db->db_cnid, tid, &key, &data, 0))) {
- txn_abort(tid);
- if (errno == EAGAIN)
- goto retry;
- goto update_err;
- }
-
- /* delete the old dev/ino mapping */
- key.data = data.data;
- key.size = CNID_DEVINO_LEN;
- if ((errno = db->db_devino->del(db->db_devino, tid, &key, 0))) {
- if (errno == EAGAIN) {
- txn_abort(tid);
- goto 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;
}
- /* silently fail on a non-existent entry */
- if (errno != DB_NOTFOUND) {
- 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 ((errno = db->db_didname->del(db->db_didname, tid, &key, 0))) {
- if (errno == EAGAIN) {
- txn_abort(tid);
- goto retry;
+ /* 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;
+ case DB_NOTFOUND:
+ /* Silently fail here. We're allowed to do this since this CNID
+ * might have been deleted out from under us, or someone has
+ * called cnid_lookup then cnid_update (which is redundant). */
+ return 0;
+ default:
+ goto update_err;
+ }
}
- /* silently fail on a non-existent entry */
- if (errno != DB_NOTFOUND) {
- txn_abort(tid);
- 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 aliases if necessary */
+ /* 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 a new dev/ino mapping in */
- key.data = data.data;
- key.size = CNID_DEVINO_LEN;
- altdata.data = (cnid_t *) &id;
- altdata.size = sizeof(id);
- if ((errno = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
- txn_abort(tid);
- if (errno == EAGAIN) {
- goto retry;
+ /* Make a new entry. */
+ data.data = make_cnid_data(st, did, name, len);
+ data.size = CNID_HEADER_LEN + len + 1;
+
+ /* 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;
+ }
}
- goto update_err;
- }
-
- /* put a new did/name mapping in */
- key.data = (char *) data.data + CNID_DEVINO_LEN;
- key.size = data.size - CNID_DEVINO_LEN;
- if ((errno = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
- txn_abort(tid);
- if (errno == EAGAIN) {
- goto retry;
+
+ /* 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;
+ }
}
- goto update_err;
- }
-
- /* update the old CNID with the new info */
- key.data = (cnid_t *) &id;
- key.size = sizeof(id);
- if ((errno = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
- txn_abort(tid);
- if (errno == EAGAIN) {
- goto retry;
+
+ /* 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;
+ }
}
- goto update_err;
- }
- /* end transaction */
- return txn_commit(tid, 0);
+
+ return txn_commit(tid, 0);
update_err:
- syslog(LOG_ERR, "cnid_update: can't update CNID(%x)", id);
- return -1;
+ syslog(LOG_ERR, "cnid_update: Unable to update CNID %u: %s",
+ ntohl(id), db_strerror(rc));
+ return -1;
}
+#endif /* CNID_DB */