+/// Generate a path to a host configuration file.
+static void make_host_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
+ snprintf(path, len, "%s" SLASH "%s" SLASH "hosts" SLASH "%s", mesh->confbase, conf_subdir, name);
+}
+
+/// Generate a path to an unused invitation file.
+static void make_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
+ snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s", mesh->confbase, conf_subdir, name);
+}
+
+/// Generate a path to a used invitation file.
+static void make_used_invitation_path(meshlink_handle_t *mesh, const char *conf_subdir, const char *name, char *path, size_t len) {
+ snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, conf_subdir, name);
+}
+
+/// Remove a directory recursively
+static void deltree(const char *dirname) {
+ DIR *d = opendir(dirname);
+
+ if(d) {
+ struct dirent *ent;
+
+ while((ent = readdir(d))) {
+ if(ent->d_name[0] == '.') {
+ continue;
+ }
+
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
+
+ if(unlink(filename)) {
+ deltree(filename);
+ }
+ }
+
+ closedir(d);
+ }
+
+ rmdir(dirname);
+}
+
+static bool sync_path(const char *pathname) {
+ int fd = open(pathname, O_RDONLY);
+
+ if(fd < 0) {
+ logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(fsync(fd)) {
+ logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
+ close(fd);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ if(close(fd)) {
+ logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
+ close(fd);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}
+
+/// Try decrypting the main configuration file from the given sub-directory.
+static bool main_config_decrypt(meshlink_handle_t *mesh, const char *conf_subdir) {
+ if(!mesh->config_key && !mesh->confbase && !conf_subdir) {
+ return false;
+ }
+
+ config_t config;
+
+ if(!main_config_read(mesh, conf_subdir, &config, mesh->config_key)) {
+ logger(mesh, MESHLINK_ERROR, "Could not read main configuration file");
+ return false;
+ }
+
+ packmsg_input_t in = {config.buf, config.len};
+
+ uint32_t version = packmsg_get_uint32(&in);
+ config_free(&config);
+
+ return version == MESHLINK_CONFIG_VERSION;
+}
+
+/// Create a fresh configuration directory
+bool config_init(meshlink_handle_t *mesh, const char *conf_subdir) {
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ if(!conf_subdir) {
+ return false;
+ }
+
+ if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
+ return false;
+ }
+
+ char path[PATH_MAX];
+
+ // Create "current" sub-directory in the confbase
+ snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
+ deltree(path);
+
+ if(mkdir(path, 0700)) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
+ return false;
+ }
+
+ make_host_path(mesh, conf_subdir, "", path, sizeof(path));
+
+ if(mkdir(path, 0700)) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
+ return false;
+ }
+
+ make_invitation_path(mesh, conf_subdir, "", path, sizeof(path));
+
+ if(mkdir(path, 0700)) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
+ return false;
+ }
+
+ return true;
+}