]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
massive commenting/autoconf changes
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /* 
2  * $Id: cnid_open.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 database support. 
8  *
9  * here's the deal:
10  *  1) afpd already caches did's. 
11  *  2) the database stores cnid's as both did/name and dev/ino pairs. 
12  *  3) RootInfo holds the value of the NextID.
13  *  4) the cnid database gets called in the following manner --
14  *     start a database:
15  *     cnid = cnid_open(root_dir);
16  *
17  *     allocate a new id: 
18  *     newid = cnid_add(cnid, dev, ino, parent did,
19  *     name, id); id is a hint for a specific id. pass 0 if you don't
20  *     care. if the id is already assigned, you won't get what you
21  *     requested.
22  *
23  *     given an id, get a did/name and dev/ino pair.
24  *     name = cnid_get(cnid, &id); given an id, return the corresponding
25  *     info.
26  *     return code = cnid_delete(cnid, id); delete an entry. 
27  *
28  * with AFP, CNIDs 0-2 have special meanings. here they are:
29  * 0 -- invalid cnid
30  * 1 -- parent of root directory (handled by afpd) 
31  * 2 -- root directory (handled by afpd)
32  *
33  * so, CNID_START begins at 3.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif /* HAVE_CONFIG_H */
39
40 #include <stdlib.h>
41 #include <string.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif /* HAVE_UNISTD_H */
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h>
47 #endif /* HAVE_FCNTL_H */
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <syslog.h>
51
52 #include <db.h>
53
54 #include <atalk/adouble.h>
55 #include <atalk/cnid.h>
56 #include <atalk/util.h>
57
58 #include "cnid_private.h"
59
60 #ifndef MIN
61 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
62 #endif /* ! MIN */
63
64 #define ROOTINFO     "RootInfo"
65 #define ROOTINFO_LEN 8
66
67 #define DBHOME       ".AppleDB"
68 #define DBCNID       "cnid.db"
69 #define DBDEVINO     "devino.db"
70 #define DBDIDNAME    "didname.db"   /* did/full name mapping */
71 #define DBSHORTNAME  "shortname.db" /* did/8+3 mapping */
72 #define DBMACNAME    "macname.db"   /* did/31 mapping */
73 #define DBLONGNAME   "longname.db"  /* did/unicode mapping */
74 #define DBLOCKFILE   "/cnid.lock"
75
76 #define DBHOMELEN    8
77 #define DBLEN        10
78
79 /* we version the did/name database so that we can change the format
80  * if necessary. the key is in the form of a did/name pair. in this case,
81  * we use 0/0. */
82 #define DBVERSION_KEY    "\0\0\0\0\0"
83 #define DBVERSION_KEYLEN 5
84 #define DBVERSION1       0x00000001U
85 #define DBVERSION        DBVERSION1
86
87 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
88 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC | DB_RECOVER)
89
90 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID 
91                             * databases. */
92
93 /* the first compare that's always done. */
94 static __inline__ int compare_did(const DBT *a, const DBT *b)
95 {
96   u_int32_t dida, didb;
97
98   memcpy(&dida, a->data, sizeof(dida));
99   memcpy(&didb, b->data, sizeof(didb));
100   return dida - didb;
101 }
102
103 /* sort did's and then names. this is for unix paths.
104  * i.e., did/unixname lookups. */
105 static int compare_unix(const DBT *a, const DBT *b)
106 {
107   u_int8_t *sa, *sb;
108   int len, ret;
109
110   /* sort by did */
111   if (ret = compare_did(a, b))
112     return ret;
113
114   sa = a->data + 4; /* shift past did */
115   sb = b->data + 4;
116   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
117     if (ret = (*sa - *sb))
118       return ret; /* sort by lexical ordering */
119   return a->size - b->size; /* sort by length */
120 }
121
122 /* sort did's and then names. this is for macified paths (i.e.,
123  * did/macname, and did/shortname. i think did/longname needs a
124  * unicode table to work. also, we can't use strdiacasecmp as that
125  * returns a match if a < b. */
126 static int compare_mac(const DBT *a, const DBT *b) 
127 {
128   u_int8_t *sa, *sb;
129   int len, ret;
130   
131   /* sort by did */
132   if (ret = compare_did(a, b))
133     return ret;
134
135   sa = a->data + 4;
136   sb = b->data + 4;
137   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
138     if (ret = (_diacasemap[*sa] - _diacasemap[*sb]))
139       return ret; /* sort by lexical ordering */
140   return a->size - b->size; /* sort by length */
141 }
142
143
144 /* for unicode names -- right now it's the same as compare_mac. */
145 #define compare_unicode(a, b) compare_mac((a), (b))
146
147 void *cnid_open(const char *dir)
148 {
149   struct stat st;
150   struct flock lock;
151   char path[MAXPATHLEN + 1];
152   CNID_private *db;
153   DB_INFO dbi;
154   DBT key, data;
155   int open_flag, len;
156
157   if (!dir)
158     return NULL;
159
160   /* this checks both RootInfo and .AppleDB */
161   if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
162     syslog(LOG_ERR, "cnid_open: path too large");
163     return NULL;
164   }
165   
166   if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
167     syslog(LOG_ERR, "cnid_open: unable to allocate memory");
168     return NULL;
169   }
170   db->magic = CNID_DB_MAGIC;
171     
172   strcpy(path, dir);
173   if (path[len - 1] != '/') {
174     strcat(path, "/");
175     len++;
176   }
177
178   lock.l_type = F_WRLCK;
179   lock.l_whence = SEEK_SET;
180
181   /* we create and initialize RootInfo if it doesn't exist. */
182   strcat(path, ROOTINFO);
183   if (ad_open(path, ADFLAGS_HF, O_RDWR, 0666, &db->rootinfo) < 0) {
184     cnid_t id;
185
186     /* see if we can open it read-only. if it's read-only, we can't
187      * add CNIDs. */
188     memset(&db->rootinfo, 0, sizeof(db->rootinfo));
189     if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0666, &db->rootinfo) == 0) {
190       db->flags = CNIDFLAG_ROOTINFO_RO;
191       syslog(LOG_INFO, "cnid_open: read-only RootInfo");
192       goto mkdir_appledb;
193     }
194
195     /* create the file */
196     memset(&db->rootinfo, 0, sizeof(db->rootinfo));
197     if (ad_open(path, ADFLAGS_HF, O_CREAT | O_RDWR, 0666, 
198                 &db->rootinfo) < 0) {
199       syslog(LOG_ERR, "cnid_open: ad_open(RootInfo)");
200       goto fail_db;
201     }
202
203     /* lock the RootInfo file. this and cnid_add are the only places
204      * that should fiddle with RootInfo. */
205     lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
206     lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
207     if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW,  &lock) < 0) {
208       syslog(LOG_ERR, "cnid_open: can't establish lock: %m");
209       goto fail_adouble;
210     }
211     
212     /* store the beginning CNID */
213     id = htonl(CNID_START);
214     memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
215     ad_flush(&db->rootinfo, ADFLAGS_HF);
216
217     /* unlock it */
218     lock.l_type = F_UNLCK;
219     fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);
220     lock.l_type = F_WRLCK;
221   }
222
223 mkdir_appledb:
224   strcpy(path + len, DBHOME);
225   if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
226     syslog(LOG_ERR, "cnid_open: mkdir failed");
227     goto fail_adouble;
228   }
229
230   /* search for a byte lock. this allows us to cleanup the log files
231    * at cnid_close() in a clean fashion.
232    *
233    * NOTE: this won't work if multiple volumes for the same user refer
234    * to the same directory. */
235   strcat(path, DBLOCKFILE);
236   if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
237     lock.l_start = 0;
238     lock.l_len = 1;
239     while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
240       if (++lock.l_start > MAXITER) {
241         syslog(LOG_INFO, "cnid_open: can't establish logfile cleanup lock.");
242         close(db->lockfd);
243         db->lockfd = -1;
244         break;
245       }      
246     }
247   } 
248
249   path[len + DBHOMELEN] = '\0';
250   open_flag = DB_CREATE;
251   /* try a full-blown transactional environment */
252   if (db_appinit(path, NULL, &db->dbenv, DBOPTIONS)) {
253
254     /* try with a shared memory pool */
255     memset(&db->dbenv, 0, sizeof(db->dbenv));
256     if (db_appinit(path, NULL, &db->dbenv, DB_INIT_MPOOL)) {
257
258       /* try without any options. */
259       memset(&db->dbenv, 0, sizeof(db->dbenv));
260       if (db_appinit(path, NULL, &db->dbenv, 0)) {
261         syslog(LOG_ERR, "cnid_open: db_appinit failed");
262         goto fail_lock;
263       }
264     }
265     db->flags |= CNIDFLAG_DB_RO;
266     open_flag = DB_RDONLY;
267     syslog(LOG_INFO, "cnid_open: read-only CNID database");
268   }
269
270   memset(&dbi, 0, sizeof(dbi));
271
272   /* did/name reverse mapping. we use a btree for this one. */
273   dbi.bt_compare = compare_unix;
274   if (db_open(DBDIDNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
275               &db->db_didname)) {
276     goto fail_appinit;
277   }
278
279   /* check for version. this way we can update the database if we need
280      to change the format in any way. */
281   memset(&key, 0, sizeof(key));
282   memset(&data, 0, sizeof(data));
283   key.data = DBVERSION_KEY;
284   key.len = DBVERSION_KEY_LEN;
285   while (errno = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) {
286     switch (errno) {
287     case EAGAIN:
288       continue;
289     
290     case DB_NOTFOUND:
291       u_int32_t version = htonl(DBVERSION);
292
293       data.data = &version;
294       data.len = sizeof(version);
295 dbversion_retry:
296       if (db->db_didname->put(db->db_didname, NULL, &key, &data,
297                               DB_NOOVERWRITE))
298         if (errno == EAGAIN)
299           goto dbversion_retry;
300       break;
301     default:
302       /* uh oh. something bad happened. bail. */
303       db->db_didname->close(db->db_didname, 0);
304       goto fail_appinit;
305     }
306   }
307   
308   /* XXX: in the future, we might check for version number here. */
309 #if 0
310   memcpy(&version, data.data, sizeof(version));
311   if (version != htonl(DBVERSION)) {
312     /* fix up stuff */
313   }
314 #endif /* 0 */
315
316   /* did/macname mapping. btree this one. */
317   dbi.bt_compare = compare_mac;
318   if (db_open(DBMACNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
319               &db->db_macname)) {
320     db->db_didname->close(db->db_didname, 0);
321     goto fail_appinit;
322   }
323
324   /* did/shortname mapping */
325   if (db_open(DBSHORTNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
326               &db->db_shortname)) {
327     db->db_didname->close(db->db_didname, 0);
328     db->db_macname->close(db->db_macname, 0);
329     goto fail_appinit;
330   }
331
332   /* did/longname mapping */
333   dbi.bt_compare = compare_unicode;
334   if (db_open(DBLONGNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
335               &db->db_longname)) {
336     db->db_didname->close(db->db_didname, 0);
337     db->db_macname->close(db->db_macname, 0);
338     db->db_shortname->close(db->db_shortname, 0);
339     goto fail_appinit;
340   }
341
342   /* dev/ino reverse mapping. we hash this one. */
343   dbi.bt_compare = NULL;
344   if (db_open(DBDEVINO, DB_HASH, open_flag, 0666, &db->dbenv, &dbi,
345               &db->db_devino)) {
346     db->db_didname->close(db->db_didname, 0);
347     db->db_macname->close(db->db_macname, 0);
348     db->db_shortname->close(db->db_shortname, 0);
349     db->db_longname->close(db->db_longname, 0);
350     goto fail_appinit;
351   }
352
353   /* main cnid database. we hash this one as well. */
354   if (db_open(DBCNID, DB_HASH, open_flag, 0666, &db->dbenv, &dbi,
355               &db->db_cnid)) {
356     db->db_didname->close(db->db_didname, 0);
357     db->db_macname->close(db->db_macname, 0);
358     db->db_shortname->close(db->db_shortname, 0);
359     db->db_longname->close(db->db_longname, 0);
360     db->db_devino->close(db->db_devino, 0);
361     goto fail_appinit;
362   }
363   return db;
364   
365 fail_appinit:
366   syslog(LOG_ERR, "cnid_open: db_open failed");
367   db_appexit(&db->dbenv);
368
369 fail_lock:
370   if (db->lockfd > -1)
371     close(db->lockfd);
372
373 fail_adouble:
374   ad_close(&db->rootinfo, ADFLAGS_HF);
375
376 fail_db:
377   free(db);
378   return NULL;
379 }