From patchwork Thu Nov 1 17:06:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 992017 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::13d; helo=mail-lf1-x13d.google.com; envelope-from=swupdate+bncbcxploxj6ikrbkhf5tpakgqejxdu2yi@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="Otb0dHyy"; dkim-atps=neutral Received: from mail-lf1-x13d.google.com (mail-lf1-x13d.google.com [IPv6:2a00:1450:4864:20::13d]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42mBSq2SRnz9sRc for ; Fri, 2 Nov 2018 04:06:51 +1100 (AEDT) Received: by mail-lf1-x13d.google.com with SMTP id o34sf1376423lfg.19 for ; Thu, 01 Nov 2018 10:06:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1541092008; cv=pass; d=google.com; s=arc-20160816; b=RiqdPTPd+QenTDDOujj+1EG/n91LcgOJJXtqSwQ2alNeq36/a+dDdgpW9FldVoJAwQ TrVaFfhS+GU2oXvcI1Rcp4cOt8bXzo6n8FAtzTkG5VQeIzqkemHc9W880EPMlYT1HYhD s2FREA1Cx7m3HpLhxaG5PZ4hjK7P/zYhzxqGhEl0NhQ3Aet4UYq4FuOSda1NFjXD9Ge7 yQv/Md5jKaPH/mPHWknju7JexPNm/ZgwhVTe7NNZGeQfZ4zjcYA8v08HPiaL0GTDR/7e G5O9wT9GMa4sILzlP9xK2eqrKxKGUprjqhpcQL79nwgK3uSEjAhNGEPps7+dRkFoMRK9 MNvg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:mime-version:sender:dkim-signature; bh=RDPetkawNj7Iq//XL8zOD3gdEIfCV1BGxud6xDOUwgY=; b=uyJf2xF9Ux4bbELDY9lpYglgA9cFPVurr/dmSMCdfWd+jw/Oy/saIQq0poD0CIGvm0 mJyLJ3H+cQoR16lH+xGCyuXr4FyEeTd8bPWjWbYVRPSPBDz0H/at+S9eDBjAJiRP24nT Klh54IB78PU6YdZgtop7hTWoVK5lJs6xzGoswkYK3qRLTBbp9iXdDnpjYcp9ycqQjLmi sf7HAo5XUv3du08xjf+OGasK+yPWmRvvCrqSxK3XaKhQatFhIX69DIrXEpeJJpZO4Aly XT9I5pEA4wuXYyChcVPW3WPm8oJxgvpF6zCASEXwmp8M3Zbw97Ww7T0sTt949iwE8Se2 hlRA== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=RDPetkawNj7Iq//XL8zOD3gdEIfCV1BGxud6xDOUwgY=; b=Otb0dHyyuYP98z83Bb8UJ9dtk35X7Yi0iuNaW4YERHSa5uCIYHCO8V9dMWW3wl6Ih1 UltvrdajJf7g5/YWf2cvklJlnncDGbPTT10JTB2L/FrmrlU5uFxAtpQaGRZF7HBgffuw tEr2HQqMZbrZPNQHXyREag2WGPDFwJMYTLjmZyToGGfGiR1oCmgyza1wrz6yIcPGGaJ3 EgY3/YvjjlIWJEeNGeG0P2SKhs7EUlqpVK9e849pHXzX6mZUuic/udoeWxmtLOB34uYU aewXay0PvYDIyFvDSQkq/EYtLKtvspFOsnV+bMOJ+tWpPOfNznyQDiRby76pnl3QbDbu m3sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:mime-version:from:to:cc:subject:date :message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=RDPetkawNj7Iq//XL8zOD3gdEIfCV1BGxud6xDOUwgY=; b=Z00O1KfVVGrrIsbfgDPG6imQpkZu0B0ty4bM289YXkXjWLQX6BTl75B2BlY3IsALpn WMbGeMJMLHMBnoDpoLSk5rCpVe0lxN2YcGcslPuqWhOhKr6UQWuoENdUtat1g2wJUWuP A8fyAr4Kw9/oQuwlFjao+5xwUIpZNlbkIQ7Ad9+vklJ3knn3Ph5T68tGmFMPrJUqZZXF FO1iOo3EWMWEF6Ly/d28nGMsQaXfNOTmyiT8aD2MlY0kUDyeklpZI+4mE2rUSQyj2uJL UDqr9fotzlt07mCv5jPpnjZOG/1ikE6Ij061obHXS/z2osNmqlPfCyLd7Vsd0BsG4tLD yEbw== Sender: swupdate@googlegroups.com X-Gm-Message-State: AGRZ1gInoIQ+bHTB1mrGnQ2D9rY56unY6Tmd3sdzqnKjiaugiAUGa6bz qBky4novol4TRCMvklvl8F8= X-Google-Smtp-Source: AJdET5eqK7YZamrmKgLtMIOuHFZOpqhOxpyGOG+hjXG5x0boEAm7s2jFa9FSpZWuh4whwJpm/Ao/sw== X-Received: by 2002:a2e:93c5:: with SMTP id p5-v6mr48575ljh.7.1541092008431; Thu, 01 Nov 2018 10:06:48 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 2002:a2e:700b:: with SMTP id l11-v6ls2028398ljc.19.gmail; Thu, 01 Nov 2018 10:06:47 -0700 (PDT) X-Received: by 2002:a2e:9597:: with SMTP id w23-v6mr205997ljh.25.1541092007887; Thu, 01 Nov 2018 10:06:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541092007; cv=none; d=google.com; s=arc-20160816; b=FdaREFTwCLLMIS+NREre2CPQG1VIQs+O428Ff8VkeORGzxkKQh02VrHfCgrBNgCa6C Eedu7oXEgvqwSlh/jaylxFLWEAdz3uFW3vv6gvejlrKGbXxaRMFk4K4lpFaGz01mk4+F AQytKTAL8z5uGRbGHLgTP1a6+2Y9262J/54nHm70+luOwSj7aHc/dCDXK8ToYwa+R1O5 2V5hoeci3yXfj9yc06CGIYGtheWvExAcIIWfhkG4HnjoL4YqNdsWwPx3LvGv96LZpQYy nXYGmhZqTvmCvBjag0fwmp2HH66iCf1jSw2WrlzVR0GbaWfiUhwtUcpSkjoPKsdkG/f3 9BMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=o++6yjSbjRWvCuyanK5XIrty9GLql9AI7FtnTmqdBn4=; b=RDOLl6FC2fCkRtmGOuWKnVhJKrkhOuFpuKq+TOvYZQK9LS0WpOEgK2PrBcO6uk4URA utHVocy3a7X5fRElaiVnnpBsdtDa4uSXsaUJowudkzMDb5iZhGUrWIS2PjDvVCNMUvSE wVP/5Xni17kH+vPvySsMk08WOB6W8KqdvphpjnmdnEPqN1koFfK0nN69ThF/0Mn4aOOj 3Ah8uY38xupC9Ge8XfeyWHfn1Uutj+t+zRrDrnp2fyCV6mpSBteX8JRQwWEUG71XyW4I YLkyQB201zAeK2aPJkmwcNX7jpo+iGkC21K+T6/dbFahzkIdZ7QCw9vQQotsHosCsU3n Spig== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Received: from mail-out.m-online.net (mail-out.m-online.net. [212.18.0.9]) by gmr-mx.google.com with ESMTPS id a79-v6si967009ljf.1.2018.11.01.10.06.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Nov 2018 10:06:47 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) client-ip=212.18.0.9; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 42mBSl2FtXz1qvwR; Thu, 1 Nov 2018 18:06:47 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 42mBSl28fyz1qsJP; Thu, 1 Nov 2018 18:06:47 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id 9A9wSKK7RwqK; Thu, 1 Nov 2018 18:06:46 +0100 (CET) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Thu, 1 Nov 2018 18:06:46 +0100 (CET) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 012DD4540360; Thu, 1 Nov 2018 18:06:46 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([127.0.0.1]) by localhost (mail.babic.homelinux.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tfFDeZ7VFenD; Thu, 1 Nov 2018 18:06:43 +0100 (CET) Received: from papero.fritz.box (papero.fritz.box [192.168.178.132]) by babic.homelinux.org (Postfix) with ESMTP id 487C4454046A; Thu, 1 Nov 2018 18:06:41 +0100 (CET) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 3/5] parser: function to get a new path of a node Date: Thu, 1 Nov 2018 18:06:33 +0100 Message-Id: <20181101170635.13493-3-sbabic@denx.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181101170635.13493-1-sbabic@denx.de> References: <20181101170635.13493-1-sbabic@denx.de> X-Original-Sender: sbabic@denx.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Using links, the path to a node must be computed again. The function set_find_path() get the string pointed to the link and modify the array of strings containing the whole path to a node. A convention is used to traverse the tree upwards. If the string begins with "..", this means to go up in the tree. Multiple leading ".." are allowed to go up into the tree. Signed-off-by: Stefano Babic --- corelib/parsing_library.c | 56 +++++++++++++++++++++++++++++++++++++++ include/parselib.h | 10 +++++++ 2 files changed, 66 insertions(+) diff --git a/corelib/parsing_library.c b/corelib/parsing_library.c index e9739fe..00463a7 100644 --- a/corelib/parsing_library.c +++ b/corelib/parsing_library.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "generated/autoconf.h" #include "bsdqueue.h" @@ -19,6 +20,8 @@ #include "swupdate.h" #include "parselib.h" +#define MAX_LINKS_DEPTH 10 + void check_field_string(const char *src, char *dst, const size_t max_len) { assert(max_len>0); @@ -159,3 +162,56 @@ void get_hash_value(parsertype p, void *elem, unsigned char *hash) ascii_to_hash(hash, hash_ascii); } + +bool set_find_path(const char **nodes, const char *newpath, char **tmp) +{ + unsigned int nleading; + char **iter, **paths; + unsigned int count = count_string_array(nodes); + unsigned int countpaths; + + if (!newpath) + return false; + + /* + * Check if we have to traverse back + */ + for (nleading = 0; newpath[nleading] == '.'; nleading++); + + /* + * delimiter at the beginning indicates a relative path + * exactly as in Unix, that mean .. for the upper directory + * .. = parent + * .... = parent of parent + * The number of leading "." must be even, else + * it is a malformed path + */ + if (nleading % 2) + return false; + + nleading /= 2; + if ((count - nleading) <= 0) + return false; + + count -= nleading; + if (count > 0) count--; + + paths = string_split(newpath, '.'); + + /* + * check if there is enough space in nodes + */ + countpaths = count_string_array((const char **)paths); + if (count + countpaths >= MAX_PARSED_NODES) + return false; + if (!countpaths) + nodes[count++] = newpath; + else + for (iter = paths; *iter != NULL; iter++, count++) + nodes[count] = *iter; + nodes[count] = NULL; + + tmp = paths; + + return true; +} diff --git a/include/parselib.h b/include/parselib.h index f53dd00..2fca3b5 100644 --- a/include/parselib.h +++ b/include/parselib.h @@ -9,6 +9,7 @@ #define _PARSE_LIBRARY_H #include +#include typedef enum { LIBCFG_PARSER, @@ -18,6 +19,12 @@ typedef enum { typedef void (*iterate_callback)(const char *name, const char *value, void *data); +/* + * This is to limit the structure (array) used to save the whole + * path to the entry to be read. + */ +#define MAX_PARSED_NODES 20 + #ifdef CONFIG_LIBCONFIG #include #define LIBCONFIG_VERSION ((LIBCONFIG_VER_MAJOR << 16) | \ @@ -83,6 +90,9 @@ void get_field(parsertype p, void *e, const char *path, void *dest); int exist_field_string(parsertype p, void *e, const char *path); void get_hash_value(parsertype p, void *elem, unsigned char *hash); void check_field_string(const char *src, char *dst, const size_t max_len); +bool find_root(parsertype p, void *root, const char **nodes); +void *get_node(parsertype p, void *root, const char **nodes); +bool set_find_path(const char **nodes, const char *newpath, char **tmp); #define GET_FIELD_STRING(p, e, name, d) \ get_field_string_with_size(p, e, name, d, sizeof(d))