From 13d2d72bb61a0d504cb7738241dc802a1597275d Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 24 Aug 2023 14:56:14 +0800 Subject: [PATCH] add seperate function for createLogs --- include/os/osDir.h | 3 +- source/common/src/tglobal.c | 2 +- source/libs/stream/src/streamMeta.c | 4 +- source/libs/stream/src/streamState.c | 2 +- source/libs/tdb/src/db/tdbDb.c | 2 +- source/os/src/osDir.c | 71 +++++++++++++++++++++++++--- 6 files changed, 71 insertions(+), 13 deletions(-) diff --git a/include/os/osDir.h b/include/os/osDir.h index 2542f9830f..e722adcdcc 100644 --- a/include/os/osDir.h +++ b/include/os/osDir.h @@ -83,7 +83,8 @@ void taosRemoveDir(const char *dirname); bool taosDirExist(const char *dirname); int32_t taosMkDir(const char *dirname); int32_t taosMulMkDir(const char *dirname); -int32_t taosMulModeMkDir(const char *dirname, int mode, bool createLogFile); +int32_t taosMulModeMkDir(const char *dirname, int mode); +int32_t taosMulModeMkLogDir(const char *dirname, int mode); void taosRemoveOldFiles(const char *dirname, int32_t keepDays); int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen); int32_t taosRealPath(char *dirname, char *realPath, int32_t maxlen); diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 3edb70e63f..611b88bc9d 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -1434,7 +1434,7 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi taosSetAllDebugFlag(cfgGetItem(pCfg, "debugFlag")->i32, false); - if (taosMulModeMkDir(tsLogDir, 0777, true) != 0) { + if (taosMulModeMkLogDir(tsLogDir, 0777) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); printf("failed to create dir:%s since %s", tsLogDir, terrstr()); cfgCleanup(pCfg); diff --git a/source/libs/stream/src/streamMeta.c b/source/libs/stream/src/streamMeta.c index 61da055b25..fe455c0190 100644 --- a/source/libs/stream/src/streamMeta.c +++ b/source/libs/stream/src/streamMeta.c @@ -52,7 +52,7 @@ SStreamMeta* streamMetaOpen(const char* path, void* ahandle, FTaskExpand expandF memset(streamPath, 0, len); sprintf(streamPath, "%s/%s", pMeta->path, "checkpoints"); - code = taosMulModeMkDir(streamPath, 0755, false); + code = taosMulModeMkDir(streamPath, 0755); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); goto _err; @@ -90,7 +90,7 @@ SStreamMeta* streamMetaOpen(const char* path, void* ahandle, FTaskExpand expandF memset(streamPath, 0, len); sprintf(streamPath, "%s/%s", pMeta->path, "state"); - code = taosMulModeMkDir(streamPath, 0755, false); + code = taosMulModeMkDir(streamPath, 0755); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); goto _err; diff --git a/source/libs/stream/src/streamState.c b/source/libs/stream/src/streamState.c index 8694e5cf4c..5b42be182c 100644 --- a/source/libs/stream/src/streamState.c +++ b/source/libs/stream/src/streamState.c @@ -169,7 +169,7 @@ SStreamState* streamStateOpen(char* path, void* pTask, bool specPath, int32_t sz sscanf(cfg, "%d\n%d\n", &szPage, &pages); } } else { - int32_t code = taosMulModeMkDir(statePath, 0755, false); + int32_t code = taosMulModeMkDir(statePath, 0755); if (code == 0) { pCfgFile = taosOpenFile(cfgPath, TD_FILE_WRITE | TD_FILE_CREATE); sprintf(cfg, "%d\n%d\n", szPage, pages); diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index 81b306e65d..4f595d8d4a 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -62,7 +62,7 @@ int32_t tdbOpen(const char *dbname, int32_t szPage, int32_t pages, TDB **ppDb, i } memset(pDb->pgrHash, 0, tsize); - ret = taosMulModeMkDir(dbname, 0755, false); + ret = taosMulModeMkDir(dbname, 0755); if (ret < 0) { return -1; } diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index e9f8c7f7e6..dff0cf9886 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -193,7 +193,7 @@ int32_t taosMulMkDir(const char *dirname) { return code; } -int32_t taosMulModeMkDir(const char *dirname, int mode, bool createLogFile) { +int32_t taosMulModeMkDir(const char *dirname, int mode) { if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return -1; char temp[TDDIRMAXLEN]; char *pos = temp; @@ -206,16 +206,73 @@ int32_t taosMulModeMkDir(const char *dirname, int mode, bool createLogFile) { #endif if (taosDirExist(temp)) { - if (createLogFile) { - if (!taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) { - code = -1; + return chmod(temp, mode); + } + + if (strncmp(temp, TD_DIRSEP, 1) == 0) { + pos += 1; + } else if (strncmp(temp, "." TD_DIRSEP, 2) == 0) { + pos += 2; + } + + for (; *pos != '\0'; pos++) { + if (*pos == TD_DIRSEP[0]) { + *pos = '\0'; +#ifdef WINDOWS + code = _mkdir(temp, mode); +#elif defined(DARWIN) + code = mkdir(dirname, 0777); +#else + code = mkdir(temp, mode); +#endif + if (code < 0 && errno != EEXIST) { + // terrno = TAOS_SYSTEM_ERROR(errno); + return code; } - return code; - } else { - return chmod(temp, mode); + *pos = TD_DIRSEP[0]; } } + if (*(pos - 1) != TD_DIRSEP[0]) { +#ifdef WINDOWS + code = _mkdir(temp, mode); +#elif defined(DARWIN) + code = mkdir(dirname, 0777); +#else + code = mkdir(temp, mode); +#endif + if (code < 0 && errno != EEXIST) { + // terrno = TAOS_SYSTEM_ERROR(errno); + return code; + } + } + + if (code < 0 && errno == EEXIST) { + return chmod(temp, mode); + } + + return chmod(temp, mode); +} + +int32_t taosMulModeMkLogDir(const char *dirname, int mode) { + if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return -1; + char temp[TDDIRMAXLEN]; + char *pos = temp; + int32_t code = 0; +#ifdef WINDOWS + taosRealPath(dirname, temp, sizeof(temp)); + if (temp[1] == ':') pos += 3; +#else + strcpy(temp, dirname); +#endif + + if (taosDirExist(temp)) { + if (!taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) { + code = -1; + } + return code; + } + if (strncmp(temp, TD_DIRSEP, 1) == 0) { pos += 1; } else if (strncmp(temp, "." TD_DIRSEP, 2) == 0) {