]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
Fix problem with dancing icons and afpd crashes.
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.12 2001-10-19 02:35:47 jmarcus Exp $
3  *
4  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5  * All Rights Reserved. See COPYRIGHT.
6  *
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.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif /* HAVE_CONFIG_H */
15
16 #ifdef CNID_DB
17 #include <stdio.h>
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 #include <string.h>
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif /* HAVE_UNISTD_H */
24 #ifdef HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif /* HAVE_FCNTL_H */
27 #include <errno.h>
28 #include <syslog.h>
29
30 #include <db.h>
31 #include <netatalk/endian.h>
32
33 #include <atalk/adouble.h>
34 #include <atalk/cnid.h>
35 #include <atalk/util.h>
36
37 #include "cnid_private.h"
38
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, DBT *key, DBT *data)
42 {
43   DBT altkey, altdata;
44   DB_TXN *tid;
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
47    * screwed. */
48   int rc = 0;
49
50   memset(&altkey, 0, sizeof(altkey));
51   memset(&altdata, 0, sizeof(altdata));
52   
53 retry:
54   if ((rc = txn_begin(db->dbenv, NULL, &tid,0))) {
55     return rc;
56   }
57
58
59   /* main database */
60   if ((rc = db->db_cnid->put(db->db_cnid, tid,
61                                 key, data, DB_NOOVERWRITE))) {
62     txn_abort(tid);
63     if (rc == DB_LOCK_DEADLOCK) {
64       goto retry;
65         }
66
67     return rc;
68   }
69
70
71   /* dev/ino database */
72   altkey.data = data->data;
73   altkey.size = CNID_DEVINO_LEN;
74   altdata.data = key->data;
75   altdata.size = key->size;
76   if ((rc = db->db_devino->put(db->db_devino, tid,
77                                   &altkey, &altdata, 0))) {
78     txn_abort(tid);
79     if (rc == DB_LOCK_DEADLOCK) {
80       goto retry;
81         }
82
83     return rc;
84   }
85
86
87   /* did/name database */
88   altkey.data = (char *) data->data + CNID_DEVINO_LEN;
89   altkey.size = data->size - CNID_DEVINO_LEN;
90   if ((rc = db->db_didname->put(db->db_didname, tid,
91                                    &altkey, &altdata, 0))) {
92     txn_abort(tid);
93     if (rc == DB_LOCK_DEADLOCK) {
94       goto retry;
95         }
96
97     return rc;
98   }
99
100   return txn_commit(tid, 0);
101 }
102
103 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
104    return that cnid if it exists.  */
105 cnid_t cnid_add(void *CNID, const struct stat *st,
106                 const cnid_t did, const char *name, const int len,
107                 cnid_t hint)
108 {
109   CNID_private *db;
110   DBT key, data;
111   DBT rootinfo_key, rootinfo_data;
112   DB_TXN *tid;
113   cnid_t id, save;
114   int rc = 0;
115
116   int debug = 0;
117
118
119   if (!(db = CNID) || !st || !name) {
120     return 0;
121   }
122
123   /* do a lookup... */
124   id = cnid_lookup(db, st, did, name, len);
125   /* ...return id if it is valid or if RootInfo is read-only. */
126   if (id || (db->flags & CNIDFLAG_DB_RO)) {
127     if (debug)
128       syslog(LOG_ERR, "cnid_add: looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
129     return id;
130   }
131
132   /* initialize everything */
133   memset(&key, 0, sizeof(key));
134   memset(&data, 0, sizeof(data));
135
136   /* just set hint, and the key will change. */
137   key.data = &hint;
138   key.size = sizeof(hint);
139
140   if ((data.data =
141        make_cnid_data(st, did, name, len)) == NULL) {
142     syslog(LOG_ERR, "cnid_add: path name too long.");
143     goto cleanup_err;
144   }
145   data.size = CNID_HEADER_LEN + len + 1;
146
147   /* start off with the hint. it should be in network byte order.
148    * we need to make sure that somebody doesn't add in restricted
149    * cnid's to the database. */
150   if (ntohl(hint) >= CNID_START) {
151     /* if the key doesn't exist, add it in. don't fiddle with nextID. */
152     rc = add_cnid(db, &key, &data);
153     switch (rc) {
154     case DB_KEYEXIST: /* need to use RootInfo after all. */
155       break;
156     default:
157       syslog(LOG_ERR, "cnid_add: unable to add CNID(%u)", ntohl(hint));
158       goto cleanup_err;
159     case 0:
160       if (debug)
161         syslog(LOG_ERR, "cnid_add: used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
162       return hint;
163     }
164   }
165
166
167   memset(&rootinfo_key, 0, sizeof(rootinfo_key));
168   memset(&rootinfo_data, 0, sizeof(rootinfo_data));
169
170   /* just set hint, and the key will change. */
171   rootinfo_key.data = ROOTINFO_KEY;
172   rootinfo_key.size = ROOTINFO_KEYLEN;
173
174   /* Get the key. */
175 getretry:
176   switch (rc = db->db_didname->get(db->db_didname, NULL, &rootinfo_key, &rootinfo_data, 0)) {
177   case DB_LOCK_DEADLOCK:
178           goto getretry;
179   case 0:
180           memcpy (&hint, rootinfo_data.data, sizeof(hint));
181           if (debug)
182             syslog(LOG_ERR, "cnid_add: found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
183           break;
184   case DB_NOTFOUND:
185           hint = htonl(CNID_START);
186           if (debug)
187             syslog(LOG_ERR, "cnid_add: using CNID_START for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
188           break;
189   default:
190           syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", rc);
191                   goto cleanup_err;
192  }
193
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
196    * fill in holes. */
197   save = id = ntohl(hint);
198   while ((rc = add_cnid(db, &key, &data))) {
199     /* don't use any of the special CNIDs */
200     if (++id < CNID_START)
201       id = CNID_START;
202
203     if ((rc != DB_KEYEXIST) || (save == id)) {
204       syslog(LOG_ERR, "cnid_add: unable to add CNID(%u)", ntohl(hint));
205       hint = 0;
206       goto cleanup_err;
207     }
208     hint = htonl(id);
209   }
210
211   /* update RootInfo with the next id. */
212   rootinfo_data.data = &hint;
213   rootinfo_data.size = sizeof(hint);
214
215   /* Abort and retry the modification. */
216   if (0) {
217 retry:    if ((rc = txn_abort(tid)) != 0)
218               syslog(LOG_ERR, "cnid_add: txn_abort failed (%d)", rc);
219           /* FALLTHROUGH */
220   }
221
222   /* Begin the transaction. */
223   if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
224     syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", rc);
225     goto cleanup_err;
226   }
227
228   switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
229   case DB_LOCK_DEADLOCK:
230           goto retry;
231   case 0:
232           break;
233   default:
234           syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", rc);
235           goto cleanup_abort;
236   }
237
238
239 cleanup_commit:
240   /* The transaction finished, commit it. */
241   if ((rc = txn_commit(tid, 0)) != 0) {
242     syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", rc);
243     goto cleanup_err;
244   }
245
246   if (debug)
247     syslog(LOG_ERR, "cnid_add: returned cnid for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
248   return hint;
249
250 cleanup_abort:
251   txn_abort(tid);
252
253 cleanup_err:
254   return 0;
255 }
256 #endif /* CNID_DB */