From 2e329426ab08ce372d9c11c635b2c2fded939e87 Mon Sep 17 00:00:00 2001 From: ehofman Date: Sat, 17 Dec 2005 22:12:53 +0000 Subject: [PATCH] Frederic Bouvier: The create_dir was totally broken. No function was used at the right place except mkdir. This patch now create directories without segfaulting. Erik: This was my bad, I've been using a really slow computer for the last ten months and recompiling SimGear with a change to the properties code takes ages, so once in a while I apply something not entirely tested. This is one really bad example which shouldn't have happened. Thanks to Frederic for fixing it. --- simgear/misc/sg_path.cxx | 49 ++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 3ae10a73..4f709d1c 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -187,21 +187,50 @@ bool SGPath::exists() const { return true; } +#ifdef _MSC_VER +# define sgMkDir(d,m) _mkdir(d) +#else +# define sgMkDir(d,m) mkdir(d,m) +#endif + void SGPath::create_dir( mode_t mode ) { string_list dirlist = sgPathSplit(dir()); - SGPath dir = dirlist[0]; - int i; - for(i=1; dir.exists() && i < dirlist.size(); i++) { - dir.add(dirlist[i]); + string path = dirlist[0]; + string_list path_elements; + string element; + while ( path.size() ) { + size_t p = path.find( sgDirPathSep ); + if ( p != string::npos ) { + element = path.substr( 0, p ); + path.erase( 0, p + 1 ); + } else { + element = path; + path = ""; + } + if ( element.size() ) + path_elements.push_back( element ); + } + + int i = 1; + SGPath dir = path_elements[0]; +#ifdef WIN32 + if ( path_elements.size() >= 2 ) { + dir.append( path_elements[1] ); + i = 2; } - for(;i < dirlist.size(); i++) { - dir.add(dirlist[i]); -#ifdef _MSC_VER - if ( _mkdir( subdir.c_str()) ) { -#else - if ( mkdir( subdir.c_str(), mode) ) { #endif + struct stat info; + for(; stat( dir.c_str(), &info ) == 0 && i < path_elements.size(); i++) { + dir.append(path_elements[i]); + } + if ( sgMkDir( dir.c_str(), mode) ) { + SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() ); + return; + } + for(;i < path_elements.size(); i++) { + dir.append(path_elements[i]); + if ( sgMkDir( dir.c_str(), mode) ) { SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() ); break; } -- 2.39.5