From 5126ae5891f62cb95ed268b9668e14928fa58f43 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Thu, 26 Jun 2014 18:04:11 +0200 Subject: [PATCH] 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. --- simgear/misc/sg_path.cxx | 61 +++++++++++++++++++++------------------- simgear/misc/sg_path.hxx | 5 +++- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 3c6e5b6b..7895e68c 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -477,14 +477,23 @@ bool SGPath::isFile() const return _exists && _isFile; } +//------------------------------------------------------------------------------ #ifdef _WIN32 # define sgMkDir(d,m) _mkdir(d) #else # define sgMkDir(d,m) mkdir(d,m) #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()); if ( dirlist.empty() ) return -1; @@ -501,37 +510,31 @@ int SGPath::create_dir( mode_t mode ) { i = 2; } #endif - struct stat info; - int r; - for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) { - dir.append(path_elements[i]); - } - if ( r == 0 ) { - return 0; // Directory already exists - } - for(;;) + struct stat info; + int r; + for(; (r = stat(dir.c_str(), &info)) == 0 && i < path_elements.size(); ++i) + dir.append(path_elements[i]); + if( r == 0 ) + return 0; // Directory already exists + + for(;;) + { + if( sgMkDir(dir.c_str(), mode) ) { - if( !dir.canWrite() ) - { - SG_LOG( SG_IO, - 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++]); + SG_LOG( SG_IO, + SG_ALERT, "Error creating directory: (" << dir.str() << ")" ); + return -2; } + else + SG_LOG(SG_IO, SG_DEBUG, "Directory created: " << dir.str()); + + if( i >= path_elements.size() ) + return 0; + + dir.append(path_elements[i++]); + } - return 0; + return 0; } string_list sgPathBranchSplit( const string &dirpath ) { diff --git a/simgear/misc/sg_path.hxx b/simgear/misc/sg_path.hxx index cce05ebc..3e5c22fe 100644 --- a/simgear/misc/sg_path.hxx +++ b/simgear/misc/sg_path.hxx @@ -206,9 +206,12 @@ public: /** * 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. */ - 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 -- 2.39.5