[189] Call fsync after persisting data.

To ensure it is correctly written. Closes #189.

Thanks to thanhvtruong.

Bug: https://github.com/eclipse/mosquitto/issues/189
This commit is contained in:
Roger A. Light 2016-06-26 22:48:16 +01:00
parent fff741613e
commit 84df2bb923
2 changed files with 18 additions and 0 deletions

View File

@ -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.

View File

@ -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