2 * $Id: cnid_add.c,v 1.5 2001-08-16 14:30:29 uhees 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 */
17 #include <sys/param.h>
22 #endif /* HAVE_UNISTD_H */
25 #endif /* HAVE_FCNTL_H */
30 #include <netatalk/endian.h>
32 #include <atalk/adouble.h>
33 #include <atalk/cnid.h>
34 #include <atalk/util.h>
36 #include "cnid_private.h"
38 /* add an entry to the CNID databases. we do this as a transaction
39 * to prevent messiness. */
40 static int add_cnid(CNID_private *db, DB_TXN *ptid, DBT *key, DBT *data)
45 memset(&altkey, 0, sizeof(altkey));
46 memset(&altdata, 0, sizeof(altdata));
49 if ((errno = txn_begin(db->dbenv, ptid, &tid,0))) {
54 if ((errno = db->db_cnid->put(db->db_cnid, tid,
55 key, data, DB_NOOVERWRITE))) {
57 if (errno == DB_LOCK_DEADLOCK)
63 /* dev/ino database */
64 altkey.data = data->data;
65 altkey.size = CNID_DEVINO_LEN;
66 altdata.data = key->data;
67 altdata.size = key->size;
68 if ((errno = db->db_devino->put(db->db_devino, tid,
69 &altkey, &altdata, 0))) {
71 if (errno == DB_LOCK_DEADLOCK)
77 /* did/name database */
78 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
79 altkey.size = data->size - CNID_DEVINO_LEN;
80 if ((errno = db->db_didname->put(db->db_didname, tid,
81 &altkey, &altdata, 0))) {
83 if (errno == DB_LOCK_DEADLOCK)
89 return txn_commit(tid, 0);
92 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
93 return that cnid if it exists. */
94 cnid_t cnid_add(void *CNID, const struct stat *st,
95 const cnid_t did, const char *name, const int len,
100 DBT rootinfo_key, rootinfo_data;
107 if (!(db = CNID) || !st || !name) {
112 id = cnid_lookup(db, st, did, name, len);
113 /* ...return id if it is valid or if RootInfo is read-only. */
114 if (id || (db->flags & CNIDFLAG_DB_RO)) {
116 syslog(LOG_ERR, "cnid_add: looked up did %d, name %s as %d", did, name, id);
120 /* initialize everything */
121 memset(&key, 0, sizeof(key));
122 memset(&data, 0, sizeof(data));
124 /* just set hint, and the key will change. */
126 key.size = sizeof(hint);
129 make_cnid_data(st, did, name, len)) == NULL) {
130 syslog(LOG_ERR, "cnid_add: path name too long.");
133 data.size = CNID_HEADER_LEN + len + 1;
135 /* start off with the hint. it should be in network byte order.
136 * we need to make sure that somebody doesn't add in restricted
137 * cnid's to the database. */
138 if (ntohl(hint) >= CNID_START) {
139 /* if the key doesn't exist, add it in. don't fiddle with nextID. */
140 errno = add_cnid(db, NULL, &key, &data);
142 case DB_KEYEXIST: /* need to use RootInfo after all. */
145 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
149 syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
154 /* Abort and retry the modification. */
156 retry: if ((errno = txn_abort(tid)) != 0)
157 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
161 /* Begin the transaction. */
162 if ((errno = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
163 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
167 memset(&rootinfo_key, 0, sizeof(&rootinfo_key));
168 memset(&rootinfo_data, 0, sizeof(&rootinfo_data));
170 /* just set hint, and the key will change. */
171 rootinfo_key.data = ROOTINFO_KEY;
172 rootinfo_key.size = ROOTINFO_KEYLEN;
175 switch (errno = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
176 case DB_LOCK_DEADLOCK:
179 memcpy (&hint, rootinfo_data.data, sizeof(hint));
181 syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, hint);
184 hint = htonl(CNID_START);
186 syslog(LOG_ERR, "cnid_add: using CNID_START for did %d, name %s as %d", did, name, hint);
189 syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", errno);
193 /* search for a new id. we keep the first id around to check for
194 * wrap-around. NOTE: i do it this way so that we can go back and
196 save = id = ntohl(hint);
197 while ((errno = add_cnid(db, tid, &key, &data))) {
198 /* don't use any of the special CNIDs */
199 if (++id < CNID_START)
202 if ((errno != DB_KEYEXIST) || (save == id)) {
203 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
210 /* update RootInfo with the next id. */
211 rootinfo_data.data = &hint;
212 rootinfo_data.size = sizeof(hint);
214 switch (errno = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
215 case DB_LOCK_DEADLOCK:
220 syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", errno);
226 /* The transaction finished, commit it. */
227 if ((errno = txn_commit(tid, 0)) != 0) {
228 syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", errno);
233 syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);