]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
fb06371f099ce46e3c494817d9b2dbb689d3b6e1
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /* 
2  * $Id: cnid_add.c,v 1.2 2001-06-29 14:14:46 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, DBT *key, DBT *data)
41 {
42   DBT altkey, altdata;
43   DB_TXN *tid;
44   DB_TXNMGR *txnp;
45
46   txnp = db->dbenv.tx_info;
47   memset(&altkey, 0, sizeof(altkey));
48   memset(&altdata, 0, sizeof(altdata));
49   
50 retry:
51   if (errno = txn_begin(txnp, NULL, &tid)) {
52     return errno;
53   }
54
55   /* main database */
56   if (errno = db->db_cnid->put(db->db_cnid, tid, 
57                                key, data, DB_NOOVERWRITE)) {
58     txn_abort(tid);
59     if (errno == EAGAIN)
60       goto retry;
61
62     return errno;
63   }
64
65   /* dev/ino database */
66   altkey.data = data->data;
67   altkey.size = CNID_DEVINO_LEN;
68   altdata.data = key->data;
69   altdata.size = key->size;
70   if ((errno = db->db_devino->put(db->db_devino, tid,
71                                   &altkey, &altdata, 0))) {
72     txn_abort(tid);
73     if (errno == EAGAIN)
74       goto retry;
75
76     return errno;
77   }
78
79   /* did/name database */
80   altkey.data = data->data + CNID_DEVINO_LEN;
81   altkey.size = data->size - CNID_DEVINO_LEN;
82   if (errno = db->db_didname->put(db->db_didname, tid,
83                                     &altkey, &altdata, 0)) {
84     txn_abort(tid);
85     if (errno == EAGAIN)
86       goto retry;
87
88     return errno;
89   }
90
91   return txn_commit(tid);
92 }
93                     
94 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
95    return that cnid if it exists.  */
96 cnid_t cnid_add(void *CNID, const struct stat *st, 
97                 const cnid_t did, const char *name, const int len,
98                 cnid_t hint)
99 {
100   CNID_private *db;
101   DBT key, data;
102   struct flock lock;
103   cnid_t id, save;
104   
105   
106   if (!(db = CNID) || !st || !name)
107     return 0;
108   
109   /* just do a lookup if RootInfo is read-only. */
110   if (db->flags & (CNIDFLAG_ROOTINFO_RO | CNIDFLAG_DB_RO))
111     return cnid_lookup(db, st, did, name, len);
112
113   /* initialize everything */
114   memset(&key, 0, sizeof(key));
115   memset(&data, 0, sizeof(data));
116
117   /* acquire a lock on RootInfo. as the cnid database is the only user 
118    * of RootInfo, we just use our own locks. 
119    *
120    * NOTE: we lock it here to serialize access to the database. */
121   lock.l_type = F_WRLCK;
122   lock.l_whence = SEEK_SET;
123   lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
124   lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
125   if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW,  &lock) < 0) {
126     syslog(LOG_ERR, "cnid_add: can't establish lock: %m");
127     goto cleanup_err;
128   }
129
130   /* if it's already stored, just return it */
131   if ((id = cnid_lookup(db, st, did, name, len))) {
132     hint = id;
133     goto cleanup_unlock;
134   }
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_unlock;
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     errno = add_cnid(db, &key, &data);
153     switch (errno) {
154     case DB_KEYEXIST: /* need to use RootInfo after all. */
155       break;
156     default:
157       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint); 
158       hint = 0;
159       /* fall through */
160     case 0:
161       goto cleanup_unlock;
162     }
163   }    
164     
165   /* no need to refresh the header file */
166   memcpy(&hint, ad_entry(&db->rootinfo, ADEID_DID), sizeof(hint));
167
168   /* search for a new id. we keep the first id around to check for
169    * wrap-around. NOTE: i do it this way so that we can go back and
170    * fill in holes. */
171   save = id = ntohl(hint);
172   while (errno = add_cnid(db, &key, &data)) {
173     /* don't use any of the special CNIDs */
174     if (++id < CNID_START)
175       id = CNID_START;
176
177     if ((errno != DB_KEYEXIST) || (save == id)) {
178       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
179       hint = 0;
180       goto cleanup_unlock;
181     }
182     hint = htonl(id);
183   }
184
185   /* update RootInfo with the next id. */
186   id = htonl(++id);
187   memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
188   ad_flush(&db->rootinfo, ADFLAGS_HF);
189
190 cleanup_unlock:
191   lock.l_type = F_UNLCK;
192   fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);
193   return hint;
194
195 cleanup_err:
196   return 0;
197 }