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 2
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_ERROR("Failed to create database: %s.", db_strerror(ret));
78 ret = database->open(database, NULL, path, NULL, DB_BTREE, DB_CREATE, 0600);
81 AFB_ERROR("Failed to open the '%s' database: %s.", path, db_strerror(ret));
82 database->close(database, 0);
88 static void xdb_put(struct afb_req req, DBT *key, DBT *data, int replace)
92 ret = database->put(database, NULL, key, data, replace ? 0 : DB_NOOVERWRITE);
94 afb_req_success(req, NULL, NULL);
97 AFB_ERROR("can't %s key %s with %s", replace ? "replace" : "insert", DATA_STR(*key), DATA_STR(*data));
98 afb_req_fail_f(req, "failed", "%s", db_strerror(ret));
102 static void xdb_delete(struct afb_req req, DBT *key)
106 ret = database->del(database, NULL, key, 0);
108 afb_req_success_f(req, NULL, NULL);
111 AFB_ERROR("can't delete key %s", DATA_STR(*key));
112 afb_req_fail_f(req, "failed", "%s", db_strerror(ret));
118 static void verb_read(struct afb_req req)
126 struct json_object* result;
127 struct json_object* val;
130 if (get_key(req, &key))
133 AFB_DEBUG("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_success_f(req, result, "db success: read %s=%s.", DATA_STR(key), DATA_STR(data));
150 afb_req_fail_f(req, "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_ERROR("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_ERROR("Fail to open/create database: %s%s%s",
189 gdbm_strerror(gdbm_errno),
191 IFSYS(strerror(errno), ""));
198 static void xdb_put(struct afb_req req, datum *key, datum *data, int replace)
202 ret = gdbm_store(database, *key, *data, replace ? GDBM_REPLACE : GDBM_INSERT);
204 afb_req_success(req, NULL, NULL);
207 AFB_ERROR("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_fail_f(req, "failed", "%s", ret > 0 ? "key already exists" : gdbm_strerror(gdbm_errno));
218 static void xdb_delete(struct afb_req req, datum *key)
222 ret = gdbm_delete(database, *key);
224 afb_req_success_f(req, NULL, NULL);
227 AFB_ERROR("can't delete key %s: %s%s%s",
229 gdbm_strerror(gdbm_errno),
231 IFSYS(strerror(errno), ""));
232 afb_req_fail_f(req, "failed", "%s", gdbm_strerror(gdbm_errno));
236 static void xdb_get(struct afb_req 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_success(req, obj, NULL);
251 AFB_ERROR("can't get key %s: %s%s%s",
253 gdbm_strerror(gdbm_errno),
255 IFSYS(strerror(errno), ""));
256 afb_req_fail_f(req, "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()
303 ret = get_database_path(path, sizeof path);
304 if (ret < 0 || (int)ret >= (int)(sizeof path))
306 AFB_ERROR("Can't compute the database filename");
310 AFB_INFO("opening database %s", path);
311 return xdb_open(path);
315 * Returns the database key for the 'req'
317 static int get_key(struct afb_req 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_fail(req, "no-key", NULL);
335 || !(jkey = json_object_to_json_string_ext(item, JSON_C_TO_STRING_PLAIN))
336 || !(ljkey = strlen(jkey)))
338 afb_req_fail(req, "bad-key", NULL);
343 appid = afb_req_get_application_id(req);
346 appid = strdup("#UNKNOWN-APP#");
350 afb_req_fail(req, "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_fail(req, "out-of-memory", NULL);
365 memcpy(&data[lappid + 1], jkey, ljkey + 1);
368 DATA_SET(key, data, size);
372 static void put(struct afb_req 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_fail(req, "no-value", NULL);
389 value = json_object_to_json_string_ext(item, TO_STRING_FLAGS);
392 afb_req_fail(req, "out-of-memory", NULL);
395 DATA_SET(&data, value, strlen(value) + 1); /* includes the tailing null */
398 if (get_key(req, &key))
401 AFB_DEBUG("put: key=%s, value=%s", DATA_STR(key), DATA_STR(data));
402 xdb_put(req, &key, &data, replace);
406 static void verb_insert(struct afb_req req)
411 static void verb_update(struct afb_req req)
416 static void verb_delete(struct afb_req req)
420 if (get_key(req, &key))
423 AFB_DEBUG("delete: key=%s", DATA_STR(key));
424 xdb_delete(req, &key);
428 static void verb_read(struct afb_req req)
432 if (get_key(req, &key))
435 AFB_DEBUG("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_v2 ll_database_binding_verbs[]= {
454 VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2),
455 VERB(update, NULL, NULL, AFB_SESSION_NONE_V2),
456 VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2),
457 VERB(read, NULL, NULL, AFB_SESSION_NONE_V2),
461 const struct afb_binding_v2 afbBindingV2 = {
462 .api = "persistence",
463 .specification = NULL,
464 .verbs = ll_database_binding_verbs,
466 .init = ll_database_binding_init,