//----------------------------------------------------------------------------
osg::ref_ptr<osg::Image> FGCanvasSystemAdapter::getImage(const std::string& path) const
{
- if( SGPath(path).isAbsolute() )
+ SGPath p(SGPath::fromUtf8(path));
+ if( p.isAbsolute() )
{
- std::string valid_path = fgValidatePath(path, false);
- if( !valid_path.empty() )
- return osgDB::readImageFile(valid_path.c_str());
+ SGPath valid_path = fgValidatePath(p, false);
+ if( !valid_path.isNull() )
+ return osgDB::readImageFile(valid_path.local8BitStr());
SG_LOG(SG_IO, SG_ALERT, "canvas::Image: reading '" << path << "' denied");
}
if (file.extension() != "sav")
file.concat(".sav");
- std::string validated_path = fgValidatePath(file, false);
- if (validated_path.empty()) {
+ SGPath validated_path = fgValidatePath(file, false);
+ if (validated_path.isNull()) {
SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
"(unauthorized access)");
return false;
}
- sg_ifstream input(SGPath::fromUtf8(validated_path));
+ sg_ifstream input(validated_path);
if (input.good() && fgLoadFlight(input)) {
input.close();
SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
if (file.extension() != "sav")
file.concat(".sav");
- std::string validated_path = fgValidatePath(file, true);
- if (validated_path.empty()) {
+ SGPath validated_path = fgValidatePath(file, true);
+ if (validated_path.isNull()) {
SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
"(unauthorized access)");
return false;
}
}
- std::string validated_path = fgValidatePath(file, false);
- if (validated_path.empty()) {
+ SGPath validated_path = fgValidatePath(file, false);
+ if (validated_path.isNull()) {
SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file << "' denied "
"(unauthorized directory - authorization no longer follows symlinks; to authorize reading additional directories, add them to --fg-aircraft)");
return false;
if (file.extension() != "xml")
file.concat(".xml");
- std::string validated_path = fgValidatePath(file, true);
- if (validated_path.empty()) {
+ SGPath validated_path = fgValidatePath(file, true);
+ if (validated_path.isNull()) {
SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file << "' denied "
"(unauthorized directory - authorization no longer follows symlinks)");
return false;
// Check that it works
std::string homePath = globals->get_fg_home().utf8Str();
- if(!fgValidatePath(homePath + "/../no.log",true).empty() ||
- !fgValidatePath(homePath + "/no.logt",true).empty() ||
- !fgValidatePath(homePath + "/nolog",true).empty() ||
- !fgValidatePath(homePath + "no.log",true).empty() ||
- !fgValidatePath(homePath + "\\..\\no.log",false).empty() ||
- fgValidatePath(homePath + "/aircraft-data/yes..xml",true).empty() ||
- fgValidatePath(homePath + "/.\\yes.bmp",false).empty()) {
+ if(!fgValidatePath(homePath + "/../no.log",true).isNull() ||
+ !fgValidatePath(homePath + "/no.logt",true).isNull() ||
+ !fgValidatePath(homePath + "/nolog",true).isNull() ||
+ !fgValidatePath(homePath + "no.log",true).isNull() ||
+ !fgValidatePath(homePath + "\\..\\no.log",false).isNull() ||
+ fgValidatePath(homePath + "/aircraft-data/yes..xml",true).isNull() ||
+ fgValidatePath(homePath + "/.\\yes.bmp",false).isNull()) {
flightgear::fatalMessageBox("Nasal initialization error",
"The FG_HOME directory must not be inside any of the FG_ROOT, FG_AIRCRAFT or FG_SCENERY directories",
"(check that you have not accidentally included an extra :, as an empty part means the current directory)");
* the current directory changes),
* always use the returned path not the original one
*/
-std::string fgValidatePath (const std::string& path, bool write)
+SGPath fgValidatePath (const SGPath& path, bool write)
{
// Normalize the path (prevents ../../.. or symlink trickery)
- std::string normed_path = SGPath(path).realpath();
+ std::string normed_path = path.realpath();
const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
size_t star_pos;
star_pos = it->find('*');
if (star_pos == std::string::npos) {
if (!(it->compare(normed_path))) {
- return normed_path;
+ return SGPath::fromUtf8(normed_path);
}
} else {
if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
&& !(it->substr(star_pos+1,it->size()-star_pos-1)
.compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
it->size()-star_pos-1))) /* after-star parts match */) {
- return normed_path;
+ return SGPath::fromUtf8(normed_path);
}
}
}
// no match found
- return "";
+ return SGPath();
}
-std::string fgValidatePath(const SGPath& path, bool write) { return fgValidatePath(path.utf8Str(),write); }
+
// end of util.cxx
* the current directory changes),
* always use the returned path not the original one
*/
-std::string fgValidatePath(const SGPath& path, bool write);
-std::string fgValidatePath(const std::string& path, bool write);
+SGPath fgValidatePath(const SGPath& path, bool write);
/**
* Set allowed paths for fgValidatePath
// Check for write access to target file
const std::string filename = ctx.requireArg<std::string>(1);
- const std::string validated_path = fgValidatePath(filename, true);
+ const SGPath validated_path = fgValidatePath(filename, true);
- if( validated_path.empty() )
+ if( validated_path.isNull() )
naRuntimeError( ctx.c,
"Access denied: can not write to %s",
filename.c_str() );
return ctx.to_nasal
(
- requireHTTPClient(ctx.c).client()->save(url, validated_path)
+ requireHTTPClient(ctx.c).client()->save(url, validated_path.utf8Str())
);
}
"realpath() to make a path absolute)");
}
- perm.read = path.isAbsolute() && !fgValidatePath(path, false).empty();
- perm.write = path.isAbsolute() && !fgValidatePath(path, true ).empty();
+ perm.read = path.isAbsolute() && !fgValidatePath(path, false).isNull();
+ perm.write = path.isAbsolute() && !fgValidatePath(path, true ).isNull();
return perm;
}
if(argc != 1 || !naIsString(args[0]))
naRuntimeError(c, "bad arguments to directory()");
- std::string dirname = fgValidatePath(naStr_data(args[0]), false);
- if(dirname.empty()) {
+ SGPath dirname = fgValidatePath(SGPath::fromUtf8(naStr_data(args[0])), false);
+ if(dirname.isNull()) {
SG_LOG(SG_NASAL, SG_ALERT, "directory(): listing '" <<
naStr_data(args[0]) << "' denied (unauthorized directory - authorization"
" no longer follows symlinks; to authorize reading additional "
return naNil();
}
- SGPath d0(dirname);
- simgear::Dir d(d0);
+ simgear::Dir d(dirname);
if(!d.exists()) return naNil();
naRef result = naNewVector(c);
naRef mode = argc > 1 ? naStringValue(c, args[1]) : naNil();
if(!naStr_data(file)) naRuntimeError(c, "bad argument to open()");
const char* modestr = naStr_data(mode) ? naStr_data(mode) : "rb";
- std::string filename = fgValidatePath(naStr_data(file),
+ SGPath filename = fgValidatePath(SGPath::fromUtf8(naStr_data(file)),
strcmp(modestr, "rb") && strcmp(modestr, "r"));
- if(filename.empty()) {
+ if(filename.isNull()) {
SG_LOG(SG_NASAL, SG_ALERT, "open(): reading/writing '" <<
naStr_data(file) << "' denied (unauthorized directory - authorization"
" no longer follows symlinks; to authorize reading additional "
naRuntimeError(c, "open(): access denied (unauthorized directory)");
return naNil();
}
+
f = fopen(filename.c_str(), modestr);
+
if(!f) naRuntimeError(c, strerror(errno));
return naIOGhost(c, f);
}
if(!(naIsNil(args[i]) || naIsFunc(args[i])))
naRuntimeError(c, "parsexml(): callback argument not a function");
- std::string file = fgValidatePath(naStr_data(args[0]), false);
- if(file.empty()) {
+ SGPath file = fgValidatePath(SGPath::fromUtf8(naStr_data(args[0])), false);
+ if(file.isNull()) {
SG_LOG(SG_NASAL, SG_ALERT, "parsexml(): reading '" <<
naStr_data(args[0]) << "' denied (unauthorized directory - authorization"
" no longer follows symlinks; to authorize reading additional "
naRuntimeError(c, "parsexml(): access denied (unauthorized directory)");
return naNil();
}
- sg_ifstream input(SGPath::fromUtf8(file));
+ sg_ifstream input(file);
NasalXMLVisitor visitor(c, argc, args);
try {
readXML(input, visitor);
file.c_str(), e.getFormattedMessage().c_str());
return naNil();
}
- return naStr_fromdata(naNewString(c), file.c_str(), file.length());
+
+ std::string fs = file.utf8Str();
+ return naStr_fromdata(naNewString(c), fs.c_str(), fs.length());
}
/**