diff --git a/src/osgPlugins/net/makeDir.cpp b/src/osgPlugins/net/makeDir.cpp new file mode 100644 index 000000000..4a38fea90 --- /dev/null +++ b/src/osgPlugins/net/makeDir.cpp @@ -0,0 +1,77 @@ +// +// This should move to osgDB::FileUtils +// + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include "makeDir.h" + +namespace TemporaryFileUtils { + +bool makeDirectory( const std::string &path ) +{ + char *cpath = new char[path.length()+1]; + strcpy( cpath, path.c_str()); + char *p = dirname(cpath); + struct stat stbuf; + + if( stat( p, &stbuf ) == 0 ) + { + if( S_ISDIR(stbuf.st_mode)) + return true; + else + { + std::cerr << "TemporaryFileUtils::makeDirectory() - "<< p + << " already exists and is not a directory!" << std::endl; + return false; + } + } + + std::stack paths; + while( true ) + { + if( p[0] == '.' ) + break; + + if( stat( p, &stbuf ) < 0 ) + { + switch( errno ) + { + case ENOENT: + case ENOTDIR: + paths.push( std::string(p) ); + break; + + default: + perror( p ); + return false; + } + } + p = dirname(p); + } ; + + while( !paths.empty() ) + { + std::string dir = paths.top(); + + if( mkdir( dir.c_str(), 0755 )< 0 ) + { + perror( dir.c_str() ); + return false; + } + + paths.pop(); + } + return true; +} + +} diff --git a/src/osgPlugins/net/makeDir.h b/src/osgPlugins/net/makeDir.h new file mode 100644 index 000000000..1901f8722 --- /dev/null +++ b/src/osgPlugins/net/makeDir.h @@ -0,0 +1,11 @@ +#ifndef TEMPORARY_MAKE_DIR_H +#define TEMPORARY_MAKE_DIR_H + +#include + +namespace TemporaryFileUtils { + +extern bool makeDirectory( const std::string & ); + +} +#endif