SGPath(): make realpath() suitable for fgValidatePath
Handle non-existent files, drop obsolete workaround
This commit is contained in:
parent
10e6bbc2c5
commit
4104f7d18f
@ -835,25 +835,39 @@ SGPath SGPath::documents(const SGPath& def)
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
std::string SGPath::realpath() const
|
std::string SGPath::realpath() const
|
||||||
{
|
{
|
||||||
#if (defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1050)
|
#if defined(_MSC_VER) /*for MS compilers */ || defined(_WIN32) /*needed for non MS windows compilers like MingW*/
|
||||||
// Workaround for Mac OS 10.5. Somehow fgfs crashes on Mac at ::realpath.
|
|
||||||
// This means relative paths cannot be used on Mac OS <= 10.5
|
|
||||||
return path;
|
|
||||||
#else
|
|
||||||
#if defined(_MSC_VER) /*for MS compilers */ || defined(_WIN32) /*needed for non MS windows compilers like MingW*/
|
|
||||||
// with absPath NULL, will allocate, and ignore length
|
// with absPath NULL, will allocate, and ignore length
|
||||||
char *buf = _fullpath( NULL, path.c_str(), _MAX_PATH );
|
char *buf = _fullpath( NULL, path.c_str(), _MAX_PATH );
|
||||||
#else
|
#else
|
||||||
// POSIX
|
// POSIX
|
||||||
char* buf = ::realpath(path.c_str(), NULL);
|
char* buf = ::realpath(path.c_str(), NULL);
|
||||||
#endif
|
#endif
|
||||||
if (!buf)
|
if (!buf) // File does not exist: return the realpath it would have if created now
|
||||||
|
// (needed for fgValidatePath security)
|
||||||
{
|
{
|
||||||
SG_LOG(SG_IO, SG_WARN, "ERROR: The path '" << path << "' does not exist in the file system.");
|
if (path.empty()) {
|
||||||
return path;
|
return SGPath(".").realpath(); // current directory
|
||||||
|
}
|
||||||
|
std::string this_dir = dir();
|
||||||
|
if (isAbsolute() && this_dir.empty()) { // top level
|
||||||
|
this_dir = "/";
|
||||||
|
}
|
||||||
|
if (file() == "..") {
|
||||||
|
this_dir = SGPath(SGPath(this_dir).realpath()).dir();
|
||||||
|
if (this_dir.empty()) { // invalid path: .. above root
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return SGPath(this_dir).realpath(); // use native path separator,
|
||||||
|
// and handle 'existing/nonexisting/../symlink' paths
|
||||||
|
}
|
||||||
|
return SGPath(this_dir).realpath() +
|
||||||
|
#if defined(_MSC_VER) || defined(_WIN32)
|
||||||
|
"\\" + file();
|
||||||
|
#else
|
||||||
|
"/" + file();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
std::string p(buf);
|
std::string p(buf);
|
||||||
free(buf);
|
free(buf);
|
||||||
return p;
|
return p;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user