From d665704b718634614856db2e0455e1cb60048771 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 2 Dec 2019 00:32:57 +0100 Subject: [PATCH] Destroy new/ and old/ subdirectories when creating a new instance. --- src/meshlink.c | 13 ++++++++++++- test/basic.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/meshlink.c b/src/meshlink.c index 88a4c319..b0cb8a93 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -868,6 +868,18 @@ static void add_local_addresses(meshlink_handle_t *mesh) { } static bool meshlink_setup(meshlink_handle_t *mesh) { + if(!config_destroy(mesh->confbase, "new")) { + logger(mesh, MESHLINK_ERROR, "Could not delete configuration in %s/new: %s\n", mesh->confbase, strerror(errno)); + meshlink_errno = MESHLINK_ESTORAGE; + return false; + } + + if(!config_destroy(mesh->confbase, "old")) { + logger(mesh, MESHLINK_ERROR, "Could not delete configuration in %s/old: %s\n", mesh->confbase, strerror(errno)); + meshlink_errno = MESHLINK_ESTORAGE; + return false; + } + if(!config_init(mesh, "current")) { logger(mesh, MESHLINK_ERROR, "Could not set up configuration in %s/current: %s\n", mesh->confbase, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; @@ -1683,7 +1695,6 @@ bool meshlink_destroy(const char *confbase) { fclose(lockfile); - /* TODO: do we need to remove confbase? Potential race condition? */ if(!sync_path(confbase)) { logger(NULL, MESHLINK_ERROR, "Cannot sync directory %s: %s\n", confbase, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; diff --git a/test/basic.c b/test/basic.c index f591df7b..e0f77c57 100644 --- a/test/basic.c +++ b/test/basic.c @@ -45,6 +45,8 @@ int main() { // Make sure we can start and stop the mesh again. assert(meshlink_start(mesh)); + assert(meshlink_start(mesh)); + meshlink_stop(mesh); meshlink_stop(mesh); // Close the mesh and open it again, now with a different name parameter. @@ -66,6 +68,28 @@ int main() { meshlink_stop(mesh); meshlink_close(mesh); + // Check that messing with the config directory will create a new instance. + + assert(unlink("basic_conf/current/meshlink.conf") == 0); + mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE); + assert(mesh); + assert(!meshlink_get_node(mesh, "foo")); + self = meshlink_get_self(mesh); + assert(self); + assert(!strcmp(self->name, "bar")); + assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT); + meshlink_close(mesh); + + assert(rename("basic_conf/current", "basic_conf/new") == 0); + mesh = meshlink_open("basic_conf", "baz", "basic", DEV_CLASS_BACKBONE); + assert(mesh); + assert(!meshlink_get_node(mesh, "bar")); + self = meshlink_get_self(mesh); + assert(self); + assert(!strcmp(self->name, "baz")); + assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT); + meshlink_close(mesh); + // Destroy the mesh. assert(meshlink_destroy("basic_conf")); @@ -81,4 +105,8 @@ int main() { } closedir(dir); + + // Check that we can destroy it again. + + assert(meshlink_destroy("basic_conf")); } -- 2.39.2