+/// 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) {
+ assert(conf_subdir);
+ assert(name);
+ assert(path);
+ assert(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) {
+ assert(conf_subdir);
+ assert(name);
+ assert(path);
+ assert(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) {
+ assert(conf_subdir);
+ assert(name);
+ assert(path);
+ assert(len);
+
+ snprintf(path, len, "%s" SLASH "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, conf_subdir, name);
+}
+
+/// Remove a directory recursively
+static bool deltree(const char *dirname) {
+ assert(dirname);
+
+ DIR *d = opendir(dirname);
+
+ if(d) {
+ struct dirent *ent;
+
+ while((ent = readdir(d))) {
+ if(ent->d_name[0] == '.') {
+ if(!ent->d_name[1] || (ent->d_name[1] == '.' && !ent->d_name[2])) {
+ continue;
+ }
+ }
+
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
+
+ if(unlink(filename)) {
+ if(!deltree(filename)) {
+ return false;
+ }
+ }
+ }
+
+ closedir(d);
+ } else {
+ return errno == ENOENT;
+ }
+
+ return rmdir(dirname) == 0;
+}
+
+bool sync_path(const char *pathname) {
+ assert(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) {
+ assert(mesh->config_key);
+ assert(mesh->confbase);
+ assert(conf_subdir);
+
+ 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) {
+ assert(conf_subdir);
+
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ char path[PATH_MAX];
+
+ // Create "current" sub-directory in the confbase
+ snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
+
+ if(!deltree(path)) {
+ logger(mesh, MESHLINK_DEBUG, "Could not delete directory %s: %s\n", path, strerror(errno));
+ return false;
+ }
+
+ 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;
+}
+
+/// Wipe an existing configuration directory
+bool config_destroy(const char *confbase, const char *conf_subdir) {
+ assert(conf_subdir);
+
+ if(!confbase) {
+ return true;
+ }
+
+ struct stat st;
+
+ char path[PATH_MAX];
+
+ // Check the presence of configuration base sub directory.
+ snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);
+
+ if(stat(path, &st)) {
+ if(errno == ENOENT) {
+ return true;
+ } else {
+ logger(NULL, MESHLINK_ERROR, "Cannot stat %s: %s\n", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+ }
+
+ // Remove meshlink.conf
+ snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "meshlink.conf", confbase, conf_subdir);
+
+ if(unlink(path)) {
+ if(errno != ENOENT) {
+ logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+ }
+
+ snprintf(path, sizeof(path), "%s" SLASH "%s", confbase, conf_subdir);