refactor: add canonical() to os_access

This commit is contained in:
Marcus Holland-Moritz 2023-12-28 17:26:53 +01:00
parent 1f8ad8fbc7
commit 1ef2b61207
6 changed files with 20 additions and 1 deletions

View File

@ -50,5 +50,7 @@ class os_access {
virtual std::unique_ptr<mmif>
map_file(std::filesystem::path const& path, size_t size) const = 0;
virtual int access(std::filesystem::path const& path, int mode) const = 0;
virtual std::filesystem::path
canonical(std::filesystem::path const& path) const = 0;
};
} // namespace dwarfs

View File

@ -42,5 +42,7 @@ class os_access_generic : public os_access {
std::unique_ptr<mmif>
map_file(std::filesystem::path const& path, size_t size) const override;
int access(std::filesystem::path const& path, int mode) const override;
std::filesystem::path
canonical(std::filesystem::path const& path) const override;
};
} // namespace dwarfs

View File

@ -23,6 +23,7 @@
#include "dwarfs/mmap.h"
#include "dwarfs/os_access_generic.h"
#include "dwarfs/util.h"
namespace dwarfs {
@ -77,4 +78,9 @@ 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 {
return canonical_path(path);
}
} // namespace dwarfs

View File

@ -76,6 +76,7 @@
#include "dwarfs/mmap.h"
#include "dwarfs/options.h"
#include "dwarfs/options_interface.h"
#include "dwarfs/os_access.h"
#include "dwarfs/overloaded.h"
#include "dwarfs/program_options_helpers.h"
#include "dwarfs/progress.h"
@ -768,7 +769,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
}
}
path = canonical_path(path);
path = iol.os->canonical(path);
bool recompress = vm.count("recompress");
rewrite_options rw_opts;

View File

@ -450,6 +450,11 @@ int os_access_mock::access(fs::path const& path, int) const {
return access_fail_set_.count(path) ? -1 : 0;
}
std::filesystem::path
os_access_mock::canonical(std::filesystem::path const& path) const {
return path;
}
std::optional<fs::path> find_binary(std::string_view name) {
auto path_str = std::getenv("PATH");
if (!path_str) {

View File

@ -103,6 +103,9 @@ class os_access_mock : public os_access {
int access(std::filesystem::path const&, int) const override;
std::filesystem::path
canonical(std::filesystem::path const& 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;