]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
b1a9bdaca10092ba71595f6a90019d3614f9a4c2
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.10 2001-09-21 15:08:32 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, DB_TXN *ptid, 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, ptid, &tid,0))) {
55     return rc;
56   }
57
58   /* main database */
59   if ((rc = db->db_cnid->put(db->db_cnid, tid,
60                                 key, data, DB_NOOVERWRITE))) {
61     txn_abort(tid);
62     if (rc == DB_LOCK_DEADLOCK)
63       goto retry;
64
65     return rc;
66   }
67
68   /* dev/ino database */
69   altkey.data = data->data;
70   altkey.size = CNID_DEVINO_LEN;
71   altdata.data = key->data;
72   altdata.size = key->size;
73   if ((rc = db->db_devino->put(db->db_devino, tid,
74                                   &altkey, &altdata, 0))) {
75     txn_abort(tid);
76     if (rc == DB_LOCK_DEADLOCK)
77       goto retry;
78
79     return rc;
80   }
81
82   /* did/name database */
83   altkey.data = (char *) data->data + CNID_DEVINO_LEN;
84   altkey.size = data->size - CNID_DEVINO_LEN;
85   if ((rc = db->db_didname->put(db->db_didname, tid,
86                                    &altkey, &altdata, 0))) {
87     txn_abort(tid);
88     if (rc == DB_LOCK_DEADLOCK)
89       goto retry;
90
91     return rc;
92   }
93
94   return txn_commit(tid, 0);
95 }
96
97 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
98    return that cnid if it exists.  */
99 cnid_t cnid_add(void *CNID, const struct stat *st,
100                 const cnid_t did, const char *name, const int len,
101                 cnid_t hint)
102 {
103   CNID_private *db;
104   DBT key, data;
105   DBT rootinfo_key, rootinfo_data;
106   DB_TXN *tid;
107   cnid_t id, save;
108   int rc = 0;
109
110   int debug = 0;
111
112
113   if (!(db = CNID) || !st || !name) {
114     return 0;
115   }
116
117   /* do a lookup... */
118   id = cnid_lookup(db, st, did, name, len);
119   /* ...return id if it is valid or if RootInfo is read-only. */
120   if (id || (db->flags & CNIDFLAG_DB_RO)) {
121     if (debug)
122       syslog(LOG_ERR, "cnid_add: looked up did %d, name %s as %d", did, name, id);
123     return id;
124   }
125
126   /* initialize everything */
127   memset(&key, 0, sizeof(key));
128   memset(&data, 0, sizeof(data));
129
130   /* just set hint, and the key will change. */
131   key.data = &hint;
132   key.size = sizeof(hint);
133
134   if ((data.data =
135        make_cnid_data(st, did, name, len)) == NULL) {
136     syslog(LOG_ERR, "cnid_add: path name too long.");
137     goto cleanup_err;
138   }
139   data.size = CNID_HEADER_LEN + len + 1;
140
141   /* start off with the hint. it should be in network byte order.
142    * we need to make sure that somebody doesn't add in restricted
143    * cnid's to the database. */
144   if (ntohl(hint) >= CNID_START) {
145     /* if the key doesn't exist, add it in. don't fiddle with nextID. */
146     rc = add_cnid(db, NULL, &key, &data);
147     switch (rc) {
148     case DB_KEYEXIST: /* need to use RootInfo after all. */
149       break;
150     default:
151       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
152       goto cleanup_err;
153     case 0:
154       if (debug)
155         syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
156       return hint;
157     }
158   }
159
160   /* Abort and retry the modification. */
161   if (0) {
162 retry:    if ((rc = txn_abort(tid)) != 0)
163               syslog(LOG_ERR, "cnid_add: txn_abort failed (%d)", rc);
164           /* FALLTHROUGH */
165   }
166
167   /* Begin the transaction. */
168   if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
169     syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", rc);
170     goto cleanup_err;
171   }
172
173   memset(&rootinfo_key, 0, sizeof(rootinfo_key));
174   memset(&rootinfo_data, 0, sizeof(rootinfo_data));
175
176   /* just set hint, and the key will change. */
177   rootinfo_key.data = ROOTINFO_KEY;
178   rootinfo_key.size = ROOTINFO_KEYLEN;
179
180   /* Get the key. */
181   switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
182   case DB_LOCK_DEADLOCK:
183           goto retry;
184   case 0:
185           memcpy (&hint, rootinfo_data.data, sizeof(hint));
186           if (debug)
187             syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, hint);
188           break;
189   case DB_NOTFOUND:
190           hint = htonl(CNID_START);
191           if (debug)
192             syslog(LOG_ERR, "cnid_add: using CNID_START for did %d, name %s as %d", did, name, hint);
193           break;
194   default:
195           syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", rc);
196                   goto cleanup_abort;
197  }
198
199   /* search for a new id. we keep the first id around to check for
200    * wrap-around. NOTE: i do it this way so that we can go back and
201    * fill in holes. */
202   save = id = ntohl(hint);
203   while ((rc = add_cnid(db, tid, &key, &data))) {
204     /* don't use any of the special CNIDs */
205     if (++id < CNID_START)
206       id = CNID_START;
207
208     if ((rc != DB_KEYEXIST) || (save == id)) {
209       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
210       hint = 0;
211       goto cleanup_abort;
212     }
213     hint = htonl(id);
214   }
215
216   /* update RootInfo with the next id. */
217   rootinfo_data.data = &hint;
218   rootinfo_data.size = sizeof(hint);
219
220   switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
221   case DB_LOCK_DEADLOCK:
222           goto retry;
223   case 0:
224           break;
225   default:
226           syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", rc);
227           goto cleanup_abort;
228   }
229
230
231 cleanup_commit:
232   /* The transaction finished, commit it. */
233   if ((rc = txn_commit(tid, 0)) != 0) {
234     syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", rc);
235     goto cleanup_err;
236   }
237
238   if (debug)
239     syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);
240   return hint;
241
242 cleanup_abort:
243   txn_abort(tid);
244
245 cleanup_err:
246   return 0;
247 }
248 #endif /* CNID_DB */