2 * $Id: cnid_update.c,v 1.19 2002-01-24 16:46:53 jmarcus Exp $
7 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
15 #include <atalk/logger.h>
18 #include <netatalk/endian.h>
19 #include <atalk/adouble.h>
20 #include <atalk/cnid.h>
22 #include "cnid_private.h"
24 /* cnid_update: takes the given cnid and updates the metadata. To
25 * handle the did/name data, there are a bunch of functions to get
26 * and set the various fields. */
27 int cnid_update(void *CNID, const cnid_t id, const struct stat *st,
28 const cnid_t did, const char *name, const int len
29 /*, const char *info, const int infolen*/)
32 DBT key, data, altdata;
36 if (!(db = CNID) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO)) {
40 memset(&key, 0, sizeof(key));
41 memset(&altdata, 0, sizeof(altdata));
44 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0))) {
45 LOG(log_error, logtype_default, "cnid_update: Failed to begin transaction: %s", db_strerror(rc));
49 /* Get the old info. */
50 key.data = (cnid_t *)&id;
51 key.size = sizeof(id);
52 memset(&data, 0, sizeof(data));
53 if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
56 case DB_LOCK_DEADLOCK:
59 /* Silently fail here. We're allowed to do this since this CNID
60 * might have been deleted out from under us, or someone has
61 * called cnid_lookup then cnid_update (which is redundant). */
68 /* Delete the old dev/ino mapping. */
70 key.size = CNID_DEVINO_LEN;
71 if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
73 case DB_LOCK_DEADLOCK:
84 /* Delete the old did/name mapping. */
85 key.data = (char *) data.data + CNID_DEVINO_LEN;
86 key.size = data.size - CNID_DEVINO_LEN;
87 if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
89 case DB_LOCK_DEADLOCK:
100 /* Make a new entry. */
101 data.data = make_cnid_data(st, did, name, len);
102 data.size = CNID_HEADER_LEN + len + 1;
104 /* Update the old CNID with the new info. */
105 key.data = (cnid_t *) &id;
106 key.size = sizeof(id);
107 if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
110 case DB_LOCK_DEADLOCK:
117 /* Put in a new dev/ino mapping. */
118 key.data = data.data;
119 key.size = CNID_DEVINO_LEN;
120 altdata.data = (cnid_t *) &id;
121 altdata.size = sizeof(id);
122 if ((rc = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
125 case DB_LOCK_DEADLOCK:
132 /* put in a new did/name mapping. */
133 key.data = (char *) data.data + CNID_DEVINO_LEN;
134 key.size = data.size - CNID_DEVINO_LEN;
135 if ((rc = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
138 case DB_LOCK_DEADLOCK:
146 return txn_commit(tid, 0);
149 LOG(log_error, logtype_default, "cnid_update: Unable to update CNID %u: %s",
150 ntohl(id), db_strerror(rc));