diff --git a/include/dwarfs/util.h b/include/dwarfs/util.h index fe4d9c38..32984fef 100644 --- a/include/dwarfs/util.h +++ b/include/dwarfs/util.h @@ -23,6 +23,7 @@ #include #include +#include #include #include "dwarfs/types.h" @@ -49,4 +50,6 @@ size_t utf8_display_width(std::string const& str); void shorten_path_string(std::string& path, char separator, size_t max_len); +std::filesystem::path canonical_path(std::filesystem::path p); + } // namespace dwarfs diff --git a/src/dwarfs/util.cpp b/src/dwarfs/util.cpp index c05e5a50..95d30ea6 100644 --- a/src/dwarfs/util.cpp +++ b/src/dwarfs/util.cpp @@ -173,4 +173,18 @@ void shorten_path_string(std::string& path, char separator, size_t max_len) { } } +std::filesystem::path canonical_path(std::filesystem::path p) { + try { + p = std::filesystem::canonical(p); + } catch (std::filesystem::filesystem_error const&) { + p = std::filesystem::absolute(p); + } + +#ifdef _WIN32 + p = std::filesystem::path(L"\\\\?\\" + p.wstring()); +#endif + + return p; +} + } // namespace dwarfs diff --git a/src/dwarfs_main.cpp b/src/dwarfs_main.cpp index f54695d7..09ca412c 100644 --- a/src/dwarfs_main.cpp +++ b/src/dwarfs_main.cpp @@ -994,7 +994,7 @@ int option_hdl(void* data, char const* arg, int key, return 1; } - opts->fsimage = std::filesystem::canonical( + opts->fsimage = canonical_path( std::filesystem::path(reinterpret_cast(arg))); return 0; diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index d3b78c0d..4b0f8364 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -676,11 +676,7 @@ int mkdwarfs_main(int argc, sys_char** argv) { } } - path = std::filesystem::canonical(path); - -#ifdef _WIN32 - path = std::filesystem::path(L"\\\\?\\" + path.wstring()); -#endif + path = canonical_path(path); bool recompress = vm.count("recompress"); rewrite_options rw_opts;