Permalink
Browse files

[189] Call fsync after persisting data.

To ensure it is correctly written. Closes #189.

Thanks to thanhvtruong.

Bug: #189
  • Loading branch information...
1 parent fff7416 commit 84df2bb9237611221c7962814ac0567c8519cf50 @ralight ralight committed Jun 26, 2016
Showing with 18 additions and 0 deletions.
  1. +2 −0 ChangeLog.txt
  2. +16 −0 src/persist.c
View
@@ -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
@@ -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

0 comments on commit 84df2bb

Please sign in to comment.