]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
Edmund Lam's compilation patches for non-CNID support
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.6 2001-08-31 14:58:48 rufustfirefly 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, DB_TXN *ptid, DBT *key, DBT *data)
42 {
43   DBT altkey, altdata;
44   DB_TXN *tid;
45
46   memset(&altkey, 0, sizeof(altkey));
47   memset(&altdata, 0, sizeof(altdata));
48   
49 retry:
50   if ((errno = txn_begin(db->dbenv, ptid, &tid,0))) {
51     return errno;
52   }
53
54   /* main database */
55   if ((errno = db->db_cnid->put(db->db_cnid, tid,
56                                 key, data, DB_NOOVERWRITE))) {
57     txn_abort(tid);
58     if (errno == DB_LOCK_DEADLOCK)
59       goto retry;
60
61     return errno;
62   }
63
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))) {
71     txn_abort(tid);
72     if (errno == DB_LOCK_DEADLOCK)
73       goto retry;
74
75     return errno;
76   }
77
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))) {
83     txn_abort(tid);
84     if (errno == DB_LOCK_DEADLOCK)
85       goto retry;
86
87     return errno;
88   }
89
90   return txn_commit(tid, 0);
91 }
92
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,
97                 cnid_t hint)
98 {
99   CNID_private *db;
100   DBT key, data;
101   DBT rootinfo_key, rootinfo_data;
102   DB_TXN *tid;
103   cnid_t id, save;
104
105   int debug = 0;
106
107
108   if (!(db = CNID) || !st || !name) {
109     return 0;
110   }
111
112   /* do a lookup... */
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)) {
116     if (debug)
117       syslog(LOG_ERR, "cnid_add: looked up did %d, name %s as %d", did, name, id);
118     return id;
119   }
120
121   /* initialize everything */
122   memset(&key, 0, sizeof(key));
123   memset(&data, 0, sizeof(data));
124
125   /* just set hint, and the key will change. */
126   key.data = &hint;
127   key.size = sizeof(hint);
128
129   if ((data.data =
130        make_cnid_data(st, did, name, len)) == NULL) {
131     syslog(LOG_ERR, "cnid_add: path name too long.");
132     goto cleanup_err;
133   }
134   data.size = CNID_HEADER_LEN + len + 1;
135
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);
142     switch (errno) {
143     case DB_KEYEXIST: /* need to use RootInfo after all. */
144       break;
145     default:
146       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
147       goto cleanup_err;
148     case 0:
149       if (debug)
150         syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
151       return hint;
152     }
153   }
154
155   /* Abort and retry the modification. */
156   if (0) {
157 retry:    if ((errno = txn_abort(tid)) != 0)
158               syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
159           /* FALLTHROUGH */
160   }
161
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);
165     goto cleanup_err;
166   }
167
168   memset(&rootinfo_key, 0, sizeof(&rootinfo_key));
169   memset(&rootinfo_data, 0, sizeof(&rootinfo_data));
170
171   /* just set hint, and the key will change. */
172   rootinfo_key.data = ROOTINFO_KEY;
173   rootinfo_key.size = ROOTINFO_KEYLEN;
174
175   /* Get the key. */
176   switch (errno = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
177   case DB_LOCK_DEADLOCK:
178           goto retry;
179   case 0:
180           memcpy (&hint, rootinfo_data.data, sizeof(hint));
181           if (debug)
182             syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, 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 %d, name %s as %d", did, name, hint);
188           break;
189   default:
190           syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", errno);
191                   goto cleanup_abort;
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 ((errno = add_cnid(db, tid, &key, &data))) {
199     /* don't use any of the special CNIDs */
200     if (++id < CNID_START)
201       id = CNID_START;
202
203     if ((errno != DB_KEYEXIST) || (save == id)) {
204       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
205       hint = 0;
206       goto cleanup_abort;
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   switch (errno = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
216   case DB_LOCK_DEADLOCK:
217           goto retry;
218   case 0:
219           break;
220   default:
221           syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", errno);
222           goto cleanup_abort;
223   }
224
225
226 cleanup_commit:
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);
230     goto cleanup_err;
231   }
232
233   if (debug)
234     syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);
235   return hint;
236
237 cleanup_abort:
238   txn_abort(tid);
239
240 cleanup_err:
241   return 0;
242 }
243 #endif /* CNID_DB */