diff options
| author | Jesper Jensen <jesper@jnsn.dev> | 2026-02-19 21:30:45 +0100 |
|---|---|---|
| committer | Jesper Jensen <jesper@jnsn.dev> | 2026-02-19 21:46:21 +0100 |
| commit | f2ff8883b298e23119729b75f5b908abeddde095 (patch) | |
| tree | 325e10c5ae079366db4fb70e9bef1140f50f9baa /src/web.c | |
| parent | b0e992e99bf0f6bcb3602f6f4fd4874cc510cfaa (diff) | |
Prepare for server deployment
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)); |
