]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
Added include for ../../sys headers
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /* 
2  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
3  * All Rights Reserved. See COPYRIGHT.
4  *
5  * cnid_add (db, dev, ino, did, name, hint): 
6  * add a name to the CNID database. we use both dev/ino and did/name
7  * to keep track of things. */
8 #include <stdio.h>
9 #include <sys/param.h>
10 #include <sys/stat.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <syslog.h>
16
17 #include <db.h>
18 #include <netatalk/endian.h>
19
20 #include <atalk/adouble.h>
21 #include <atalk/cnid.h>
22 #include <atalk/util.h>
23
24 #include "cnid_private.h"
25
26 /* add an entry to the CNID databases. we do this as a transaction
27  * to prevent messiness. */
28 static int add_cnid(CNID_private *db, DBT *key, DBT *data)
29 {
30   DBT altkey, altdata;
31   DB_TXN *tid;
32   DB_TXNMGR *txnp;
33
34   txnp = db->dbenv.tx_info;
35   memset(&altkey, 0, sizeof(altkey));
36   memset(&altdata, 0, sizeof(altdata));
37   
38 retry:
39   if (errno = txn_begin(txnp, NULL, &tid)) {
40     return errno;
41   }
42
43   /* main database */
44   if (errno = db->db_cnid->put(db->db_cnid, tid, 
45                                key, data, DB_NOOVERWRITE)) {
46     txn_abort(tid);
47     if (errno == EAGAIN)
48       goto retry;
49
50     return errno;
51   }
52
53   /* dev/ino database */
54   altkey.data = data->data;
55   altkey.size = CNID_DEVINO_LEN;
56   altdata.data = key->data;
57   altdata.size = key->size;
58   if ((errno = db->db_devino->put(db->db_devino, tid,
59                                   &altkey, &altdata, 0))) {
60     txn_abort(tid);
61     if (errno == EAGAIN)
62       goto retry;
63
64     return errno;
65   }
66
67   /* did/name database */
68   altkey.data = data->data + CNID_DEVINO_LEN;
69   altkey.size = data->size - CNID_DEVINO_LEN;
70   if (errno = db->db_didname->put(db->db_didname, tid,
71                                     &altkey, &altdata, 0)) {
72     txn_abort(tid);
73     if (errno == EAGAIN)
74       goto retry;
75
76     return errno;
77   }
78
79   return txn_commit(tid);
80 }
81                     
82 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
83    return that cnid if it exists.  */
84 cnid_t cnid_add(void *CNID, const struct stat *st, 
85                 const cnid_t did, const char *name, const int len,
86                 cnid_t hint)
87 {
88   CNID_private *db;
89   DBT key, data;
90   struct flock lock;
91   cnid_t id, save;
92   
93   
94   if (!(db = CNID) || !st || !name)
95     return 0;
96   
97   /* just do a lookup if RootInfo is read-only. */
98   if (db->flags & (CNIDFLAG_ROOTINFO_RO | CNIDFLAG_DB_RO))
99     return cnid_lookup(db, st, did, name, len);
100
101   /* initialize everything */
102   memset(&key, 0, sizeof(key));
103   memset(&data, 0, sizeof(data));
104
105   /* acquire a lock on RootInfo. as the cnid database is the only user 
106    * of RootInfo, we just use our own locks. 
107    *
108    * NOTE: we lock it here to serialize access to the database. */
109   lock.l_type = F_WRLCK;
110   lock.l_whence = SEEK_SET;
111   lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
112   lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
113   if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW,  &lock) < 0) {
114     syslog(LOG_ERR, "cnid_add: can't establish lock: %m");
115     goto cleanup_err;
116   }
117
118   /* if it's already stored, just return it */
119   if ((id = cnid_lookup(db, st, did, name, len))) {
120     hint = id;
121     goto cleanup_unlock;
122   }
123   
124   /* just set hint, and the key will change. */
125   key.data = &hint;
126   key.size = sizeof(hint);
127   
128   if ((data.data = 
129        make_cnid_data(st, did, name, len)) == NULL) {
130     syslog(LOG_ERR, "cnid_add: path name too long.");
131     goto cleanup_unlock;
132   }
133   data.size = CNID_HEADER_LEN + len + 1;
134   
135   /* start off with the hint. it should be in network byte order. 
136    * we need to make sure that somebody doesn't add in restricted
137    * cnid's to the database. */
138   if (ntohl(hint) >= CNID_START) {
139     /* if the key doesn't exist, add it in. don't fiddle with nextID. */
140     errno = add_cnid(db, &key, &data);
141     switch (errno) {
142     case DB_KEYEXIST: /* need to use RootInfo after all. */
143       break;
144     default:
145       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint); 
146       hint = 0;
147       /* fall through */
148     case 0:
149       goto cleanup_unlock;
150     }
151   }    
152     
153   /* no need to refresh the header file */
154   memcpy(&hint, ad_entry(&db->rootinfo, ADEID_DID), sizeof(hint));
155
156   /* search for a new id. we keep the first id around to check for
157    * wrap-around. NOTE: i do it this way so that we can go back and
158    * fill in holes. */
159   save = id = ntohl(hint);
160   while (errno = add_cnid(db, &key, &data)) {
161     /* don't use any of the special CNIDs */
162     if (++id < CNID_START)
163       id = CNID_START;
164
165     if ((errno != DB_KEYEXIST) || (save == id)) {
166       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
167       hint = 0;
168       goto cleanup_unlock;
169     }
170     hint = htonl(id);
171   }
172
173   /* update RootInfo with the next id. */
174   id = htonl(++id);
175   memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
176   ad_flush(&db->rootinfo, ADFLAGS_HF);
177
178 cleanup_unlock:
179   lock.l_type = F_UNLCK;
180   fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);
181   return hint;
182
183 cleanup_err:
184   return 0;
185 }