This will remove all configuration files related to a MeshLink instance.
return meshlink_strerror(err);
}
+ /// Destroy a MeshLink instance.
+ /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
+ * does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
+ * new instance.
+ *
+ * @param confbase The directory in which MeshLink stores its configuration files.
+ * After the function returns, the application is free to overwrite or free @a confbase @a.
+ *
+ * @return This function will return true if the MeshLink instance was succesfully destroyed, false otherwise.
+ */
+ static bool destroy(const char *confbase) {
+ return meshlink_destroy(confbase);
+ }
}
#endif // MESHLINKPP_H
free(mesh);
}
+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);
+ return;
+}
+
+bool meshlink_destroy(const char *confbase) {
+ if(!confbase) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", confbase);
+
+ if(unlink(filename)) {
+ if(errno == ENOENT) {
+ meshlink_errno = MESHLINK_ENOENT;
+ return false;
+ } else {
+ logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+ }
+
+ deltree(confbase);
+
+ return true;
+}
+
void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
*/
extern void meshlink_close(meshlink_handle_t *mesh);
+/// Destroy a MeshLink instance.
+/** This function remove all configuration files of a MeshLink instance. It should only be called when the application
+ * does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
+ * new instance.
+ *
+ * @param confbase The directory in which MeshLink stores its configuration files.
+ * After the function returns, the application is free to overwrite or free @a confbase @a.
+ *
+ * @return This function will return true if the MeshLink instance was succesfully destroyed, false otherwise.
+ */
+extern bool meshlink_destroy(const char *confbase);
+
/// A callback for receiving data from the mesh.
/** @param mesh A handle which represents an instance of MeshLink.
* @param source A pointer to a meshlink_node_t describing the source of the data.
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
+#include <errno.h>
#include "meshlink.h"
meshlink_close(mesh);
+ // Destroy the mesh.
+
+ if(!meshlink_destroy("basic_conf")) {
+ fprintf(stderr, "Could not destroy configuration\n");
+ return 1;
+ }
+
+ if(!access("basic_conf", F_OK) || errno != ENOENT) {
+ fprintf(stderr, "Configuration not fully destroyed\n");
+ return 1;
+ }
+
return 0;
}
#include <cstring>
#include <iostream>
+#include <unistd.h>
+#include <cerrno>
#include "meshlink++.h"
mesh.stop();
+ if(!meshlink::destroy("basicpp_conf")) {
+ cerr << "Could not destroy configuration\n";
+ return 1;
+ }
+
+ if(!access("basic.conf", F_OK) || errno != ENOENT) {
+ cerr << "Configuration not fully destroyed\n";
+ return 1;
+ }
+
return 0;
}