Merge pull request #14551 from taosdata/fix/ZhiqiangWang/TD-13063-add-file-auto-del-func
os: add file auto del func
This commit is contained in:
commit
78cbb5150a
|
@ -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();
|
||||
|
|
|
@ -58,6 +58,15 @@ typedef struct 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-";
|
||||
|
@ -238,7 +247,33 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void autoDelFileListAdd(const char *path) { return; }
|
||||
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;
|
||||
|
@ -283,10 +318,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 +330,9 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
|
|||
pFile->fd = fd;
|
||||
pFile->fp = fp;
|
||||
pFile->refId = 0;
|
||||
if (tdFileOptions & TD_FILE_AUTO_DEL) {
|
||||
autoDelFileListAdd(path);
|
||||
}
|
||||
return pFile;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue