Skip to content

Commit

Permalink
Merge pull request #825 from alaaeddineelamri/dev/alaaeddineelamri/ma…
Browse files Browse the repository at this point in the history
…ster/fix-full-path-globbing-bug

fix full path globbing bug
  • Loading branch information
pstorz committed May 28, 2021
2 parents b7fecb5 + 5993761 commit eb06c1e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion core/src/dird/ua_select.cc
Expand Up @@ -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();

Expand Down
41 changes: 22 additions & 19 deletions core/src/dird/ua_tree.cc
Expand Up @@ -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 {
Expand Down
24 changes: 21 additions & 3 deletions core/src/tests/globbing_test.cc
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit eb06c1e

Please sign in to comment.