Skip to content
Snippets Groups Projects
Commit a681a89d authored by niikonst's avatar niikonst
Browse files

fix UbSystem::makeDirectory() for Linux

parent 47f22646
No related branches found
No related tags found
1 merge request!240fix UbSystem::makeDirectory() for Linux
...@@ -68,7 +68,7 @@ UbFileOutputBinary::UbFileOutputBinary(const string &filename, UbFileOutput::CRE ...@@ -68,7 +68,7 @@ UbFileOutputBinary::UbFileOutputBinary(const string &filename, UbFileOutput::CRE
string path = UbSystem::getPathFromString(filename); string path = UbSystem::getPathFromString(filename);
if (path.size() > 0) { if (path.size() > 0) {
outfile.clear(); // flags ruecksetzen (ansonsten liefert utern if(!outfile) weiterhin true!!! outfile.clear(); // flags ruecksetzen (ansonsten liefert utern if(!outfile) weiterhin true!!!
UbSystem::makeDirectory(path, 20); UbSystem::makeDirectory(path);
this->open(filename, opt); this->open(filename, opt);
} }
......
...@@ -276,24 +276,26 @@ inline bool isDirectory(const std::string &dir, const unsigned & /*attemptions*/ ...@@ -276,24 +276,26 @@ inline bool isDirectory(const std::string &dir, const unsigned & /*attemptions*/
#if defined(CAB_BOOST) #if defined(CAB_BOOST)
static boost::mutex mtx_makeDirectory; static boost::mutex mtx_makeDirectory;
#endif #endif
inline bool makeDirectory(const std::string &dir, const unsigned &attemptions = 3) inline bool makeDirectory(const std::string &dir)
{ {
UBLOG(logDEBUG5, "UbSystem::makeDirectory - start, dir=" << dir << " #attemptions=" << attemptions); UBLOG(logDEBUG5, "UbSystem::makeDirectory - start, dir=" << dir);
if (dir.empty()) if (dir.empty())
UB_THROW(UbException(UB_EXARGS, "dir is empty")); UB_THROW(UbException(UB_EXARGS, "dir is empty"));
std::string path = UbSystem::replaceInString(dir, "\\", "/"); std::string path = UbSystem::replaceInString(dir, "\\", "/");
bool dirCreated = true; bool dirCreated = true;
#if defined UBSYSTEM_WINDOWS
if (path[path.size() - 1] != '/') if (path[path.size() - 1] != '/')
path += "/"; path += "/";
size_t pos = 0; size_t pos = 0;
while ((pos = path.find("/", pos + 1)) != std::string::npos) { while ((pos = path.find("/", pos + 1)) != std::string::npos) {
std::string tmpdir = path.substr(0, pos); std::string tmpdir = path.substr(0, pos);
#if defined(CAB_BOOST) #if defined(CAB_BOOST)
boost::mutex::scoped_lock lock(mtx_makeDirectory); boost::mutex::scoped_lock lock(mtx_makeDirectory);
#endif #endif
#if defined UBSYSTEM_WINDOWS
if ( if (
#ifndef _UNICODE #ifndef _UNICODE
_access(tmpdir.c_str(), 0) == -1 && _mkdir(tmpdir.c_str()) == -1 _access(tmpdir.c_str(), 0) == -1 && _mkdir(tmpdir.c_str()) == -1
...@@ -301,34 +303,23 @@ inline bool makeDirectory(const std::string &dir, const unsigned &attemptions = ...@@ -301,34 +303,23 @@ inline bool makeDirectory(const std::string &dir, const unsigned &attemptions =
_waccess(tmpdir.c_str(), 0) == -1 && _wmkdir(tmpdir.c_str()) == -1 _waccess(tmpdir.c_str(), 0) == -1 && _wmkdir(tmpdir.c_str()) == -1
#endif #endif
) { ) {
UBLOG(logDEBUG5, "UbSystem::makeDirectory- dir=\"" << tmpdir << "\" doesn't exit or makedir failed"); UBLOG(logDEBUG5, "UbSystem::makeDirectory - dir=\"" << tmpdir << "\" - doesn't exist or makedir failed");
dirCreated = false; dirCreated = false;
break; break;
} }
}
#elif defined(UBSYSTEM_LINUX) || defined(UBSYSTEM_APPLE) || defined(UBSYSTEM_AIX) #elif defined(UBSYSTEM_LINUX) || defined(UBSYSTEM_APPLE) || defined(UBSYSTEM_AIX)
std::string command = "mkdir -p \"" + path + "\""; int status = mkdir(tmpdir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
{ if (status == 0) {
#if defined(CAB_BOOST) UBLOG(logDEBUG5,"UbSystem::makeDirectory - dir=\"" << tmpdir << " - directory created successfully.");
boost::mutex::scoped_lock lock(mtx_makeDirectory); dirCreated = true;
#endif } else {
if (system(command.c_str()) != 0) { UBLOG(logDEBUG5,"UbSystem::makeDirectory - dir=\"" << tmpdir << " - mkdir() failed" << " ERROR: " << strerror(errno));
UBLOG(logDEBUG5, "UbSystem::makeDirectory- dir=\"" << path << "\" doesn't exit or makedir failed");
dirCreated = false; dirCreated = false;
} }
}
#else #else
#error "UbSystem::makeDirectory - UnknownMachine" #error "UbSystem::makeDirectory - UnknownMachine"
#endif #endif
if (!dirCreated && attemptions > 1) {
UBLOG(logDEBUG5, "UbSystem::makeDirectory - internal call of UbSystem::makeDirectory");
UbSystem::sleepMs(500);
dirCreated = UbSystem::makeDirectory(path, attemptions - 1);
} }
UBLOG(logDEBUG5,
"UbSystem::makeDirectory - end (success=" << dirCreated << ", attemptions = " << attemptions << ")");
return dirCreated; return dirCreated;
} }
/*==========================================================*/ /*==========================================================*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment