2 * $Id: cnid_add.c,v 1.6 2001-08-31 14:58:48 rufustfirefly 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)
46 memset(&altkey, 0, sizeof(altkey));
47 memset(&altdata, 0, sizeof(altdata));
50 if ((errno = txn_begin(db->dbenv, ptid, &tid,0))) {
55 if ((errno = db->db_cnid->put(db->db_cnid, tid,
56 key, data, DB_NOOVERWRITE))) {
58 if (errno == DB_LOCK_DEADLOCK)
64 /* dev/ino database */
65 altkey.data = data->data;
66 altkey.size = CNID_DEVINO_LEN;
67 altdata.data = key->data;
68 altdata.size = key->size;
69 if ((errno = db->db_devino->put(db->db_devino, tid,
70 &altkey, &altdata, 0))) {
72 if (errno == DB_LOCK_DEADLOCK)
78 /* did/name database */
79 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
80 altkey.size = data->size - CNID_DEVINO_LEN;
81 if ((errno = db->db_didname->put(db->db_didname, tid,
82 &altkey, &altdata, 0))) {
84 if (errno == DB_LOCK_DEADLOCK)
90 return txn_commit(tid, 0);
93 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
94 return that cnid if it exists. */
95 cnid_t cnid_add(void *CNID, const struct stat *st,
96 const cnid_t did, const char *name, const int len,
101 DBT rootinfo_key, rootinfo_data;
108 if (!(db = CNID) || !st || !name) {
113 id = cnid_lookup(db, st, did, name, len);
114 /* ...return id if it is valid or if RootInfo is read-only. */
115 if (id || (db->flags & CNIDFLAG_DB_RO)) {
117 syslog(LOG_ERR, "cnid_add: looked up did %d, name %s as %d", did, name, id);
121 /* initialize everything */
122 memset(&key, 0, sizeof(key));
123 memset(&data, 0, sizeof(data));
125 /* just set hint, and the key will change. */
127 key.size = sizeof(hint);
130 make_cnid_data(st, did, name, len)) == NULL) {
131 syslog(LOG_ERR, "cnid_add: path name too long.");
134 data.size = CNID_HEADER_LEN + len + 1;
136 /* start off with the hint. it should be in network byte order.
137 * we need to make sure that somebody doesn't add in restricted
138 * cnid's to the database. */
139 if (ntohl(hint) >= CNID_START) {
140 /* if the key doesn't exist, add it in. don't fiddle with nextID. */
141 errno = add_cnid(db, NULL, &key, &data);
143 case DB_KEYEXIST: /* need to use RootInfo after all. */
146 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
150 syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
155 /* Abort and retry the modification. */
157 retry: if ((errno = txn_abort(tid)) != 0)
158 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
162 /* Begin the transaction. */
163 if ((errno = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
164 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
168 memset(&rootinfo_key, 0, sizeof(&rootinfo_key));
169 memset(&rootinfo_data, 0, sizeof(&rootinfo_data));
171 /* just set hint, and the key will change. */
172 rootinfo_key.data = ROOTINFO_KEY;
173 rootinfo_key.size = ROOTINFO_KEYLEN;
176 switch (errno = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
177 case DB_LOCK_DEADLOCK:
180 memcpy (&hint, rootinfo_data.data, sizeof(hint));
182 syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, hint);
185 hint = htonl(CNID_START);
187 syslog(LOG_ERR, "cnid_add: using CNID_START for did %d, name %s as %d", did, name, hint);
190 syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", errno);
194 /* search for a new id. we keep the first id around to check for
195 * wrap-around. NOTE: i do it this way so that we can go back and
197 save = id = ntohl(hint);
198 while ((errno = add_cnid(db, tid, &key, &data))) {
199 /* don't use any of the special CNIDs */
200 if (++id < CNID_START)
203 if ((errno != DB_KEYEXIST) || (save == id)) {
204 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
211 /* update RootInfo with the next id. */
212 rootinfo_data.data = &hint;
213 rootinfo_data.size = sizeof(hint);
215 switch (errno = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
216 case DB_LOCK_DEADLOCK:
221 syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", errno);
227 /* The transaction finished, commit it. */
228 if ((errno = txn_commit(tid, 0)) != 0) {
229 syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", errno);
234 syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);