From patchwork Sat Feb 13 10:08:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousong Zhou X-Patchwork-Id: 582392 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 A40231402EC for ; Sat, 13 Feb 2016 23:50:45 +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=lxHqliPF; dkim-atps=neutral Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 708E328BB55; Sat, 13 Feb 2016 13:50:25 +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 2D35E28A687 for ; Sat, 13 Feb 2016 13:50:20 +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-f52.google. - helo-domain: .google.) FROM/MX_MATCHES_HELO(DOMAIN)=-2; rate: -8.5 Received: from mail-pa0-f52.google.com (mail-pa0-f52.google.com [209.85.220.52]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Sat, 13 Feb 2016 13:50:19 +0100 (CET) Received: by mail-pa0-f52.google.com with SMTP id fy10so21372260pac.1 for ; Sat, 13 Feb 2016 04:50:19 -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=5tb6/GBkX/5+c1c6PsIINqsnc2xXH62aFeRfOb4aZeo=; b=lxHqliPFKub9a0vs7M7GOLYCJ+9Fc2yxlxfnd2WQNZL1v3ZfUgn8k8bbVQIKIQmbq0 chwUbxw6fMivHcuS+uVBOzmtL3JVsBWpXDULA8EaAIYkVpBnRT8CaofWZyMHVaqtTkkW lJsK9JWmCAwLqrbEc/6or5BVQPjjyTOteTfajzgjbtQVzDUW20WPy2Hjm3cPod3QtJUC 8JHh3sS4DRxQQHYIVFXu1S+KTgmhUOR7oAxAnqSlKoZI9cfdo//zNjNKya+KJgscSbQr Ftc1YU1JNgsISIC4XfZOaJS1/uhRCNXvhtB36CgIAYlPcHitMjTjvR8rzmEna8jGq6kp ZBhA== 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=5tb6/GBkX/5+c1c6PsIINqsnc2xXH62aFeRfOb4aZeo=; b=fVNpOGPIK6lJ2IG9suqIKAiBy/UmhDATqziaI9g21ZnwMgG2zqTqPBevmbyFJG4yuR bMYFSmkpjTRv2WjuBiAVopYPCG0TnpAm5NaKB51YU+Zdt3UaMqCgW9YZi8xfgU/vtjiw Ai5qXU1RK76ECCyPUWlqakf5UDrlaT8PmliBhxFilZRuU4MJFVyMb8wetutjSKoTJxyp /Luz1LZO3Evt/IPlNBnv/eEGmMuXy0/vm5SE8cLBdDPufX53LMVNtVN0Jr6ZCk+VyBZl iE3Yu+4YxejwlJfEBc5jsD+NqqZn++JcEeUuhm6QcEX7SGuNLkW3snQOOkQleMV8fmgf SGGA== X-Gm-Message-State: AG10YORj0VD+IdaiedVWK2AAcpZd4SEiZB3fvvfP9a+nifkaGoe9a0ZMXR9xtqd3/BvrIw== X-Received: by 10.66.122.3 with SMTP id lo3mr8655886pab.25.1455358137648; Sat, 13 Feb 2016 02:08:57 -0800 (PST) Received: from yousongs-Air.lan (ln.tengcy.org. [192.81.129.91]) by smtp.gmail.com with ESMTPSA id b63sm25124296pfj.25.2016.02.13.02.08.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 13 Feb 2016 02:08:56 -0800 (PST) From: Yousong Zhou To: nbd@openwrt.org Date: Sat, 13 Feb 2016 18:08:26 +0800 Message-Id: <1455358106-18326-1-git-send-email-yszhou4tech@gmail.com> X-Mailer: git-send-email 2.6.2 Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH][libubox] 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 --- This is intended for use by hotplug system to check whether /etc/hotplug.d/%SUBSYSTEM% is a directory 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 73c2502..ab876a0 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 ret; + 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