Skip to content

Commit 6ea7070

Browse files
committed
btrfs-progs: return errors from parse_tree_id
Parsing functions should not call exit, so convert parse_tree_id() to return error and take the output parameter instead. Signed-off-by: David Sterba <[email protected]>
1 parent 9c85e22 commit 6ea7070

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

cmds/inspect-dump-tree.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,11 @@ static int cmd_inspect_dump_tree(const struct cmd_struct *cmd,
344344
goto out;
345345
break;
346346
case 't':
347-
tree_id = parse_tree_id(optarg);
347+
ret = parse_tree_id(optarg, &tree_id);
348+
if (ret < 0) {
349+
error("cannot parse tree id: %s");
350+
return 1;
351+
}
348352
break;
349353
case GETOPT_VAL_FOLLOW:
350354
follow = BTRFS_PRINT_TREE_FOLLOW;

cmds/inspect-tree-stats.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,11 @@ static int cmd_inspect_tree_stats(const struct cmd_struct *cmd,
468468
unit_mode = UNITS_RAW;
469469
break;
470470
case 't':
471-
tree_id = parse_tree_id(optarg);
471+
ret = parse_tree_id(optarg, &tree_id);
472+
if (ret < 0) {
473+
error("cannot parse tree id: %s");
474+
return 1;
475+
}
472476
break;
473477
default:
474478
usage_unknown_option(cmd, argv);

common/parse-utils.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,24 +457,21 @@ static u64 tree_id_from_string(const char *str, const char **end)
457457
return id;
458458
}
459459

460-
u64 parse_tree_id(const char* str) {
461-
u64 tree_id = 0;
460+
int parse_tree_id(const char* str, u64 *tree_id) {
462461
const char *end = NULL;
463462

464463
if (string_is_numerical(str))
465-
tree_id = arg_strtou64(str);
464+
*tree_id = arg_strtou64(str);
466465
else
467-
tree_id = tree_id_from_string(str, &end);
466+
*tree_id = tree_id_from_string(str, &end);
468467

469-
if (!tree_id) {
470-
error("unrecognized tree id: %s", str);
471-
exit(1);
472-
}
468+
if (*tree_id == 0)
469+
return -EINVAL;
473470

474471
if (end && *end) {
475472
error("unexpected tree id suffix of '%s': %s", str, end);
476-
exit(1);
473+
return -EINVAL;
477474
}
478475

479-
return tree_id;
476+
return 0;
480477
}

common/parse-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int parse_bg_profile(const char *profile, u64 *flags);
3030
int parse_compress_type(const char *type);
3131
int parse_qgroupid(const char *str, u64 *qgroupid);
3232
u64 parse_qgroupid_or_path(const char *p);
33-
u64 parse_tree_id(const char* str);
33+
int parse_tree_id(const char* str, u64 *tree_id);
3434
int fls64(u64 x);
3535

3636
#endif

0 commit comments

Comments
 (0)