Whamcloud - gitweb
LU-16944 utils: lfs find: handle multiple paths correctly 78/51578/2
authorThomas Bertschinger <bertschinger@lanl.gov>
Wed, 5 Jul 2023 14:19:22 +0000 (10:19 -0400)
committerOleg Drokin <green@whamcloud.com>
Wed, 19 Jul 2023 16:45:16 +0000 (16:45 +0000)
When lfs find is used with multiple paths and the first non-path
option is a '!' or an option without an argument like '-print',
the code skipped the final path because it assumed that the first
non-path option would be an option with an argument.

This commit resolves the bug by remembering the last-processed argv
index so that the index of the final path argument is correct whether
the next option consumes 2 indexes or just 1.

Signed-off-by: Thomas Bertschinger <bertschinger@lanl.gov>
Change-Id: I03133a43641af7a53a20d947b8ef82529e453251
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/51578
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Timothy Day <timday@amazon.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/tests/sanity.sh
lustre/utils/lfs.c

index 2d5b67c..befc9ea 100755 (executable)
@@ -8808,6 +8808,34 @@ test_56edb() {
 }
 run_test 56edb "check lfs find --links for directory striped on multiple MDTs"
 
+test_56ef() {
+       local dir=$DIR/$tdir
+       local dir1=$dir/d1
+       local dir2=$dir/d2
+       local nfiles
+
+       test_mkdir -p $dir
+
+       mkdir $dir1
+       mkdir $dir2
+
+       touch $dir1/f
+       touch $dir2/f
+
+       nfiles=$($LFS find $dir1 $dir2 ! -type d | wc -l)
+       (( $nfiles == 2 )) ||
+               error "(1) lfs find expected 2 files, got $nfiles"
+
+       nfiles=$($LFS find $dir1 $dir2 -type f | wc -l)
+       (( $nfiles == 2 )) ||
+               error "(2) lfs find expected 2 files, got $nfiles"
+
+       nfiles=$($LFS find -type f $dir1 $dir2 | wc -l)
+       (( $nfiles == 2 )) ||
+               error "(3) lfs find expected 2 files, got $nfiles"
+}
+run_test 56ef "lfs find with multiple paths"
+
 test_57a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run"
        # note test will not do anything if MDS is not local
index d34b0ec..021e8af 100644 (file)
@@ -5155,6 +5155,7 @@ static int lfs_find(int argc, char **argv)
                                                .has_arg = required_argument },
        { .val = 'z',   .name = "ext-size",     .has_arg = required_argument },
        { .name = NULL } };
+       int prev_optind = optind;
        int optidx = 0;
        int pathstart = -1;
        int pathend = -1;
@@ -5192,9 +5193,12 @@ static int lfs_find(int argc, char **argv)
                        goto err;
                }
                if (!isoption && pathstart == -1)
-                       pathstart = optind - 1;
+                       pathstart = prev_optind;
                if (isoption && pathstart != -1 && pathend == -1)
-                       pathend = optind - 2;
+                       pathend = prev_optind;
+
+               prev_optind = optind;
+
                switch (c) {
                case 0:
                        /* Long options. */