Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of UNC paths on Windows #755

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}