diff --git a/ChangeLog.txt b/ChangeLog.txt index 9e652ad5..64459fa2 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -7,6 +7,8 @@ Broker: - Don't disconnect client on HUP before reading the pending data. Closes #7. - Fix some $SYS messages being incorrectly persisted. Closes #191. - Support OpenSSL 1.1.0. +- Call fsync after persisting data to ensure it is correctly written. Closes + #189. Client library: - Support OpenSSL 1.1.0. diff --git a/src/persist.c b/src/persist.c index f416aeab..406675f9 100644 --- a/src/persist.c +++ b/src/persist.c @@ -350,6 +350,9 @@ int mqtt3_db_backup(struct mosquitto_db *db, bool shutdown) char err[256]; char *outfile = NULL; int len; +#ifndef WIN32 + int dir_fd; +#endif if(!db || !db->config || !db->config->persistence_filepath) return MOSQ_ERR_INVAL; _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Saving in-memory database to %s.", db->config->persistence_filepath); @@ -395,6 +398,19 @@ int mqtt3_db_backup(struct mosquitto_db *db, bool shutdown) mqtt3_db_client_write(db, db_fptr); mqtt3_db_subs_retain_write(db, db_fptr); +#ifndef WIN32 + fsync(fileno(db_fptr)); + + if(db->config->persistence_location){ + dir_fd = open(db->config->persistence_location, O_DIRECTORY); + }else{ + dir_fd = open(".", O_DIRECTORY); + } + if(dir_fd > 0){ + fsync(dir_fd); + close(dir_fd); + } +#endif fclose(db_fptr); #ifdef WIN32