diff --git a/src/gui/themes.cpp b/src/gui/themes.cpp index 4900194e5..60963cc9b 100644 --- a/src/gui/themes.cpp +++ b/src/gui/themes.cpp @@ -163,8 +163,9 @@ 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 (mkdirhier(THEMEDIR_VAR) && errno != EEXIST) { + if (CFileHelpers::createDir(THEMEDIR_VAR) && errno != EEXIST) { printf("[neutrino theme] error creating %s\n", THEMEDIR_VAR); + } if (access(THEMEDIR_VAR, F_OK) == 0 ) { themes.addItem(GenericMenuSeparatorLine); @@ -343,7 +344,7 @@ void CThemes::move_userDir() { if (access(USERDIR, F_OK) == 0) { - if (mkdirhier(THEMEDIR_VAR) && errno != EEXIST) + if (CFileHelpers::createDir(THEMEDIR_VAR) && errno != EEXIST) { printf("[neutrino theme] error creating %s\n", THEMEDIR_VAR); return; diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index 4280c0913..b8dff1eec 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -38,12 +38,14 @@ #include #include #include +#include #include #include #include - +#include "debug.h" #include #include +using namespace std; void mySleep(int sec) { struct timeval timeout; @@ -215,7 +217,7 @@ FILE* my_popen( pid_t& pid, const char *cmdstring, const char *type) } return(fp); } - +#if 0 int mkdirhier(const char *pathname, mode_t mode) { int res = -1; @@ -236,7 +238,7 @@ int mkdirhier(const char *pathname, mode_t mode) res = 0; return res; } - +# endif int safe_mkdir(const char * path) { @@ -671,32 +673,25 @@ bool CFileHelpers::copyDir(const char *Src, const char *Dst, bool backupMode) return true; } -bool CFileHelpers::createDir(const char *Dir, mode_t mode) +int CFileHelpers::createDir(string& Dir, mode_t mode) { - char dirPath[PATH_MAX]; - DIR *dir; - if ((dir = opendir(Dir)) != NULL) { - closedir(dir); - errno = EEXIST; - return false; - } - - int ret = -1; - while (ret == -1) { - strcpy(dirPath, Dir); - ret = mkdir(dirPath, mode); - if ((errno == ENOENT) && (ret == -1)) { - char * pos = strrchr(dirPath,'/'); - if (pos != NULL) { - pos[0] = '\0'; - createDir(dirPath, 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) { + 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)); } - else - return !ret || (errno == EEXIST); + iter = newIter; + if(newIter != Dir.end()) + ++ iter; } - errno = 0; - return true; + return res; } bool CFileHelpers::removeDir(const char *Dir) diff --git a/src/system/helpers.h b/src/system/helpers.h index afa14d132..b9074331b 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -44,8 +44,8 @@ FILE* my_popen( pid_t& pid, const char *cmdstring, const char *type); int safe_mkdir(const char * path); inline int safe_mkdir(std::string path) { return safe_mkdir(path.c_str()); } -int mkdirhier(const char *pathname, mode_t mode = 0755); -inline int mkdirhier(std::string path, mode_t mode = 0755) { return mkdirhier(path.c_str(), mode); } +//int mkdirhier(const char *pathname, mode_t mode = 0755); +//inline int mkdirhier(std::string path, mode_t mode = 0755) { return mkdirhier(path.c_str(), mode); } off_t file_size(const char *filename); bool file_exists(const char *filename); void wakeup_hdd(const char *hdd_dir); @@ -88,7 +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); - bool createDir(const char *Dir, mode_t mode); + 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);} bool removeDir(const char *Dir); };