]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_resolve.c
c542a4ed31e4a00b2b0cc64d61d4c6d5b96d3b74
[netatalk.git] / libatalk / cnid / cnid_resolve.c
1 /*
2  * $Id: cnid_resolve.c,v 1.12 2002-03-24 17:43:42 jmarcus 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 <atalk/logger.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 the did/name pair corresponding to a CNID. */
25 char *cnid_resolve(void *CNID, cnid_t *id, void *buffer, u_int32_t len) {
26     CNID_private *db;
27     DBT key, data;
28     int rc;
29
30     if (!(db = CNID) || !id || !(*id)) {
31         return NULL;
32     }
33
34     memset(&key, 0, sizeof(key));
35     memset(&data, 0, sizeof(data));
36
37     data.data = buffer;
38     data.ulen = len;
39     data.flags = DB_DBT_USERMEM;
40
41     key.data = id;
42     key.size = sizeof(cnid_t);
43     while ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0))) {
44         if (rc == DB_LOCK_DEADLOCK) {
45             continue;
46         }
47
48         if (rc != DB_NOTFOUND) {
49             LOG(log_error, logtype_default, "cnid_resolve: Unable to get did/name: %s",
50                 db_strerror(rc));
51         }
52
53         *id = 0;
54         return NULL;
55     }
56
57     memcpy(id, (char *)data.data + CNID_DEVINO_LEN, sizeof(cnid_t));
58 #ifdef DEBUG
59     LOG(log_info, logtype_default, "cnid_resolve: Returning id = %u, did/name = %s",
60         ntohl(*id), (char *)data.data + CNID_HEADER_LEN);
61 #endif
62     return (char *)data.data + CNID_HEADER_LEN;
63 }
64 #endif /* CNID_DB */