Commit 93eb1a1f authored by Clinton Stimpson's avatar Clinton Stimpson
Browse files

SystemTools: Improve RelativePath() to handle ./ ../ and //

This fixes bug #12065, #14462 and #14765.

Change-Id: I80c6506767d8b1c7ab2607800458ae3064909b3f
parent 32023afd
......@@ -3425,9 +3425,12 @@ kwsys_stl::string SystemTools::RelativePath(const kwsys_stl::string& local, cons
return "";
}
kwsys_stl::string l = SystemTools::CollapseFullPath(local);
kwsys_stl::string r = SystemTools::CollapseFullPath(remote);
// split up both paths into arrays of strings using / as a separator
kwsys_stl::vector<kwsys::String> localSplit = SystemTools::SplitString(local, '/', true);
kwsys_stl::vector<kwsys::String> remoteSplit = SystemTools::SplitString(remote, '/', true);
kwsys_stl::vector<kwsys::String> localSplit = SystemTools::SplitString(l, '/', true);
kwsys_stl::vector<kwsys::String> remoteSplit = SystemTools::SplitString(r, '/', true);
kwsys_stl::vector<kwsys::String> commonPath; // store shared parts of path in this array
kwsys_stl::vector<kwsys::String> finalPath; // store the final relative path here
// count up how many matching directory names there are from the start
......
......@@ -562,6 +562,33 @@ static bool CheckEnvironmentOperations()
return res;
}
static bool CheckRelativePath(
const kwsys_stl::string& local,
const kwsys_stl::string& remote,
const kwsys_stl::string& expected)
{
kwsys_stl::string result = kwsys::SystemTools::RelativePath(local, remote);
if(expected != result)
{
kwsys_ios::cerr << "RelativePath(" << local << ", " << remote
<< ") yielded " << result << " instead of " << expected << kwsys_ios::endl;
return false;
}
return true;
}
static bool CheckRelativePaths()
{
bool res = true;
res &= CheckRelativePath("/usr/share", "/bin/bash", "../../bin/bash");
res &= CheckRelativePath("/usr/./share/", "/bin/bash", "../../bin/bash");
res &= CheckRelativePath("/usr//share/", "/bin/bash", "../../bin/bash");
res &= CheckRelativePath("/usr/share/../bin/", "/bin/bash", "../../bin/bash");
res &= CheckRelativePath("/usr/share", "/usr/share//bin", "bin");
return res;
}
//----------------------------------------------------------------------------
int testSystemTools(int, char*[])
{
......@@ -593,5 +620,7 @@ int testSystemTools(int, char*[])
res &= CheckEnvironmentOperations();
res &= CheckRelativePaths();
return res ? 0 : 1;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment