]> git.meshlink.io Git - meshlink/blobdiff - src/conf.c
Fix potential double fclose().
[meshlink] / src / conf.c
index bafa87d49c5bd5a563e9d82d5969ca4a51f7ec72..3003a6f4205b17e040d58e3e7ad12e8438804746 100644 (file)
@@ -80,18 +80,21 @@ static bool sync_path(const char *pathname) {
 
        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;
        }
 
@@ -343,6 +346,31 @@ bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const c
        return rename(old_path, new_path) == 0;
 }
 
+bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
+       if(!mesh->confbase) {
+               return true;
+       }
+
+       if(!conf_subdir) {
+               return false;
+       }
+
+       char path[PATH_MAX];
+       snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
+
+       if(!sync_path(path)) {
+               return false;
+       }
+
+       snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
+
+       if(!sync_path(path)) {
+               return false;
+       }
+
+       return true;
+}
+
 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
        if(!mesh->confbase) {
                return false;
@@ -453,7 +481,6 @@ bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const
        if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
                logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
                meshlink_errno = MESHLINK_ESTORAGE;
-               fclose(f);
                return false;
        }
 
@@ -462,7 +489,6 @@ bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const
        if(fread(buf, len, 1, f) != 1) {
                logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
                meshlink_errno = MESHLINK_ESTORAGE;
-               fclose(f);
                return false;
        }
 
@@ -711,6 +737,7 @@ bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const c
 
        if(rename(tmp_path, path)) {
                logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
+               fclose(f);
                return false;
        }
 
@@ -805,6 +832,7 @@ bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const ch
 
        if(fsync(fileno(f))) {
                logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
+               fclose(f);
                return false;
        }