2 * Copyright 2017 IoT.bzh
4 * author: Loïc Collignon <loic.collignon@iot.bzh>
5 * author: Jose Bollo <jose.bollo@iot.bzh>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <sys/types.h>
28 #include <json-c/json.h>
31 #define AFB_BINDING_VERSION 2
32 #include <afb/afb-binding.h>
34 #define DBFILE "ll-database-binding.db"
36 #if !defined(TO_STRING_FLAGS)
37 # if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
38 # define JSON_C_TO_STRING_NOSLASHESCAPE (1<<4)
40 # define TO_STRING_FLAGS (JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE)
43 // ----- Globals -----
46 // ----- Binding's declarations -----
47 static int ll_database_binding_init();
48 static void verb_insert(struct afb_req req);
49 static void verb_update(struct afb_req req);
50 static void verb_delete(struct afb_req req);
51 static void verb_read(struct afb_req req);
53 // ----- Binding's implementations -----
56 * @brief Get the path to the database
58 static int get_database_path(char *buffer, size_t size)
60 static const char dbfile[] = DBFILE;
65 config = secure_getenv("XDG_CONFIG_HOME");
67 rc = snprintf(buffer, size, "%s/%s", config, dbfile);
70 home = secure_getenv("HOME");
72 rc = snprintf(buffer, size, "%s/.config/%s", home, dbfile);
75 struct passwd *pwd = getpwuid(getuid());
77 rc = snprintf(buffer, size, "%s/.config/%s", pwd->pw_dir, dbfile);
79 rc = snprintf(buffer, size, "/home/%d/.config/%s", (int)getuid(), dbfile);
86 * @brief Initialize the binding.
87 * @return Exit code, zero if success.
89 static int ll_database_binding_init()
94 ret = get_database_path(path, sizeof path);
95 if (ret < 0 || (int)ret >= (int)(sizeof path))
97 AFB_ERROR("Can't compute the database filename");
101 AFB_INFO("opening database %s", path);
103 ret = db_create(&database, NULL, 0);
106 AFB_ERROR("Failed to create database: %s.", db_strerror(ret));
110 ret = database->open(database, NULL, path, NULL, DB_BTREE, DB_CREATE, 0664);
113 AFB_ERROR("Failed to open the '%s' database: %s.", path, db_strerror(ret));
114 database->close(database, 0);
122 * Returns the database key for the 'req'
124 static int get_key(struct afb_req req, DBT *key)
129 size_t ljkey, lappid, size;
131 struct json_object* args;
132 struct json_object* item;
135 args = afb_req_json(req);
136 if (!json_object_object_get_ex(args, "key", &item))
138 afb_req_fail(req, "no-key", NULL);
142 || !(jkey = json_object_get_string(item))
143 || !(ljkey = strlen(jkey)))
145 afb_req_fail(req, "bad-key", NULL);
150 appid = afb_req_get_application_id(req);
153 appid = strdup("#UNKNOWN-APP#");
157 afb_req_fail(req, "bad-context", NULL);
161 /* make the db-key */
162 lappid = strlen(appid);
163 size = lappid + ljkey + 2;
164 data = realloc(appid, size);
168 afb_req_fail(req, "out-of-memory", NULL);
172 memcpy(&data[lappid + 1], jkey, ljkey + 1);
175 memset(key, 0, sizeof *key);
177 key->size = (uint32_t)size;
181 static void put(struct afb_req req, int replace)
189 struct json_object* args;
190 struct json_object* item;
192 args = afb_req_json(req);
194 if (!json_object_object_get_ex(args, "value", &item))
196 afb_req_fail(req, "no-value", NULL);
200 value = json_object_to_json_string_ext(item, TO_STRING_FLAGS);
203 afb_req_fail(req, "out-of-memory", NULL);
207 if (get_key(req, &key))
210 AFB_INFO("put: key=%s, value=%s", (char*)key.data, value);
212 memset(&data, 0, sizeof data);
213 data.data = (void*)value;
214 data.size = (uint32_t)strlen(value) + 1; /* includes the tailing null */
216 ret = database->put(database, NULL, &key, &data, replace ? 0 : DB_NOOVERWRITE);
218 afb_req_success(req, NULL, NULL);
221 AFB_ERROR("can't %s key %s with %s", replace ? "replace" : "insert", (char*)key.data, (char*)data.data);
222 afb_req_fail_f(req, "failed", "%s", db_strerror(ret));
227 static void verb_insert(struct afb_req req)
232 static void verb_update(struct afb_req req)
237 static void verb_delete(struct afb_req req)
242 if (get_key(req, &key))
245 AFB_INFO("delete: key=%s", (char*)key.data);
247 if ((ret = database->del(database, NULL, &key, 0)) == 0)
248 afb_req_success_f(req, NULL, "db success: delete %s.", (char *)key.data);
250 afb_req_fail_f(req, "Failed to delete datas.", "db fail: delete %s - %s", (char*)key.data, db_strerror(ret));
255 static void verb_read(struct afb_req req)
263 struct json_object* result;
264 struct json_object* val;
267 if (get_key(req, &key))
270 AFB_INFO("read: key=%s", (char*)key.data);
272 memset(&data, 0, sizeof data);
275 data.flags = DB_DBT_USERMEM;
277 ret = database->get(database, NULL, &key, &data, 0);
280 result = json_object_new_object();
281 val = json_tokener_parse((char*)data.data);
282 json_object_object_add(result, "value", val ? val : json_object_new_string((char*)data.data));
284 afb_req_success_f(req, result, "db success: read %s=%s.", (char*)key.data, (char*)data.data);
287 afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", (char*)key.data, db_strerror(ret));
292 // ----- Binding's configuration -----
294 static const struct afb_auth ll_database_binding_auths[] = {
298 #define VERB(name_,auth_,info_,sess_) {\
300 .callback = verb_##name_, \
305 static const afb_verb_v2 ll_database_binding_verbs[]= {
306 VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2),
307 VERB(update, NULL, NULL, AFB_SESSION_NONE_V2),
308 VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2),
309 VERB(read, NULL, NULL, AFB_SESSION_NONE_V2),
313 const struct afb_binding_v2 afbBindingV2 = {
314 .api = "ll-database",
315 .specification = NULL,
316 .verbs = ll_database_binding_verbs,
318 .init = ll_database_binding_init,