diff --git a/src/verify.c b/src/verify.c index c843f22c..c3695150 100644 --- a/src/verify.c +++ b/src/verify.c @@ -882,15 +882,30 @@ static struct list *keep_matching_path(struct list *all_files) struct list *matching_files = NULL; struct list *iter = NULL; struct file *file; + char *tmp_dir = NULL; + + /* when considering the item as a directory, make sure the item + * finishes with a forward slash so we can match items accuratelly, + * oherwise we can end up with items that partially match */ + tmp_dir = str_or_die("%s%s", cmdline_option_file, cmdline_option_file[str_len(cmdline_option_file)] == '/' ? "" : "/"); for (iter = all_files; iter; iter = iter->next) { file = iter->data; - if (strncmp(cmdline_option_file, file->filename, str_len(cmdline_option_file)) == 0) { + + /* if the item entered by the user is a file, the match should + * be exact, so look for an exact match first, if there is none, + * look for a match treating the item as directory */ + if (str_cmp(cmdline_option_file, file->filename) == 0) { + /* preserving the order is important */ + matching_files = list_append_data(matching_files, file); + } else if (strncmp(tmp_dir, file->filename, str_len(tmp_dir)) == 0) { /* preserving the order is important */ matching_files = list_append_data(matching_files, file); } } + free_and_clear_pointer(&tmp_dir); + return list_head(matching_files); } diff --git a/test/functional/diagnose/diagnose-path.bats b/test/functional/diagnose/diagnose-path.bats index 9640d686..e0bcb406 100755 --- a/test/functional/diagnose/diagnose-path.bats +++ b/test/functional/diagnose/diagnose-path.bats @@ -207,4 +207,25 @@ test_setup() { assert_is_output "$expected_output" } + +@test "DIA028: Diagnose with --file doesn't consider partial matches" { + + # when using "diagnose --file ", swupd will match the ITEM with + # either the full name of a file or a directory (recursively), but won't + # use partial matches + + run sudo sh -c "$SWUPD diagnose $SWUPD_OPTS --file /ba" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + Diagnosing version 20 + Downloading missing manifests... + Inspected 0 files + Diagnose successful + EOM + ) + assert_is_output "$expected_output" + +} + #WEIGHT=36