]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
CNID/DID patch from Uwe Hees (moderately tested)
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.3 2001-08-14 14:00:10 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 #include <stdio.h>
17 #include <sys/param.h>
18 #include <sys/stat.h>
19 #include <string.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif /* HAVE_UNISTD_H */
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif /* HAVE_FCNTL_H */
26 #include <errno.h>
27 #include <syslog.h>
28
29 #include <db.h>
30 #include <netatalk/endian.h>
31
32 #include <atalk/adouble.h>
33 #include <atalk/cnid.h>
34 #include <atalk/util.h>
35
36 #include "cnid_private.h"
37
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)
41 {
42   DBT altkey, altdata;
43   DB_TXN *tid;
44
45   memset(&altkey, 0, sizeof(altkey));
46   memset(&altdata, 0, sizeof(altdata));
47   
48 retry:
49   if (errno = txn_begin(db->dbenv, ptid, &tid,0)) {
50     return errno;
51   }
52
53   /* main database */
54   if (errno = db->db_cnid->put(db->db_cnid, tid,
55                                key, data, DB_NOOVERWRITE)) {
56     txn_abort(tid);
57     if (errno == EAGAIN)
58       goto retry;
59
60     return errno;
61   }
62
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))) {
70     txn_abort(tid);
71     if (errno == EAGAIN)
72       goto retry;
73
74     return errno;
75   }
76
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)) {
82     txn_abort(tid);
83     if (errno == EAGAIN)
84       goto retry;
85
86     return errno;
87   }
88
89   return txn_commit(tid, 0);
90 }
91
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,
96                 cnid_t hint)
97 {
98   CNID_private *db;
99   DBT key, data;
100   DBT rootinfo_key, rootinfo_data;
101   DB_TXN *tid;
102   struct flock lock;
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   /* Abort and retry the modification. */
137   if (0) {
138 retry:    if ((errno = txn_abort(tid)) != 0)
139               syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
140           /* FALLTHROUGH */
141   }
142
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);
146     goto cleanup_err;
147   }
148
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);
155     switch (errno) {
156     case DB_KEYEXIST: /* need to use RootInfo after all. */
157       break;
158     default:
159       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
160       hint = 0;
161       goto cleanup_abort;
162     case 0:
163       if (debug)
164         syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
165       goto cleanup_commit;
166     }
167   }
168
169   memset(&rootinfo_key, 0, sizeof(&rootinfo_key));
170   memset(&rootinfo_data, 0, sizeof(&rootinfo_data));
171
172   /* just set hint, and the key will change. */
173   rootinfo_key.data = ROOTINFO_KEY;
174   rootinfo_key.size = ROOTINFO_KEYLEN;
175
176   /* Get the key. */
177   switch (errno = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
178   case DB_LOCK_DEADLOCK:
179           goto retry;
180   case 0:
181           memcpy (&hint, rootinfo_data.data, sizeof(hint));
182           if (debug)
183             syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, hint);
184           break;
185   case DB_NOTFOUND:
186           hint = htonl(CNID_START);
187           if (debug)
188             syslog(LOG_ERR, "cnid_add: using CNID_START for did %d, name %s as %d", did, name, hint);
189           break;
190   default:
191           syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", errno);
192                   goto cleanup_abort;
193  }
194
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
197    * fill in holes. */
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)
202       id = CNID_START;
203
204     if ((errno != DB_KEYEXIST) || (save == id)) {
205       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
206       hint = 0;
207       goto cleanup_abort;
208     }
209     hint = htonl(id);
210   }
211
212   /* update RootInfo with the next id. */
213   rootinfo_data.data = &hint;
214   rootinfo_data.size = sizeof(hint);
215
216   switch (errno = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
217   case DB_LOCK_DEADLOCK:
218           goto retry;
219   case 0:
220           break;
221   default:
222           syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", errno);
223           goto cleanup_abort;
224   }
225
226
227 cleanup_commit:
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);
231     goto cleanup_err;
232   }
233
234   if (debug)
235     syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);
236   return hint;
237
238 cleanup_abort:
239   txn_abort(tid);
240
241 cleanup_err:
242   return 0;
243 }