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

Support absolute Windows paths with forward slashes in common::FindFile #389

Merged
merged 1 commit into from
Jul 11, 2022
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
47 changes: 26 additions & 21 deletions src/SystemPaths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,38 +400,43 @@ std::string SystemPaths::FindFile(const std::string &_filename,
if (filename.empty())
return path;

// Handle as URI
if (ignition::common::URI::Valid(filename))
{
path = this->FindFileURI(ignition::common::URI(filename));
}
// Handle as local absolute path
else if (filename[0] == '/')
{
path = filename;
}
#ifdef _WIN32
// Handle as Windows absolute path
else if (filename.length() >= 2 && filename[1] == ':')
// First of all, try if filename as a Windows absolute path exists
// The Windows absolute path is tried first as a Windows drive such as
// C:/ is also a valid URI scheme
if (filename.length() >= 2 && filename[1] == ':' && exists(filename))
{
path = filename;
}
#endif // _WIN32
// Try appending to local paths
else
{
auto cwdPath = joinPaths(cwd(), filename);
if (_searchLocalPath && exists(cwdPath))
// If the filename is not an existing absolute Windows path, try others
if (path.empty()) {
// Handle as URI
if (ignition::common::URI::Valid(filename))
{
path = cwdPath;
path = this->FindFileURI(ignition::common::URI(filename));
}
else if ((filename[0] == '.' || _searchLocalPath) && exists(filename))
// Handle as local absolute path
else if (filename[0] == '/')
{
path = filename;
}
else if (this->dataPtr->findFileCB)
// Try appending to local paths
else
{
path = this->dataPtr->findFileCB(filename);
auto cwdPath = joinPaths(cwd(), filename);
if (_searchLocalPath && exists(cwdPath))
{
path = cwdPath;
}
else if ((filename[0] == '.' || _searchLocalPath) && exists(filename))
{
path = filename;
}
else if (this->dataPtr->findFileCB)
{
path = this->dataPtr->findFileCB(filename);
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/SystemPaths_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ TEST_F(SystemPathsFixture, FindFile)
EXPECT_EQ("", sp.FindFile(uriBadDir));
}

// Windows path with only one forward slash
#ifdef _WIN32
const auto tmpDirForwardSlash = "C:/Windows";
const auto homeDirForwardSlash = "C:/Users";
const auto badDirForwardSlash = "C:/bad";
{
// We do not expect equality as sp.FindFile could normalize the
// path and return a different string
// The behaviour tested here is just that C:/Windows, C:/Users are found
EXPECT_NE("", sp.FindFile(tmpDirForwardSlash));
EXPECT_NE("", sp.FindFile(homeDirForwardSlash));
EXPECT_EQ("", sp.FindFile(badDirForwardSlash));
}
#endif

// Custom callback
{
auto tmpCb = [tmpDir](const std::string &_s)
Expand Down