[PATCH v2 combined] bcachefs-tools: add fs_usage & return -EINVAL when no matching subcommand

Integral
2024-11-11T03:49:56Z

Add the missing usage function for "fs" subcommand; when no matching
subcommand exists, print the usage and return -EINVAL.

Signed-off-by: Integral <integral@archlinuxcn.org>
---
c_src/bcachefs.c | 15 ++++++++-------
c_src/cmd_fs.c | 12 ++++++++++++
c_src/cmds.h | 1 +
3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/c_src/bcachefs.c b/c_src/bcachefs.c
index 77bf6215..89e13b75 100644
--- a/c_src/bcachefs.c
+++ b/c_src/bcachefs.c
@@ -113,14 +113,13 @@ int fs_cmds(int argc, char *argv[])
{
char *cmd = pop_cmd(&argc, argv);

- if (argc < 1) {
- bcachefs_usage();
- exit(EXIT_FAILURE);
- }
+ if (argc < 1)
+ return fs_usage();
if (!strcmp(cmd, "usage"))
return cmd_fs_usage(argc, argv);

- return 0;
+ fs_usage();
+ return -EINVAL;
}

int device_cmds(int argc, char *argv[])
@@ -146,7 +145,8 @@ int device_cmds(int argc, char *argv[])
if (!strcmp(cmd, "resize-journal"))
return cmd_device_resize_journal(argc, argv);

- return 0;
+ device_usage();
+ return -EINVAL;
}

int data_cmds(int argc, char *argv[])
@@ -160,5 +160,6 @@ int data_cmds(int argc, char *argv[])
if (!strcmp(cmd, "job"))
return cmd_data_job(argc, argv);

- return 0;
+ data_usage();
+ return -EINVAL;
}
diff --git a/c_src/cmd_fs.c b/c_src/cmd_fs.c
index 82eeceff..eb8f7d2e 100644
--- a/c_src/cmd_fs.c
+++ b/c_src/cmd_fs.c
@@ -487,6 +487,18 @@ devs:
bcache_fs_close(fs);
}

+int fs_usage(void)
+{
+ puts("bcachefs fs - manage a running filesystem\n"
+ "Usage: bcachefs fs <CMD> [OPTIONS]\n"
+ "\n"
+ "Commands:\n"
+ " usage Display detailed filesystem usage\n"
+ "\n"
+ "Report bugs to <linux-bcachefs@vger.kernel.org>");
+ return 0;
+}
+
static void fs_usage_usage(void)
{
puts("bcachefs fs usage - display detailed filesystem usage\n"
diff --git a/c_src/cmds.h b/c_src/cmds.h
index 64267dc4..99016d52 100644
--- a/c_src/cmds.h
+++ b/c_src/cmds.h
@@ -14,6 +14,7 @@ int cmd_show_super(int argc, char *argv[]);
int cmd_reset_counters(int argc, char *argv[]);
int cmd_set_option(int argc, char *argv[]);

+int fs_usage(void);
int cmd_fs_usage(int argc, char *argv[]);

int device_usage(void);
--
2.47.0