From f26ef893af2edc38fd4f40d7f88d8c240f3ec787 Mon Sep 17 00:00:00 2001 From: Alaa Eddine Elamri Date: Fri, 28 May 2021 15:33:12 +0100 Subject: [PATCH 1/3] dir: fix for full path bug --- core/src/dird/ua_tree.cc | 41 +++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/core/src/dird/ua_tree.cc b/core/src/dird/ua_tree.cc index 45a2ed6f539..9c579930344 100644 --- a/core/src/dird/ua_tree.cc +++ b/core/src/dird/ua_tree.cc @@ -461,46 +461,49 @@ static int MarkElements(UaContext* ua, TreeContext* tree) for (int i = 1; i < ua->argc; i++) { StripTrailingSlash(ua->argk[i]); - // See if this is a full path. + // See if this is a complex path. if (strchr(ua->argk[i], '/')) { int pnl, fnl; - POOLMEM* file_pattern = GetPoolMemory(PM_FNAME); - POOLMEM* path_pattern = GetPoolMemory(PM_FNAME); + POOLMEM* given_file_pattern = GetPoolMemory(PM_FNAME); + POOLMEM* given_path_pattern = GetPoolMemory(PM_FNAME); // Split the argument into a path pattern and file pattern. - SplitPathAndFilename(ua->argk[i], path_pattern, &pnl, file_pattern, &fnl); - - // Initialize the node by CDing into current cwd - node = tree_cwd(path_pattern, tree->root, tree->node); + SplitPathAndFilename(ua->argk[i], given_path_pattern, &pnl, + given_file_pattern, &fnl); - if (!node) { - ua->WarningMsg(_("Invalid path %s given.\n"), path_pattern); - FreePoolMemory(file_pattern); - FreePoolMemory(path_pattern); + if (!tree_cwd(given_path_pattern, tree->root, tree->node)) { + ua->WarningMsg(_("Invalid path %s given.\n"), given_path_pattern); + FreePoolMemory(given_file_pattern); + FreePoolMemory(given_path_pattern); continue; } - std::string fullpath_pattern = tree_getpath(tree->node); - fullpath_pattern.append(path_pattern); + std::string fullpath_pattern{}; + if (ua->argk[i][0] != '/') { + fullpath_pattern = tree_getpath(tree->node); + } + fullpath_pattern.append(given_path_pattern); POOLMEM* node_filename = GetPoolMemory(PM_FNAME); POOLMEM* node_path = GetPoolMemory(PM_FNAME); - for (node = tree->node; node; node = NextTreeNode(node)) { - std::string node_cwd = tree_getpath(node); - SplitPathAndFilename(node_cwd.c_str(), node_path, &pnl, node_filename, + for (node = (strcmp(tree_getpath(tree->node), "/") == 0) + ? FirstTreeNode(tree->root) + : tree->node; + node; node = NextTreeNode(node)) { + SplitPathAndFilename(tree_getpath(node), node_path, &pnl, node_filename, &fnl); if (fnmatch(fullpath_pattern.c_str(), node_path, 0) == 0) { - if (fnmatch(file_pattern, node->fname, 0) == 0) { + if (fnmatch(given_file_pattern, node->fname, 0) == 0) { count += SetExtract(ua, node, tree, true); } } } - FreePoolMemory(file_pattern); - FreePoolMemory(path_pattern); + FreePoolMemory(given_file_pattern); + FreePoolMemory(given_path_pattern); FreePoolMemory(node_filename); FreePoolMemory(node_path); } else { From 51b6e01a6e094e7217c0bf9c8f4740fb7ccec8e5 Mon Sep 17 00:00:00 2001 From: Alaa Eddine Elamri Date: Fri, 28 May 2021 14:38:25 +0100 Subject: [PATCH 2/3] unittests: added missing full path use cases --- core/src/tests/globbing_test.cc | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/core/src/tests/globbing_test.cc b/core/src/tests/globbing_test.cc index faba5b63df2..044fa46708c 100644 --- a/core/src/tests/globbing_test.cc +++ b/core/src/tests/globbing_test.cc @@ -156,8 +156,29 @@ TEST(globbing, globbing_in_markcmd) PopulateTree(files, &tree); + // testing full paths + FakeCdCmd(&ua, &tree, "/"); + EXPECT_EQ(FakeMarkCmd(&ua, &tree, "*"), files.size()); + + EXPECT_EQ( + FakeMarkCmd(&ua, &tree, "/testingwildcards/lonesubdirectory/whatever"), + 1); + + EXPECT_EQ( + FakeMarkCmd(&ua, &tree, "testingwildcards/lonesubdirectory/whatever"), 1); + + // Using full path while being in a different folder than root FakeCdCmd(&ua, &tree, "/some/weirdfiles/"); + EXPECT_EQ( + FakeMarkCmd(&ua, &tree, "/testingwildcards/lonesubdirectory/whatever"), + 1); + EXPECT_EQ(FakeMarkCmd(&ua, &tree, "/testingwildcards/sub*"), 6); + EXPECT_EQ( + FakeMarkCmd(&ua, &tree, "testingwildcards/lonesubdirectory/whatever"), 0); + + // Testing patterns in different folders + FakeCdCmd(&ua, &tree, "/some/weirdfiles/"); EXPECT_EQ(FakeMarkCmd(&ua, &tree, "nope"), 0); EXPECT_EQ(FakeMarkCmd(&ua, &tree, "potato"), 1); EXPECT_EQ(FakeMarkCmd(&ua, &tree, "potato*"), 2); @@ -171,9 +192,6 @@ TEST(globbing, globbing_in_markcmd) EXPECT_EQ(FakeMarkCmd(&ua, &tree, "wei*/subroza/*"), 0); EXPECT_EQ(FakeMarkCmd(&ua, &tree, "w*efiles/sub*/*"), 6); - FakeCdCmd(&ua, &tree, "/"); - EXPECT_EQ(FakeMarkCmd(&ua, &tree, "*"), files.size()); - FakeCdCmd(&ua, &tree, "/testingwildcards/"); EXPECT_EQ(FakeMarkCmd(&ua, &tree, "p?tato"), 1); EXPECT_EQ(FakeMarkCmd(&ua, &tree, "subdirectory?/file1"), 1); From 59937615ee6d3cbccff8e422edcb3fa48d7210a1 Mon Sep 17 00:00:00 2001 From: Alaa Eddine Elamri Date: Fri, 28 May 2021 15:39:13 +0100 Subject: [PATCH 3/3] dir: fixed a warning in ua_select.cc --- core/src/dird/ua_select.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/dird/ua_select.cc b/core/src/dird/ua_select.cc index 19b59da9d7f..8c3371f3f22 100644 --- a/core/src/dird/ua_select.cc +++ b/core/src/dird/ua_select.cc @@ -1078,7 +1078,7 @@ std::string FormatMulticolumnPrompts(const UaContext* ua, const int window_width, const int min_lines_threshold) { - size_t max_prompt_length = 1; + unsigned int max_prompt_length = 1; const int max_prompt_index_length = std::to_string(ua->num_prompts).length();