]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_get.c
Edmund Lam's compilation patches for non-CNID support
[netatalk.git] / libatalk / cnid / cnid_get.c
1 /*
2  * $Id: cnid_get.c,v 1.4 2001-08-31 14:58:48 rufustfirefly Exp $
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8
9 #ifdef CNID_DB
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 #include <syslog.h>
15 #include <errno.h>
16
17 #include <db.h>
18 #include <netatalk/endian.h>
19 #include <atalk/adouble.h>
20 #include <atalk/cnid.h>
21
22 #include "cnid_private.h"
23
24 /* return CNID for a given did/name */
25 cnid_t cnid_get(void *CNID, const cnid_t did, const char *name,
26                 const int len) 
27 {
28   char start[CNID_DID_LEN + MAXPATHLEN + 1], *buf;
29   CNID_private *db;
30   DBT key, data;
31   cnid_t id;
32
33   if (!(db = CNID) || (len > MAXPATHLEN))
34     return 0;
35
36   memset(&key, 0, sizeof(key));
37   memset(&data, 0, sizeof(data));
38
39   buf = start;
40   memcpy(buf, &did, sizeof(did));
41   buf += sizeof(did);
42   memcpy(buf, name, len);
43   *(buf + len) = '\0'; /* make sure to nul terminate. */
44   key.data = start;
45   key.size = CNID_DID_LEN + len + 1;
46
47   while ((errno = db->db_didname->get(db->db_didname, NULL,
48                                       &key, &data, 0))) {
49     if (errno == EAGAIN) 
50       continue;
51
52     if (errno != DB_NOTFOUND)
53       syslog(LOG_ERR, "cnid_get: can't get CNID(%u:%s)", did, name);
54
55     return 0;
56   }
57
58   memcpy(&id, data.data, sizeof(id));
59   return id;
60 }
61 #endif /* CNID_DB */