2 * $Id: cnid_add.c,v 1.10 2001-09-21 15:08:32 jmarcus Exp $
4 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
7 * cnid_add (db, dev, ino, did, name, hint):
8 * add a name to the CNID database. we use both dev/ino and did/name
9 * to keep track of things.
14 #endif /* HAVE_CONFIG_H */
18 #include <sys/param.h>
23 #endif /* HAVE_UNISTD_H */
26 #endif /* HAVE_FCNTL_H */
31 #include <netatalk/endian.h>
33 #include <atalk/adouble.h>
34 #include <atalk/cnid.h>
35 #include <atalk/util.h>
37 #include "cnid_private.h"
39 /* add an entry to the CNID databases. we do this as a transaction
40 * to prevent messiness. */
41 static int add_cnid(CNID_private *db, DB_TXN *ptid, DBT *key, DBT *data)
45 /* We create rc here because using errno is bad. Why? Well, if you
46 * use errno once, then call another function which resets it, you're
50 memset(&altkey, 0, sizeof(altkey));
51 memset(&altdata, 0, sizeof(altdata));
54 if ((rc = txn_begin(db->dbenv, ptid, &tid,0))) {
59 if ((rc = db->db_cnid->put(db->db_cnid, tid,
60 key, data, DB_NOOVERWRITE))) {
62 if (rc == DB_LOCK_DEADLOCK)
68 /* dev/ino database */
69 altkey.data = data->data;
70 altkey.size = CNID_DEVINO_LEN;
71 altdata.data = key->data;
72 altdata.size = key->size;
73 if ((rc = db->db_devino->put(db->db_devino, tid,
74 &altkey, &altdata, 0))) {
76 if (rc == DB_LOCK_DEADLOCK)
82 /* did/name database */
83 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
84 altkey.size = data->size - CNID_DEVINO_LEN;
85 if ((rc = db->db_didname->put(db->db_didname, tid,
86 &altkey, &altdata, 0))) {
88 if (rc == DB_LOCK_DEADLOCK)
94 return txn_commit(tid, 0);
97 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
98 return that cnid if it exists. */
99 cnid_t cnid_add(void *CNID, const struct stat *st,
100 const cnid_t did, const char *name, const int len,
105 DBT rootinfo_key, rootinfo_data;
113 if (!(db = CNID) || !st || !name) {
118 id = cnid_lookup(db, st, did, name, len);
119 /* ...return id if it is valid or if RootInfo is read-only. */
120 if (id || (db->flags & CNIDFLAG_DB_RO)) {
122 syslog(LOG_ERR, "cnid_add: looked up did %d, name %s as %d", did, name, id);
126 /* initialize everything */
127 memset(&key, 0, sizeof(key));
128 memset(&data, 0, sizeof(data));
130 /* just set hint, and the key will change. */
132 key.size = sizeof(hint);
135 make_cnid_data(st, did, name, len)) == NULL) {
136 syslog(LOG_ERR, "cnid_add: path name too long.");
139 data.size = CNID_HEADER_LEN + len + 1;
141 /* start off with the hint. it should be in network byte order.
142 * we need to make sure that somebody doesn't add in restricted
143 * cnid's to the database. */
144 if (ntohl(hint) >= CNID_START) {
145 /* if the key doesn't exist, add it in. don't fiddle with nextID. */
146 rc = add_cnid(db, NULL, &key, &data);
148 case DB_KEYEXIST: /* need to use RootInfo after all. */
151 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
155 syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
160 /* Abort and retry the modification. */
162 retry: if ((rc = txn_abort(tid)) != 0)
163 syslog(LOG_ERR, "cnid_add: txn_abort failed (%d)", rc);
167 /* Begin the transaction. */
168 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
169 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", rc);
173 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
174 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
176 /* just set hint, and the key will change. */
177 rootinfo_key.data = ROOTINFO_KEY;
178 rootinfo_key.size = ROOTINFO_KEYLEN;
181 switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
182 case DB_LOCK_DEADLOCK:
185 memcpy (&hint, rootinfo_data.data, sizeof(hint));
187 syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, hint);
190 hint = htonl(CNID_START);
192 syslog(LOG_ERR, "cnid_add: using CNID_START for did %d, name %s as %d", did, name, hint);
195 syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", rc);
199 /* search for a new id. we keep the first id around to check for
200 * wrap-around. NOTE: i do it this way so that we can go back and
202 save = id = ntohl(hint);
203 while ((rc = add_cnid(db, tid, &key, &data))) {
204 /* don't use any of the special CNIDs */
205 if (++id < CNID_START)
208 if ((rc != DB_KEYEXIST) || (save == id)) {
209 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
216 /* update RootInfo with the next id. */
217 rootinfo_data.data = &hint;
218 rootinfo_data.size = sizeof(hint);
220 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
221 case DB_LOCK_DEADLOCK:
226 syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", rc);
232 /* The transaction finished, commit it. */
233 if ((rc = txn_commit(tid, 0)) != 0) {
234 syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", rc);
239 syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);