]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
Add a missing '/'.
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.14.2.3 2001-12-15 06:35:28 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 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif /* HAVE_SYS_TIME_H */
32
33 #include <db.h>
34 #include <netatalk/endian.h>
35
36 #include <atalk/adouble.h>
37 #include <atalk/cnid.h>
38 #include <atalk/util.h>
39
40 #include "cnid_private.h"
41
42 #define MAX_ABORTS 255
43
44 /* add an entry to the CNID databases. we do this as a transaction
45  * to prevent messiness. */
46 static int add_cnid(CNID_private *db, DB_TXN *ptid, DBT *key, DBT *data) {
47     DBT altkey, altdata;
48     DB_TXN *tid;
49     /* We create rc here because using errno is bad.  Why?  Well, if you
50      * use errno once, then call another function which resets it, you're
51      * screwed. */
52     int rc, ret, aborts = 0;
53
54     memset(&altkey, 0, sizeof(altkey));
55     memset(&altdata, 0, sizeof(altdata));
56
57     if (0) {
58 retry:
59         if ((rc = txn_abort(tid)) != 0) {
60             return rc;
61         }
62         if (++aborts > MAX_ABORTS) {
63             return DB_LOCK_DEADLOCK;
64         }
65     }
66
67     if ((rc = txn_begin(db->dbenv, ptid, &tid, 0)) != 0) {
68         return rc;
69     }
70
71     /* main database */
72     if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
73         if (rc == DB_LOCK_DEADLOCK) {
74             goto retry;
75         }
76
77         goto abort;
78     }
79
80
81     /* dev/ino database */
82     altkey.data = data->data;
83     altkey.size = CNID_DEVINO_LEN;
84     altdata.data = key->data;
85     altdata.size = key->size;
86     if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
87         if (rc == DB_LOCK_DEADLOCK) {
88             goto retry;
89         }
90
91         goto abort;
92     }
93
94
95     /* did/name database */
96     altkey.data = (char *) data->data + CNID_DEVINO_LEN;
97     altkey.size = data->size - CNID_DEVINO_LEN;
98     if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
99         if (rc == DB_LOCK_DEADLOCK) {
100             goto retry;
101         }
102
103         goto abort;
104     }
105
106     if ((rc = txn_commit(tid, 0)) != 0) {
107         syslog(LOG_ERR, "add_cnid: Failed to commit transaction: %s",
108                db_strerror(rc));
109         return rc;
110     }
111     return 0;
112
113 abort:
114     if ((ret = txn_abort(tid)) != 0) {
115         return ret;
116     }
117     return rc;
118
119 }
120
121 cnid_t cnid_add(void *CNID, const struct stat *st,
122                 const cnid_t did, const char *name, const int len,
123                 cnid_t hint)
124 {
125     CNID_private *db;
126     DBT key, data, rootinfo_key, rootinfo_data;
127     DB_TXN *tid;
128     struct timeval t;
129     cnid_t id, save;
130     int rc;
131
132     if (!(db = CNID) || !st || !name) {
133         return 0;
134     }
135
136     /* Do a lookup. */
137     id = cnid_lookup(db, st, did, name, len);
138     /* ... Return id if it is valid, or if Rootinfo is read-only. */
139     if (id || (db->flags & CNIDFLAG_DB_RO)) {
140 #ifdef DEBUG
141         syslog(LOG_INFO, "cnid_add: Looked up did %u, name %s as %u",
142                ntohl(did), name, ntohl(id));
143 #endif
144         return id;
145     }
146
147     /* Initialize our DBT data structures. */
148     memset(&key, 0, sizeof(key));
149     memset(&data, 0, sizeof(data));
150
151     /* Just tickle hint, and the key will change (gotta love pointers). */
152     key.data = &hint;
153     key.size = sizeof(hint);
154
155     if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
156         syslog(LOG_ERR, "cnid_add: Path name is too long");
157         goto cleanup_err;
158     }
159
160     data.size = CNID_HEADER_LEN + len + 1;
161
162     /* Start off with the hint.  It should be in network byte order.
163      * We need to make sure that somebody doesn't add in restricted
164      * cnid's to the database. */
165     if (ntohl(hint) >= CNID_START) {
166         /* If the key doesn't exist, add it in.  Don't fiddle with nextID. */
167         rc = add_cnid(db, NULL, &key, &data);
168         switch (rc) {
169         case DB_KEYEXIST: /* Need to use RootInfo after all. */
170             break;
171         default:
172             syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
173                    ntohl(hint), db_strerror(rc));
174             goto cleanup_err;
175         case 0:
176 #ifdef DEBUG
177             syslog(LOG_INFO, "cnid_add: Used hint for did %u, name %s as %u",
178                    ntohl(did), name, ntohl(hint));
179 #endif
180             return hint;
181         }
182     }
183
184     /* We need to create a random sleep interval to prevent deadlocks. */
185     (void)srand(getpid() ^ time(NULL));
186     t.tv_sec = 0;
187
188     memset(&rootinfo_key, 0, sizeof(rootinfo_key));
189     memset(&rootinfo_data, 0, sizeof(rootinfo_data));
190     rootinfo_key.data = ROOTINFO_KEY;
191     rootinfo_key.size = ROOTINFO_KEYLEN;
192
193     /* Get the key. */
194 retry_get:
195     switch (rc = db->db_didname->get(db->db_didname, NULL, &rootinfo_key,
196                                      &rootinfo_data, 0)) {
197     case DB_LOCK_DEADLOCK:
198         if ((rc = txn_abort(tid)) != 0) {
199             syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
200             goto cleanup_err;
201         }
202         goto retry_get;
203     case 0:
204         memcpy(&hint, rootinfo_data.data, sizeof(hint));
205 #ifdef DEBUG
206         syslog(LOG_INFO, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
207 #endif
208         break;
209     case DB_NOTFOUND:
210         hint = htonl(CNID_START);
211 #ifdef DEBUG
212         syslog(LOG_INFO, "cnid_add: Using CNID_START for did %u, name %s",
213                ntohl(did), name);
214 #endif
215         break;
216     default:
217         syslog(LOG_ERR, "cnid_add: Unable to lookup rootinfo: %s",
218                db_strerror(rc));
219         goto cleanup_err;
220     }
221
222
223     if (0) {
224 retry:
225         t.tv_usec = rand() % 1000000;
226 #ifdef DEBUG
227         syslog(LOG_INFO, "cnid_add: Hitting MAX_ABORTS, sleeping");
228 #endif
229         (void)select(0, NULL, NULL, NULL, &t);
230     }
231     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
232         syslog(LOG_ERR, "cnid_add: Failed to begin transaction: %s",
233                db_strerror(rc));
234         goto cleanup_err;
235     }
236
237     /* Search for a new id.  We keep the first id around to check for
238      * wrap-around.  NOTE: I do it this way so that we can go back and
239      * fill in holes. */
240     save = id = ntohl(hint);
241     while ((rc = add_cnid(db, tid, &key, &data)) != 0) {
242         /* Don't use any special CNIDs. */
243         if (++id < CNID_START) {
244             id = CNID_START;
245         }
246         if (rc == DB_LOCK_DEADLOCK) {
247             if ((rc = txn_abort(tid)) != 0) {
248                 syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
249                 goto cleanup_err;
250             }
251             goto retry;
252         }
253
254         if ((rc != DB_KEYEXIST) || (save == id)) {
255             syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
256                    ntohl(hint), db_strerror(rc));
257             goto cleanup_abort;
258         }
259         hint = htonl(id);
260     }
261
262     rootinfo_data.data = &hint;
263     rootinfo_data.size = sizeof(hint);
264
265     switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
266     case DB_LOCK_DEADLOCK:
267         if ((rc = txn_abort(tid)) != 0) {
268             syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
269             goto cleanup_err;
270         }
271         goto retry;
272     case 0:
273         break;
274     default:
275         syslog(LOG_ERR, "cnid_add: Unable to update rootinfo: %s",
276                db_strerror(rc));
277         goto cleanup_abort;
278     }
279
280 cleanup_commit:
281     /* The transaction finished, commit it. */
282     if ((rc = txn_commit(tid, 0)) != 0) {
283         syslog(LOG_ERR, "cnid_add: Unable to commit transaction: %s",
284                db_strerror(rc));
285         goto cleanup_err;
286     }
287
288 #ifdef DEBUG
289     syslog(LOG_INFO, "cnid_add: Returned CNID for did %u, name %s as %u",
290            ntohl(did), name, ntohl(hint));
291 #endif
292     return hint;
293
294 cleanup_abort:
295     txn_abort(tid);
296
297 cleanup_err:
298     return 0;
299 }
300 #endif /* CNID_DB */
301