file: create a file on write if it does not exist
authorLuka Perkov <luka@openwrt.org>
Wed, 29 Jan 2014 11:21:57 +0000 (11:21 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Wed, 29 Jan 2014 13:29:40 +0000 (13:29 +0000)
Signed-off-by: Luka Perkov <luka@openwrt.org>
file.c

diff --git a/file.c b/file.c
index d22add56caceffbdcd1798bd2f4a664f394154ff..8eef2b5013acf8c3c6a7f95bfb61d63f5aca622d 100644 (file)
--- a/file.c
+++ b/file.c
@@ -203,17 +203,15 @@ rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
                struct blob_attr *msg)
 {
        int fd;
-       char *path;
-       struct stat s;
-       struct blob_attr **tb;
+       struct blob_attr *tb[__RPC_F_MAX];
 
-       if (!(tb = rpc_check_path(msg, &path, &s)))
-               return rpc_errno_status();
+       blobmsg_parse(rpc_file_policy, __RPC_F_MAX, tb,
+                     blob_data(msg), blob_len(msg));
 
-       if (!tb[RPC_F_DATA])
+       if (!tb[RPC_F_PATH] || !tb[RPC_F_DATA])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       if ((fd = open(path, O_WRONLY)) < 0)
+       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]));