diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-03-16 01:46:49 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-03-16 01:46:49 +0100 |
| commit | 7e2e37d40ac130079570a4f8111d4d5ca5fc9344 (patch) | |
| tree | 356b6f8b1f5017512568f30753356b04001a0f0e | |
| parent | 955ae07f5550559d0570210273fe975624e99f1f (diff) | |
main.c: Ensure essential directories on startup
So far, slcl failed with poorly described error messages when any of the
essential directories were missing. Now, these are created automatically
so that the initial setup is easier.
| -rw-r--r-- | main.c | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -1113,6 +1113,77 @@ static int parse_args(const int argc, char *const argv[], return 0; } +static int ensure_dir(const char *const dir) +{ + struct stat sb; + + if (stat(dir, &sb)) + { + switch (errno) + { + case ENOENT: + if (mkdir(dir, S_IRWXU)) + { + fprintf(stderr, "%s: mkdir(2) %s: %s\n", + __func__, dir, strerror(errno)); + return -1; + } + + printf("Created empty directory at %s\n", dir); + break; + + default: + fprintf(stderr, "%s: stat(2): %s\n", __func__, strerror(errno)); + return -1; + } + } + + return 0; +} + +static int init_dirs(const char *const dir) +{ + int ret = -1; + struct dynstr user, public; + struct sb; + + dynstr_init(&user); + dynstr_init(&public); + + if (dynstr_append(&user, "%s/user", dir)) + { + fprintf(stderr, "%s: dynstr_append user failed\n", __func__); + goto end; + } + else if (dynstr_append(&public, "%s/public", dir)) + { + fprintf(stderr, "%s: dynstr_append public failed\n", __func__); + goto end; + } + else if (ensure_dir(dir)) + { + fprintf(stderr, "%s: ensure_dir dir failed\n", __func__); + goto end; + } + else if (ensure_dir(user.str)) + { + fprintf(stderr, "%s: ensure_dir user failed\n", __func__); + goto end; + } + else if (ensure_dir(public.str)) + { + fprintf(stderr, "%s: ensure_dir public failed\n", __func__); + goto end; + } + + ret = 0; + +end: + dynstr_free(&user); + dynstr_free(&public); + return ret; +} + int main(const int argc, char *const argv[]) { int ret = EXIT_FAILURE; @@ -1122,6 +1193,7 @@ int main(const int argc, char *const argv[]) unsigned short port; if (parse_args(argc, argv, &dir, &port, &tmpdir) + || init_dirs(dir) || !(a = auth_alloc(dir))) goto end; |
