Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / src / frontend / query.c
1 /*
2  * SysDB - src/frontend/query.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "sysdb.h"
30 #include "core/store.h"
31 #include "frontend/connection-private.h"
32 #include "utils/error.h"
33 #include "utils/strbuf.h"
35 #include <errno.h>
37 /*
38  * private helper functions
39  */
41 static int
42 lookup_tojson(sdb_store_obj_t *obj, void *user_data)
43 {
44         sdb_strbuf_t *buf = user_data;
45         if (sdb_strbuf_len(buf) > 1)
46                 sdb_strbuf_append(buf, ",");
47         return sdb_store_host_tojson(obj, buf, /* flags = */ 0);
48 } /* lookup_tojson */
50 /*
51  * public API
52  */
54 int
55 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
56 {
57         if (! node)
58                 return -1;
60         switch (node->cmd) {
61                 case CONNECTION_FETCH:
62                         return sdb_fe_fetch(conn, CONN_FETCH(node)->name);
63                 case CONNECTION_LIST:
64                         return sdb_fe_list(conn);
65                 case CONNECTION_LOOKUP:
66                         return sdb_fe_lookup(conn, CONN_LOOKUP(node)->matcher->matcher);
68                 default:
69                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
70                         return -1;
71         }
72         return -1;
73 } /* sdb_fe_exec */
75 int
76 sdb_fe_fetch(sdb_conn_t *conn, const char *name)
77 {
78         sdb_strbuf_t *buf;
79         sdb_store_obj_t *host;
81         host = sdb_store_get_host(name);
82         if (! host) {
83                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
84                                 "not found", name);
86                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
87                 return -1;
88         }
90         buf = sdb_strbuf_create(1024);
91         if (! buf) {
92                 char errbuf[1024];
93                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
94                                 "buffer to handle FETCH command: %s",
95                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
97                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
98                 sdb_strbuf_destroy(buf);
99                 sdb_object_deref(SDB_OBJ(host));
100                 return -1;
101         }
103         if (sdb_store_host_tojson(host, buf, /* flags = */ 0)) {
104                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
105                                 "host '%s' to JSON", name);
106                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
107                 sdb_strbuf_destroy(buf);
108                 sdb_object_deref(SDB_OBJ(host));
109                 return -1;
110         }
112         sdb_connection_send(conn, CONNECTION_OK,
113                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
114         sdb_strbuf_destroy(buf);
115         sdb_object_deref(SDB_OBJ(host));
116         return 0;
117 } /* sdb_fe_fetch */
119 int
120 sdb_fe_list(sdb_conn_t *conn)
122         sdb_strbuf_t *buf;
124         buf = sdb_strbuf_create(1024);
125         if (! buf) {
126                 char errbuf[1024];
127                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
128                                 "buffer to handle LIST command: %s",
129                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
131                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
132                 sdb_strbuf_destroy(buf);
133                 return -1;
134         }
136         if (sdb_store_tojson(buf, /* flags = */ SDB_SKIP_ALL)) {
137                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
138                                 "store to JSON");
139                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
140                 sdb_strbuf_destroy(buf);
141                 return -1;
142         }
144         sdb_connection_send(conn, CONNECTION_OK,
145                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
146         sdb_strbuf_destroy(buf);
147         return 0;
148 } /* sdb_fe_list */
150 int
151 sdb_fe_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m)
153         sdb_strbuf_t *buf;
155         buf = sdb_strbuf_create(1024);
156         if (! buf) {
157                 char errbuf[1024];
158                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
159                                 "buffer to handle LOOKUP command: %s",
160                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
162                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
163                 sdb_strbuf_destroy(buf);
164                 return -1;
165         }
167         sdb_strbuf_append(buf, "[");
169         if (sdb_store_lookup(m, lookup_tojson, buf)) {
170                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
171                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
172                 sdb_strbuf_destroy(buf);
173                 return -1;
174         }
176         sdb_strbuf_append(buf, "]");
178         sdb_connection_send(conn, CONNECTION_OK,
179                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
180         sdb_strbuf_destroy(buf);
181         return 0;
182 } /* sdb_fe_lookup */
184 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */