SGPath: some tweaks/fixes/typo on permission checker.

This commit is contained in:
Thomas Geymayer 2013-12-12 21:25:13 +01:00
parent 031c833e4d
commit 8dccbe8029
2 changed files with 41 additions and 37 deletions

View File

@ -77,7 +77,7 @@ SGPath::fix()
// default constructor // default constructor
SGPath::SGPath(PermissonChecker validator) SGPath::SGPath(PermissonChecker validator)
: path(""), : path(""),
_permisson_checker(validator), _permission_checker(validator),
_cached(false), _cached(false),
_rwCached(false), _rwCached(false),
_cacheEnabled(true) _cacheEnabled(true)
@ -88,7 +88,7 @@ SGPath::SGPath(PermissonChecker validator)
// create a path based on "path" // create a path based on "path"
SGPath::SGPath( const std::string& p, PermissonChecker validator ) SGPath::SGPath( const std::string& p, PermissonChecker validator )
: path(p), : path(p),
_permisson_checker(validator), _permission_checker(validator),
_cached(false), _cached(false),
_rwCached(false), _rwCached(false),
_cacheEnabled(true) _cacheEnabled(true)
@ -101,7 +101,7 @@ SGPath::SGPath( const SGPath& p,
const std::string& r, const std::string& r,
PermissonChecker validator ) PermissonChecker validator )
: path(p.path), : path(p.path),
_permisson_checker(validator), _permission_checker(validator),
_cached(false), _cached(false),
_rwCached(false), _rwCached(false),
_cacheEnabled(p._cacheEnabled) _cacheEnabled(p._cacheEnabled)
@ -112,7 +112,7 @@ SGPath::SGPath( const SGPath& p,
SGPath::SGPath(const SGPath& p) : SGPath::SGPath(const SGPath& p) :
path(p.path), path(p.path),
_permisson_checker(p._permisson_checker), _permission_checker(p._permission_checker),
_cached(p._cached), _cached(p._cached),
_rwCached(p._rwCached), _rwCached(p._rwCached),
_cacheEnabled(p._cacheEnabled), _cacheEnabled(p._cacheEnabled),
@ -128,7 +128,7 @@ SGPath::SGPath(const SGPath& p) :
SGPath& SGPath::operator=(const SGPath& p) SGPath& SGPath::operator=(const SGPath& p)
{ {
path = p.path; path = p.path;
_permisson_checker = p._permisson_checker, _permission_checker = p._permission_checker,
_cached = p._cached; _cached = p._cached;
_rwCached = p._rwCached; _rwCached = p._rwCached;
_cacheEnabled = p._cacheEnabled; _cacheEnabled = p._cacheEnabled;
@ -157,14 +157,14 @@ void SGPath::set( const string& p ) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void SGPath::setPermissonChecker(PermissonChecker validator) void SGPath::setPermissonChecker(PermissonChecker validator)
{ {
_permisson_checker = validator; _permission_checker = validator;
_rwCached = false; _rwCached = false;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
SGPath::PermissonChecker SGPath::getPermissonChecker() const SGPath::PermissonChecker SGPath::getPermissonChecker() const
{ {
return _permisson_checker; return _permission_checker;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -348,9 +348,9 @@ void SGPath::checkAccess() const
if( _rwCached && _cacheEnabled ) if( _rwCached && _cacheEnabled )
return; return;
if( _permisson_checker ) if( _permission_checker )
{ {
Permissions p = _permisson_checker(*this); Permissions p = _permission_checker(*this);
_canRead = p.read; _canRead = p.read;
_canWrite = p.write; _canWrite = p.write;
} }
@ -411,7 +411,7 @@ int SGPath::create_dir( mode_t mode ) {
bool absolute = !path.empty() && path[0] == sgDirPathSep; bool absolute = !path.empty() && path[0] == sgDirPathSep;
unsigned int i = 1; 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] ); dir.concat( path_elements[0] );
#ifdef _WIN32 #ifdef _WIN32
if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) { if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
@ -432,7 +432,7 @@ int SGPath::create_dir( mode_t mode ) {
if( !dir.canWrite() ) if( !dir.canWrite() )
{ {
SG_LOG( SG_IO, SG_LOG( SG_IO,
SG_ALERT, "Error creating directory: (" << dir.str() << ")" << SG_WARN, "Error creating directory: (" << dir.str() << ")" <<
" reason: access denied" ); " reason: access denied" );
return -3; return -3;
} }
@ -575,7 +575,7 @@ bool SGPath::operator!=(const SGPath& other) const
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool SGPath::rename(const SGPath& newName) bool SGPath::rename(const SGPath& newName)
{ {
if( !newName.canWrite() ) if( !canRead() || !canWrite() || !newName.canWrite() )
{ {
SG_LOG( SG_IO, SG_WARN, "rename failed: from " << str() << SG_LOG( SG_IO, SG_WARN, "rename failed: from " << str() <<
" to " << newName.str() << " to " << newName.str() <<
@ -600,6 +600,12 @@ bool SGPath::rename(const SGPath& newName)
} }
path = newName.path; 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; _cached = false;
_rwCached = false; _rwCached = false;
@ -611,22 +617,22 @@ SGPath SGPath::fromEnv(const char* name, const SGPath& def)
{ {
const char* val = getenv(name); const char* val = getenv(name);
if( val && val[0] ) if( val && val[0] )
return SGPath(val, def._permisson_checker); return SGPath(val, def._permission_checker);
return def; return def;
} }
#ifdef _WIN32 #ifdef _WIN32
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
SGPath SGPath::home() SGPath SGPath::home(const SGPath& def)
{ {
// TODO // TODO
return SGPath(); return def;
} }
#else #else
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
SGPath SGPath::home() SGPath SGPath::home(const SGPath& def)
{ {
return fromEnv("HOME"); return fromEnv("HOME", def);
} }
#endif #endif
@ -635,7 +641,7 @@ SGPath SGPath::home()
#include <ShlObj.h> // for CSIDL #include <ShlObj.h> // for CSIDL
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
SGPath SGPath::desktop() SGPath SGPath::desktop(const SGPath& def)
{ {
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPSTR, int, BOOL); typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPSTR, int, BOOL);
static GetSpecialFolderPath SHGetSpecialFolderPath = NULL; static GetSpecialFolderPath SHGetSpecialFolderPath = NULL;
@ -647,39 +653,37 @@ SGPath SGPath::desktop()
} }
if (!SHGetSpecialFolderPath){ if (!SHGetSpecialFolderPath){
return SGPath(); return def;
} }
char path[MAX_PATH]; char path[MAX_PATH];
if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, false)) { 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" ); SG_LOG(SG_GENERAL, SG_ALERT, "SGPath::desktop() failed, bad" );
return SGPath(); return def;
} }
#elif __APPLE__ #elif __APPLE__
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
SGPath SGPath::desktop() SGPath SGPath::desktop(const SGPath& def)
{ {
FSRef ref; FSRef ref;
OSErr err = FSFindFolder(kUserDomain, kDesktopFolderType, false, &ref); OSErr err = FSFindFolder(kUserDomain, kDesktopFolderType, false, &ref);
if (err) { if (err)
return SGPath(); return def;
}
unsigned char path[1024]; unsigned char path[1024];
if (FSRefMakePath(&ref, path, 1024) != noErr) { if (FSRefMakePath(&ref, path, 1024) != noErr)
return SGPath(); return def
}
return SGPath((const char*) path); return SGPath((const char*) path, def._permission_checker);
} }
#else #else
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
SGPath SGPath::desktop() SGPath SGPath::desktop(const SGPath& def)
{ {
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
@ -706,12 +710,12 @@ SGPath SGPath::desktop()
const std::string HOME = "$HOME"; const std::string HOME = "$HOME";
if( starts_with(line, 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 #endif

View File

@ -269,12 +269,12 @@ public:
/** /**
* Get path to user's home directory * Get path to user's home directory
*/ */
static SGPath home(); static SGPath home(const SGPath& def = SGPath());
/** /**
* Get path to the user's desktop directory * Get path to the user's desktop directory
*/ */
static SGPath desktop(); static SGPath desktop(const SGPath& def = SGPath());
private: private:
@ -284,7 +284,7 @@ private:
void checkAccess() const; void checkAccess() const;
std::string path; std::string path;
PermissonChecker _permisson_checker; PermissonChecker _permission_checker;
mutable bool _cached : 1; mutable bool _cached : 1;
mutable bool _rwCached : 1; mutable bool _rwCached : 1;