refactor: add current_path() abstraction to os_access

This commit is contained in:
Marcus Holland-Moritz 2023-12-30 00:47:11 +01:00
parent 709c6bb01d
commit 84b5b6f32a
6 changed files with 12 additions and 3 deletions

View File

@ -52,5 +52,6 @@ class os_access {
virtual int access(std::filesystem::path const& path, int mode) const = 0;
virtual std::filesystem::path
canonical(std::filesystem::path const& path) const = 0;
virtual std::filesystem::path current_path() const = 0;
};
} // namespace dwarfs

View File

@ -44,5 +44,6 @@ class os_access_generic : public os_access {
int access(std::filesystem::path const& path, int mode) const override;
std::filesystem::path
canonical(std::filesystem::path const& path) const override;
std::filesystem::path current_path() const override;
};
} // namespace dwarfs

View File

@ -78,9 +78,10 @@ int os_access_generic::access(fs::path const& path, int mode) const {
#endif
}
std::filesystem::path
os_access_generic::canonical(std::filesystem::path const& path) const {
fs::path os_access_generic::canonical(fs::path const& path) const {
return canonical_path(path);
}
fs::path os_access_generic::current_path() const { return fs::current_path(); }
} // namespace dwarfs

View File

@ -753,7 +753,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
options.with_specials = true;
if (!vm.count("input")) {
path = std::filesystem::current_path();
path = iol.os->current_path();
}
std::filesystem::path input_list_path(input_list_str);

View File

@ -455,6 +455,10 @@ os_access_mock::canonical(std::filesystem::path const& path) const {
return path;
}
std::filesystem::path os_access_mock::current_path() const {
return root_->name;
}
std::optional<fs::path> find_binary(std::string_view name) {
auto path_str = std::getenv("PATH");
if (!path_str) {

View File

@ -106,6 +106,8 @@ class os_access_mock : public os_access {
std::filesystem::path
canonical(std::filesystem::path const& path) const override;
std::filesystem::path current_path() const override;
private:
static std::vector<std::string> splitpath(std::filesystem::path const& path);
struct mock_dirent* find(std::filesystem::path const& path) const;