Skip to content

Commit

Permalink
Allow use of UNC paths on Windows
Browse files Browse the repository at this point in the history
This fixes issue #619.
  • Loading branch information
kyllingstad committed Feb 26, 2024
1 parent 02e1d8c commit fc067cd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/cosim/orchestration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ std::shared_ptr<model> fmu_file_uri_sub_resolver::lookup_model(const uri& modelU
{
assert(modelUri.scheme().has_value());
if (*modelUri.scheme() != "file") return nullptr;
#ifndef _WIN32
if (modelUri.authority().has_value() &&
!(modelUri.authority()->empty() || *modelUri.authority() == "localhost")) {
return nullptr;
}
#endif
if (modelUri.query().has_value() || modelUri.fragment().has_value()) {
BOOST_LOG_SEV(log::logger(), log::warning)
<< "Query and/or fragment component(s) in a file:// URI were ignored: "
Expand Down
5 changes: 2 additions & 3 deletions src/cosim/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,6 @@ uri path_to_file_uri(const cosim::filesystem::path& path)
cosim::filesystem::path file_uri_to_path(const uri& fileUri)
{
COSIM_INPUT_CHECK(fileUri.scheme() && *fileUri.scheme() == "file");
COSIM_INPUT_CHECK(fileUri.authority() &&
(fileUri.authority()->empty() || *fileUri.authority() == "localhost"));

#ifdef _WIN32
// Windows has some special rules for file URIs; better use the built-in
// functions.
Expand All @@ -554,6 +551,8 @@ cosim::filesystem::path file_uri_to_path(const uri& fileUri)
return cosim::filesystem::path(pathBuffer, pathBuffer + pathSize);

#else
COSIM_INPUT_CHECK(fileUri.authority() &&
(fileUri.authority()->empty() || *fileUri.authority() == "localhost"));
return cosim::filesystem::path(percent_decode(fileUri.path()));
#endif
}
Expand Down
4 changes: 3 additions & 1 deletion tests/uri_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ BOOST_AUTO_TEST_CASE(file_uri_conversions)
BOOST_TEST(path_to_file_uri("\\foo bar\\baz") == "file:///foo%20bar/baz");
BOOST_TEST(path_to_file_uri("/foo bar/baz") == "file:///foo%20bar/baz");
BOOST_TEST(path_to_file_uri("c:\\foo bar\\baz") == "file:///c:/foo%20bar/baz");
BOOST_TEST(path_to_file_uri("\\\\foo\\bar\\baz") == "file://foo/bar/baz");
#endif

// From URI to path
Expand All @@ -213,12 +214,13 @@ BOOST_AUTO_TEST_CASE(file_uri_conversions)
BOOST_TEST(file_uri_to_path("file:///c:/foo%20bar/baz") == "c:\\foo bar\\baz");
BOOST_TEST(file_uri_to_path("file://localhost/foo%20bar/baz") == "\\foo bar\\baz");
BOOST_TEST(file_uri_to_path("file://localhost/c:/foo%20bar/baz") == "c:\\foo bar\\baz");
BOOST_TEST(file_uri_to_path("file://foo/bar/baz") == "\\\\foo\\bar\\baz");
#else
BOOST_TEST(file_uri_to_path("file:///foo%20bar/baz") == "/foo bar/baz");
BOOST_TEST(file_uri_to_path("file:///c:/foo%20bar/baz") == "/c:/foo bar/baz");
BOOST_TEST(file_uri_to_path("file://localhost/foo%20bar/baz") == "/foo bar/baz");
BOOST_TEST(file_uri_to_path("file://localhost/c:/foo%20bar/baz") == "/c:/foo bar/baz");
BOOST_CHECK_THROW(file_uri_to_path("file://foo/bar"), std::invalid_argument);
#endif
BOOST_CHECK_THROW(file_uri_to_path("http://foo/bar"), std::invalid_argument);
BOOST_CHECK_THROW(file_uri_to_path("file://foo/bar"), std::invalid_argument);
}

0 comments on commit fc067cd

Please sign in to comment.