From f7ce2a0ae35ff48cd0d3dbde0dcd3a7c139d567d Mon Sep 17 00:00:00 2001 From: Thomas Bertschinger Date: Wed, 5 Jul 2023 10:19:22 -0400 Subject: [PATCH] LU-16944 utils: lfs find: handle multiple paths correctly 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 Change-Id: I03133a43641af7a53a20d947b8ef82529e453251 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/51578 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Timothy Day Reviewed-by: Oleg Drokin --- lustre/tests/sanity.sh | 28 ++++++++++++++++++++++++++++ lustre/utils/lfs.c | 8 ++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lustre/tests/sanity.sh b/lustre/tests/sanity.sh index 2d5b67c..befc9ea3 100755 --- a/lustre/tests/sanity.sh +++ b/lustre/tests/sanity.sh @@ -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 diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index d34b0ec..021e8af 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -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. */ -- 1.8.3.1