diff --git a/include/os/osDir.h b/include/os/osDir.h index d3597cab36..e7da54bf54 100644 --- a/include/os/osDir.h +++ b/include/os/osDir.h @@ -38,6 +38,7 @@ typedef struct TdDirEntry *TdDirEntryPtr; void taosRemoveDir(const char *dirname); bool taosDirExist(char *dirname); int32_t taosMkDir(const char *dirname); +int32_t taosMulMkDir(const char *dirname); void taosRemoveOldFiles(const char *dirname, int32_t keepDays); int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen); int32_t taosRealPath(char *dirname, int32_t maxlen); diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index df4433e6e4..58bc7235a1 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -616,7 +616,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi taosSetAllDebugFlag(cfgGetItem(pCfg, "debugFlag")->i32); - if (taosMkDir(tsLogDir) != 0) { + if (taosMulMkDir(tsLogDir) != 0) { uError("failed to create dir:%s since %s", tsLogDir, terrstr()); cfgCleanup(pCfg); return -1; diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index b4058b3c0e..a955fb3b0a 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -76,6 +76,47 @@ int32_t taosMkDir(const char *dirname) { return code; } +int32_t taosMulMkDir(const char *dirname) { + if (dirname == NULL) return -1; + char *temp = strdup(dirname); + char *pos = temp; + int32_t code = 0; + + if (strncmp(temp, "/", 1) == 0) { + pos += 1; + } else if (strncmp(temp, "./", 2) == 0) { + pos += 2; + } + + for ( ; *pos != '\0'; pos++) { + if (*pos == '/') { + *pos = '\0'; + code = mkdir(temp, 0755); + if (code < 0 && errno != EEXIST) { + free(temp); + return code; + } + *pos = '/'; + } + } + + if (*(pos - 1) != '/') { + code = mkdir(temp, 0755); + if (code < 0 && errno != EEXIST) { + free(temp); + return code; + } + } + free(temp); + + // int32_t code = mkdir(dirname, 0755); + if (code < 0 && errno == EEXIST) { + return 0; + } + + return code; +} + void taosRemoveOldFiles(const char *dirname, int32_t keepDays) { DIR *dir = opendir(dirname); if (dir == NULL) return;