[OpenWrt-Devel] [PATCH 3/3] [rpcd] file: add support for setting mode when writing files

Luka Perkov luka at openwrt.org
Sun Mar 15 17:00:52 EDT 2015


Signed-off-by: Luka Perkov <luka at openwrt.org>
---
 file.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/file.c b/file.c
index 54f5d8a..95dd4d4 100644
--- a/file.c
+++ b/file.c
@@ -92,6 +92,7 @@ static const struct blobmsg_policy rpc_file_rb_policy[__RPC_F_RB_MAX] = {
 enum {
 	RPC_F_RW_PATH,
 	RPC_F_RW_DATA,
+	RPC_F_RW_MODE,
 	RPC_F_RW_BASE64,
 	__RPC_F_RW_MAX,
 };
@@ -99,6 +100,7 @@ enum {
 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
 	[RPC_F_RW_PATH]   = { .name = "path",   .type = BLOBMSG_TYPE_STRING },
 	[RPC_F_RW_DATA]   = { .name = "data",   .type = BLOBMSG_TYPE_STRING },
+	[RPC_F_RW_MODE]   = { .name = "mode",   .type = BLOBMSG_TYPE_INT32  },
 	[RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL   },
 };
 
@@ -372,6 +374,7 @@ rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
 {
 	struct blob_attr *tb[__RPC_F_RW_MAX];
 	bool base64 = false;
+	mode_t mode = 0644;
 	int fd, rv = 0;
 	void *rbuf;
 	size_t rbuf_len;
@@ -382,12 +385,16 @@ rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
 	if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
 		return UBUS_STATUS_INVALID_ARGUMENT;
 
-	if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | O_TRUNC)) < 0)
-		return rpc_errno_status();
+	if (tb[RPC_F_RW_MODE])
+		mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
 
 	if (tb[RPC_F_RW_BASE64])
 		base64 = blobmsg_get_bool(tb[RPC_F_RW_BASE64]);
 
+	umask(0);
+	if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | O_TRUNC, mode)) < 0)
+		return rpc_errno_status();
+
 	if (base64) {
 		rbuf_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
 		rbuf = b64decode(blobmsg_data(tb[RPC_F_RW_DATA]), &rbuf_len);
-- 
2.3.3
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list