From 73ee4e7fe31f31fae6b823086f706869b50adec6 Mon Sep 17 00:00:00 2001 From: Arshad Hussain Date: Mon, 12 Dec 2022 09:42:44 -0500 Subject: [PATCH] LU-16386 mkfs: Handle --version argument correctly Running mkfs.lustre with --version or -V argument fails instead of printing the version. This patch fixes the error. Without patch: -------------- $ ./lustre/utils/mkfs.lustre --version usage: mkfs.lustre [--backfstype=ldiskfs] With patch: ----------- $ ./lustre/utils/mkfs.lustre --version mkfs.lustre 2.15.52_175_ge7aa83d Test-Parameters: trivial fstype=zfs testlist=sanity Signed-off-by: Arshad Hussain Change-Id: I4d4d1144d669fce8b02e9f8c3fb5f45f68b337b4 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/49379 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Jian Yu Reviewed-by: Oleg Drokin --- lustre/utils/mkfs_lustre.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/lustre/utils/mkfs_lustre.c b/lustre/utils/mkfs_lustre.c index 8caf62d..251b57b 100644 --- a/lustre/utils/mkfs_lustre.c +++ b/lustre/utils/mkfs_lustre.c @@ -825,6 +825,41 @@ int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop, return 0; } +/* + * This function checks args for sanity. + * + * Returns 0 if all is found good. + * Returns 1 if invalid args is detected + */ +int chk_args(int argc, char *const argv[]) +{ + /* If no argument is given to mkfs.lustre, bail out */ + if (argc < 2) + return 1; + + /* If argc is exactly 2, make sure the user wants "--version" + * or "-V" and handle correctly if that is what the user wants + * without bailing out with error. + */ + if (argc == 2) { + if (strncmp(argv[1], "--version", strlen(argv[1])) == 0 && + (strlen(argv[1]) == 9)) + return 0; + + if (strncmp(argv[1], "-V", strlen(argv[1])) == 0 && + (strlen(argv[1]) == 2)) + return 0; + } + + /* This check validates if the last argument is really a device name + * and does not start with char '-'. + */ + if ((argv[argc - 1][0] == '-')) + return 1; + else + return 0; +} + int main(int argc, char *const argv[]) { struct mkfs_opts mop; @@ -842,7 +877,7 @@ int main(int argc, char *const argv[]) else progname = argv[0]; - if ((argc < 2) || (argv[argc - 1][0] == '-')) { + if (chk_args(argc, argv)) { usage(stderr); return EINVAL; } -- 1.8.3.1