diff --git a/src/gui/themes.cpp b/src/gui/themes.cpp index 996b522e1..bef79a397 100644 --- a/src/gui/themes.cpp +++ b/src/gui/themes.cpp @@ -161,10 +161,10 @@ int CThemes::Show() CKeyboardInput nameInput(LOCALE_COLORTHEMEMENU_NAME, &file_name); CMenuForwarder *m1 = new CMenuForwarder(LOCALE_COLORTHEMEMENU_SAVE, true , NULL, &nameInput, NULL, CRCInput::RC_green); - if (CFileHelpers::createDir(THEMESDIR_VAR) && errno != EEXIST) { + if (!CFileHelpers::createDir(THEMESDIR_VAR)) { printf("[neutrino theme] error creating %s\n", THEMESDIR_VAR); - } + if (access(THEMESDIR_VAR, F_OK) == 0 ) { themes.addItem(GenericMenuSeparatorLine); themes.addItem(m1); @@ -365,7 +365,7 @@ void CThemes::move_userDir() { if (access(USERDIR, F_OK) == 0) { - if (CFileHelpers::createDir(THEMESDIR_VAR) && errno != EEXIST) + if (!CFileHelpers::createDir(THEMESDIR_VAR)) { printf("[neutrino theme] error creating %s\n", THEMESDIR_VAR); return; diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index f00d282f7..56983c9d0 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -629,11 +629,9 @@ bool CFileHelpers::copyDir(const char *Src, const char *Dst, bool backupMode) } else { // directory - if (createDir(Dst, FileInfo.st_mode & 0x0FFF) == false) { - if (errno != EEXIST) { - closedir(Directory); - return false; - } + if (!createDir(Dst, FileInfo.st_mode & 0x0FFF)) { + closedir(Directory); + return false; } } @@ -676,25 +674,34 @@ bool CFileHelpers::copyDir(const char *Src, const char *Dst, bool backupMode) return true; } -int CFileHelpers::createDir(string& Dir, mode_t mode) +// returns: true - success. +// false - errno is set +bool CFileHelpers::createDir(string& Dir, mode_t mode) { - struct stat st; int res = 0; for(string::iterator iter = Dir.begin() ; iter != Dir.end();) { string::iterator newIter = find(iter, Dir.end(), '/' ); string newPath = string( Dir.begin(), newIter ); - if( !newPath.empty() && stat(newPath.c_str(), &st) != 0) { + if(!newPath.empty() && !file_exists(newPath.c_str())) { res = mkdir( newPath.c_str(), mode); - if (errno == EEXIST) - res = 0; - if(res != 0) - dprintf(DEBUG_NORMAL, "[CFileHelpers %s] creating directory %s: %s\n", __func__, newPath.c_str(), strerror(errno)); + if (res == -1) { + if (errno == EEXIST) { + res = 0; + } else { + // We can assume that if an error + // occured, following will fail too, + // so break here. + dprintf(DEBUG_NORMAL, "[CFileHelpers %s] creating directory %s: %s\n", __func__, newPath.c_str(), strerror(errno)); + break; + } + } } iter = newIter; if(newIter != Dir.end()) ++ iter; } - return res; + + return (res == 0 ? true : false); } bool CFileHelpers::removeDir(const char *Dir) diff --git a/src/system/helpers.h b/src/system/helpers.h index bfb4c5eca..205a4f30a 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -88,8 +88,8 @@ class CFileHelpers bool copyFile(const char *Src, const char *Dst, mode_t mode); bool copyDir(const char *Src, const char *Dst, bool backupMode=false); - static int createDir(std::string& Dir, mode_t mode = 755); - static int createDir(const char *Dir, mode_t mode = 755){std::string dir = std::string(Dir);return createDir(dir, mode);} + static bool createDir(std::string& Dir, mode_t mode = 755); + static bool createDir(const char *Dir, mode_t mode = 755){std::string dir = std::string(Dir);return createDir(dir, mode);} static bool removeDir(const char *Dir); static uint64_t getDirSize(const char *dir); static uint64_t getDirSize(const std::string& dir){return getDirSize(dir.c_str());};