aboutsummaryrefslogtreecommitdiffstats
path: root/ll-database-binding/src
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-10-24 19:48:16 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-10-24 19:48:16 +0200
commitc0b15b4346418b9ef02508dadfd3a75ae2b57d36 (patch)
treeb21762c8bfc145c442d510e94dedb149b53144ac /ll-database-binding/src
parentd6c97a322e5c5605e4fd3b1dd25ad1b6767537e0 (diff)
database: refactor path compute of database
Change-Id: I6d0f90e72d9896c1b9a2827d318e4b1296a6f47d Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'll-database-binding/src')
-rw-r--r--ll-database-binding/src/ll-database-binding.c60
1 files changed, 39 insertions, 21 deletions
diff --git a/ll-database-binding/src/ll-database-binding.c b/ll-database-binding/src/ll-database-binding.c
index 2206bb0..ae0003b 100644
--- a/ll-database-binding/src/ll-database-binding.c
+++ b/ll-database-binding/src/ll-database-binding.c
@@ -35,7 +35,7 @@
#define MAX_PATH 1024
#endif
-#define DBFILE "/ll-database-binding.db"
+#define DBFILE "ll-database-binding.db"
#if !defined(TO_STRING_FLAGS)
# if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
@@ -45,8 +45,7 @@
#endif
// ----- Globals -----
-static DB* database;
-static char database_file[MAX_PATH];
+static DB *database;
// ----- Binding's declarations -----
static int ll_database_binding_init();
@@ -58,33 +57,52 @@ static void verb_read(struct afb_req req);
// ----- Binding's implementations -----
/**
+ * @brief Get the path to the database
+ */
+static int get_database_path(char *buffer, size_t size)
+{
+ static const char dbfile[] = DBFILE;
+
+ char *home, *config;
+ int rc;
+
+ config = secure_getenv("XDG_CONFIG_HOME");
+ if (config)
+ rc = snprintf(buffer, size, "%s/.config/%s", home, dbfile);
+ else
+ {
+ home = secure_getenv("HOME");
+ if (home)
+ rc = snprintf(buffer, size, "%s/.config/%s", home, dbfile);
+ else
+ {
+ struct passwd *pwd = getpwuid(getuid());
+ if (pwd)
+ rc = snprintf(buffer, size, "%s/.config/%s", result->pw_dir, dbfile);
+ else
+ rc = snprintf(buffer, size, "/home/%d/.config/%s", (int)getuid(), dbfile);
+ }
+ }
+ return rc;
+}
+
+/**
* @brief Initialize the binding.
* @return Exit code, zero if success.
*/
static int ll_database_binding_init()
{
- struct passwd pwd;
- struct passwd* result;
- char buf[MAX_PATH];
- size_t bufsize;
+ char path[MAX_PATH];
int ret;
- bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
- if (bufsize == -1 || bufsize > MAX_PATH) bufsize = MAX_PATH;
-
- ret = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
- if (result == NULL)
+ ret = get_database_path(path, sizeof path);
+ if (ret < 0 || (int)ret >= (int)(sizeof path))
{
- if (ret == 0) AFB_ERROR("User not found");
- else AFB_ERROR("getpwuid_r failed with %d code", ret);
+ AFB_ERROR("Can't compute the database filename");
return -1;
}
- memset(database_file, 0, MAX_PATH);
- strcat(database_file, result->pw_dir);
- strcat(database_file, DBFILE);
-
- AFB_INFO("The database file is '%s'", database_file);
+ AFB_INFO("opening database %s", path);
if ((ret = db_create(&database, NULL, 0)) != 0)
{
@@ -92,9 +110,9 @@ static int ll_database_binding_init()
return -1;
}
- if ((ret = database->open(database, NULL, database_file, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
+ if ((ret = database->open(database, NULL, path, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
{
- AFB_ERROR("Failed to open the '%s' database: %s.", database_file, db_strerror(ret));
+ AFB_ERROR("Failed to open the '%s' database: %s.", path, db_strerror(ret));
database->close(database, 0);
return -1;
}