From b055d934d156a379a219b01e53a1cbfe043ebfd1 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 5 Nov 2019 19:22:09 +0100 Subject: [PATCH] Add missing calls to fflush(). We need to ensure file handles have flushed the stream buffer to disk before calling fsync(). Also remove redundant calls to fsync(), config_write_file() already takes care of it. --- src/conf.c | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/conf.c b/src/conf.c index ac4d71a2..79db093e 100644 --- a/src/conf.c +++ b/src/conf.c @@ -580,6 +580,12 @@ bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config, return false; } + if(fflush(f)) { + logger(mesh, MESHLINK_ERROR, "Failed to flush file: %s", strerror(errno)); + meshlink_errno = MESHLINK_ESTORAGE; + return false; + } + if(fsync(fileno(f))) { logger(mesh, MESHLINK_ERROR, "Failed to sync file: %s\n", strerror(errno)); return false; @@ -704,12 +710,6 @@ bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char * return false; } - if(fsync(fileno(f))) { - logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno)); - fclose(f); - return false; - } - if(fclose(f)) { logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno)); return false; @@ -780,12 +780,6 @@ bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const c return false; } - if(fsync(fileno(f))) { - logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno)); - fclose(f); - return false; - } - if(rename(tmp_path, path)) { logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno)); fclose(f); @@ -889,12 +883,6 @@ bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const ch return false; } - if(fsync(fileno(f))) { - logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno)); - fclose(f); - return false; - } - if(fclose(f)) { logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno)); return false; -- 2.39.5