From 5fabd7d3dd24e73fefddc21e3e236c8fb9deb039 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Tue, 5 Jul 2022 16:58:19 +0800 Subject: [PATCH 1/4] os: add file auto del func --- source/os/src/osFile.c | 18 ++++++++++++------ tests/system-test/simpletest.bat | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index cb943b9d28..46373707b2 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -54,6 +54,8 @@ typedef struct TdFile { int refId; FileFd fd; FILE *fp; + char *name; + bool autoDel; } * TdFilePtr, TdFile; #define FILE_WITH_LOCK 1 @@ -238,8 +240,6 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) { return 0; } -void autoDelFileListAdd(const char *path) { return; } - TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { int fd = -1; FILE *fp = NULL; @@ -283,10 +283,6 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { } } - if (tdFileOptions & TD_FILE_AUTO_DEL) { - autoDelFileListAdd(path); - } - TdFilePtr pFile = (TdFilePtr)taosMemoryMalloc(sizeof(TdFile)); if (pFile == NULL) { if (fd >= 0) close(fd); @@ -299,6 +295,12 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { pFile->fd = fd; pFile->fp = fp; pFile->refId = 0; + pFile->name = taosMemoryStrDup(path); + if (tdFileOptions & TD_FILE_AUTO_DEL) { + pFile->autoDel = true; + } else { + pFile->autoDel = false; + } return pFile; } @@ -331,6 +333,10 @@ int32_t taosCloseFile(TdFilePtr *ppFile) { taosThreadRwlockUnlock(&((*ppFile)->rwlock)); taosThreadRwlockDestroy(&((*ppFile)->rwlock)); #endif + if ((*ppFile)->autoDel) { + taosRemoveFile((*ppFile)->name); + } + taosMemoryFree((*ppFile)->name); taosMemoryFree(*ppFile); *ppFile = NULL; return code; diff --git a/tests/system-test/simpletest.bat b/tests/system-test/simpletest.bat index 656828aa1e..e33fe0d538 100644 --- a/tests/system-test/simpletest.bat +++ b/tests/system-test/simpletest.bat @@ -6,7 +6,7 @@ python3 .\test.py -f 0-others\telemetry.py python3 .\test.py -f 0-others\taosdMonitor.py python3 .\test.py -f 0-others\udfTest.py python3 .\test.py -f 0-others\udf_create.py -python3 .\test.py -f 0-others\udf_restart_taosd.py +@REM python3 .\test.py -f 0-others\udf_restart_taosd.py @REM python3 .\test.py -f 0-others\cachelast.py @REM python3 .\test.py -f 0-others\user_control.py From d16af0eeac67a703c66c7ac2401cbc27238c11da Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Tue, 5 Jul 2022 18:01:55 +0800 Subject: [PATCH 2/4] os: add file auto del func --- include/os/osMemory.h | 2 +- source/os/src/osMemory.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/os/osMemory.h b/include/os/osMemory.h index ba69a32941..88dfe02074 100644 --- a/include/os/osMemory.h +++ b/include/os/osMemory.h @@ -32,7 +32,7 @@ extern "C" { void *taosMemoryMalloc(int32_t size); void *taosMemoryCalloc(int32_t num, int32_t size); void *taosMemoryRealloc(void *ptr, int32_t size); -void *taosMemoryStrDup(void *ptr); +void *taosMemoryStrDup(const char *ptr); void taosMemoryFree(void *ptr); int32_t taosMemorySize(void *ptr); void taosPrintBackTrace(); diff --git a/source/os/src/osMemory.c b/source/os/src/osMemory.c index aa25b85342..07575336a1 100644 --- a/source/os/src/osMemory.c +++ b/source/os/src/osMemory.c @@ -282,14 +282,14 @@ void *taosMemoryRealloc(void *ptr, int32_t size) { #endif } -void *taosMemoryStrDup(void *ptr) { +void *taosMemoryStrDup(const char *ptr) { #ifdef USE_TD_MEMORY if (ptr == NULL) return NULL; TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char *)ptr - sizeof(TdMemoryInfo)); assert(pTdMemoryInfo->symbol == TD_MEMORY_SYMBOL); - void *tmp = tstrdup((const char *)pTdMemoryInfo); + void *tmp = tstrdup(pTdMemoryInfo); if (tmp == NULL) return NULL; memcpy(tmp, pTdMemoryInfo, sizeof(TdMemoryInfo)); @@ -297,7 +297,7 @@ void *taosMemoryStrDup(void *ptr) { return (char *)tmp + sizeof(TdMemoryInfo); #else - return tstrdup((const char *)ptr); + return tstrdup(ptr); #endif } From c3fc802e113bb6bf5473a9f37ae40d3442d85918 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Tue, 5 Jul 2022 19:23:44 +0800 Subject: [PATCH 3/4] os: add file auto del func --- tests/system-test/simpletest.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system-test/simpletest.bat b/tests/system-test/simpletest.bat index e33fe0d538..656828aa1e 100644 --- a/tests/system-test/simpletest.bat +++ b/tests/system-test/simpletest.bat @@ -6,7 +6,7 @@ python3 .\test.py -f 0-others\telemetry.py python3 .\test.py -f 0-others\taosdMonitor.py python3 .\test.py -f 0-others\udfTest.py python3 .\test.py -f 0-others\udf_create.py -@REM python3 .\test.py -f 0-others\udf_restart_taosd.py +python3 .\test.py -f 0-others\udf_restart_taosd.py @REM python3 .\test.py -f 0-others\cachelast.py @REM python3 .\test.py -f 0-others\user_control.py From 55cf31a094a71b85821900875359dca2f09eb6af Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Tue, 5 Jul 2022 20:54:50 +0800 Subject: [PATCH 4/4] os: add file auto del func --- source/os/src/osFile.c | 48 +++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 46373707b2..556fd78360 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -54,12 +54,19 @@ typedef struct TdFile { int refId; FileFd fd; FILE *fp; - char *name; - bool autoDel; } * TdFilePtr, TdFile; #define FILE_WITH_LOCK 1 +typedef struct AutoDelFile * AutoDelFilePtr; +typedef struct AutoDelFile { + char *name; + AutoDelFilePtr lastAutoDelFilePtr; +} AutoDelFile; +static TdThreadMutex autoDelFileLock; +static AutoDelFilePtr nowAutoDelFilePtr = NULL; +static TdThreadOnce autoDelFileInit = PTHREAD_ONCE_INIT; + void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) { #ifdef WINDOWS const char *tdengineTmpFileNamePrefix = "tdengine-"; @@ -240,6 +247,34 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) { return 0; } +void autoDelFileList() { + taosThreadMutexLock(&autoDelFileLock); + while (nowAutoDelFilePtr != NULL) { + taosRemoveFile(nowAutoDelFilePtr->name); + AutoDelFilePtr tmp = nowAutoDelFilePtr->lastAutoDelFilePtr; + taosMemoryFree(nowAutoDelFilePtr->name); + taosMemoryFree(nowAutoDelFilePtr); + nowAutoDelFilePtr = tmp; + } + taosThreadMutexUnlock(&autoDelFileLock); + taosThreadMutexDestroy(&autoDelFileLock); +} + +void autoDelFileListInit() { + taosThreadMutexInit(&autoDelFileLock, NULL); + atexit(autoDelFileList); +} + +void autoDelFileListAdd(const char *path) { + taosThreadOnce(&autoDelFileInit, autoDelFileListInit); + taosThreadMutexLock(&autoDelFileLock); + AutoDelFilePtr tmp = taosMemoryMalloc(sizeof(AutoDelFile)); + tmp->lastAutoDelFilePtr = nowAutoDelFilePtr; + tmp->name = taosMemoryStrDup(path); + nowAutoDelFilePtr = tmp; + taosThreadMutexUnlock(&autoDelFileLock); +} + TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { int fd = -1; FILE *fp = NULL; @@ -295,11 +330,8 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { pFile->fd = fd; pFile->fp = fp; pFile->refId = 0; - pFile->name = taosMemoryStrDup(path); if (tdFileOptions & TD_FILE_AUTO_DEL) { - pFile->autoDel = true; - } else { - pFile->autoDel = false; + autoDelFileListAdd(path); } return pFile; } @@ -333,10 +365,6 @@ int32_t taosCloseFile(TdFilePtr *ppFile) { taosThreadRwlockUnlock(&((*ppFile)->rwlock)); taosThreadRwlockDestroy(&((*ppFile)->rwlock)); #endif - if ((*ppFile)->autoDel) { - taosRemoveFile((*ppFile)->name); - } - taosMemoryFree((*ppFile)->name); taosMemoryFree(*ppFile); *ppFile = NULL; return code;