Skip to content

Commit

Permalink
refactor(filesystem_v2): char const* -> string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Aug 23, 2024
1 parent 848eedd commit c5aadf1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
9 changes: 5 additions & 4 deletions include/dwarfs/reader/filesystem_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>

Expand Down Expand Up @@ -128,13 +129,13 @@ class filesystem_v2 {

dir_entry_view root() const { return impl_->root(); }

std::optional<dir_entry_view> find(const char* path) const {
std::optional<dir_entry_view> find(std::string_view path) const {
return impl_->find(path);
}

std::optional<inode_view> find(int inode) const { return impl_->find(inode); }

std::optional<dir_entry_view> find(int inode, const char* name) const {
std::optional<dir_entry_view> find(int inode, std::string_view name) const {
return impl_->find(inode, name);
}

Expand Down Expand Up @@ -362,10 +363,10 @@ class filesystem_v2 {
virtual void
walk_data_order(std::function<void(dir_entry_view)> const& func) const = 0;
virtual dir_entry_view root() const = 0;
virtual std::optional<dir_entry_view> find(const char* path) const = 0;
virtual std::optional<dir_entry_view> find(std::string_view path) const = 0;
virtual std::optional<inode_view> find(int inode) const = 0;
virtual std::optional<dir_entry_view>
find(int inode, const char* name) const = 0;
find(int inode, std::string_view name) const = 0;
virtual file_stat getattr(inode_view entry, std::error_code& ec) const = 0;
virtual file_stat getattr(inode_view entry, getattr_options const& opts,
std::error_code& ec) const = 0;
Expand Down
8 changes: 4 additions & 4 deletions include/dwarfs/reader/internal/metadata_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ class metadata_v2 {

dir_entry_view root() const { return impl_->root(); }

std::optional<dir_entry_view> find(const char* path) const {
std::optional<dir_entry_view> find(std::string_view path) const {
return impl_->find(path);
}

std::optional<inode_view> find(int inode) const { return impl_->find(inode); }

std::optional<dir_entry_view> find(int inode, const char* name) const {
std::optional<dir_entry_view> find(int inode, std::string_view name) const {
return impl_->find(inode, name);
}

Expand Down Expand Up @@ -201,10 +201,10 @@ class metadata_v2 {

virtual dir_entry_view root() const = 0;

virtual std::optional<dir_entry_view> find(const char* path) const = 0;
virtual std::optional<dir_entry_view> find(std::string_view path) const = 0;
virtual std::optional<inode_view> find(int inode) const = 0;
virtual std::optional<dir_entry_view>
find(int inode, const char* name) const = 0;
find(int inode, std::string_view name) const = 0;

virtual file_stat getattr(inode_view iv, std::error_code& ec) const = 0;
virtual file_stat getattr(inode_view iv, getattr_options const& opts,
Expand Down
8 changes: 4 additions & 4 deletions src/reader/filesystem_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ class filesystem_ final : public filesystem_v2::impl {
void walk_data_order(
std::function<void(dir_entry_view)> const& func) const override;
dir_entry_view root() const override;
std::optional<dir_entry_view> find(const char* path) const override;
std::optional<dir_entry_view> find(std::string_view path) const override;
std::optional<inode_view> find(int inode) const override;
std::optional<dir_entry_view>
find(int inode, const char* name) const override;
find(int inode, std::string_view name) const override;
file_stat getattr(inode_view entry, std::error_code& ec) const override;
file_stat getattr(inode_view entry, getattr_options const& opts,
std::error_code& ec) const override;
Expand Down Expand Up @@ -743,7 +743,7 @@ dir_entry_view filesystem_<LoggerPolicy>::root() const {

template <typename LoggerPolicy>
std::optional<dir_entry_view>
filesystem_<LoggerPolicy>::find(const char* path) const {
filesystem_<LoggerPolicy>::find(std::string_view path) const {
PERFMON_CLS_SCOPED_SECTION(find_path)
return meta_.find(path);
}
Expand All @@ -756,7 +756,7 @@ std::optional<inode_view> filesystem_<LoggerPolicy>::find(int inode) const {

template <typename LoggerPolicy>
std::optional<dir_entry_view>
filesystem_<LoggerPolicy>::find(int inode, const char* name) const {
filesystem_<LoggerPolicy>::find(int inode, std::string_view name) const {
PERFMON_CLS_SCOPED_SECTION(find_inode_name)
return meta_.find(inode, name);
}
Expand Down
36 changes: 22 additions & 14 deletions src/reader/internal/metadata_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,10 @@ class metadata_ final : public metadata_v2::impl {

dir_entry_view root() const override { return root_; }

std::optional<dir_entry_view> find(const char* path) const override;
std::optional<dir_entry_view> find(std::string_view path) const override;
std::optional<inode_view> find(int inode) const override;
std::optional<dir_entry_view>
find(int inode, const char* name) const override;
find(int inode, std::string_view name) const override;

file_stat getattr(inode_view iv, std::error_code& ec) const override;
file_stat getattr(inode_view iv, getattr_options const& opts,
Expand Down Expand Up @@ -1650,30 +1650,38 @@ metadata_<LoggerPolicy>::find(directory_view dir, std::string_view name) const {

template <typename LoggerPolicy>
std::optional<dir_entry_view>
metadata_<LoggerPolicy>::find(const char* path) const {
while (*path == '/') {
++path;
metadata_<LoggerPolicy>::find(std::string_view path) const {
auto start = path.find_first_not_of('/');

if (start != std::string_view::npos) {
path.remove_prefix(start);
} else {
path = {};
}

auto dev = std::make_optional(root_);

while (*path) {
const char* next = ::strchr(path, '/');
size_t clen = next ? next - path : ::strlen(path); // Flawfinder: ignore

while (!path.empty()) {
auto iv = dev->inode();

if (!iv.is_directory()) {
return std::nullopt;
}

dev = find(make_directory_view(iv), std::string_view(path, clen));
auto name = path;

if (auto sep = path.find('/'); sep != std::string_view::npos) {
name = path.substr(0, sep);
path.remove_prefix(sep + 1);
} else {
path = {};
}

dev = find(make_directory_view(iv), name);

if (!dev) {
break;
}

path = next ? next + 1 : path + clen;
}

return dev;
Expand All @@ -1686,9 +1694,9 @@ std::optional<inode_view> metadata_<LoggerPolicy>::find(int inode) const {

template <typename LoggerPolicy>
std::optional<dir_entry_view>
metadata_<LoggerPolicy>::find(int inode, const char* name) const {
metadata_<LoggerPolicy>::find(int inode, std::string_view name) const {
if (auto iv = get_entry(inode); iv and iv->is_directory()) {
return find(make_directory_view(*iv), std::string_view(name));
return find(make_directory_view(*iv), name);
}

return std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion test/compat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ void check_compat(logger& lgr, reader::filesystem_v2 const& fs,
};

for (auto const& [td, expected] : testdirs) {
auto entry = fs.find(td.c_str());
auto entry = fs.find(td);
ASSERT_TRUE(entry) << td;
auto dir = fs.opendir(entry->inode());
ASSERT_TRUE(dir) << td;
Expand Down
4 changes: 2 additions & 2 deletions test/dwarfs_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,10 @@ TEST(filesystem, find_by_path) {
EXPECT_GT(paths.size(), 10);

for (auto const& p : paths) {
auto dev = fs.find(p.c_str());
auto dev = fs.find(p);
ASSERT_TRUE(dev) << p;
EXPECT_FALSE(fs.find(dev->inode().inode_num(), "desktop.ini")) << p;
EXPECT_FALSE(fs.find((p + "/desktop.ini").c_str())) << p;
EXPECT_FALSE(fs.find(p + "/desktop.ini")) << p;
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/tool_main_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ TEST(mkdwarfs_test, metadata_path) {
auto e4 = entries.at(16);
auto e5 = entries.at(32);

auto dev = fs.find(d1.string().c_str());
auto dev = fs.find(d1.string());
ASSERT_TRUE(dev);
auto iv = dev->inode();

Expand Down Expand Up @@ -1104,7 +1104,7 @@ TEST(mkdwarfs_test, metadata_directory_iterator) {
};

for (auto const& [path, expected_names] : testdirs) {
auto dev = fs.find(path.c_str());
auto dev = fs.find(path);
ASSERT_TRUE(dev) << path;

auto dir = fs.opendir(dev->inode());
Expand Down Expand Up @@ -2340,7 +2340,7 @@ TEST(mkdwarfs_test, max_similarity_size) {

for (auto size : sizes) {
auto path = "/file" + std::to_string(size);
auto dev = fs.find(path.c_str());
auto dev = fs.find(path);
assert(dev);
auto info = fs.get_inode_info(dev->inode());
assert(1 == info["chunks"].size());
Expand Down Expand Up @@ -2814,7 +2814,7 @@ TEST(block_cache, sequential_access_detector) {
#ifdef _WIN32
std::replace(pstr.begin(), pstr.end(), '\\', '/');
#endif
auto dev = fs.find(pstr.c_str());
auto dev = fs.find(pstr);
ASSERT_TRUE(dev);
auto iv = dev->inode();
ASSERT_TRUE(iv.is_regular_file());
Expand Down Expand Up @@ -2920,7 +2920,7 @@ TEST(file_scanner, large_file_handling) {
auto fs = t.fs_from_stdout();

for (size_t i = 0; i < data.size(); ++i) {
auto dev = fs.find(fmt::format("f{}", i).c_str());
auto dev = fs.find(fmt::format("f{}", i));
ASSERT_TRUE(dev) << i;
auto iv = dev->inode();
auto st = fs.getattr(iv);
Expand Down

0 comments on commit c5aadf1

Please sign in to comment.