Whamcloud - gitweb
LU-16386 mkfs: Handle --version argument correctly 79/49379/3
authorArshad Hussain <arshad.hussain@aeoncomputing.com>
Mon, 12 Dec 2022 14:42:44 +0000 (09:42 -0500)
committerOleg Drokin <green@whamcloud.com>
Tue, 20 Dec 2022 14:44:28 +0000 (14:44 +0000)
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 <target type> [--backfstype=ldiskfs]
<snip>

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 <arshad.hussain@aeoncomputing.com>
Change-Id: I4d4d1144d669fce8b02e9f8c3fb5f45f68b337b4
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/49379
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Jian Yu <yujian@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/utils/mkfs_lustre.c

index 8caf62d..251b57b 100644 (file)
@@ -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;
        }