// default constructor
SGPath::SGPath(PermissonChecker validator)
: path(""),
- _permisson_checker(validator),
+ _permission_checker(validator),
_cached(false),
_rwCached(false),
_cacheEnabled(true)
// create a path based on "path"
SGPath::SGPath( const std::string& p, PermissonChecker validator )
: path(p),
- _permisson_checker(validator),
+ _permission_checker(validator),
_cached(false),
_rwCached(false),
_cacheEnabled(true)
const std::string& r,
PermissonChecker validator )
: path(p.path),
- _permisson_checker(validator),
+ _permission_checker(validator),
_cached(false),
_rwCached(false),
_cacheEnabled(p._cacheEnabled)
SGPath::SGPath(const SGPath& p) :
path(p.path),
- _permisson_checker(p._permisson_checker),
+ _permission_checker(p._permission_checker),
_cached(p._cached),
_rwCached(p._rwCached),
_cacheEnabled(p._cacheEnabled),
SGPath& SGPath::operator=(const SGPath& p)
{
path = p.path;
- _permisson_checker = p._permisson_checker,
+ _permission_checker = p._permission_checker,
_cached = p._cached;
_rwCached = p._rwCached;
_cacheEnabled = p._cacheEnabled;
//------------------------------------------------------------------------------
void SGPath::setPermissonChecker(PermissonChecker validator)
{
- _permisson_checker = validator;
+ _permission_checker = validator;
_rwCached = false;
}
//------------------------------------------------------------------------------
SGPath::PermissonChecker SGPath::getPermissonChecker() const
{
- return _permisson_checker;
+ return _permission_checker;
}
//------------------------------------------------------------------------------
if( _rwCached && _cacheEnabled )
return;
- if( _permisson_checker )
+ if( _permission_checker )
{
- Permissions p = _permisson_checker(*this);
+ Permissions p = _permission_checker(*this);
_canRead = p.read;
_canWrite = p.write;
}
bool absolute = !path.empty() && path[0] == sgDirPathSep;
unsigned int i = 1;
- SGPath dir(absolute ? string( 1, sgDirPathSep ) : "", _permisson_checker);
+ SGPath dir(absolute ? string( 1, sgDirPathSep ) : "", _permission_checker);
dir.concat( path_elements[0] );
#ifdef _WIN32
if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
if( !dir.canWrite() )
{
SG_LOG( SG_IO,
- SG_ALERT, "Error creating directory: (" << dir.str() << ")" <<
- " reason: access denied" );
+ SG_WARN, "Error creating directory: (" << dir.str() << ")" <<
+ " reason: access denied" );
return -3;
}
else if( sgMkDir(dir.c_str(), mode) )
//------------------------------------------------------------------------------
bool SGPath::rename(const SGPath& newName)
{
- if( !newName.canWrite() )
+ if( !canRead() || !canWrite() || !newName.canWrite() )
{
SG_LOG( SG_IO, SG_WARN, "rename failed: from " << str() <<
" to " << newName.str() <<
}
path = newName.path;
+
+ // Do not remove permission checker (could happen for example if just using
+ // a std::string as new name)
+ if( newName._permission_checker )
+ _permission_checker = newName._permission_checker;
+
_cached = false;
_rwCached = false;
{
const char* val = getenv(name);
if( val && val[0] )
- return SGPath(val, def._permisson_checker);
+ return SGPath(val, def._permission_checker);
return def;
}
#ifdef _WIN32
//------------------------------------------------------------------------------
-SGPath SGPath::home()
+SGPath SGPath::home(const SGPath& def)
{
// TODO
- return SGPath();
+ return def;
}
#else
//------------------------------------------------------------------------------
-SGPath SGPath::home()
+SGPath SGPath::home(const SGPath& def)
{
- return fromEnv("HOME");
+ return fromEnv("HOME", def);
}
#endif
#include <ShlObj.h> // for CSIDL
//------------------------------------------------------------------------------
-SGPath SGPath::desktop()
+SGPath SGPath::desktop(const SGPath& def)
{
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPSTR, int, BOOL);
static GetSpecialFolderPath SHGetSpecialFolderPath = NULL;
}
if (!SHGetSpecialFolderPath){
- return SGPath();
+ return def;
}
char path[MAX_PATH];
if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, false)) {
- return SGPath(path);
+ return SGPath(path, def._permission_checker);
}
SG_LOG(SG_GENERAL, SG_ALERT, "SGPath::desktop() failed, bad" );
- return SGPath();
+ return def;
}
#elif __APPLE__
#include <CoreServices/CoreServices.h>
//------------------------------------------------------------------------------
-SGPath SGPath::desktop()
+SGPath SGPath::desktop(const SGPath& def)
{
FSRef ref;
OSErr err = FSFindFolder(kUserDomain, kDesktopFolderType, false, &ref);
- if (err) {
- return SGPath();
- }
+ if (err)
+ return def;
unsigned char path[1024];
- if (FSRefMakePath(&ref, path, 1024) != noErr) {
- return SGPath();
- }
+ if (FSRefMakePath(&ref, path, 1024) != noErr)
+ return def
- return SGPath((const char*) path);
+ return SGPath((const char*) path, def._permission_checker);
}
#else
//------------------------------------------------------------------------------
-SGPath SGPath::desktop()
+SGPath SGPath::desktop(const SGPath& def)
{
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
const std::string HOME = "$HOME";
if( starts_with(line, HOME) )
- return home() / simgear::strutils::unescape(line.substr(HOME.length()));
+ return home(def) / simgear::strutils::unescape(line.substr(HOME.length()));
- return SGPath(line);
+ return SGPath(line, def._permission_checker);
}
- return home() / "Desktop";
+ return home(def) / "Desktop";
}
#endif