2 * $Id: cnid_update.c,v 1.8 2001-09-21 15:08:37 jmarcus Exp $
7 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
18 #include <netatalk/endian.h>
19 #include <atalk/adouble.h>
20 #include <atalk/cnid.h>
22 #include "cnid_private.h"
25 /* cnid_update: takes the given cnid and updates the metadata. to
26 handle the did/name data, there are a bunch of functions to get
27 and set the various fields. */
28 int cnid_update(void *CNID, const cnid_t id, const struct stat *st,
29 const cnid_t did, const char *name, const int len/*,
30 const char *info, const int infolen*/)
33 DBT key, data, altdata;
37 if (!(db = CNID) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO))
40 memset(&key, 0, sizeof(key));
41 memset(&data, 0, sizeof(data));
42 memset(&altdata, 0, sizeof(altdata));
44 /* begin a transaction */
46 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0))) {
47 syslog(LOG_ERR, "cnid_update: txn_begin failed with: %d", rc);
51 /* get the old info */
52 key.data = (cnid_t *) &id;
53 key.size = sizeof(id);
54 if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, 0))) {
56 if (rc == DB_LOCK_DEADLOCK)
61 /* delete the old dev/ino mapping */
63 key.size = CNID_DEVINO_LEN;
64 if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
65 if (rc == DB_LOCK_DEADLOCK) {
70 /* silently fail on a non-existent entry */
71 if (rc != DB_NOTFOUND) {
77 /* delete the old did/name mapping */
78 key.data = (char *) data.data + CNID_DEVINO_LEN;
79 key.size = data.size - CNID_DEVINO_LEN;
80 if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
81 if (rc == DB_LOCK_DEADLOCK) {
86 /* silently fail on a non-existent entry */
87 if (rc != DB_NOTFOUND) {
93 /* delete the old aliases if necessary */
96 /* make a new entry */
97 data.data = make_cnid_data(st, did, name, len);
98 data.size = CNID_HEADER_LEN + len + 1;
100 /* put a new dev/ino mapping in */
101 key.data = data.data;
102 key.size = CNID_DEVINO_LEN;
103 altdata.data = (cnid_t *) &id;
104 altdata.size = sizeof(id);
105 if ((rc = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
107 if (rc == DB_LOCK_DEADLOCK) {
113 /* put a new did/name mapping in */
114 key.data = (char *) data.data + CNID_DEVINO_LEN;
115 key.size = data.size - CNID_DEVINO_LEN;
116 if ((rc = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
118 if (rc == DB_LOCK_DEADLOCK) {
124 /* update the old CNID with the new info */
125 key.data = (cnid_t *) &id;
126 key.size = sizeof(id);
127 if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
129 if (rc == DB_LOCK_DEADLOCK) {
135 /* end transaction */
136 return txn_commit(tid, 0);
139 syslog(LOG_ERR, "cnid_update: can't update CNID(%x)", id);