summary refs log tree commit diff
path: root/database.h
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-08-20 21:52:16 -0400
committerJune McEnroe <june@causal.agency>2020-08-20 22:20:30 -0400
commit7ee5fbd0ab8993b2e569909ff2edbaff21910d4a (patch)
tree4b5ac7ef287feba8e155d44b092e5996fece8671 /database.h
parentImport xdg.c from catgirl (diff)
downloadlitterbox-7ee5fbd0ab8993b2e569909ff2edbaff21910d4a.tar.gz
litterbox-7ee5fbd0ab8993b2e569909ff2edbaff21910d4a.zip
Use dataPath for dbFind
Diffstat (limited to '')
-rw-r--r--database.h39
1 files changed, 9 insertions, 30 deletions
diff --git a/database.h b/database.h
index d2db1f0..9fc1fc8 100644
--- a/database.h
+++ b/database.h
@@ -55,7 +55,7 @@ int getopt_config(
 	const struct option *longopts, int *longindex
 );
 
-#define DATABASE_PATH "litterbox/litterbox.sqlite"
+static const char *DatabasePath = "litterbox.sqlite";
 
 enum { DatabaseVersion = 5 };
 
@@ -97,15 +97,7 @@ static inline void dbExec(const char *sql) {
 	if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), sql);
 }
 
-static inline void dbOpen(char *path, int flags) {
-	char *base = strrchr(path, '/');
-	if (flags & SQLITE_OPEN_CREATE && base) {
-		*base = '\0';
-		int error = mkdir(path, 0700);
-		if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", path);
-		*base = '/';
-	}
-
+static inline void dbOpen(const char *path, int flags) {
 	int error = sqlite3_open_v2(path, &db, flags, NULL);
 	if (error == SQLITE_CANTOPEN) {
 		sqlite3_close(db);
@@ -119,35 +111,22 @@ static inline void dbOpen(char *path, int flags) {
 	dbExec(SQL(PRAGMA foreign_keys = true;));
 }
 
-static inline void dbFind(char *path, int flags) {
+static inline void dbFind(const char *path, int flags) {
 	if (path) {
 		dbOpen(path, flags);
 		if (db) return;
 		errx(EX_NOINPUT, "%s: database not found", path);
 	}
 
-	const char *home = getenv("HOME");
-	const char *dataHome = getenv("XDG_DATA_HOME");
-	const char *dataDirs = getenv("XDG_DATA_DIRS");
+	if (flags & SQLITE_OPEN_CREATE) {
+		dataMkdir("");
+	}
 
 	char buf[PATH_MAX];
-	if (dataHome) {
-		snprintf(buf, sizeof(buf), "%s/" DATABASE_PATH, dataHome);
-	} else {
-		if (!home) errx(EX_CONFIG, "HOME unset");
-		snprintf(buf, sizeof(buf), "%s/.local/share/" DATABASE_PATH, home);
-	}
-	dbOpen(buf, flags);
-	if (db) return;
-
-	if (!dataDirs) dataDirs = "/usr/local/share:/usr/share";
-	while (*dataDirs) {
-		size_t len = strcspn(dataDirs, ":");
-		snprintf(buf, sizeof(buf), "%.*s/" DATABASE_PATH, (int)len, dataDirs);
-		dbOpen(buf, flags);
+	const char *dirs = NULL;
+	while (NULL != (path = dataPath(buf, sizeof(buf), &dirs, DatabasePath))) {
+		dbOpen(path, flags);
 		if (db) return;
-		dataDirs += len;
-		if (*dataDirs) dataDirs++;
 	}
 	errx(EX_NOINPUT, "database not found; initialize it with litterbox -i");
 }