[LEDE-DEV] [PATCH uci] file: use fsync after renaming temp file

Rafał Miłecki zajec5 at gmail.com
Tue Sep 6 23:11:03 PDT 2016


From: Rafał Miłecki <rafal at milecki.pl>

We already use a temp file and rename it to avoid ending up with an
empty file e.g. on power cut. What was missing however is fsync after
rename. It's required to make sure directory starts using the new file
instead of the old one.

With this patch UCI changes are stored correctly even with power cut
immediatelly following UCI commit.

Signed-off-by: Rafał Miłecki <rafal at milecki.pl>
---
 file.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/file.c b/file.c
index 7e1e4e6..86d9032 100644
--- a/file.c
+++ b/file.c
@@ -23,6 +23,7 @@
 #include <stdbool.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <libgen.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <glob.h>
@@ -801,11 +802,20 @@ done:
 	free(path);
 	uci_close_stream(f1);
 	if (do_rename) {
+		int dir_fd;
+
 		path = realpath(p->path, NULL);
 		if (!path || rename(filename, path)) {
 			unlink(filename);
 			UCI_THROW(ctx, UCI_ERR_IO);
 		}
+
+		dir_fd = open(dirname(path), O_RDONLY);
+		if (dir_fd) {
+			fsync(dir_fd);
+			close(dir_fd);
+		}
+
 		free(path);
 	}
 	free(filename);
-- 
2.9.3




More information about the Lede-dev mailing list