From 67ea8dd6502d0c1104e271056fef42bb82210b51 Mon Sep 17 00:00:00 2001 From: martii Date: Mon, 6 Jan 2014 12:44:45 +0100 Subject: [PATCH] system/helpers: add mkdirhier(); gui/themes: use mkdirhier() Conflicts: src/system/helpers.h Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/431657e35d6d985dfcf7a5bdffa1aaf0fbc0b477 Author: martii Date: 2014-01-06 (Mon, 06 Jan 2014) --- src/gui/themes.cpp | 11 ++++------- src/system/helpers.cpp | 22 ++++++++++++++++++++++ src/system/helpers.h | 5 +++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/gui/themes.cpp b/src/gui/themes.cpp index 4f864b1ba..9570284c5 100644 --- a/src/gui/themes.cpp +++ b/src/gui/themes.cpp @@ -32,10 +32,12 @@ #include #include #include +#include #include #include #include #include "widget/menue.h" +#include #include #include #include @@ -157,13 +159,8 @@ int CThemes::Show() CStringInputSMS nameInput(LOCALE_COLORTHEMEMENU_NAME, &file_name, 30, NONEXISTANT_LOCALE, NONEXISTANT_LOCALE, "abcdefghijklmnopqrstuvwxyz0123456789- "); CMenuForwarder *m1 = new CMenuForwarder(LOCALE_COLORTHEMEMENU_SAVE, true , NULL, &nameInput, NULL, CRCInput::RC_green, NEUTRINO_ICON_BUTTON_GREEN); - // Don't show SAVE if UserDir does'nt exist - if ( access(USERDIR, F_OK) != 0 ) { // check for existance - // mkdir must be called for each subdir which does not exist - // mkdir (USERDIR, S_IRUSR | S_IREAD | S_IWUSR | S_IWRITE | S_IXUSR | S_IEXEC) == 0) { - if (system (((std::string)"mkdir -p " + USERDIR).c_str()) != 0) { - printf("[neutrino theme] error creating %s\n", USERDIR); - } + if (mkdirhier(USERDIR) && errno != EEXIST) { + printf("[neutrino theme] error creating %s\n", USERDIR); } if (access(USERDIR, F_OK) == 0 ) { themes.addItem(GenericMenuSeparatorLine); diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index e05e93c03..e6548be4b 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -213,6 +213,28 @@ FILE* my_popen( pid_t& pid, const char *cmdstring, const char *type) return(fp); } +int mkdirhier(const char *pathname, mode_t mode) +{ + int res = -1; + if (!pathname || !*pathname) + return res; + char path[strlen(pathname) + 1]; + strcpy(path, pathname); + char *p = path; + while ((p = strchr(p + 1, '/'))) { + *p = 0; + res = mkdir(path, mode); + if (res < 0 && errno != EEXIST) + break; + *p = '/'; + } + res = mkdir(path, mode); + if (errno == EEXIST) + res = 0; + return res; +} + + int safe_mkdir(const char * path) { struct statfs s; diff --git a/src/system/helpers.h b/src/system/helpers.h index 6ea1b64da..6726e941a 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -34,6 +34,9 @@ #include #include +#include +#include + int my_system(const char * cmd); int my_system(int argc, const char *arg, ...); /* argc is number of arguments including command */ @@ -41,6 +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); } off_t file_size(const char *filename); bool file_exists(const char *filename); void wakeup_hdd(const char *hdd_dir);