SGPath: fix creating paths with permission checker.

Check permission of the whole path and not to individual
directories, as none of the intermediate directories need
to be writeable by IORules.
This commit is contained in:
Thomas Geymayer
2014-06-26 16:07:33 +02:00
parent 49bcf49db1
commit 5126ae5891
2 changed files with 36 additions and 30 deletions
+32 -29
View File
@@ -477,14 +477,23 @@ bool SGPath::isFile() const
return _exists && _isFile; return _exists && _isFile;
} }
//------------------------------------------------------------------------------
#ifdef _WIN32 #ifdef _WIN32
# define sgMkDir(d,m) _mkdir(d) # define sgMkDir(d,m) _mkdir(d)
#else #else
# define sgMkDir(d,m) mkdir(d,m) # define sgMkDir(d,m) mkdir(d,m)
#endif #endif
int SGPath::create_dir(mode_t mode)
{
if( !canWrite() )
{
SG_LOG( SG_IO,
SG_WARN, "Error creating directory for '" << str() << "'"
" reason: access denied" );
return -3;
}
int SGPath::create_dir( mode_t mode ) {
string_list dirlist = sgPathSplit(dir()); string_list dirlist = sgPathSplit(dir());
if ( dirlist.empty() ) if ( dirlist.empty() )
return -1; return -1;
@@ -501,37 +510,31 @@ int SGPath::create_dir( mode_t mode ) {
i = 2; i = 2;
} }
#endif #endif
struct stat info; struct stat info;
int r; int r;
for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) { for(; (r = stat(dir.c_str(), &info)) == 0 && i < path_elements.size(); ++i)
dir.append(path_elements[i]); dir.append(path_elements[i]);
} if( r == 0 )
if ( r == 0 ) { return 0; // Directory already exists
return 0; // Directory already exists
} for(;;)
for(;;) {
if( sgMkDir(dir.c_str(), mode) )
{ {
if( !dir.canWrite() ) SG_LOG( SG_IO,
{ SG_ALERT, "Error creating directory: (" << dir.str() << ")" );
SG_LOG( SG_IO, return -2;
SG_WARN, "Error creating directory: (" << dir.str() << ")" <<
" reason: access denied" );
return -3;
}
else if( sgMkDir(dir.c_str(), mode) )
{
SG_LOG( SG_IO,
SG_ALERT, "Error creating directory: (" << dir.str() << ")" );
return -2;
}
if( i >= path_elements.size() )
return 0;
dir.append(path_elements[i++]);
} }
else
SG_LOG(SG_IO, SG_DEBUG, "Directory created: " << dir.str());
return 0; if( i >= path_elements.size() )
return 0;
dir.append(path_elements[i++]);
}
return 0;
} }
string_list sgPathBranchSplit( const string &dirpath ) { string_list sgPathBranchSplit( const string &dirpath ) {
+4 -1
View File
@@ -206,9 +206,12 @@ public:
/** /**
* Create the designated directory. * Create the designated directory.
*
* @param mode Permissions. See:
* http://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation
* @return 0 on success, or <0 on failure. * @return 0 on success, or <0 on failure.
*/ */
int create_dir(mode_t mode); int create_dir(mode_t mode = 0755);
/** /**
* Check if reading file is allowed. Readabilty does not imply the existance * Check if reading file is allowed. Readabilty does not imply the existance