file: when writing a file make sure it's contents are stored
authorLuka Perkov <luka@openwrt.org>
Wed, 29 Jan 2014 11:21:58 +0000 (11:21 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Wed, 29 Jan 2014 13:29:48 +0000 (13:29 +0000)
Fixes problems with ubi(fs).

Signed-off-by: Luka Perkov <luka@openwrt.org>
file.c

diff --git a/file.c b/file.c
index 8eef2b5013acf8c3c6a7f95bfb61d63f5aca622d..f9c65a20da6d03397d3900322377f6d8886ce834 100644 (file)
--- a/file.c
+++ b/file.c
@@ -214,8 +214,14 @@ rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
        if ((fd = open(blobmsg_data(tb[RPC_F_PATH]), O_CREAT | O_TRUNC | O_WRONLY)) < 0)
                return rpc_errno_status();
 
-       write(fd, blobmsg_data(tb[RPC_F_DATA]), blobmsg_data_len(tb[RPC_F_DATA]));
+       if (write(fd, blobmsg_data(tb[RPC_F_DATA]), blobmsg_data_len(tb[RPC_F_DATA])) < 0)
+               return rpc_errno_status();
+
+       if (fsync(fd) < 0)
+               return rpc_errno_status();
+
        close(fd);
+       sync();
 
        return 0;
 }