2 * $Id: cnid_add.c,v 1.3 2001-08-14 14:00:10 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 */
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)) {
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))) {
77 /* did/name database */
78 altkey.data = 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)) {
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;
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 /* Abort and retry the modification. */
138 retry: if ((errno = txn_abort(tid)) != 0)
139 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
143 /* Begin the transaction. */
144 if ((errno = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
145 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
149 /* start off with the hint. it should be in network byte order.
150 * we need to make sure that somebody doesn't add in restricted
151 * cnid's to the database. */
152 if (ntohl(hint) >= CNID_START) {
153 /* if the key doesn't exist, add it in. don't fiddle with nextID. */
154 errno = add_cnid(db, tid, &key, &data);
156 case DB_KEYEXIST: /* need to use RootInfo after all. */
159 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
164 syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
169 memset(&rootinfo_key, 0, sizeof(&rootinfo_key));
170 memset(&rootinfo_data, 0, sizeof(&rootinfo_data));
172 /* just set hint, and the key will change. */
173 rootinfo_key.data = ROOTINFO_KEY;
174 rootinfo_key.size = ROOTINFO_KEYLEN;
177 switch (errno = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
178 case DB_LOCK_DEADLOCK:
181 memcpy (&hint, rootinfo_data.data, sizeof(hint));
183 syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, hint);
186 hint = htonl(CNID_START);
188 syslog(LOG_ERR, "cnid_add: using CNID_START for did %d, name %s as %d", did, name, hint);
191 syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", errno);
195 /* search for a new id. we keep the first id around to check for
196 * wrap-around. NOTE: i do it this way so that we can go back and
198 save = id = ntohl(hint);
199 while (errno = add_cnid(db, tid, &key, &data)) {
200 /* don't use any of the special CNIDs */
201 if (++id < CNID_START)
204 if ((errno != DB_KEYEXIST) || (save == id)) {
205 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
212 /* update RootInfo with the next id. */
213 rootinfo_data.data = &hint;
214 rootinfo_data.size = sizeof(hint);
216 switch (errno = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
217 case DB_LOCK_DEADLOCK:
222 syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", errno);
228 /* The transaction finished, commit it. */
229 if ((errno = txn_commit(tid, 0)) != 0) {
230 syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", errno);
235 syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);