Sanitize ProjectPath

This commit is contained in:
Phillip Stephens 2015-08-04 18:54:35 -07:00
parent 31aa40cc77
commit 5d75ee63b6
3 changed files with 50 additions and 8 deletions

View File

@ -122,6 +122,10 @@ inline std::string operator+(const char* lhs, const SystemStringView& rhs) {retu
typedef struct stat Sstat; typedef struct stat Sstat;
#endif #endif
void SanitizePath(std::string& path);
void SanitizePath(std::wstring& path);
static inline void MakeDir(const SystemChar* dir) static inline void MakeDir(const SystemChar* dir)
{ {
#if _WIN32 #if _WIN32
@ -386,8 +390,12 @@ protected:
std::string m_utf8RelPath; std::string m_utf8RelPath;
#endif #endif
ProjectPath(const SystemString& projRoot) ProjectPath(const SystemString& projRoot)
: m_projRoot(projRoot), m_absPath(projRoot), m_relPath(_S(".")), m_hash(m_relPath) : m_projRoot(projRoot), m_absPath(projRoot), m_relPath(_S("."))
{ {
SanitizePath(m_projRoot);
SanitizePath(m_relPath);
SanitizePath(m_absPath);
m_hash = Hash(m_relPath);
#if HECL_UCS2 #if HECL_UCS2
m_utf8AbsPath = WideToUTF8(m_absPath); m_utf8AbsPath = WideToUTF8(m_absPath);
m_utf8RelPath = "."; m_utf8RelPath = ".";
@ -411,10 +419,10 @@ public:
* @param parentPath previously constructed ProjectPath which ultimately connects to a ProjectRootPath * @param parentPath previously constructed ProjectPath which ultimately connects to a ProjectRootPath
* @param path valid filesystem-path (relative or absolute) to subpath * @param path valid filesystem-path (relative or absolute) to subpath
*/ */
ProjectPath(const ProjectPath& parentPath, const SystemString& path) {assign(parentPath, path);} /* ProjectPath(const ProjectPath& parentPath, const SystemString& path) {assign(parentPath, path);}
void assign(const ProjectPath& parentPath, const SystemString& path); void assign(const ProjectPath& parentPath, const SystemString& path);*/
#if HECL_UCS2 #ifndef HECL_UCS2
ProjectPath(const ProjectPath& parentPath, const std::string& path) {assign(parentPath, path);} ProjectPath(const ProjectPath& parentPath, const std::string& path) {assign(parentPath, path);}
void assign(const ProjectPath& parentPath, const std::string& path); void assign(const ProjectPath& parentPath, const std::string& path);
#endif #endif
@ -532,7 +540,6 @@ public:
relTarget += target.m_relPath; relTarget += target.m_relPath;
MakeLink(relTarget.c_str(), m_absPath.c_str()); MakeLink(relTarget.c_str(), m_absPath.c_str());
} }
/** /**
* @brief HECL-specific blowfish hash * @brief HECL-specific blowfish hash
* @return unique hash value * @return unique hash value

View File

@ -3,4 +3,30 @@
namespace HECL namespace HECL
{ {
LogVisor::LogModule LogModule("HECL"); LogVisor::LogModule LogModule("HECL");
template <class T>
inline void replaceAll(T& str, const T& from, const T& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
}
void SanitizePath(std::string& path)
{
path.erase(std::remove(path.begin(), path.end(), '\n'), path.end());
path.erase(std::remove(path.begin(), path.end(), '\r'), path.end());
replaceAll<std::string>(path, "<>:\"|?*", "_");
}
void SanitizePath(std::wstring& path)
{
path.erase(std::remove(path.begin(), path.end(), L'\n'), path.end());
path.erase(std::remove(path.begin(), path.end(), L'\r'), path.end());
replaceAll<std::wstring>(path, L"<>:\"|?*", L"_");
}
} }

View File

@ -3,7 +3,6 @@
namespace HECL namespace HECL
{ {
static const SystemRegex regGLOB(_S("\\*"), SystemRegex::ECMAScript|SystemRegex::optimize); static const SystemRegex regGLOB(_S("\\*"), SystemRegex::ECMAScript|SystemRegex::optimize);
static const SystemRegex regPATHCOMP(_S("[/\\\\]*([^/\\\\]+)"), SystemRegex::ECMAScript|SystemRegex::optimize); static const SystemRegex regPATHCOMP(_S("[/\\\\]*([^/\\\\]+)"), SystemRegex::ECMAScript|SystemRegex::optimize);
static const SystemRegex regDRIVELETTER(_S("^([^/]*)/"), SystemRegex::ECMAScript|SystemRegex::optimize); static const SystemRegex regDRIVELETTER(_S("^([^/]*)/"), SystemRegex::ECMAScript|SystemRegex::optimize);
@ -21,6 +20,7 @@ static SystemString canonRelPath(const SystemString& path)
std::vector<SystemString> comps; std::vector<SystemString> comps;
HECL::SystemRegexMatch matches; HECL::SystemRegexMatch matches;
SystemString in = path; SystemString in = path;
SanitizePath(in);
for (; std::regex_search(in, matches, regPATHCOMP) ; in = matches.suffix()) for (; std::regex_search(in, matches, regPATHCOMP) ; in = matches.suffix())
{ {
const SystemString& match = matches[1]; const SystemString& match = matches[1];
@ -57,10 +57,15 @@ static SystemString canonRelPath(const SystemString& path)
void ProjectPath::assign(const ProjectPath& parentPath, const SystemString& path) void ProjectPath::assign(const ProjectPath& parentPath, const SystemString& path)
{ {
SystemString in = path;
m_projRoot = parentPath.m_projRoot; m_projRoot = parentPath.m_projRoot;
m_relPath = canonRelPath(parentPath.m_relPath + _S('/') + path); m_relPath = canonRelPath(parentPath.m_relPath + _S('/') + in);
m_absPath = parentPath.m_projRoot + _S('/') + m_relPath; m_absPath = parentPath.m_projRoot + _S('/') + m_relPath;
SanitizePath(m_projRoot);
SanitizePath(m_relPath);
SanitizePath(m_absPath);
m_hash = Hash(m_relPath); m_hash = Hash(m_relPath);
#if HECL_UCS2 #if HECL_UCS2
m_utf8AbsPath = WideToUTF8(m_absPath); m_utf8AbsPath = WideToUTF8(m_absPath);
m_utf8RelPath = WideToUTF8(m_relPath); m_utf8RelPath = WideToUTF8(m_relPath);
@ -70,10 +75,14 @@ void ProjectPath::assign(const ProjectPath& parentPath, const SystemString& path
#if HECL_UCS2 #if HECL_UCS2
void ProjectPath::assign(const ProjectPath& parentPath, const std::string& path) void ProjectPath::assign(const ProjectPath& parentPath, const std::string& path)
{ {
std::string in = path;
m_projRoot = parentPath.m_projRoot; m_projRoot = parentPath.m_projRoot;
std::wstring wpath = UTF8ToWide(path); std::wstring wpath = UTF8ToWide(in);
m_relPath = canonRelPath(parentPath.m_relPath + _S('/') + wpath); m_relPath = canonRelPath(parentPath.m_relPath + _S('/') + wpath);
m_absPath = parentPath.m_projRoot + _S('/') + m_relPath; m_absPath = parentPath.m_projRoot + _S('/') + m_relPath;
SanitizePath(m_projRoot);
SanitizePath(m_relPath);
SanitizePath(m_absPath);
m_hash = Hash(m_relPath); m_hash = Hash(m_relPath);
m_utf8AbsPath = WideToUTF8(m_absPath); m_utf8AbsPath = WideToUTF8(m_absPath);
m_utf8RelPath = WideToUTF8(m_relPath); m_utf8RelPath = WideToUTF8(m_relPath);