Commit 08b2dcff authored by Brad King's avatar Brad King
Browse files

KWSys: Fix GetActualCaseForPath for UNC paths

See issue #11023.

Author: Clinton Stimpson <clinton@elemtech.com>
parent 3a4d43c0
...@@ -3059,21 +3059,35 @@ kwsys_stl::string SystemTools::RelativePath(const char* local, const char* remot ...@@ -3059,21 +3059,35 @@ kwsys_stl::string SystemTools::RelativePath(const char* local, const char* remot
static int GetCasePathName(const kwsys_stl::string & pathIn, static int GetCasePathName(const kwsys_stl::string & pathIn,
kwsys_stl::string & casePath) kwsys_stl::string & casePath)
{ {
kwsys_stl::string::size_type iFound = pathIn.rfind('/'); kwsys_stl::vector<kwsys::String> path_components =
if (iFound > 1 && iFound != pathIn.npos) SystemTools::SplitString(pathIn.c_str(), '/', true);
if(path_components.empty())
{ {
// recurse to peel off components casePath = "";
// return 0;
if (GetCasePathName(pathIn.substr(0, iFound), casePath) > 0) }
kwsys_stl::vector<kwsys_stl::string>::size_type idx = 0;
// assume always absolute path, so just take first
casePath = path_components[idx++];
// If network path, fill casePath with server/share so FindFirstFile
// will work after that. Maybe someday call other APIs to get
// actual case of servers and shares.
if(path_components.size() > 2 && pathIn.size() >= 2 &&
pathIn[0] == '/' && pathIn[1] == '/')
{ {
casePath += '/'; casePath += path_components[idx++];
if (pathIn[1] != '/') casePath += "/";
casePath += path_components[idx++];
}
for(; idx < path_components.size(); idx++)
{ {
WIN32_FIND_DATA findData; casePath += "/";
kwsys_stl::string test_str = casePath;
test_str += path_components[idx];
// append the long component name to the path WIN32_FIND_DATA findData;
// HANDLE hFind = ::FindFirstFile(test_str.c_str(), &findData);
HANDLE hFind = ::FindFirstFile(pathIn.c_str(), &findData);
if (INVALID_HANDLE_VALUE != hFind) if (INVALID_HANDLE_VALUE != hFind)
{ {
casePath += findData.cFileName; casePath += findData.cFileName;
...@@ -3081,18 +3095,10 @@ static int GetCasePathName(const kwsys_stl::string & pathIn, ...@@ -3081,18 +3095,10 @@ static int GetCasePathName(const kwsys_stl::string & pathIn,
} }
else else
{ {
// if FindFirstFile fails, return the error code
//
casePath = ""; casePath = "";
return 0; return 0;
} }
} }
}
}
else
{
casePath = pathIn;
}
return (int)casePath.size(); return (int)casePath.size();
} }
#endif #endif
...@@ -3104,25 +3110,26 @@ kwsys_stl::string SystemTools::GetActualCaseForPath(const char* p) ...@@ -3104,25 +3110,26 @@ kwsys_stl::string SystemTools::GetActualCaseForPath(const char* p)
#ifndef _WIN32 #ifndef _WIN32
return p; return p;
#else #else
kwsys_stl::string casePath = p;
// make sure drive letter is always upper case
if(casePath.size() > 1 && casePath[1] == ':')
{
casePath[0] = toupper(casePath[0]);
}
// Check to see if actual case has already been called // Check to see if actual case has already been called
// for this path, and the result is stored in the LongPathMap // for this path, and the result is stored in the LongPathMap
SystemToolsTranslationMap::iterator i = SystemToolsTranslationMap::iterator i =
SystemTools::LongPathMap->find(p); SystemTools::LongPathMap->find(casePath);
if(i != SystemTools::LongPathMap->end()) if(i != SystemTools::LongPathMap->end())
{ {
return i->second; return i->second;
} }
kwsys_stl::string casePath;
int len = GetCasePathName(p, casePath); int len = GetCasePathName(p, casePath);
if(len == 0 || len > MAX_PATH+1) if(len == 0 || len > MAX_PATH+1)
{ {
return p; return p;
} }
// make sure drive letter is always upper case
if(casePath.size() > 1 && casePath[1] == ':')
{
casePath[0] = toupper(casePath[0]);
}
(*SystemTools::LongPathMap)[p] = casePath; (*SystemTools::LongPathMap)[p] = casePath;
return casePath; return casePath;
#endif #endif
......
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