提交 55cf31a0 编写于 作者: wafwerar's avatar wafwerar

os: add file auto del func

上级 c3fc802e
...@@ -54,12 +54,19 @@ typedef struct TdFile { ...@@ -54,12 +54,19 @@ typedef struct TdFile {
int refId; int refId;
FileFd fd; FileFd fd;
FILE *fp; FILE *fp;
char *name;
bool autoDel;
} * TdFilePtr, TdFile; } * TdFilePtr, TdFile;
#define FILE_WITH_LOCK 1 #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) { void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) {
#ifdef WINDOWS #ifdef WINDOWS
const char *tdengineTmpFileNamePrefix = "tdengine-"; const char *tdengineTmpFileNamePrefix = "tdengine-";
...@@ -240,6 +247,34 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) { ...@@ -240,6 +247,34 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) {
return 0; 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) { TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
int fd = -1; int fd = -1;
FILE *fp = NULL; FILE *fp = NULL;
...@@ -295,11 +330,8 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { ...@@ -295,11 +330,8 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
pFile->fd = fd; pFile->fd = fd;
pFile->fp = fp; pFile->fp = fp;
pFile->refId = 0; pFile->refId = 0;
pFile->name = taosMemoryStrDup(path);
if (tdFileOptions & TD_FILE_AUTO_DEL) { if (tdFileOptions & TD_FILE_AUTO_DEL) {
pFile->autoDel = true; autoDelFileListAdd(path);
} else {
pFile->autoDel = false;
} }
return pFile; return pFile;
} }
...@@ -333,10 +365,6 @@ int32_t taosCloseFile(TdFilePtr *ppFile) { ...@@ -333,10 +365,6 @@ int32_t taosCloseFile(TdFilePtr *ppFile) {
taosThreadRwlockUnlock(&((*ppFile)->rwlock)); taosThreadRwlockUnlock(&((*ppFile)->rwlock));
taosThreadRwlockDestroy(&((*ppFile)->rwlock)); taosThreadRwlockDestroy(&((*ppFile)->rwlock));
#endif #endif
if ((*ppFile)->autoDel) {
taosRemoveFile((*ppFile)->name);
}
taosMemoryFree((*ppFile)->name);
taosMemoryFree(*ppFile); taosMemoryFree(*ppFile);
*ppFile = NULL; *ppFile = NULL;
return code; return code;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册