summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2023-06-08 15:40:42 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2023-06-09 08:41:45 +0200
commit2cc56260a049afc35152f04f28820b1314e8c4e3 (patch)
tree9158dc7fde2219fb7c8a1c57a5a98e517f7b1a16 /commands
parentc565766f01db9ae27035c5f48231939ec5e053ef (diff)
downloadbarebox-2cc56260a049afc35152f04f28820b1314e8c4e3.tar.gz
barebox-2cc56260a049afc35152f04f28820b1314e8c4e3.tar.xz
commands: of_diff: simplify error handling
The error check and message is duplicated for each argument and could be unified if we move it to get_tree instead. While at it, we limit argc to exactly 3 in case we want to add options in the future. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20230608134040.2123869-3-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands')
-rw-r--r--commands/of_diff.c55
1 files changed, 19 insertions, 36 deletions
diff --git a/commands/of_diff.c b/commands/of_diff.c
index 19a4a26d20..ff3d46c7f5 100644
--- a/commands/of_diff.c
+++ b/commands/of_diff.c
@@ -17,60 +17,43 @@ static struct device_node *get_tree(const char *filename, struct device_node *ro
{
struct device_node *node;
- if (!strcmp(filename, "-")) {
- node = of_get_root_node();
- if (!node)
- return ERR_PTR(-ENOENT);
-
- return of_dup(node);
- }
-
- if (!strcmp(filename, "+")) {
- node = of_get_root_node();
- if (!node)
- return ERR_PTR(-ENOENT);
+ if (!root)
+ root = ERR_PTR(-ENOENT);
+ if (!strcmp(filename, "-")) {
node = of_dup(root);
-
- of_fix_tree(node);
-
- return node;
+ } else if (!strcmp(filename, "+")) {
+ node = of_dup(root);
+ if (!IS_ERR(node))
+ of_fix_tree(node);
+ } else {
+ node = of_read_file(filename);
}
- return of_read_file(filename);
+ if (IS_ERR(node))
+ printf("Cannot read %s: %pe\n", filename, node);
+
+ return node;
}
static int do_of_diff(int argc, char *argv[])
{
- int ret = 0;
+ int ret = COMMAND_ERROR;
struct device_node *a, *b, *root;
- if (argc < 3)
+ if (argc != 3)
return COMMAND_ERROR_USAGE;
root = of_get_root_node();
a = get_tree(argv[1], root);
b = get_tree(argv[2], root);
- if (IS_ERR(a)) {
- printf("Cannot read %s: %pe\n", argv[1], a);
- ret = COMMAND_ERROR;
- a = NULL;
- goto out;
- }
-
- if (IS_ERR(b)) {
- printf("Cannot read %s: %pe\n", argv[2], b);
- ret = COMMAND_ERROR;
- b = NULL;
- goto out;
- }
+ if (!IS_ERR(a) && !IS_ERR(b))
+ ret = of_diff(a, b, 0) ? COMMAND_ERROR : COMMAND_SUCCESS;
- ret = of_diff(a, b, 0) ? COMMAND_ERROR : COMMAND_SUCCESS;
-out:
- if (a && a != root)
+ if (!IS_ERR(a) && a != root)
of_delete_node(a);
- if (b && b != root)
+ if (!IS_ERR(b) && b != root)
of_delete_node(b);
return ret;