2 * $Id: cnid_update.c,v 1.20 2002-08-30 03:12:52 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"
26 #endif /* CNID_DB_CDB */
28 /* cnid_update: takes the given cnid and updates the metadata. To
29 * handle the did/name data, there are a bunch of functions to get
30 * and set the various fields. */
31 int cnid_update(void *CNID, const cnid_t id, const struct stat *st,
32 const cnid_t did, const char *name, const int len
33 /*, const char *info, const int infolen*/)
36 DBT key, data, altdata;
39 #endif /* CNID_DB_CDB */
42 if (!(db = CNID) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO)) {
46 memset(&key, 0, sizeof(key));
47 memset(&altdata, 0, sizeof(altdata));
51 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0))) {
52 LOG(log_error, logtype_default, "cnid_update: Failed to begin transaction: %s", db_strerror(rc));
55 #endif /* CNID_DB_CDB */
57 /* Get the old info. */
58 key.data = (cnid_t *)&id;
59 key.size = sizeof(id);
60 memset(&data, 0, sizeof(data));
62 if ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0))) {
63 #else /* CNID_DB_CDB */
64 if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
66 #endif /* CNID_DB_CDB */
69 case DB_LOCK_DEADLOCK:
71 #endif /* CNID_DB_CDB */
73 /* Silently fail here. We're allowed to do this since this CNID
74 * might have been deleted out from under us, or someone has
75 * called cnid_lookup then cnid_update (which is redundant). */
82 /* Delete the old dev/ino mapping. */
84 key.size = CNID_DEVINO_LEN;
85 if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
88 case DB_LOCK_DEADLOCK:
91 #endif /* CNID_DB_CDB */
97 #endif /* CNID_DB_CDB */
102 /* Delete the old did/name mapping. */
103 key.data = (char *) data.data + CNID_DEVINO_LEN;
104 key.size = data.size - CNID_DEVINO_LEN;
105 if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
108 case DB_LOCK_DEADLOCK:
111 #endif /* CNID_DB_CDB */
117 #endif /* CNID_DB_CDB */
122 /* Make a new entry. */
123 data.data = make_cnid_data(st, did, name, len);
124 data.size = CNID_HEADER_LEN + len + 1;
126 /* Update the old CNID with the new info. */
127 key.data = (cnid_t *) &id;
128 key.size = sizeof(id);
129 if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
132 #endif /* CNID_DB_CDB */
135 case DB_LOCK_DEADLOCK:
137 #endif /* CNID_DB_CDB */
143 /* Put in a new dev/ino mapping. */
144 key.data = data.data;
145 key.size = CNID_DEVINO_LEN;
146 altdata.data = (cnid_t *) &id;
147 altdata.size = sizeof(id);
148 if ((rc = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
151 #endif /* CNID_DB_CDB */
154 case DB_LOCK_DEADLOCK:
156 #endif /* CNID_DB_CDB */
162 /* put in a new did/name mapping. */
163 key.data = (char *) data.data + CNID_DEVINO_LEN;
164 key.size = data.size - CNID_DEVINO_LEN;
165 if ((rc = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
168 #endif /* CNID_DB_CDB */
171 case DB_LOCK_DEADLOCK:
173 #endif /* CNID_DB_CDB */
182 #else /* CNID_DB_CDB */
183 return txn_commit(tid, 0);
184 #endif /* CNID_DB_CDB */
187 LOG(log_error, logtype_default, "cnid_update: Unable to update CNID %u: %s",
188 ntohl(id), db_strerror(rc));