diff options
Diffstat (limited to 'src/web.c')
| -rw-r--r-- | src/web.c | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -2,6 +2,9 @@ #include "mytime.h" #include <assert.h> +#include <errno.h> +#include <linux/limits.h> +#include <libgen.h> #include <sys/socket.h> #include <string.h> #include <sys/stat.h> @@ -60,6 +63,33 @@ static int is_authenticated(struct App *app, struct MHD_Connection *connection) return strcmp(cookie, expected) == 0; } +// https://stackoverflow.com/questions/2336242/recursive-mkdir-system-call-on-unix +static void _mkdir(const char *dir) { + char tmp[PATH_MAX]; + char *p = NULL; + size_t len; + int rc; + + snprintf(tmp, sizeof(tmp),"%s",dir); + len = strlen(tmp); + if (tmp[len - 1] == '/') + tmp[len - 1] = 0; + for (p = tmp + 1; *p; p++) + if (*p == '/') { + *p = 0; + rc = mkdir(tmp, S_IRWXU); + if(rc != 0 && errno != EEXIST) { + abort(); + } + *p = '/'; + } + rc = mkdir(tmp, S_IRWXU); + if(rc != 0 && errno != EEXIST) { + abort(); + } + +} + void prepare_database(struct App *app) { // @CLEANUP: Move this to main? doesn't belong here at least sqlite3_config(SQLITE_CONFIG_LOG, sqliteError, NULL); @@ -69,6 +99,13 @@ void prepare_database(struct App *app) { printf("Running migrations against %s\n", app->dbname); + if(memcmp("file::memory", app->dbname, 12) != 0) { + char *full = strdup(app->dbname); + char *dbdir = dirname(full); + _mkdir(dbdir); + free(full); + } + sqlite3 *conn; if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) { fprintf(stderr, "(%d) %s\n", err, sqlite3_errmsg(conn)); |
