未验证 提交 f3d39e08 编写于 作者: wafwerar's avatar wafwerar 提交者: GitHub

Merge pull request #10646 from taosdata/fix/ZhiqiangWang/TD-13761-redefine-dir-api

[TD-13761]<fix>: redefine dir api.
...@@ -16,10 +16,24 @@ ...@@ -16,10 +16,24 @@
#ifndef _TD_OS_DIR_H_ #ifndef _TD_OS_DIR_H_
#define _TD_OS_DIR_H_ #define _TD_OS_DIR_H_
// If the error is in a third-party library, place this header file under the third-party library header file.
#ifndef ALLOW_FORBID_FUNC
#define opendir OPENDIR_FUNC_TAOS_FORBID
#define readdir READDIR_FUNC_TAOS_FORBID
#define closedir CLOSEDIR_FUNC_TAOS_FORBID
#define dirname DIRNAME_FUNC_TAOS_FORBID
#undef basename
#define basename BASENAME_FUNC_TAOS_FORBID
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef struct TdDir *TdDirPtr;
typedef struct TdDirEntry *TdDirEntryPtr;
void taosRemoveDir(const char *dirname); void taosRemoveDir(const char *dirname);
bool taosDirExist(char *dirname); bool taosDirExist(char *dirname);
int32_t taosMkDir(const char *dirname); int32_t taosMkDir(const char *dirname);
...@@ -27,6 +41,14 @@ void taosRemoveOldFiles(const char *dirname, int32_t keepDays); ...@@ -27,6 +41,14 @@ void taosRemoveOldFiles(const char *dirname, int32_t keepDays);
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen); int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen);
int32_t taosRealPath(char *dirname, int32_t maxlen); int32_t taosRealPath(char *dirname, int32_t maxlen);
bool taosIsDir(const char *dirname); bool taosIsDir(const char *dirname);
char* taosDirName(char *dirname);
char* taosDirEntryBaseName(char *dirname);
TdDirPtr taosOpenDir(const char *dirname);
TdDirEntryPtr taosReadDir(TdDirPtr pDir);
bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry);
char* taosGetDirEntryName(TdDirEntryPtr pDirEntry);
int32_t taosCloseDir(TdDirPtr pDir);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -365,7 +365,7 @@ int tsdbCreateDFile(STsdb *pRepo, SDFile *pDFile, bool updateHeader, TSDB_FILE_T ...@@ -365,7 +365,7 @@ int tsdbCreateDFile(STsdb *pRepo, SDFile *pDFile, bool updateHeader, TSDB_FILE_T
if (errno == ENOENT) { if (errno == ENOENT) {
// Try to create directory recursively // Try to create directory recursively
char *s = strdup(TSDB_FILE_REL_NAME(pDFile)); char *s = strdup(TSDB_FILE_REL_NAME(pDFile));
if (tfsMkdirRecurAt(pRepo->pTfs, dirname(s), TSDB_FILE_DID(pDFile)) < 0) { if (tfsMkdirRecurAt(pRepo->pTfs, taosDirName(s), TSDB_FILE_DID(pDFile)) < 0) {
tfree(s); tfree(s);
return -1; return -1;
} }
......
...@@ -722,13 +722,13 @@ static SArray* tfileGetFileList(const char* path) { ...@@ -722,13 +722,13 @@ static SArray* tfileGetFileList(const char* path) {
uint32_t version; uint32_t version;
SArray* files = taosArrayInit(4, sizeof(void*)); SArray* files = taosArrayInit(4, sizeof(void*));
DIR* dir = opendir(path); TdDirPtr pDir = taosOpenDir(path);
if (NULL == dir) { if (NULL == pDir) {
return NULL; return NULL;
} }
struct dirent* entry; TdDirEntryPtr pDirEntry;
while ((entry = readdir(dir)) != NULL) { while ((pDirEntry = taosReadDir(pDir)) != NULL) {
char* file = entry->d_name; char* file = taosGetDirEntryName(pDirEntry);
if (0 != tfileParseFileName(file, &suid, buf, &version)) { if (0 != tfileParseFileName(file, &suid, buf, &version)) {
continue; continue;
} }
...@@ -738,7 +738,7 @@ static SArray* tfileGetFileList(const char* path) { ...@@ -738,7 +738,7 @@ static SArray* tfileGetFileList(const char* path) {
sprintf(buf, "%s/%s", path, file); sprintf(buf, "%s/%s", path, file);
taosArrayPush(files, &buf); taosArrayPush(files, &buf);
} }
closedir(dir); taosCloseDir(pDir);
taosArraySort(files, tfileCompare); taosArraySort(files, tfileCompare);
tfileRmExpireFile(files); tfileRmExpireFile(files);
......
...@@ -59,7 +59,7 @@ typedef struct STfsDir { ...@@ -59,7 +59,7 @@ typedef struct STfsDir {
SDiskID did; SDiskID did;
char dirname[TSDB_FILENAME_LEN]; char dirname[TSDB_FILENAME_LEN];
STfsFile tfile; STfsFile tfile;
DIR *dir; TdDirPtr pDir;
STfs *pTfs; STfs *pTfs;
} STfsDir; } STfsDir;
......
...@@ -192,14 +192,14 @@ void tfsBasename(const STfsFile *pFile, char *dest) { ...@@ -192,14 +192,14 @@ void tfsBasename(const STfsFile *pFile, char *dest) {
char tname[TSDB_FILENAME_LEN] = "\0"; char tname[TSDB_FILENAME_LEN] = "\0";
tstrncpy(tname, pFile->aname, TSDB_FILENAME_LEN); tstrncpy(tname, pFile->aname, TSDB_FILENAME_LEN);
tstrncpy(dest, basename(tname), TSDB_FILENAME_LEN); tstrncpy(dest, taosDirEntryBaseName(tname), TSDB_FILENAME_LEN);
} }
void tfsDirname(const STfsFile *pFile, char *dest) { void tfsDirname(const STfsFile *pFile, char *dest) {
char tname[TSDB_FILENAME_LEN] = "\0"; char tname[TSDB_FILENAME_LEN] = "\0";
tstrncpy(tname, pFile->aname, TSDB_FILENAME_LEN); tstrncpy(tname, pFile->aname, TSDB_FILENAME_LEN);
tstrncpy(dest, dirname(tname), TSDB_FILENAME_LEN); tstrncpy(dest, taosDirName(tname), TSDB_FILENAME_LEN);
} }
int32_t tfsRemoveFile(const STfsFile *pFile) { return taosRemoveFile(pFile->aname); } int32_t tfsRemoveFile(const STfsFile *pFile) { return taosRemoveFile(pFile->aname); }
...@@ -233,7 +233,7 @@ int32_t tfsMkdirRecurAt(STfs *pTfs, const char *rname, SDiskID diskId) { ...@@ -233,7 +233,7 @@ int32_t tfsMkdirRecurAt(STfs *pTfs, const char *rname, SDiskID diskId) {
// the pointer directly in this recursion. // the pointer directly in this recursion.
// See // See
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dirname.3.html // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dirname.3.html
char *dir = strdup(dirname(s)); char *dir = strdup(taosDirName(s));
if (tfsMkdirRecurAt(pTfs, dir, diskId) < 0) { if (tfsMkdirRecurAt(pTfs, dir, diskId) < 0) {
free(s); free(s);
...@@ -324,45 +324,46 @@ STfsDir *tfsOpendir(STfs *pTfs, const char *rname) { ...@@ -324,45 +324,46 @@ STfsDir *tfsOpendir(STfs *pTfs, const char *rname) {
return pDir; return pDir;
} }
const STfsFile *tfsReaddir(STfsDir *pDir) { const STfsFile *tfsReaddir(STfsDir *pTfsDir) {
if (pDir == NULL || pDir->dir == NULL) return NULL; if (pTfsDir == NULL || pTfsDir->pDir == NULL) return NULL;
char bname[TMPNAME_LEN * 2] = "\0"; char bname[TMPNAME_LEN * 2] = "\0";
while (true) { while (true) {
struct dirent *dp = NULL; TdDirEntryPtr pDirEntry = NULL;
dp = readdir(pDir->dir); pDirEntry = taosReadDir(pTfsDir->pDir);
if (dp != NULL) { if (pDirEntry != NULL) {
// Skip . and .. // Skip . and ..
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue; char *name = taosGetDirEntryName(pDirEntry);
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
if (pDir->dirname == NULL || pDir->dirname[0] == 0) { if (pTfsDir->dirname == NULL || pTfsDir->dirname[0] == 0) {
snprintf(bname, TMPNAME_LEN * 2, "%s", dp->d_name); snprintf(bname, TMPNAME_LEN * 2, "%s", name);
} else { } else {
snprintf(bname, TMPNAME_LEN * 2, "%s%s%s", pDir->dirname, TD_DIRSEP, dp->d_name); snprintf(bname, TMPNAME_LEN * 2, "%s%s%s", pTfsDir->dirname, TD_DIRSEP, name);
} }
tfsInitFile(pDir->pTfs, &pDir->tfile, pDir->did, bname); tfsInitFile(pTfsDir->pTfs, &pTfsDir->tfile, pTfsDir->did, bname);
return &pDir->tfile; return &pTfsDir->tfile;
} }
if (tfsOpendirImpl(pDir->pTfs, pDir) < 0) { if (tfsOpendirImpl(pTfsDir->pTfs, pTfsDir) < 0) {
return NULL; return NULL;
} }
if (pDir->dir == NULL) { if (pTfsDir->pDir == NULL) {
terrno = TSDB_CODE_SUCCESS; terrno = TSDB_CODE_SUCCESS;
return NULL; return NULL;
} }
} }
} }
void tfsClosedir(STfsDir *pDir) { void tfsClosedir(STfsDir *pTfsDir) {
if (pDir) { if (pTfsDir) {
if (pDir->dir != NULL) { if (pTfsDir->pDir != NULL) {
closedir(pDir->dir); taosCloseDir(pTfsDir->pDir);
pDir->dir = NULL; pTfsDir->pDir = NULL;
} }
free(pDir); free(pTfsDir);
} }
} }
...@@ -487,29 +488,29 @@ static STfsDisk *tfsGetDiskByName(STfs *pTfs, const char *dir) { ...@@ -487,29 +488,29 @@ static STfsDisk *tfsGetDiskByName(STfs *pTfs, const char *dir) {
return pDisk; return pDisk;
} }
static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pDir) { static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) {
STfsDisk *pDisk = NULL; STfsDisk *pDisk = NULL;
char adir[TMPNAME_LEN * 2] = "\0"; char adir[TMPNAME_LEN * 2] = "\0";
if (pDir->dir != NULL) { if (pTfsDir->pDir != NULL) {
closedir(pDir->dir); taosCloseDir(pTfsDir->pDir);
pDir->dir = NULL; pTfsDir->pDir = NULL;
} }
while (true) { while (true) {
pDisk = tfsNextDisk(pTfs, &pDir->iter); pDisk = tfsNextDisk(pTfs, &pTfsDir->iter);
if (pDisk == NULL) return 0; if (pDisk == NULL) return 0;
pDir->did.level = pDisk->level; pTfsDir->did.level = pDisk->level;
pDir->did.id = pDisk->id; pTfsDir->did.id = pDisk->id;
if (pDisk->path == NULL || pDisk->path[0] == 0) { if (pDisk->path == NULL || pDisk->path[0] == 0) {
snprintf(adir, TMPNAME_LEN * 2, "%s", pDir->dirname); snprintf(adir, TMPNAME_LEN * 2, "%s", pTfsDir->dirname);
} else { } else {
snprintf(adir, TMPNAME_LEN * 2, "%s%s%s", pDisk->path, TD_DIRSEP, pDir->dirname); snprintf(adir, TMPNAME_LEN * 2, "%s%s%s", pDisk->path, TD_DIRSEP, pTfsDir->dirname);
} }
pDir->dir = opendir(adir); pTfsDir->pDir = taosOpenDir(adir);
if (pDir->dir != NULL) break; if (pTfsDir->pDir != NULL) break;
} }
return 0; return 0;
......
...@@ -130,16 +130,16 @@ int walCheckAndRepairMeta(SWal* pWal) { ...@@ -130,16 +130,16 @@ int walCheckAndRepairMeta(SWal* pWal) {
regcomp(&logRegPattern, logPattern, REG_EXTENDED); regcomp(&logRegPattern, logPattern, REG_EXTENDED);
regcomp(&idxRegPattern, idxPattern, REG_EXTENDED); regcomp(&idxRegPattern, idxPattern, REG_EXTENDED);
DIR* dir = opendir(pWal->path); TdDirPtr pDir = taosOpenDir(pWal->path);
if (dir == NULL) { if (pDir == NULL) {
wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno)); wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
return -1; return -1;
} }
// scan log files and build new meta // scan log files and build new meta
struct dirent* ent; TdDirEntryPtr pDirEntry;
while ((ent = readdir(dir)) != NULL) { while ((pDirEntry = taosReadDir(pDir)) != NULL) {
char* name = basename(ent->d_name); char* name = taosDirEntryBaseName(taosGetDirEntryName(pDirEntry));
int code = regexec(&logRegPattern, name, 0, NULL, 0); int code = regexec(&logRegPattern, name, 0, NULL, 0);
if (code == 0) { if (code == 0) {
SWalFileInfo fileInfo; SWalFileInfo fileInfo;
...@@ -149,7 +149,7 @@ int walCheckAndRepairMeta(SWal* pWal) { ...@@ -149,7 +149,7 @@ int walCheckAndRepairMeta(SWal* pWal) {
} }
} }
closedir(dir); taosCloseDir(pDir);
regfree(&logRegPattern); regfree(&logRegPattern);
regfree(&idxRegPattern); regfree(&idxRegPattern);
...@@ -337,25 +337,25 @@ static int walFindCurMetaVer(SWal* pWal) { ...@@ -337,25 +337,25 @@ static int walFindCurMetaVer(SWal* pWal) {
regex_t walMetaRegexPattern; regex_t walMetaRegexPattern;
regcomp(&walMetaRegexPattern, pattern, REG_EXTENDED); regcomp(&walMetaRegexPattern, pattern, REG_EXTENDED);
DIR* dir = opendir(pWal->path); TdDirPtr pDir = taosOpenDir(pWal->path);
if (dir == NULL) { if (pDir == NULL) {
wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno)); wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
return -1; return -1;
} }
struct dirent* ent; TdDirEntryPtr pDirEntry;
// find existing meta-ver[x].json // find existing meta-ver[x].json
int metaVer = -1; int metaVer = -1;
while ((ent = readdir(dir)) != NULL) { while ((pDirEntry = taosReadDir(pDir)) != NULL) {
char* name = basename(ent->d_name); char* name = taosDirEntryBaseName(taosGetDirEntryName(pDirEntry));
int code = regexec(&walMetaRegexPattern, name, 0, NULL, 0); int code = regexec(&walMetaRegexPattern, name, 0, NULL, 0);
if (code == 0) { if (code == 0) {
sscanf(name, "meta-ver%d", &metaVer); sscanf(name, "meta-ver%d", &metaVer);
break; break;
} }
} }
closedir(dir); taosCloseDir(pDir);
regfree(&walMetaRegexPattern); regfree(&walMetaRegexPattern);
return metaVer; return metaVer;
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
*/ */
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#define ALLOW_FORBID_FUNC
#include "os.h" #include "os.h"
#include "osString.h" #include "osString.h"
...@@ -36,6 +37,10 @@ ...@@ -36,6 +37,10 @@
#include <unistd.h> #include <unistd.h>
#include <wordexp.h> #include <wordexp.h>
typedef struct dirent dirent;
typedef struct DIR TdDir;
typedef struct dirent TdDirent;
void taosRemoveDir(const char *dirname) { void taosRemoveDir(const char *dirname) {
DIR *dir = opendir(dirname); DIR *dir = opendir(dirname);
if (dir == NULL) return; if (dir == NULL) return;
...@@ -149,4 +154,47 @@ bool taosIsDir(const char *dirname) { ...@@ -149,4 +154,47 @@ bool taosIsDir(const char *dirname) {
return false; return false;
} }
char* taosDirName(char *name) {
return dirname(name);
}
char* taosDirEntryBaseName(char *name) {
return basename(name);
}
TdDirPtr taosOpenDir(const char *dirname) {
if (dirname == NULL) {
return NULL;
}
return (TdDirPtr)opendir(dirname);
}
TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
if (pDir == NULL) {
return NULL;
}
return (TdDirEntryPtr)readdir((DIR*)pDir);
}
bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry) {
if (pDirEntry == NULL) {
return false;
}
return (((dirent*)pDirEntry)->d_type & DT_DIR) != 0;
}
char* taosGetDirEntryName(TdDirEntryPtr pDirEntry) {
if (pDirEntry == NULL) {
return NULL;
}
return ((dirent*)pDirEntry)->d_name;
}
int32_t taosCloseDir(TdDirPtr pDir) {
if (pDir == NULL) {
return -1;
}
return closedir((DIR*)pDir);
}
#endif #endif
...@@ -99,7 +99,7 @@ static void *sem_thread_routine(void *arg) { ...@@ -99,7 +99,7 @@ static void *sem_thread_routine(void *arg) {
sem_port = mach_task_self(); sem_port = mach_task_self();
kern_return_t ret = semaphore_create(sem_port, &sem_exit, SYNC_POLICY_FIFO, 0); kern_return_t ret = semaphore_create(sem_port, &sem_exit, SYNC_POLICY_FIFO, 0);
if (ret != KERN_SUCCESS) { if (ret != KERN_SUCCESS) {
fprintf(stderr, "==%s[%d]%s()==failed to create sem_exit\n", basename(__FILE__), __LINE__, __func__); fprintf(stderr, "==%s[%d]%s()==failed to create sem_exit\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
sem_inited = -1; sem_inited = -1;
return NULL; return NULL;
} }
...@@ -112,7 +112,7 @@ static void once_init(void) { ...@@ -112,7 +112,7 @@ static void once_init(void) {
int r = 0; int r = 0;
r = pthread_create(&sem_thread, NULL, sem_thread_routine, NULL); r = pthread_create(&sem_thread, NULL, sem_thread_routine, NULL);
if (r) { if (r) {
fprintf(stderr, "==%s[%d]%s()==failed to create thread\n", basename(__FILE__), __LINE__, __func__); fprintf(stderr, "==%s[%d]%s()==failed to create thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
return; return;
} }
while (sem_inited == 0) { while (sem_inited == 0) {
...@@ -139,14 +139,14 @@ struct tsem_s { ...@@ -139,14 +139,14 @@ struct tsem_s {
}; };
int tsem_init(tsem_t *sem, int pshared, unsigned int value) { int tsem_init(tsem_t *sem, int pshared, unsigned int value) {
// fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", basename(__FILE__), __LINE__, __func__, sem); // fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
if (*sem) { if (*sem) {
fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
struct tsem_s *p = (struct tsem_s *)calloc(1, sizeof(*p)); struct tsem_s *p = (struct tsem_s *)calloc(1, sizeof(*p));
if (!p) { if (!p) {
fprintf(stderr, "==%s[%d]%s():[%p]==out of memory\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==out of memory\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
...@@ -162,7 +162,7 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { ...@@ -162,7 +162,7 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) {
p->val = value; p->val = value;
} while (0); } while (0);
if (r) { if (r) {
fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
#elif defined(SEM_USE_POSIX) #elif defined(SEM_USE_POSIX)
...@@ -181,27 +181,27 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { ...@@ -181,27 +181,27 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) {
int e = errno; int e = errno;
if (e == EEXIST) continue; if (e == EEXIST) continue;
if (e == EINTR) continue; if (e == EINTR) continue;
fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem, e,
strerror(e)); strerror(e));
abort(); abort();
} while (p->sem == SEM_FAILED); } while (p->sem == SEM_FAILED);
#elif defined(SEM_USE_SEM) #elif defined(SEM_USE_SEM)
pthread_once(&sem_once, once_init); pthread_once(&sem_once, once_init);
if (sem_inited != 1) { if (sem_inited != 1) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
kern_return_t ret = semaphore_create(sem_port, &p->sem, SYNC_POLICY_FIFO, value); kern_return_t ret = semaphore_create(sem_port, &p->sem, SYNC_POLICY_FIFO, value);
if (ret != KERN_SUCCESS) { if (ret != KERN_SUCCESS) {
fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
// we fail-fast here, because we have less-doc about semaphore_create for the moment // we fail-fast here, because we have less-doc about semaphore_create for the moment
abort(); abort();
} }
#else // SEM_USE_PTHREAD #else // SEM_USE_PTHREAD
p->sem = dispatch_semaphore_create(value); p->sem = dispatch_semaphore_create(value);
if (p->sem == NULL) { if (p->sem == NULL) {
fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
#endif // SEM_USE_PTHREAD #endif // SEM_USE_PTHREAD
...@@ -215,28 +215,28 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { ...@@ -215,28 +215,28 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) {
int tsem_wait(tsem_t *sem) { int tsem_wait(tsem_t *sem) {
if (!*sem) { if (!*sem) {
fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
struct tsem_s *p = *sem; struct tsem_s *p = *sem;
if (!p->valid) { if (!p->valid) {
fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
#ifdef SEM_USE_PTHREAD #ifdef SEM_USE_PTHREAD
if (pthread_mutex_lock(&p->lock)) { if (pthread_mutex_lock(&p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
p->val -= 1; p->val -= 1;
if (p->val < 0) { if (p->val < 0) {
if (pthread_cond_wait(&p->cond, &p->lock)) { if (pthread_cond_wait(&p->cond, &p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
} }
if (pthread_mutex_unlock(&p->lock)) { if (pthread_mutex_unlock(&p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
return 0; return 0;
...@@ -251,28 +251,28 @@ int tsem_wait(tsem_t *sem) { ...@@ -251,28 +251,28 @@ int tsem_wait(tsem_t *sem) {
int tsem_post(tsem_t *sem) { int tsem_post(tsem_t *sem) {
if (!*sem) { if (!*sem) {
fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
struct tsem_s *p = *sem; struct tsem_s *p = *sem;
if (!p->valid) { if (!p->valid) {
fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
#ifdef SEM_USE_PTHREAD #ifdef SEM_USE_PTHREAD
if (pthread_mutex_lock(&p->lock)) { if (pthread_mutex_lock(&p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
p->val += 1; p->val += 1;
if (p->val <= 0) { if (p->val <= 0) {
if (pthread_cond_signal(&p->cond)) { if (pthread_cond_signal(&p->cond)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
} }
if (pthread_mutex_unlock(&p->lock)) { if (pthread_mutex_unlock(&p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
return 0; return 0;
...@@ -286,34 +286,34 @@ int tsem_post(tsem_t *sem) { ...@@ -286,34 +286,34 @@ int tsem_post(tsem_t *sem) {
} }
int tsem_destroy(tsem_t *sem) { int tsem_destroy(tsem_t *sem) {
// fprintf(stderr, "==%s[%d]%s():[%p]==destroying\n", basename(__FILE__), __LINE__, __func__, sem); // fprintf(stderr, "==%s[%d]%s():[%p]==destroying\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
if (!*sem) { if (!*sem) {
// fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); // fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
// abort(); // abort();
return 0; return 0;
} }
struct tsem_s *p = *sem; struct tsem_s *p = *sem;
if (!p->valid) { if (!p->valid) {
// fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); // fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
// abort(); // abort();
return 0; return 0;
} }
#ifdef SEM_USE_PTHREAD #ifdef SEM_USE_PTHREAD
if (pthread_mutex_lock(&p->lock)) { if (pthread_mutex_lock(&p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
p->valid = 0; p->valid = 0;
if (pthread_cond_destroy(&p->cond)) { if (pthread_cond_destroy(&p->cond)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
if (pthread_mutex_unlock(&p->lock)) { if (pthread_mutex_unlock(&p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
if (pthread_mutex_destroy(&p->lock)) { if (pthread_mutex_destroy(&p->lock)) {
fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
abort(); abort();
} }
#elif defined(SEM_USE_POSIX) #elif defined(SEM_USE_POSIX)
...@@ -322,7 +322,7 @@ int tsem_destroy(tsem_t *sem) { ...@@ -322,7 +322,7 @@ int tsem_destroy(tsem_t *sem) {
int r = sem_unlink(name); int r = sem_unlink(name);
if (r) { if (r) {
int e = errno; int e = errno;
fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem, e,
strerror(e)); strerror(e));
abort(); abort();
} }
......
...@@ -809,7 +809,8 @@ int32_t taosGetFqdn(char *fqdn) { ...@@ -809,7 +809,8 @@ int32_t taosGetFqdn(char *fqdn) {
char hostname[1024]; char hostname[1024];
hostname[1023] = '\0'; hostname[1023] = '\0';
if (gethostname(hostname, 1023) == -1) { if (gethostname(hostname, 1023) == -1) {
// printf("failed to get hostname, reason:%s", strerror(errno)); printf("failed to get hostname, reason:%s", strerror(errno));
assert(0);
return -1; return -1;
} }
...@@ -826,7 +827,8 @@ int32_t taosGetFqdn(char *fqdn) { ...@@ -826,7 +827,8 @@ int32_t taosGetFqdn(char *fqdn) {
#endif // __APPLE__ #endif // __APPLE__
int32_t ret = getaddrinfo(hostname, NULL, &hints, &result); int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
if (!result) { if (!result) {
// printf("failed to get fqdn, code:%d, reason:%s", ret, gai_strerror(ret)); printf("failed to get fqdn, code:%d, reason:%s", ret, gai_strerror(ret));
assert(0);
return -1; return -1;
} }
......
...@@ -79,7 +79,7 @@ static void* timer_routine(void* arg) { ...@@ -79,7 +79,7 @@ static void* timer_routine(void* arg) {
struct kevent64_s kev[10] = {0}; struct kevent64_s kev[10] = {0};
r = kevent64(timer_kq, NULL, 0, kev, sizeof(kev) / sizeof(kev[0]), 0, &to); r = kevent64(timer_kq, NULL, 0, kev, sizeof(kev) / sizeof(kev[0]), 0, &to);
if (r != 0) { if (r != 0) {
fprintf(stderr, "==%s[%d]%s()==kevent64 failed\n", basename(__FILE__), __LINE__, __func__); fprintf(stderr, "==%s[%d]%s()==kevent64 failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
abort(); abort();
} }
timer_callback(SIGALRM); // just mock timer_callback(SIGALRM); // just mock
...@@ -97,14 +97,14 @@ int taosInitTimer(void (*callback)(int), int ms) { ...@@ -97,14 +97,14 @@ int taosInitTimer(void (*callback)(int), int ms) {
timer_kq = kqueue(); timer_kq = kqueue();
if (timer_kq == -1) { if (timer_kq == -1) {
fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", basename(__FILE__), __LINE__, __func__); fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
// since no caller of this func checks the return value for the moment // since no caller of this func checks the return value for the moment
abort(); abort();
} }
r = pthread_create(&timer_thread, NULL, timer_routine, NULL); r = pthread_create(&timer_thread, NULL, timer_routine, NULL);
if (r) { if (r) {
fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", basename(__FILE__), __LINE__, __func__); fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
// since no caller of this func checks the return value for the moment // since no caller of this func checks the return value for the moment
abort(); abort();
} }
...@@ -116,7 +116,7 @@ void taosUninitTimer() { ...@@ -116,7 +116,7 @@ void taosUninitTimer() {
timer_stop = 1; timer_stop = 1;
r = pthread_join(timer_thread, NULL); r = pthread_join(timer_thread, NULL);
if (r) { if (r) {
fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", basename(__FILE__), __LINE__, __func__); fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
// since no caller of this func checks the return value for the moment // since no caller of this func checks the return value for the moment
abort(); abort();
} }
......
...@@ -192,11 +192,11 @@ static void msg_process(tmq_message_t* message) { tmqShowMsg(message); } ...@@ -192,11 +192,11 @@ static void msg_process(tmq_message_t* message) { tmqShowMsg(message); }
// calc dir size (not include itself 4096Byte) // calc dir size (not include itself 4096Byte)
int64_t getDirectorySize(char *dir) int64_t getDirectorySize(char *dir)
{ {
DIR *dp; TdDirPtr pDir;
struct dirent *entry; TdDirEntryPtr pDirEntry;
int64_t totalSize=0; int64_t totalSize=0;
if ((dp = opendir(dir)) == NULL) { if ((pDir = taosOpenDir(dir)) == NULL) {
fprintf(stderr, "Cannot open dir: %s\n", dir); fprintf(stderr, "Cannot open dir: %s\n", dir);
return -1; return -1;
} }
...@@ -204,26 +204,27 @@ int64_t getDirectorySize(char *dir) ...@@ -204,26 +204,27 @@ int64_t getDirectorySize(char *dir)
//lstat(dir, &statbuf); //lstat(dir, &statbuf);
//totalSize+=statbuf.st_size; //totalSize+=statbuf.st_size;
while ((entry = readdir(dp)) != NULL) { while ((pDirEntry = taosReadDir(pDir)) != NULL) {
char subdir[1024]; char subdir[1024];
sprintf(subdir, "%s/%s", dir, entry->d_name); char* fileName = taosGetDirEntryName(pDirEntry);
sprintf(subdir, "%s/%s", dir, fileName);
//printf("===d_name: %s\n", entry->d_name); //printf("===d_name: %s\n", entry->d_name);
if (taosIsDir(subdir)) { if (taosIsDir(subdir)) {
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) { if (strcmp(".", fileName) == 0 || strcmp("..", fileName) == 0) {
continue; continue;
} }
int64_t subDirSize = getDirectorySize(subdir); int64_t subDirSize = getDirectorySize(subdir);
totalSize+=subDirSize; totalSize+=subDirSize;
} else if (0 == strcmp(strchr(entry->d_name, '.'), ".log")) { // only calc .log file size, and not include .idx file } else if (0 == strcmp(strchr(fileName, '.'), ".log")) { // only calc .log file size, and not include .idx file
int64_t file_size = 0; int64_t file_size = 0;
taosStatFile(subdir, &file_size, NULL); taosStatFile(subdir, &file_size, NULL);
totalSize+=file_size; totalSize+=file_size;
} }
} }
closedir(dp); taosCloseDir(pDir);
return totalSize; return totalSize;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册