From: Youngjae Cho Date: Wed, 26 Jul 2023 07:34:36 +0000 (+0900) Subject: libcommon: Add syscommon_mkdir() X-Git-Tag: accepted/tizen/unified/20230731.175325~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=76100deb82b6fbfdc0cf51ad501e6be921c3dfdf;p=platform%2Fcore%2Fsystem%2Flibsyscommon.git libcommon: Add syscommon_mkdir() It makes directory of the given path, including the whole intermediate path if not exists. That is, it is equivalent to command 'mkdir -p'. It has come from the deviced as the function is eligible to being a common function to be utilized by overall system libs/daemons. Change-Id: Ifbf23c3fe721a149c54d6f650cce093dfb6f9e10 Signed-off-by: Youngjae Cho --- diff --git a/include/libsyscommon/common.h b/include/libsyscommon/common.h index f6cd108..14d20de 100644 --- a/include/libsyscommon/common.h +++ b/include/libsyscommon/common.h @@ -24,6 +24,7 @@ #define __SYSCOMMON_COMMON_H__ #include +#include #ifdef __cplusplus extern "C" { @@ -50,6 +51,13 @@ bool syscommon_is_container(void); */ bool syscommon_is_mounted(const char *path); +/** + * @brief Make directory including intermediate directories + * + * @return 0 on succes, otherwise negative error value + */ +int syscommon_mkdir(const char *path, mode_t mode); + #ifdef __cplusplus } #endif diff --git a/src/libcommon/common.c b/src/libcommon/common.c index 0a220c7..116c131 100644 --- a/src/libcommon/common.c +++ b/src/libcommon/common.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "shared/log.h" #include "libsyscommon/common.h" @@ -33,6 +34,8 @@ #define FEATURE_MODEL_NAME_EMULATOR "Emulator" #define CONTAINER_FILE_PATH "/run/systemd/container" +#define PATH_MAX 256 + bool syscommon_is_emulator(void) { int ret = 0; @@ -99,3 +102,32 @@ bool syscommon_is_mounted(const char *path) endmntent(fp); return ret; } + +int syscommon_mkdir(const char *path, mode_t mode) +{ + char dir[PATH_MAX] = { 0, }; + size_t sep, l; + int pos; + int ret; + + if (!path) + return -EINVAL; + + l = strlen(path); + + for (pos = 0, sep = 0; pos < l; pos += sep + 1) { + sep = strcspn(path + pos, "/"); + if (!sep) + continue; + + ret = snprintf(dir, pos + sep + 1, "%s", path); + if (ret < 0) + return ret; + + ret = mkdir(dir, mode); + if (ret < 0 && errno != EEXIST) + return -errno; + } + + return 0; +}