From patchwork Mon Oct 19 13:55:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1384314 X-Patchwork-Delegate: trini@ti.com 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=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=WHvHBXFu; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (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 4CFJGC32lGz9sPB for ; Tue, 20 Oct 2020 00:56:51 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 83D9E823C4; Mon, 19 Oct 2020 15:56:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="WHvHBXFu"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0CBA281B79; Mon, 19 Oct 2020 15:56:20 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd42.google.com (mail-io1-xd42.google.com [IPv6:2607:f8b0:4864:20::d42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 96F9781B79 for ; Mon, 19 Oct 2020 15:56:12 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd42.google.com with SMTP id z5so4455491iob.1 for ; Mon, 19 Oct 2020 06:56:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uKCZ7ivx5WLVg3h43Ng2ow+kLLnxVBM9eH6g+KVYG/A=; b=WHvHBXFu1ouNzOxT+NPlJgfYimAP8JYjDjQfjxSe1Faubk2OBOFdlh2WmVLJ8943kr hUgypn3uTAm/JyH4HWDUJf4BEtnYNsjzPz+YDLjvzkPqHhOQ9V/YBZhBpFJ7UsYgcsHP aybzkGmlKXawDWnMzRkOJolZJNSpkH+mjIV/k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uKCZ7ivx5WLVg3h43Ng2ow+kLLnxVBM9eH6g+KVYG/A=; b=Pn/lLdKfbV/a5aQflg8Rc3QMqocC4qExKKlfTTPOuK0WRJBRFRwYVY+euUCGU7rlOD qKKwEi7m/dNUHnbxzMee7BMnhFQDYXnxjeYyHK9bgpYvyFBxLkvzWCFrMRWwIlCyqwht gYd+wdalX9bgezJvR61TbV8lcH8MzJIEXaJPY5UhofM27uc1+ZcjBMLfZkFRe3oMXc6X fTT3QZlvX/m5CojrbY+iw7dhmFuuAkBlc17bJGL6sDobnnIBh7Rz+TQx/u33uC5o0NL/ c4BCyNCrNHdoIiOKlB5/c45fD46qUqRK0Mp+54qC8EIJq1vxJX1gfUQJBZyWYd6AIfY7 vQTQ== X-Gm-Message-State: AOAM532kOZDnaPEIHvuj1/WbVCtJS4b4oghlMvshUIMlI5DCx0liz6vW oa1ep+D2Z/mtQq7U65ovaEoCdOpgWT9VhLHB X-Google-Smtp-Source: ABdhPJz/5t3s+v5Ezp+EdQg7n2wZX95uTquoETg218EytWDWOO0typIgRzWuSx2yyoV0Z2CkHISbnQ== X-Received: by 2002:a02:b709:: with SMTP id g9mr22940jam.90.1603115770743; Mon, 19 Oct 2020 06:56:10 -0700 (PDT) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id r17sm10164078iov.7.2020.10.19.06.56.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2020 06:56:10 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Andy Shevchenko , Heinrich Schuchardt , Bernhard Messerklinger , Wolfgang Wallner , Walter Lozano , Simon Glass , AKASHI Takahiro , Joe Hershberger , Marek Vasut , Patrice Chotard , Patrick Delaunay , Pedro Aguilar , Rasmus Villemoes , Sean Anderson , Wolfgang Denk Subject: [PATCH 01/11] env: Allow returning errors from hdelete_r() Date: Mon, 19 Oct 2020 07:55:52 -0600 Message-Id: <20201019135602.3943835-2-sjg@chromium.org> X-Mailer: git-send-email 2.29.0.rc1.297.gfa9743e501-goog In-Reply-To: <20201019135602.3943835-1-sjg@chromium.org> References: <20201019135602.3943835-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean At present this function returns 1 on success and 0 on failure. But in the latter case it provides no indication of what went wrong. If an attempt is made to delete a non-existent variable, the caller may want to ignore this error. This happens when setting a non-existent variable to "", for example. Update the function to return 0 on success and a useful error code on failure. Add a function comment too. Make sure that env_set() does not return an error if it is deleting a variable that doesn't exist. We could update env_set() to return useful error numbers also, but that is beyond the scope of this change. Signed-off-by: Simon Glass wip --- cmd/nvedit.c | 6 ++++-- include/search.h | 11 ++++++++++- lib/hashtable.c | 12 ++++++------ test/env/hashtable.c | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 7fce723800d..d0d2eca9047 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -266,7 +266,9 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) /* Delete only ? */ if (argc < 3 || argv[2] == NULL) { int rc = hdelete_r(name, &env_htab, env_flag); - return !rc; + + /* If the variable didn't exist, don't report an error */ + return rc && rc != -ENOENT ? 1 : 0; } /* @@ -895,7 +897,7 @@ static int do_env_delete(struct cmd_tbl *cmdtp, int flag, while (--argc > 0) { char *name = *++argv; - if (!hdelete_r(name, &env_htab, env_flag)) + if (hdelete_r(name, &env_htab, env_flag)) ret = 1; } diff --git a/include/search.h b/include/search.h index e56843c26fd..d0bb44388e1 100644 --- a/include/search.h +++ b/include/search.h @@ -80,7 +80,16 @@ int hsearch_r(struct env_entry item, enum env_action action, int hmatch_r(const char *match, int last_idx, struct env_entry **retval, struct hsearch_data *htab); -/* Search and delete entry matching "key" in internal hash table. */ +/** + * hdelete_r() - Search and delete entry in internal hash table + * + * @key: Name of entry to delete + * @htab: Hash table + * @flag: Flags to use (H_...) + * @return 0 on success, -ENOENT if not found, -EPERM if the hash table callback + * rejected changing the variable, -EINVAL if the hash table refused to + * delete the variable + */ int hdelete_r(const char *key, struct hsearch_data *htab, int flag); ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag, diff --git a/lib/hashtable.c b/lib/hashtable.c index 7c08f5c8055..ff5ff726394 100644 --- a/lib/hashtable.c +++ b/lib/hashtable.c @@ -472,7 +472,7 @@ int hdelete_r(const char *key, struct hsearch_data *htab, int flag) idx = hsearch_r(e, ENV_FIND, &ep, htab, 0); if (idx == 0) { __set_errno(ESRCH); - return 0; /* not found */ + return -ENOENT; /* not found */ } /* Check for permission */ @@ -481,7 +481,7 @@ int hdelete_r(const char *key, struct hsearch_data *htab, int flag) debug("change_ok() rejected deleting variable " "%s, skipping it!\n", key); __set_errno(EPERM); - return 0; + return -EPERM; } /* If there is a callback, call it */ @@ -490,12 +490,12 @@ int hdelete_r(const char *key, struct hsearch_data *htab, int flag) debug("callback() rejected deleting variable " "%s, skipping it!\n", key); __set_errno(EINVAL); - return 0; + return -EINVAL; } _hdelete(key, htab, ep, idx); - return 1; + return 0; } #if !(defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_SAVEENV)) @@ -917,7 +917,7 @@ int himport_r(struct hsearch_data *htab, if (!drop_var_from_set(name, nvars, localvars)) continue; - if (hdelete_r(name, htab, flag) == 0) + if (hdelete_r(name, htab, flag)) debug("DELETE ERROR ##############################\n"); continue; @@ -979,7 +979,7 @@ int himport_r(struct hsearch_data *htab, * b) if the variable was not present in current env, we notify * it might be a typo */ - if (hdelete_r(localvars[i], htab, flag) == 0) + if (hdelete_r(localvars[i], htab, flag)) printf("WARNING: '%s' neither in running nor in imported env!\n", localvars[i]); else printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]); diff --git a/test/env/hashtable.c b/test/env/hashtable.c index 339cc19ba14..70102f9121c 100644 --- a/test/env/hashtable.c +++ b/test/env/hashtable.c @@ -80,7 +80,7 @@ static int htab_create_delete(struct unit_test_state *uts, ut_asserteq_str(key, ritem->key); ut_asserteq_str(key, ritem->data); - ut_asserteq(1, hdelete_r(key, htab, 0)); + ut_asserteq(0, hdelete_r(key, htab, 0)); } return 0;