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>
30 #define AFB_BINDING_VERSION 3
31 #include <afb/afb-binding.h>
33 #if !defined(TO_STRING_FLAGS)
34 # if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
35 # define JSON_C_TO_STRING_NOSLASHESCAPE (1<<4)
37 # define TO_STRING_FLAGS (JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE)
40 #if defined(USE_BERKELEY_DB)
41 # undef USE_BERKELEY_DB
47 # define USE_BERKELEY_DB 0
50 # define USE_BERKELEY_DB 1
53 // ----- Berkeley database -----
58 #define DBFILE "ll-database-binding.db"
60 #define DATA_SET(k,d,s) do{memset((k),0,sizeof*(k));(k)->data=(void*)d;(k)->size=(uint32_t)s;}while(0)
61 #define DATA_PTR(k) ((void*)((k).data))
62 #define DATA_STR(k) ((char*)((k).data))
63 #define DATA_SZ(k) ((size_t)((k).size))
67 static int xdb_open(const char *path)
71 ret = db_create(&database, NULL, 0);
74 AFB_API_ERROR(afbBindingRoot, "Failed to create database: %s.", db_strerror(ret));
78 ret = database->open(database, NULL, path, NULL, DB_BTREE, DB_CREATE, 0600);
81 AFB_API_ERROR(afbBindingRoot, "Failed to open the '%s' database: %s.", path, db_strerror(ret));
82 database->close(database, 0);
88 static void xdb_put(afb_req_t req, DBT *key, DBT *data, int replace)
92 ret = database->put(database, NULL, key, data, replace ? 0 : DB_NOOVERWRITE);
94 afb_req_reply(req, NULL, NULL, NULL);
97 AFB_API_ERROR(afbBindingRoot, "can't %s key %s with %s", replace ? "replace" : "insert", DATA_STR(*key), DATA_STR(*data));
98 afb_req_reply_f(req, NULL, "failed", "%s", db_strerror(ret));
102 static void xdb_delete(afb_req_t req, DBT *key)
106 ret = database->del(database, NULL, key, 0);
108 afb_req_reply_f(req, NULL, NULL, NULL);
111 AFB_API_ERROR(afbBindingRoot, "can't delete key %s", DATA_STR(*key));
112 afb_req_reply_f(req, NULL, "failed", "%s", db_strerror(ret));
118 static void verb_read(afb_req_t req)
126 struct json_object* result;
127 struct json_object* val;
130 if (get_key(req, &key))
133 AFB_API_DEBUG(afbBindingRoot, "read: key=%s", DATA_STR(key));
135 memset(&data, 0, sizeof data);
138 data.flags = DB_DBT_USERMEM;
140 ret = database->get(database, NULL, &key, &data, 0);
143 result = json_object_new_object();
144 val = json_tokener_parse(DATA_STR(data));
145 json_object_object_add(result, "value", val ? val : json_object_new_string(DATA_STR(data)));
147 afb_req_reply_f(req, result, NULL, "db success: read %s=%s.", DATA_STR(key), DATA_STR(data));
150 afb_req_reply_f(req, NULL, "Failed to read datas.", "db fail: read %s - %s", DATA_STR(key), db_strerror(ret));
157 // ----- gdbm database -----
163 #define DBFILE "ll-database-binding.dbm"
165 #define DATA_SET(k,d,s) do{(k)->dptr=(char*)d;(k)->dsize=(int)s;}while(0)
166 #define DATA_PTR(k) ((void*)((k).dptr))
167 #define DATA_STR(k) ((char*)((k).dptr))
168 #define DATA_SZ(k) ((size_t)((k).dsize))
170 #if GDBM_VERSION_MAJOR > 1 || (GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13)
171 # define IFSYS(yes,no) (gdbm_syserr[gdbm_errno] ? (yes) : (no))
173 # define IFSYS(yes,no) (no)
176 static GDBM_FILE database;
178 static void onfatal(const char *text)
180 AFB_API_ERROR(afbBindingRoot, "fatal gdbm message: %s", text);
183 static int xdb_open(const char *path)
185 database = gdbm_open(path, 512, GDBM_WRCREAT|GDBM_SYNC, 0600, onfatal);
188 AFB_API_ERROR(afbBindingRoot, "Fail to open/create database: %s%s%s",
189 gdbm_strerror(gdbm_errno),
191 IFSYS(strerror(errno), ""));
198 static void xdb_put(afb_req_t req, datum *key, datum *data, int replace)
202 ret = gdbm_store(database, *key, *data, replace ? GDBM_REPLACE : GDBM_INSERT);
204 afb_req_reply(req, NULL, NULL, NULL);
207 AFB_API_ERROR(afbBindingRoot, "can't %s key %s with %s: %s%s%s",
208 replace ? "replace" : "insert",
211 gdbm_strerror(gdbm_errno),
213 IFSYS(strerror(errno), ""));
214 afb_req_reply_f(req, NULL, "failed", "%s", ret > 0 ? "key already exists" : gdbm_strerror(gdbm_errno));
218 static void xdb_delete(afb_req_t req, datum *key)
222 ret = gdbm_delete(database, *key);
224 afb_req_reply_f(req, NULL, NULL, NULL);
227 AFB_API_ERROR(afbBindingRoot, "can't delete key %s: %s%s%s",
229 gdbm_strerror(gdbm_errno),
231 IFSYS(strerror(errno), ""));
232 afb_req_reply_f(req, NULL, "failed", "%s", gdbm_strerror(gdbm_errno));
236 static void xdb_get(afb_req_t req, datum *key)
238 struct json_object* obj;
241 result = gdbm_fetch(database, *key);
244 obj = json_object_new_object();
245 json_object_object_add(obj, "value", json_tokener_parse(result.dptr));
246 afb_req_reply(req, obj, NULL, NULL);
251 AFB_API_ERROR(afbBindingRoot, "can't get key %s: %s%s%s",
253 gdbm_strerror(gdbm_errno),
255 IFSYS(strerror(errno), ""));
256 afb_req_reply_f(req, NULL, "failed", "%s", gdbm_strerror(gdbm_errno));
261 // ----- Binding's implementations -----
264 * @brief Get the path to the database
266 static int get_database_path(char *buffer, size_t size)
268 static const char dbfile[] = DBFILE;
273 config = secure_getenv("XDG_CONFIG_HOME");
275 rc = snprintf(buffer, size, "%s/%s", config, dbfile);
278 home = secure_getenv("HOME");
280 rc = snprintf(buffer, size, "%s/.config/%s", home, dbfile);
283 uid_t uid = getuid();
284 struct passwd *pwd = getpwuid(uid);
286 rc = snprintf(buffer, size, "%s/.config/%s", pwd->pw_dir, dbfile);
288 rc = snprintf(buffer, size, "/home/%d/.config/%s", (int)uid, dbfile);
295 * @brief Initialize the binding.
296 * @return Exit code, zero if success.
298 static int ll_database_binding_init(afb_api_t api)
303 ret = get_database_path(path, sizeof path);
304 if (ret < 0 || (int)ret >= (int)(sizeof path))
306 AFB_API_ERROR(afbBindingRoot, "Can't compute the database filename");
310 AFB_API_INFO(afbBindingRoot, "opening database %s", path);
311 return xdb_open(path);
315 * Returns the database key for the 'req'
317 static int get_key(afb_req_t req, DATA *key)
322 size_t ljkey, lappid, size;
324 struct json_object* args;
325 struct json_object* item;
328 args = afb_req_json(req);
329 if (!json_object_object_get_ex(args, "key", &item))
331 afb_req_reply(req, NULL, "no-key", NULL);
335 || !(jkey = json_object_to_json_string_ext(item, JSON_C_TO_STRING_PLAIN))
336 || !(ljkey = strlen(jkey)))
338 afb_req_reply(req, NULL, "bad-key", NULL);
343 appid = afb_req_get_application_id(req);
346 appid = strdup("#UNKNOWN-APP#");
350 afb_req_reply(req, NULL, "bad-context", NULL);
354 /* make the db-key */
355 lappid = strlen(appid);
356 size = lappid + ljkey + 2;
357 data = realloc(appid, size);
361 afb_req_reply(req, NULL, "out-of-memory", NULL);
365 memcpy(&data[lappid + 1], jkey, ljkey + 1);
368 DATA_SET(key, data, size);
372 static void put(afb_req_t req, int replace)
379 struct json_object* args;
380 struct json_object* item;
383 args = afb_req_json(req);
384 if (!json_object_object_get_ex(args, "value", &item))
386 afb_req_reply(req, NULL, "no-value", NULL);
389 value = json_object_to_json_string_ext(item, TO_STRING_FLAGS);
392 afb_req_reply(req, NULL, "out-of-memory", NULL);
395 DATA_SET(&data, value, strlen(value) + 1); /* includes the tailing null */
398 if (get_key(req, &key))
401 AFB_API_DEBUG(afbBindingRoot, "put: key=%s, value=%s", DATA_STR(key), DATA_STR(data));
402 xdb_put(req, &key, &data, replace);
406 static void verb_insert(afb_req_t req)
411 static void verb_update(afb_req_t req)
416 static void verb_delete(afb_req_t req)
420 if (get_key(req, &key))
423 AFB_API_DEBUG(afbBindingRoot, "delete: key=%s", DATA_STR(key));
424 xdb_delete(req, &key);
428 static void verb_read(afb_req_t req)
432 if (get_key(req, &key))
435 AFB_API_DEBUG(afbBindingRoot, "read: key=%s", DATA_STR(key));
440 // ----- Binding's configuration -----
442 static const struct afb_auth ll_database_binding_auths[] = {
446 #define VERB(name_,auth_,info_,sess_) {\
448 .callback = verb_##name_, \
453 static const afb_verb_t ll_database_binding_verbs[]= {
454 VERB(insert, NULL, NULL, AFB_SESSION_NONE),
455 VERB(update, NULL, NULL, AFB_SESSION_NONE),
456 VERB(delete, NULL, NULL, AFB_SESSION_NONE),
457 VERB(read, NULL, NULL, AFB_SESSION_NONE),
461 const afb_binding_t afbBindingExport = {
462 .api = "persistence",
463 .specification = NULL,
464 .verbs = ll_database_binding_verbs,
466 .init = ll_database_binding_init,