2 * $Id: cnid_update.c,v 1.16 2002-01-04 04:45:48 sibaz 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",
50 /* Get the old info. */
51 key.data = (cnid_t *)&id;
52 key.size = sizeof(id);
53 memset(&data, 0, sizeof(data));
54 if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
57 case DB_LOCK_DEADLOCK:
60 /* Silently fail here. We're allowed to do this since this CNID
61 * might have been deleted out from under us, or someone has
62 * called cnid_lookup then cnid_update (which is redundant). */
69 /* Delete the old dev/ino mapping. */
71 key.size = CNID_DEVINO_LEN;
72 if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
74 case DB_LOCK_DEADLOCK:
85 /* Delete the old did/name mapping. */
86 key.data = (char *) data.data + CNID_DEVINO_LEN;
87 key.size = data.size - CNID_DEVINO_LEN;
88 if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
90 case DB_LOCK_DEADLOCK:
101 /* Make a new entry. */
102 data.data = make_cnid_data(st, did, name, len);
103 data.size = CNID_HEADER_LEN + len + 1;
105 /* Update the old CNID with the new info. */
106 key.data = (cnid_t *) &id;
107 key.size = sizeof(id);
108 if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
111 case DB_LOCK_DEADLOCK:
118 /* Put in a new dev/ino mapping. */
119 key.data = data.data;
120 key.size = CNID_DEVINO_LEN;
121 altdata.data = (cnid_t *) &id;
122 altdata.size = sizeof(id);
123 if ((rc = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
126 case DB_LOCK_DEADLOCK:
133 /* put in a new did/name mapping. */
134 key.data = (char *) data.data + CNID_DEVINO_LEN;
135 key.size = data.size - CNID_DEVINO_LEN;
136 if ((rc = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
139 case DB_LOCK_DEADLOCK:
147 return txn_commit(tid, 0);
150 LOG(log_error, logtype_default, "cnid_update: Unable to update CNID %u: %s",
151 ntohl(id), db_strerror(rc));