about summary refs log tree commit diff
path: root/database.h
diff options
context:
space:
mode:
Diffstat (limited to 'database.h')
-rw-r--r--database.h17
1 files changed, 8 insertions, 9 deletions
diff --git a/database.h b/database.h
index 15704f0..a6db904 100644
--- a/database.h
+++ b/database.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
+/* Copyright (C) 2019  June McEnroe <june@causal.agency>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -41,11 +41,10 @@
 #define SQL(...) #__VA_ARGS__
 #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
 
-const char *configPath(const char **dirs, const char *path);
-const char *dataPath(const char **dirs, const char *path);
+char *configPath(char *buf, size_t cap, const char *path, int i);
+char *dataPath(char *buf, size_t cap, const char *path, int i);
 FILE *configOpen(const char *path, const char *mode);
 FILE *dataOpen(const char *path, const char *mode);
-void dataMkdir(const char *path);
 int getopt_config(
 	int argc, char *const *argv, const char *optstring,
 	const struct option *longopts, int *longindex
@@ -114,13 +113,14 @@ static inline void dbFind(const char *path, int flags) {
 		errx(EX_NOINPUT, "%s: database not found", path);
 	}
 
+	char buf[PATH_MAX];
 	if (flags & SQLITE_OPEN_CREATE) {
-		dataMkdir("");
+		int error = mkdir(dataPath(buf, sizeof(buf), "", 0), S_IRWXU);
+		if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", buf);
 	}
 
-	const char *dirs = NULL;
-	while (NULL != (path = dataPath(&dirs, DatabasePath))) {
-		dbOpen(path, flags);
+	for (int i = 0; dataPath(buf, sizeof(buf), DatabasePath, i); ++i) {
+		dbOpen(buf, flags);
 		if (db) return;
 	}
 	errx(EX_NOINPUT, "database not found; initialize it with litterbox -i");
@@ -152,7 +152,6 @@ static inline void dbClose(void) {
 		free(persist);
 		persist = prev;
 	}
-	dbExec(SQL(PRAGMA optimize;));
 	sqlite3_close(db);
 }