]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
0f26c118bd5ca68cabe32a85f9bd81bbbe5ff8e4
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /*
2  * $Id: cnid_open.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 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 <errno.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif /* HAVE_UNISTD_H */
46 #ifdef HAVE_FCNTL_H
47 #include <fcntl.h>
48 #endif /* HAVE_FCNTL_H */
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 #include <syslog.h>
52
53 #include <db.h>
54
55 #include <atalk/adouble.h>
56 #include <atalk/cnid.h>
57 #include <atalk/util.h>
58
59 #include "cnid_private.h"
60
61 #ifndef MIN
62 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
63 #endif /* ! MIN */
64
65 #define DBHOME       ".AppleDB"
66 #define DBCNID       "cnid.db"
67 #define DBDEVINO     "devino.db"
68 #define DBDIDNAME    "didname.db"   /* did/full name mapping */
69 #define DBSHORTNAME  "shortname.db" /* did/8+3 mapping */
70 #define DBMACNAME    "macname.db"   /* did/31 mapping */
71 #define DBLONGNAME   "longname.db"  /* did/unicode mapping */
72 #define DBLOCKFILE   "cnid.lock"
73
74 #define DBHOMELEN    8
75 #define DBLEN        10
76
77 /* we version the did/name database so that we can change the format
78  * if necessary. the key is in the form of a did/name pair. in this case,
79  * we use 0/0. */
80 #define DBVERSION_KEY    "\0\0\0\0\0"
81 #define DBVERSION_KEYLEN 5
82 #define DBVERSION1       0x00000001U
83 #define DBVERSION        DBVERSION1
84
85 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
86 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC | DB_RECOVER)
87
88 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID
89                             * databases. */
90
91 /* the first compare that's always done. */
92 static __inline__ int compare_did(const DBT *a, const DBT *b)
93 {
94   u_int32_t dida, didb;
95
96   memcpy(&dida, a->data, sizeof(dida));
97   memcpy(&didb, b->data, sizeof(didb));
98   return dida - didb;
99 }
100
101 /* sort did's and then names. this is for unix paths.
102  * i.e., did/unixname lookups. */
103 static int compare_unix(const DBT *a, const DBT *b)
104 {
105   u_int8_t *sa, *sb;
106   int len, ret;
107
108   /* sort by did */
109   if (ret = compare_did(a, b))
110     return ret;
111
112   sa = a->data + 4; /* shift past did */
113   sb = b->data + 4;
114   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
115     if (ret = (*sa - *sb))
116       return ret; /* sort by lexical ordering */
117
118   return a->size - b->size; /* sort by length */
119 }
120
121 /* sort did's and then names. this is for macified paths (i.e.,
122  * did/macname, and did/shortname. i think did/longname needs a
123  * unicode table to work. also, we can't use strdiacasecmp as that
124  * returns a match if a < b. */
125 static int compare_mac(const DBT *a, const DBT *b)
126 {
127   u_int8_t *sa, *sb;
128   int len, ret;
129
130   /* sort by did */
131   if (ret = compare_did(a, b))
132     return ret;
133
134   sa = a->data + 4;
135   sb = b->data + 4;
136   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
137     if (ret = (_diacasemap[*sa] - _diacasemap[*sb]))
138           return ret; /* sort by lexical ordering */
139
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 static int compare_unicode(const DBT *a, const DBT *b)
146 {
147         return compare_mac(a,b);
148 }
149
150 void *cnid_open(const char *dir)
151 {
152   struct stat st;
153   struct flock lock;
154   char path[MAXPATHLEN + 1];
155   CNID_private *db;
156   DBT key, data;
157   int open_flag, len;
158
159   if (!dir)
160     return NULL;
161
162   /* this checks .AppleDB */
163   if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
164     syslog(LOG_ERR, "cnid_open: path too large");
165     return NULL;
166   }
167   
168   if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
169     syslog(LOG_ERR, "cnid_open: unable to allocate memory");
170     return NULL;
171   }
172   db->magic = CNID_DB_MAGIC;
173
174   strcpy(path, dir);
175   if (path[len - 1] != '/') {
176     strcat(path, "/");
177     len++;
178   }
179
180   lock.l_type = F_WRLCK;
181   lock.l_whence = SEEK_SET;
182
183 mkdir_appledb:
184   strcpy(path + len, DBHOME);
185   if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
186     syslog(LOG_ERR, "cnid_open: mkdir failed");
187     goto fail_adouble;
188   }
189
190   /* search for a byte lock. this allows us to cleanup the log files
191    * at cnid_close() in a clean fashion.
192    *
193    * NOTE: this won't work if multiple volumes for the same user refer
194    * to the same directory. */
195   strcat(path, DBLOCKFILE);
196   if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
197     lock.l_start = 0;
198     lock.l_len = 1;
199     while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
200       if (++lock.l_start > MAXITER) {
201         syslog(LOG_INFO, "cnid_open: can't establish logfile cleanup lock.");
202         close(db->lockfd);
203         db->lockfd = -1;
204         break;
205       }
206     }
207   }
208
209   path[len + DBHOMELEN] = '\0';
210   open_flag = DB_CREATE;
211   /* try a full-blown transactional environment */
212   if (db_env_create(&db->dbenv, 0)) {
213     syslog(LOG_ERR, "cnid_open: db_env_create failed");
214     goto fail_lock;
215   }
216
217   if (db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666)) {
218
219     /* try with a shared memory pool */
220         if (db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) {
221
222       /* try without any options. */
223       if (db->dbenv->open(db->dbenv, path, 0, 0666)) {
224                 syslog(LOG_ERR, "cnid_open: db_env_open failed");
225                 goto fail_lock;
226       }
227     }
228     db->flags |= CNIDFLAG_DB_RO;
229     open_flag = DB_RDONLY;
230     syslog(LOG_INFO, "cnid_open: read-only CNID database");
231   }
232
233   /* did/name reverse mapping. we use a btree for this one. */
234   if (db_create(&db->db_didname, db->dbenv, 0))
235         goto fail_appinit;
236
237   db->db_didname->set_bt_compare(db->db_didname, compare_unix);
238   if (db->db_didname->open(db->db_didname, DBDIDNAME, NULL, DB_BTREE, open_flag, 0666)) {
239     goto fail_appinit;
240   }
241
242   /* check for version. this way we can update the database if we need
243      to change the format in any way. */
244   memset(&key, 0, sizeof(key));
245   memset(&data, 0, sizeof(data));
246   key.data = DBVERSION_KEY;
247   key.size = DBVERSION_KEYLEN;
248   while (errno = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) {
249     switch (errno) {
250     case EAGAIN:
251       continue;
252
253     case DB_NOTFOUND:
254         {
255       u_int32_t version = htonl(DBVERSION);
256
257       data.data = &version;
258       data.size = sizeof(version);
259         }
260 dbversion_retry:
261       if (db->db_didname->put(db->db_didname, NULL, &key, &data,
262                               DB_NOOVERWRITE))
263         if (errno == EAGAIN)
264           goto dbversion_retry;
265       break;
266     default:
267       /* uh oh. something bad happened. bail. */
268       db->db_didname->close(db->db_didname, 0);
269       goto fail_appinit;
270     }
271   }
272
273   /* XXX: in the future, we might check for version number here. */
274 #if 0
275   memcpy(&version, data.data, sizeof(version));
276   if (version != htonl(DBVERSION)) {
277     /* fix up stuff */
278   }
279 #endif /* 0 */
280
281 #ifdef EXTENDED_DB
282   /* did/macname mapping. btree this one. */
283   if (db_create(&db->db_macname, db->dbenv, 0))
284     goto fail_appinit;
285
286   db->db_macname->set_bt_compare(db->db_macname, compare_mac);
287   if (db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) {
288     db->db_didname->close(db->db_didname, 0);
289     goto fail_appinit;
290   }
291
292   /* did/shortname mapping */
293   if (db_create(&db->db_shortname, db->dbenv, 0))
294     goto fail_appinit;
295
296   db->db_shortname->set_bt_compare(db->db_shortname, compare_mac);
297   if (db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) {
298     db->db_didname->close(db->db_didname, 0);
299     db->db_macname->close(db->db_macname, 0);
300     goto fail_appinit;
301   }
302
303   /* did/longname mapping */
304   if (db_create(&db->db_longname, db->dbenv, 0))
305     goto fail_appinit;
306
307   db->db_longname->set_bt_compare(db->db_longname, compare_unicode);
308   if (db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) {
309     db->db_didname->close(db->db_didname, 0);
310     db->db_macname->close(db->db_macname, 0);
311     db->db_shortname->close(db->db_shortname, 0);
312     goto fail_appinit;
313   }
314 #endif /* EXTENDED_DB */
315
316   /* dev/ino reverse mapping. we hash this one. */
317   if (db_create(&db->db_devino, db->dbenv, 0))
318     goto fail_appinit;
319
320   if (db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) {
321     db->db_didname->close(db->db_didname, 0);
322 #ifdef EXTENDED_DB
323     db->db_macname->close(db->db_macname, 0);
324     db->db_shortname->close(db->db_shortname, 0);
325     db->db_longname->close(db->db_longname, 0);
326 #endif /* EXTENDED_DB */
327     goto fail_appinit;
328   }
329
330   /* main cnid database. we hash this one as well. */
331   if (db_create(&db->db_cnid, db->dbenv, 0))
332     goto fail_appinit;
333
334   if (db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) {
335     db->db_didname->close(db->db_didname, 0);
336 #ifdef EXTENDED_DB
337     db->db_macname->close(db->db_macname, 0);
338     db->db_shortname->close(db->db_shortname, 0);
339     db->db_longname->close(db->db_longname, 0);
340 #endif /* EXTENDED_DB */
341     db->db_devino->close(db->db_devino, 0);
342     goto fail_appinit;
343   }
344
345   return db;
346
347 fail_appinit:
348   syslog(LOG_ERR, "cnid_open: db_open failed");
349   db->dbenv->close(db->dbenv, 0);
350   /* db->dbenv->remove(db->dbenv, db->dbenv->db_home, 0); */
351
352 fail_lock:
353   if (db->lockfd > -1)
354     close(db->lockfd);
355
356 fail_adouble:
357
358 fail_db:
359   free(db);
360   return NULL;
361 }