From patchwork Mon Jun 15 08:27:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stijn Devriendt X-Patchwork-Id: 1309326 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::438; helo=mail-wr1-x438.google.com; envelope-from=swupdate+bncbclnzfwotyirbx7btt3qkgqe6sx4xja@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=Cp9LFxqP; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=nxB93yV6; dkim-atps=neutral Received: from mail-wr1-x438.google.com (mail-wr1-x438.google.com [IPv6:2a00:1450:4864:20::438]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49lkw35cpCz9sT8 for ; Mon, 15 Jun 2020 18:27:15 +1000 (AEST) Received: by mail-wr1-x438.google.com with SMTP id e1sf6769442wrm.3 for ; Mon, 15 Jun 2020 01:27:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1592209632; cv=pass; d=google.com; s=arc-20160816; b=JTsoge+m1e6lhr5XSRJahAA0CsNg/p13G2J2LriOfkIJ7GVt5T/JIRP+0+SRyefJMk chPgt6+u3YspCaeoglcerO4WVf/9M8U3s+U2zZRP2Gsm3DmpUBdUhWceXq/7v0k8oZU6 M+fgQ+uRkgjBPbMcVBS2a39EJwnLoWhiybHT6OzgLxVW9RT31EH1WGtW/1Auja1qip+x mDFtDTyolHA0kaCAlRtDyegr0zg3vkZgHl1aChWSRDC0YbKWmtdj3KXalorrRbHW7+l0 5B2pz72C8qO4EV3IC6NADDJaZMEhLpR2Hah7mM38eAg7kU42kP/ZkIuGBBo8UVyPmz9E vkgg== 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 :dkim-signature; bh=/PvI0kGyPLEEeGtshXvjvXM2RuRAo9Z5pSwiZJqxr6A=; b=KUQNJqwMIrMT8x+8ui7TBDrFZFoAj+7qCT+tMHpdru157YpGsA/OV+V0ln6XxHqk6E ahVvpaXtkJN+JhIhWXUGmdDFjDNGoJYYI38ZpY/63tIYUNIHORlRlHtefqFzCc96ZzVq nUjHq9a1Rp78wSnm1c+KD8gh8qU2ZqA2TS0vRzG2+PO0XPwDF4KH9M+QSgbYLXOTWVg5 vHzfTyO5WPxZ0wh6bik8deauChh+C/Adj5zzczVPqCXnFoEY/yf9u+89xLWMC+PQWqDs tcEGzDzamNwlv+16QUSANTkj8ArZfggqkyV3HKXZmn9kkTl6wPKKGMFEO4UyndtfGdOF jMOQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=S7N0y+wa; spf=pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::531 as permitted sender) smtp.mailfrom=highguy@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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=/PvI0kGyPLEEeGtshXvjvXM2RuRAo9Z5pSwiZJqxr6A=; b=Cp9LFxqPgoThY9wb4vAh8Z9OwU0JUsPswrrqO4TDPLx559lOiWIxrrqcTvjgkzK3Mh U9PIVXStMR4pBhwGfXnn2Kb0UK7NPcAjsdPFIkfCCVWMiK5x030dVVR5TJv3jKh8oCKB YfEnE4TxSIl6qIeDOI28Cz5CFfaF6h3zdV4q96gd/wKUNsfzYory8TrsNDGBvc+7KzCe VTty/1f9ePzikvrV2cRH3pLVmZNBpESnVqpMHmIdVYH99vMVU9FVhe0Vl0a0FfGW7H1i 9F2PiPHAOjgthtksyliPi13he5IJqjsq07WJyAMV7brx4CmX1xISJlHSmqALEizD57DK tLCQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=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=/PvI0kGyPLEEeGtshXvjvXM2RuRAo9Z5pSwiZJqxr6A=; b=nxB93yV6qEaFQNlNLVMw1EJdo3m+ooiz8Rue5afRbSXF1urg//TPpePhcqv/Tbfrn2 oPX15Kp+cMZYlVn0bRQDkeVQpB77vAljAKS2aGdqXL/11spGlAyo8avIRzN8oaeoCoM2 SW7NmKsjlVXaJC8FK7DogJZTpq8HPtmjIAZkiAbMbbwi8izxzlx34oNvzsnGo7FVIH+T TysTuaDcP0vNE3E+6OW09hL0s4oMEZTB+9teFBc88krp601B82n5I4uW0dFacAhVxXxK mi/0ZKpQEwrBoLRV6jk6WbBP7Rwl50oQfKzPJobHzkCf7LF2IGjLD+sXUbH0/QGAVKNO 8QOA== 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=/PvI0kGyPLEEeGtshXvjvXM2RuRAo9Z5pSwiZJqxr6A=; b=DE/SuxPVZ36Mi303E/TOFd7M+o0jBjpS9GiSRSBVZW0y6C0j8WLXmulgxb0byXA5SJ PRgZwDJXOG7yDyIWk0OufEh0AH73Lp/2kDv7+SzIT1wlQ9C/ofcxTtYF8qABi0sFFa12 JCxg8/Gh10bibDmsm3qcEcfWoWEAk5N4UJ+KxYm9unFkIRWiAggiwpaQjUnQdLXyUfnZ vopSCLp50jOkfZMJoN222tliNHIh50+jKyMB8+qJMgJ/EEjxnIo58rI7guEZ/a9mVvQ+ /AxyNQXLqbE6iowdFD20jDGga1TyQII8DK+PtNplUL2dU2p+ruZXwY1y9fvK8W524q0H NLkQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM532SVyjGKi/Ls1ebnhV96mV2N477IzaUjXta9ZKSM7+D0kh3HNaU vhsFbQqYH0rxLOp061ikrcg= X-Google-Smtp-Source: ABdhPJxVfoXvO6ITco8BeChoa1Yvlwykske3KKfLxHiu4x1GvQP1Q/gcwypreg1ZBxX3FXuOd470iQ== X-Received: by 2002:a5d:690b:: with SMTP id t11mr29766105wru.213.1592209632217; Mon, 15 Jun 2020 01:27:12 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 2002:adf:db45:: with SMTP id f5ls8043028wrj.0.gmail; Mon, 15 Jun 2020 01:27:11 -0700 (PDT) X-Received: by 2002:a5d:6444:: with SMTP id d4mr28461490wrw.239.1592209631630; Mon, 15 Jun 2020 01:27:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592209631; cv=none; d=google.com; s=arc-20160816; b=Rwa7W//CjvdJ7nYbhNHmyTRa2X5cg7LCM68dyYisD7A30VGBpQMELlJPt1lLJ1Tnr6 YPtVe9xyQU8ud6WJjcStX+ZTWxoIUIP5kj6XjHVhHu1Y9RiO+ZQxTJB2RppYAo/TuHR9 4sCyy7MYCr2rwnZvPvaC59mPGcBaBioHXvMjD7cOucFMNqMwoGiDCpa6UrRK8JFXxLwW fR63itLhNN82bKKlVOMo+5q2dvGxGXZkKOpUced1Di+n59fdMsWVb07qnBfbGYbpQOIi 6wYaM1155PAxPi3F+9/fdgix4858ZDiK+2e94AB/z/MMjmY/qZr4slxnnVD/Z54wIo3h bwNQ== 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 :dkim-signature; bh=xCL7d3eUhF5xsK7KmRYFzG4Ln1kq4+YOeZCO+nz/Ue8=; b=JznSq/oomynHkuQGlwFaCVi/3Z+m/5J/pVhhz3pAl0fDlPso0YmhfYGr07OtCbWAY5 irIuIVRewHTCrXt5pofmLrBdV62abeTS+IRLyd4Ag/Cyuda5tthAhOUpwMZJVYmkK9jq a+c2JsL8WKhQI+ut9nrKO2QPwl0rOwuSuwGMQjgYhcHbnuQAt7uTFVV3GuXJHA122Xuy 8f4yKzezDvIudAb6Z3DOVrBEgOAKA0HkFWM0IDfJNqWE795T84Fy+VPQUzpUm0TicS9C 2iCsD7TJwyUZjOoowlsPVToeXzsqhYu5JgwznM5hh5UHUHiBq7RBkr7SSL7nXTz55onH oEcw== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=S7N0y+wa; spf=pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::531 as permitted sender) smtp.mailfrom=highguy@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com. [2a00:1450:4864:20::531]) by gmr-mx.google.com with ESMTPS id 12si969111wmk.3.2020.06.15.01.27.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 Jun 2020 01:27:11 -0700 (PDT) Received-SPF: pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::531 as permitted sender) client-ip=2a00:1450:4864:20::531; Received: by mail-ed1-x531.google.com with SMTP id p18so10827285eds.7 for ; Mon, 15 Jun 2020 01:27:11 -0700 (PDT) X-Received: by 2002:a05:6402:b37:: with SMTP id bo23mr23335860edb.24.1592209631309; Mon, 15 Jun 2020 01:27:11 -0700 (PDT) Received: from sde1.telecom-it.be (ptr-dv4l9auv4w1m1i5kotr.18120a2.ip6.access.telenet.be. [2a02:1811:ce13:ba00:717d:f0a2:e207:f7af]) by smtp.googlemail.com with ESMTPSA id fi13sm8656314ejb.34.2020.06.15.01.27.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 01:27:10 -0700 (PDT) From: Stijn Devriendt To: HIGHGuY@gmail.com, sbabic@denx.de Cc: tomas@aparicio.me, swupdate@googlegroups.com, Stijn Devriendt Subject: [swupdate] [PATCH v3 5/7] Compare versions according to semver Date: Mon, 15 Jun 2020 10:27:00 +0200 Message-Id: <20200615082702.32693-6-sde@unmatched.eu> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200612100354.3591-1-sde@unmatched.eu> References: <20200612100354.3591-1-sde@unmatched.eu> X-Original-Sender: HIGHGuY@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=S7N0y+wa; spf=pass (google.com: domain of highguy@gmail.com designates 2a00:1450:4864:20::531 as permitted sender) smtp.mailfrom=highguy@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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: , Still, fallback to legacy versioning scheme if needed. Signed-off-by: Stijn Devriendt --- core/artifacts_versions.c | 48 ++++++++++++++++++++++++++++++++++++++- core/parser.c | 11 ++++----- include/util.h | 2 +- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/core/artifacts_versions.c b/core/artifacts_versions.c index ccc283e..2ccba85 100644 --- a/core/artifacts_versions.c +++ b/core/artifacts_versions.c @@ -210,7 +210,7 @@ void cleanup_version(char* str) * Also major.minor or major.minor.revision are allowed * The conversion generates a 64 bit value that can be compared */ -__u64 version_to_number(const char *version_string) +static __u64 version_to_number(const char *version_string) { char **versions = NULL; char **ver; @@ -235,3 +235,49 @@ __u64 version_to_number(const char *version_string) return version; } + +/* + * Compare 2 versions. + * + * Mind that this function accepts both version types: + * - old-style: major.minor.revision.buildinfo + * - semantic versioning: major.minor.patch[-prerelease][+buildinfo] + * see https://semver.org + * + * Returns -1, 0 or 1 of left is respectively lower than, equal to or greater than right. + */ +int compare_versions(const char* left_version, const char* right_version) +{ + if (is_oldstyle_version(left_version) && is_oldstyle_version(right_version)) + { + __u64 left_u64 = version_to_number(left_version); + __u64 right_u64 = version_to_number(right_version); + + if (left_u64 < right_u64) + return -1; + else if (left_u64 > right_u64) + return 1; + else + return 0; + } + else + { + semver_t left_sem = {}; + semver_t right_sem = {}; + int comparison; + + /* There's no error checking here. + * Oldstyle code also defaults to treating unparseable version as 0. + * Failed semver_parse also leads to 0.0.0 if properly initialized. + */ + semver_parse(left_version, &left_sem); + semver_parse(right_version, &right_sem); + + comparison = semver_compare(left_sem, right_sem); + + semver_free(&left_sem); + semver_free(&right_sem); + + return comparison; + } +} diff --git a/core/parser.c b/core/parser.c index e7e68ea..ba0d2a6 100644 --- a/core/parser.c +++ b/core/parser.c @@ -172,15 +172,15 @@ static int is_image_higher(struct swver *sw_ver_list, return false; LIST_FOREACH(swver, sw_ver_list, next) { - __u64 minimum_version = version_to_number(swver->version); - __u64 newversion = version_to_number(img->id.version); + const char* current_version = swver->version; + const char* proposed_version = img->id.version; /* * Check if name are identical and the new version is lower * or equal. */ if (!strncmp(img->id.name, swver->name, sizeof(img->id.name)) && - (minimum_version >= newversion)) { + (compare_versions(proposed_version, current_version) < 0)) { TRACE("%s(%s) has a higher version installed, skipping...", img->id.name, img->id.version); @@ -306,10 +306,7 @@ int parse(struct swupdate_cfg *sw, const char *descfile) * newer version */ if (sw->globals.no_downgrading) { - __u64 minimum_version = version_to_number(sw->globals.minimum_version); - __u64 newversion = version_to_number(sw->version); - - if (newversion < minimum_version) { + if (compare_versions(sw->version, sw->globals.minimum_version) < 0) { ERROR("No downgrading allowed: new version %s <= installed %s", sw->version, sw->globals.minimum_version); return -EPERM; diff --git a/include/util.h b/include/util.h index 6a027c6..adf585e 100644 --- a/include/util.h +++ b/include/util.h @@ -217,7 +217,7 @@ void freeargs (char **argv); int get_hw_revision(struct hw_type *hw); void get_sw_versions(char *cfgfname, struct swupdate_cfg *sw); void cleanup_version(char* str); -__u64 version_to_number(const char *version_string); +int compare_versions(const char* left_version, const char* right_version); int hwid_match(const char* rev, const char* hwrev); int check_hw_compatibility(struct swupdate_cfg *cfg); int count_elem_list(struct imglist *list);