aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2023-08-01 16:57:00 -0400
committerKent Overstreet <kent.overstreet@linux.dev>2023-08-01 16:57:00 -0400
commit3e39c57c700f0b83d82df1572135d5634a6e8b43 (patch)
treef9377e77dce32528886542f3ce8327dbb37201f8
parent71ed9f183ba9a2cb4230353b7ae62ac335c53c7c (diff)
downloadbcachefs-tools-3e39c57c700f0b83d82df1572135d5634a6e8b43.tar.gz
bpos_parse() now handles symbol constants
We can now parse U32_MAX/U64_MAX, as printed by bch2_bpos_to_text(). Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
-rw-r--r--tools-util.c35
1 files changed, 32 insertions, 3 deletions
diff --git a/tools-util.c b/tools-util.c
index 65835edf..bb650931 100644
--- a/tools-util.c
+++ b/tools-util.c
@@ -605,6 +605,35 @@ int dev_mounted(char *dev)
return 2;
}
+static int kstrtoull_symbolic(const char *s, unsigned int base, unsigned long long *res)
+{
+ if (!strcmp(s, "U64_MAX")) {
+ *res = U64_MAX;
+ return 0;
+ }
+
+ if (!strcmp(s, "U32_MAX")) {
+ *res = U32_MAX;
+ return 0;
+ }
+
+ return kstrtoull(s, base, res);
+}
+
+static int kstrtouint_symbolic(const char *s, unsigned int base, unsigned *res)
+{
+ unsigned long long tmp;
+ int rv;
+
+ rv = kstrtoull_symbolic(s, base, &tmp);
+ if (rv < 0)
+ return rv;
+ if (tmp != (unsigned long long)(unsigned int)tmp)
+ return -ERANGE;
+ *res = tmp;
+ return 0;
+}
+
struct bpos bpos_parse(char *buf)
{
char *orig = strdup(buf);
@@ -620,14 +649,14 @@ struct bpos bpos_parse(char *buf)
u64 inode_v = 0, offset_v = 0;
u32 snapshot_v = 0;
- if (kstrtoull(inode_s, 10, &inode_v))
+ if (kstrtoull_symbolic(inode_s, 10, &inode_v))
die("invalid bpos.inode %s", inode_s);
- if (kstrtoull(offset_s, 10, &offset_v))
+ if (kstrtoull_symbolic(offset_s, 10, &offset_v))
die("invalid bpos.offset %s", offset_s);
if (snapshot_s &&
- kstrtouint(snapshot_s, 10, &snapshot_v))
+ kstrtouint_symbolic(snapshot_s, 10, &snapshot_v))
die("invalid bpos.snapshot %s", snapshot_s);
return (struct bpos) { .inode = inode_v, .offset = offset_v, .snapshot = snapshot_v };