Skip to content

Commit c477c89

Browse files
committed
fix(metadata): exit from find() if we found a non-directory inode
1 parent aeb4cc4 commit c477c89

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/dwarfs/metadata_v2.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ class metadata_ final : public metadata_v2::impl {
541541

542542
directory_view make_directory_view(inode_view iv) const {
543543
// TODO: revisit: is this the way to do it?
544+
DWARFS_CHECK(iv.is_directory(), "not a directory");
544545
return directory_view(iv.inode_num(), &global_);
545546
}
546547

@@ -1257,6 +1258,10 @@ metadata_<LoggerPolicy>::find(const char* path) const {
12571258
const char* next = ::strchr(path, '/');
12581259
size_t clen = next ? next - path : ::strlen(path); // Flawfinder: ignore
12591260

1261+
if (!iv->is_directory()) {
1262+
return std::nullopt;
1263+
}
1264+
12601265
iv = find(make_directory_view(*iv), std::string_view(path, clen));
12611266

12621267
if (!iv) {
@@ -1280,6 +1285,10 @@ metadata_<LoggerPolicy>::find(int inode, const char* name) const {
12801285
auto iv = get_entry(inode);
12811286

12821287
if (iv) {
1288+
if (!iv->is_directory()) {
1289+
return std::nullopt;
1290+
}
1291+
12831292
iv = find(make_directory_view(*iv), std::string_view(name));
12841293
}
12851294

test/dwarfs.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,3 +982,27 @@ TEST(section_index_regression, github183) {
982982
EXPECT_THROW(filesystem_v2::identify(lgr, mm, idss, 3),
983983
dwarfs::runtime_error);
984984
}
985+
986+
TEST(filesystem, find_by_path) {
987+
std::ostringstream logss;
988+
stream_logger lgr(logss); // TODO: mock
989+
lgr.set_policy<prod_logger_policy>();
990+
991+
auto input = test::os_access_mock::create_test_instance();
992+
auto fsimage = build_dwarfs(lgr, input, "null");
993+
auto mm = std::make_shared<test::mmap_mock>(std::move(fsimage));
994+
995+
filesystem_v2 fs(lgr, mm);
996+
997+
std::vector<std::string> paths;
998+
fs.walk([&](auto e) { paths.emplace_back(e.unix_path()); });
999+
1000+
EXPECT_GT(paths.size(), 10);
1001+
1002+
for (auto const& p : paths) {
1003+
auto iv = fs.find(p.c_str());
1004+
ASSERT_TRUE(iv) << p;
1005+
EXPECT_FALSE(fs.find(iv->inode_num(), "desktop.ini")) << p;
1006+
EXPECT_FALSE(fs.find((p + "/desktop.ini").c_str())) << p;
1007+
}
1008+
}

0 commit comments

Comments
 (0)