Skip to content

Commit 5deedaa

Browse files
committed
[rfile] ListKeys: don't include the root directory itself in the listing
1 parent 296a013 commit 5deedaa

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

io/io/inc/ROOT/RFile.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ public:
376376
/// `basePath` (defaulting to include the content of all subdirectories).
377377
/// By default, keys referring to directories are not returned: only those referring to leaf objects are.
378378
/// If `basePath` is the path of a leaf object, only `basePath` itself will be returned.
379+
/// If `basePath` is the path of a directory, it won't appear in the listing.
379380
/// `flags` is a bitmask specifying the listing mode.
380381
/// If `(flags & kListObjects) != 0`, the listing will include keys of non-directory objects (default);
381382
/// If `(flags & kListDirs) != 0`, the listing will include keys of directory objects;

io/io/src/RFile.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ void ROOT::Experimental::RFileKeyIterable::RIterator::Advance()
475475
assert(!fIterStack.empty());
476476
const std::size_t nesting = fIterStack.size() - 1;
477477

478+
// Skip key if it's the root dir itself
479+
if (isDir && fullPath == fPattern)
480+
continue;
481+
478482
// Skip key if it's not a child of root dir
479483
if (!ROOT::StartsWith(fullPath, fPattern))
480484
continue;

io/io/test/rfile.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,10 @@ TEST(RFile, IterateKeysOnlyDirs)
482482
{
483483
auto file = RFile::Open(fileGuard.GetPath());
484484
EXPECT_EQ(JoinKeyNames(file->ListKeys("", RFile::kListDirs | RFile::kListRecursive)), "a, a/b, e, e/c");
485-
EXPECT_EQ(JoinKeyNames(file->ListKeys("a", RFile::kListDirs | RFile::kListRecursive)), "a, a/b");
486-
EXPECT_EQ(JoinKeyNames(file->ListKeys("a/b", RFile::kListDirs | RFile::kListRecursive)), "a/b");
485+
EXPECT_EQ(JoinKeyNames(file->ListKeys("a", RFile::kListDirs | RFile::kListRecursive)), "a/b");
486+
EXPECT_EQ(JoinKeyNames(file->ListKeys("a/b", RFile::kListDirs | RFile::kListRecursive)), "");
487487
EXPECT_EQ(JoinKeyNames(file->ListKeys("a/b/c", RFile::kListDirs | RFile::kListRecursive)), "");
488-
EXPECT_EQ(JoinKeyNames(file->ListKeys("e", RFile::kListDirs | RFile::kListRecursive)), "e, e/c");
488+
EXPECT_EQ(JoinKeyNames(file->ListKeys("e", RFile::kListDirs | RFile::kListRecursive)), "e/c");
489489
}
490490
}
491491

@@ -506,10 +506,10 @@ TEST(RFile, IterateKeysOnlyDirsNonRecursive)
506506
{
507507
auto file = RFile::Open(fileGuard.GetPath());
508508
EXPECT_EQ(JoinKeyNames(file->ListKeys("", RFile::kListDirs)), "a, e");
509-
EXPECT_EQ(JoinKeyNames(file->ListKeys("a", RFile::kListDirs)), "a, a/b");
510-
EXPECT_EQ(JoinKeyNames(file->ListKeys("a/b", RFile::kListDirs)), "a/b");
509+
EXPECT_EQ(JoinKeyNames(file->ListKeys("a", RFile::kListDirs)), "a/b");
510+
EXPECT_EQ(JoinKeyNames(file->ListKeys("a/b", RFile::kListDirs)), "");
511511
EXPECT_EQ(JoinKeyNames(file->ListKeys("a/b/c", RFile::kListDirs)), "");
512-
EXPECT_EQ(JoinKeyNames(file->ListKeys("e", RFile::kListDirs)), "e, e/c");
512+
EXPECT_EQ(JoinKeyNames(file->ListKeys("e", RFile::kListDirs)), "e/c");
513513
}
514514
}
515515

io/io/test/rfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def test_listkeys(self):
115115
[key.GetPath() for key in rfile.ListKeys("", listDirs=True, listObjects=False, listRecursive=False)],
116116
["foo"],
117117
)
118+
self.assertEqual(
119+
[key.GetPath() for key in rfile.ListKeys("foo", listDirs=True, listObjects=False, listRecursive=False)],
120+
["foo/bar"],
121+
)
118122
self.assertEqual(
119123
[key.GetPath() for key in rfile.ListKeys("", listDirs=True, listRecursive=False)], ["hist", "foo"]
120124
)

0 commit comments

Comments
 (0)