From patchwork Sun Mar 15 21:00:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luka Perkov X-Patchwork-Id: 450337 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id B93B914007F for ; Mon, 16 Mar 2015 08:03:06 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id DA99028039B; Sun, 15 Mar 2015 22:01:35 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=unavailable version=3.3.2 Received: from localhost (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 67C382803F3; Sun, 15 Mar 2015 22:01:04 +0100 (CET) X-Virus-Scanned: at arrakis.dune.hu Received: from t530.lan (141-138-0-166.dhcp.iskon.hr [141.138.0.166]) by arrakis.dune.hu (Postfix) with ESMTPSA id 586D0281488; Sun, 15 Mar 2015 22:00:58 +0100 (CET) From: Luka Perkov To: openwrt-devel@lists.openwrt.org Date: Sun, 15 Mar 2015 22:00:52 +0100 Message-Id: <1426453252-30326-3-git-send-email-luka@openwrt.org> X-Mailer: git-send-email 2.3.3 In-Reply-To: <1426453252-30326-1-git-send-email-luka@openwrt.org> References: <1426453252-30326-1-git-send-email-luka@openwrt.org> Cc: Luka Perkov Subject: [OpenWrt-Devel] [PATCH 3/3] [rpcd] file: add support for setting mode when writing files X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" Signed-off-by: Luka Perkov --- 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);