]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbd_search.c
66979424f868f8507c5ba0368d6c330067cb43a3
[netatalk.git] / etc / cnid_dbd / dbd_search.c
1 /*
2  * Copyright (C) Frank Lahm 2010
3  * All Rights Reserved.  See COPYING.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <string.h>
11 #include <errno.h>
12 #include <arpa/inet.h>
13
14 #include <atalk/logger.h>
15 #include <atalk/cnid_dbd_private.h>
16
17 #include "dbif.h"
18 #include "dbd.h"
19 #include "pack.h"
20
21 int dbd_search(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
22 {
23     DBT key;
24     int results;
25     static char resbuf[DBD_MAX_SRCH_RSLTS * sizeof(cnid_t)];
26
27     LOG(log_debug, logtype_cnid, "dbd_search(\"%s\"):", rqst->name);
28
29     memset(&key, 0, sizeof(key));
30     rply->name = resbuf;
31     rply->namelen = 0;
32
33     key.data = (char *)rqst->name;
34     key.size = rqst->namelen;
35
36     if ((results = dbif_search(dbd, &key, resbuf)) < 0) {
37         LOG(log_error, logtype_cnid, "dbd_search(\"%s\"): db error", rqst->name);
38         rply->result = CNID_DBD_RES_ERR_DB;
39         return -1;
40     }
41     if (results) {
42         LOG(log_debug, logtype_cnid, "dbd_search(\"%s\"): %d matches", rqst->name, results);
43         rply->namelen = results * sizeof(cnid_t);
44         rply->result = CNID_DBD_RES_OK;
45     } else {
46         LOG(log_debug, logtype_cnid, "dbd_search(\"%s\"): no matches", rqst->name);
47         rply->result = CNID_DBD_RES_NOTFOUND;
48     }
49
50     return 1;
51 }