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