From patchwork Wed Feb 17 07:45:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousong Zhou X-Patchwork-Id: 583958 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.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4111E1402A1 for ; Wed, 17 Feb 2016 18:45:30 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=Ehc4EmjA; dkim-atps=neutral Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 6EA87284DDB; Wed, 17 Feb 2016 08:45:21 +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=-1.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id B8FC82803BD for ; Wed, 17 Feb 2016 08:45:15 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 CL_IP_EQ_HELO_IP=-2 (check from: .gmail. - helo: .mail-pa0-f49.google. - helo-domain: .google.) FROM/MX_MATCHES_HELO(DOMAIN)=-2; rate: -8.5 Received: from mail-pa0-f49.google.com (mail-pa0-f49.google.com [209.85.220.49]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Wed, 17 Feb 2016 08:45:14 +0100 (CET) Received: by mail-pa0-f49.google.com with SMTP id fy10so6904109pac.1 for ; Tue, 16 Feb 2016 23:45:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=HOT7PP35QdmvqTWOWck6pfzWarFOR6KhRG3e9yXx+GM=; b=Ehc4EmjALAozgEwTJdoHegdEEdnnFn6SVqbcnoy1J6i+K5xoIXWNgGFPs36VjMTNkN bCNoxst7ZeDCzmEebLfBume3Fe5AdVbI29B48T+5R3Yo3IIzNMrqVkpWcHumQbHN758a a2AQ1YZbm8j51okSBr9Mg8Pw2ht1gOg5o+wNUQGxn41SOaBgKJf+AvXdmhiu+uethpQD YhI7beQR7zHmeA4VPWfpYIY6DxtKWwPlcZIT7tj5LYgbl8nK2FQanbzRHPFmI6YEFwuW jYE2FIWy4ST16DUZs9a2HlDAD4XW6Gfpp7BUWmzylVwfh1ccAoRY8PbT/UvJEeRVs3kk Ki0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=HOT7PP35QdmvqTWOWck6pfzWarFOR6KhRG3e9yXx+GM=; b=ViqMMc24w2e9iaBkgUJp4Ei8OkMxGNA7mnGek3hhE40h96Mh44B1j6V82TWx0EzpV8 V2r3B+n1NlJ1CU7WJJSuq1BJInWgKCLbYKIycySp8MTr+4j7XEYwVY0oIZXCsS+6/QYm RuFRj0YUAHEE03qk0QAHIAiZVJnY/GuvDgTREYyInZzx99JQ2gxcOe8P/dWeVsS2c3WY QEaRQFIN8PG0wxiQ7yg7p0HjctJQ9brUnxxOHvr+qrjcEEPtIXH9iMRbSFoFf2yY/n5A Iyda3Ssog5nWelrW2wS6VqbA3yLs3b8gcuN2ICGSgrtYM6hrTjGaBIsU+8wGgabe+kTe 6Hig== X-Gm-Message-State: AG10YOQrMvxBG1kzGETDx+R5eqSjSW12yejK+k1qRfBkZ9mx8eB+PTmBVeVAPwI9XltDPg== X-Received: by 10.66.187.36 with SMTP id fp4mr116810pac.47.1455695113873; Tue, 16 Feb 2016 23:45:13 -0800 (PST) Received: from titan.office.mos ([103.29.140.57]) by smtp.gmail.com with ESMTPSA id c24sm265866pfj.41.2016.02.16.23.45.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 16 Feb 2016 23:45:12 -0800 (PST) From: Yousong Zhou To: nbd@openwrt.org Date: Wed, 17 Feb 2016 15:45:05 +0800 Message-Id: <1455695107-20218-1-git-send-email-yszhou4tech@gmail.com> X-Mailer: git-send-email 2.6.4 Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [libubox] [PATCH v3 1/2] json_script: add "isdir" support 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: Yousong Zhou --- examples/json_script-example.json | 5 +++++ json_script.c | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/examples/json_script-example.json b/examples/json_script-example.json index 45636b7..5328e59 100644 --- a/examples/json_script-example.json +++ b/examples/json_script-example.json @@ -27,6 +27,11 @@ [ "exec_if_or", "%ORVAR%" ] ], + [ "if", + [ "isdir", "%ISDIRVAR%" ], + [ "exec_isdir", "%ISDIRVAR%" ] + ], + [ "return", "foobar" ], [ "exec_non_reachable", "Arghhh" ] diff --git a/json_script.c b/json_script.c index 7cef481..b5d414d 100644 --- a/json_script.c +++ b/json_script.c @@ -32,6 +32,7 @@ struct json_handler { static int json_process_expr(struct json_call *call, struct blob_attr *cur); static int json_process_cmd(struct json_call *call, struct blob_attr *cur); +static int eval_string(struct json_call *call, struct blob_buf *buf, const char *name, const char *pattern); struct json_script_file * json_script_file_from_blobmsg(const char *name, void *data, int len) @@ -345,6 +346,30 @@ static int handle_expr_not(struct json_call *call, struct blob_attr *expr) return !ret; } +static int handle_expr_isdir(struct json_call *call, struct blob_attr *expr) +{ + static struct blob_buf b; + struct blob_attr *tb[3]; + const char *pattern, *path; + struct stat s; + int ret; + + json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, 0); + if (!tb[1] || blobmsg_type(tb[1]) != BLOBMSG_TYPE_STRING) + return -1; + pattern = blobmsg_data(tb[1]); + + blob_buf_init(&b, 0); + ret = eval_string(call, &b, NULL, pattern); + if (ret < 0) + return ret; + path = blobmsg_data(blob_data(b.head)); + ret = stat(path, &s); + if (ret < 0) + return 0; + return S_ISDIR(s.st_mode); +} + static const struct json_handler expr[] = { { "eq", handle_expr_eq }, { "regex", handle_expr_regex }, @@ -352,6 +377,7 @@ static const struct json_handler expr[] = { { "and", handle_expr_and }, { "or", handle_expr_or }, { "not", handle_expr_not }, + { "isdir", handle_expr_isdir }, }; static int