From 3b03341de486c3cd0e4e526dc6852047b22d2e20 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 7 Feb 2023 18:15:55 +0800 Subject: [PATCH] fix: setting logKeepDays to -1 result in old log files not compressed and empty. --- include/os/osFile.h | 2 ++ source/os/CMakeLists.txt | 6 ++++-- source/os/src/osFile.c | 46 ++++++++++++++++++++++++++++++++++++++++ source/util/src/tlog.c | 45 --------------------------------------- 4 files changed, 52 insertions(+), 47 deletions(-) diff --git a/include/os/osFile.h b/include/os/osFile.h index ae77e0f27a..0e93002706 100644 --- a/include/os/osFile.h +++ b/include/os/osFile.h @@ -110,6 +110,8 @@ bool taosValidFile(TdFilePtr pFile); int32_t taosGetErrorFile(TdFilePtr pFile); +int32_t taosCompressFile(char *srcFileName, char *destFileName); + #ifdef __cplusplus } #endif diff --git a/source/os/CMakeLists.txt b/source/os/CMakeLists.txt index 3aac5e9775..bc554c48a3 100644 --- a/source/os/CMakeLists.txt +++ b/source/os/CMakeLists.txt @@ -37,7 +37,9 @@ if(CHECK_STR2INT_ERROR) add_definitions(-DTD_CHECK_STR_TO_INT_ERROR) endif() target_link_libraries( - os PUBLIC pthread + os + PUBLIC pthread + PUBLIC zlibstatic ) if(TD_WINDOWS) target_link_libraries( @@ -63,4 +65,4 @@ ENDIF () if(${BUILD_TEST}) add_subdirectory(test) -endif(${BUILD_TEST}) \ No newline at end of file +endif(${BUILD_TEST}) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 3f527e9130..4cdf4cbdbc 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -15,6 +15,7 @@ #define ALLOW_FORBID_FUNC #include "os.h" #include "osSemaphore.h" +#include "zlib.h" #ifdef WINDOWS #include @@ -830,3 +831,48 @@ bool taosCheckAccessFile(const char *pathname, int32_t tdFileAccessOptions) { } bool taosCheckExistFile(const char *pathname) { return taosCheckAccessFile(pathname, TD_FILE_ACCESS_EXIST_OK); }; + +int32_t taosCompressFile(char *srcFileName, char *destFileName) { + int32_t compressSize = 163840; + int32_t ret = 0; + int32_t len = 0; + char *data = taosMemoryMalloc(compressSize); + gzFile dstFp = NULL; + + TdFilePtr pSrcFile = taosOpenFile(srcFileName, TD_FILE_READ | TD_FILE_STREAM); + if (pSrcFile == NULL) { + ret = -1; + goto cmp_end; + } + + TdFilePtr pFile = taosOpenFile(destFileName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); + if (pFile == NULL) { + ret = -2; + goto cmp_end; + } + + dstFp = gzdopen(pFile->fd, "wb6f"); + if (dstFp == NULL) { + ret = -3; + taosCloseFile(&pFile); + goto cmp_end; + } + + while (!feof(pSrcFile->fp)) { + len = (int32_t)fread(data, 1, compressSize, pSrcFile->fp); + (void)gzwrite(dstFp, data, len); + } + +cmp_end: + if (pSrcFile) { + taosCloseFile(&pSrcFile); + } + + if (dstFp) { + gzclose(dstFp); + } + + taosMemoryFree(data); + + return ret; +} diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 62f074db5b..89dd51a892 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -115,7 +115,6 @@ static int32_t taosPushLogBuffer(SLogBuff *pLogBuf, const char *msg, int32_t m static SLogBuff *taosLogBuffNew(int32_t bufSize); static void taosCloseLogByFd(TdFilePtr pFile); static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum); -static int32_t taosCompressFile(char *srcFileName, char *destFileName); static FORCE_INLINE void taosUpdateDaylight() { struct tm Tm, *ptm; @@ -748,50 +747,6 @@ static void *taosAsyncOutputLog(void *param) { return NULL; } -int32_t taosCompressFile(char *srcFileName, char *destFileName) { - int32_t compressSize = 163840; - int32_t ret = 0; - int32_t len = 0; - char *data = taosMemoryMalloc(compressSize); - // gzFile dstFp = NULL; - - // srcFp = fopen(srcFileName, "r"); - TdFilePtr pSrcFile = taosOpenFile(srcFileName, TD_FILE_READ); - if (pSrcFile == NULL) { - ret = -1; - goto cmp_end; - } - - TdFilePtr pFile = taosOpenFile(destFileName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); - if (pFile == NULL) { - ret = -2; - goto cmp_end; - } - - // dstFp = gzdopen(fd, "wb6f"); - // if (dstFp == NULL) { - // ret = -3; - // close(fd); - // goto cmp_end; - // } - // - // while (!feof(srcFp)) { - // len = (int32_t)fread(data, 1, compressSize, srcFp); - // (void)gzwrite(dstFp, data, len); - // } - -cmp_end: - if (pSrcFile) { - taosCloseFile(&pSrcFile); - } - // if (dstFp) { - // gzclose(dstFp); - // } - taosMemoryFree(data); - - return ret; -} - bool taosAssertDebug(bool condition, const char *file, int32_t line, const char *format, ...) { if (condition) return false;