common/xfs: Initialise OPTIND for getopts calls
authorDave Chinner <dchinner@redhat.com>
Fri, 23 Feb 2018 03:54:54 +0000 (14:54 +1100)
committerEryu Guan <eguan@redhat.com>
Fri, 23 Feb 2018 05:11:53 +0000 (13:11 +0800)
According to the bash man page:

OPTIND is initialized to 1 each time the shell or a shell
script is invoked.

This doesn't appear to be true - in tests scripts with no other
getopts calls, I'm seeing the getopts loop in _xfs_check to fail to
parse input parameters correctly. Tracing shows the parameters are
being passed to _xfs_check correctly, but on occassion getopts
simply doesn't see them.

Hence when running tests with both external log and real time
devices, tests are failing at random because xfs_check is
mis-parsing the parameters passed to it and not configuring the
external log correctly:

_check_xfs_filesystem: filesystem on /dev/sdg is inconsistent (c)
*** xfs_check output ***
aborting - no external log specified for FS with an external log
*** end xfs_check output

Fix this by ensuring OPTIND is correctly initialised before using
getopts. Do it for all places that call getopts that don't already
set OPTIND=1 before starting their parsing loop.

Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Eryu Guan <eguan@redhat.com>
Signed-off-by: Eryu Guan <eguan@redhat.com>
common/xfs
tests/generic/074
tests/generic/075
tests/generic/112

index 9ed78dee08ffeeaac4d1979192876100d5575d07..354f584f76558beb3beccc401f887b20b1347635 100644 (file)
@@ -119,6 +119,7 @@ _xfs_check()
        DBOPTS=" "
        USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
 
+       OPTIND=1
        while getopts "b:fi:l:stvV" c; do
                case $c in
                        s) OPTS=$OPTS"-s ";;
index 5f205b509eb6ece916017dae27e08f5598a122f0..d39f9a3d4309b47c0d351a1fa8b585fcccdf7dfc 100755 (executable)
@@ -87,6 +87,7 @@ _usage()
 
 _process_args()
 {
+    OPTIND=1
     while getopts "f:l:n:?" c $@
     do
         case $c
index 321c72177618a9e2db8239a8590d0f7b2db08618..e9dd4a5c5dcdbccdce62da00b7cc177a2969827c 100755 (executable)
@@ -98,6 +98,7 @@ _usage()
 
 _process_args()
 {
+    OPTIND=1
     while getopts "l:n:N:?" c $@
     do
         case $c
index 0ab1c8c87907e76f365dbe1821dff18f759080f5..5ab1cb3ad4ca0ab3716edb07006fe145f0b74e6b 100755 (executable)
@@ -94,6 +94,7 @@ _usage()
 
 _process_args()
 {
+    OPTIND=1
     while getopts "l:n:N:?" c $@
     do
         case $c