fix: add function return code validation

This commit is contained in:
dapan1121 2024-07-23 15:03:24 +08:00
parent 7718d5e042
commit 9e8d94a69a
17 changed files with 197 additions and 128 deletions

View File

@ -54,7 +54,7 @@ extern SDiskSpace tsDataSpace;
extern SDiskSpace tsLogSpace; extern SDiskSpace tsLogSpace;
extern SDiskSpace tsTempSpace; extern SDiskSpace tsTempSpace;
void osDefaultInit(); int32_t osDefaultInit();
void osUpdate(); void osUpdate();
void osCleanup(); void osCleanup();
@ -66,7 +66,7 @@ bool osLogSpaceSufficient();
bool osDataSpaceSufficient(); bool osDataSpaceSufficient();
bool osTempSpaceSufficient(); bool osTempSpaceSufficient();
void osSetTimezone(const char *timezone); int32_t osSetTimezone(const char *timezone);
void osSetSystemLocale(const char *inLocale, const char *inCharSet); void osSetSystemLocale(const char *inLocale, const char *inCharSet);
void osSetProcPath(int32_t argc, char **argv); void osSetProcPath(int32_t argc, char **argv);

View File

@ -55,7 +55,7 @@ enum TdTimezone {
}; };
void taosGetSystemTimezone(char *outTimezone, enum TdTimezone *tsTimezone); void taosGetSystemTimezone(char *outTimezone, enum TdTimezone *tsTimezone);
void taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight, enum TdTimezone *tsTimezone); int32_t taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight, enum TdTimezone *tsTimezone);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -801,9 +801,8 @@ int32_t monitorInit() {
} }
if (taosMulModeMkDir(tmpSlowLogPath, 0777, true) != 0) { if (taosMulModeMkDir(tmpSlowLogPath, 0777, true) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
tscError("failed to create dir:%s since %s", tmpSlowLogPath, terrstr()); tscError("failed to create dir:%s since %s", tmpSlowLogPath, terrstr());
return -1; return terrno;
} }
if (tsem2_init(&monitorSem, 0, 0) != 0) { if (tsem2_init(&monitorSem, 0, 0) != 0) {

View File

@ -1306,7 +1306,6 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi
taosSetAllDebugFlag(pCfg, cfgGetItem(pCfg, "debugFlag")->i32); taosSetAllDebugFlag(pCfg, cfgGetItem(pCfg, "debugFlag")->i32);
if (taosMulModeMkDir(tsLogDir, 0777, true) != 0) { if (taosMulModeMkDir(tsLogDir, 0777, true) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
printf("failed to create dir:%s since %s", tsLogDir, terrstr()); printf("failed to create dir:%s since %s", tsLogDir, terrstr());
cfgCleanup(pCfg); cfgCleanup(pCfg);
return -1; return -1;

View File

@ -244,7 +244,6 @@ static int32_t dmProcessCreateNodeReq(EDndNodeType ntype, SRpcMsg *pMsg) {
pWrapper = &pDnode->wrappers[ntype]; pWrapper = &pDnode->wrappers[ntype];
if (taosMkDir(pWrapper->path) != 0) { if (taosMkDir(pWrapper->path) != 0) {
dmReleaseWrapper(pWrapper); dmReleaseWrapper(pWrapper);
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to create dir:%s since %s", pWrapper->path, terrstr()); dError("failed to create dir:%s since %s", pWrapper->path, terrstr());
return -1; return -1;
} }
@ -314,7 +313,6 @@ static int32_t dmProcessAlterNodeTypeReq(EDndNodeType ntype, SRpcMsg *pMsg) {
pWrapper = &pDnode->wrappers[ntype]; pWrapper = &pDnode->wrappers[ntype];
if (taosMkDir(pWrapper->path) != 0) { if (taosMkDir(pWrapper->path) != 0) {
taosThreadMutexUnlock(&pDnode->mutex); taosThreadMutexUnlock(&pDnode->mutex);
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to create dir:%s since %s", pWrapper->path, terrstr()); dError("failed to create dir:%s since %s", pWrapper->path, terrstr());
return -1; return -1;
} }

View File

@ -20,7 +20,6 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
SDnode *pDnode = pWrapper->pDnode; SDnode *pDnode = pWrapper->pDnode;
if (taosMkDir(pWrapper->path) != 0) { if (taosMkDir(pWrapper->path) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, terrstr()); dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, terrstr());
return -1; return -1;
} }

View File

@ -328,7 +328,6 @@ int32_t dmUpdateEncryptKey(char *key, bool toLogFile) {
terrno = 0; terrno = 0;
if (taosMkDir(folder) != 0) { if (taosMkDir(folder) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
encryptError("failed to create dir:%s since %s", folder, terrstr()); encryptError("failed to create dir:%s since %s", folder, terrstr());
goto _OVER; goto _OVER;
} }

View File

@ -454,7 +454,6 @@ static int32_t mndCreateDir(SMnode *pMnode, const char *path) {
} }
if (taosMkDir(pMnode->path) != 0) { if (taosMkDir(pMnode->path) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1; return -1;
} }

View File

@ -70,7 +70,7 @@ int32_t tdbOpen(const char *dbname, int32_t szPage, int32_t pages, TDB **ppDb, i
ret = taosMulModeMkDir(dbname, 0755, false); ret = taosMulModeMkDir(dbname, 0755, false);
if (ret < 0) { if (ret < 0) {
return -1; return terrno;
} }
#ifdef USE_MAINDB #ifdef USE_MAINDB

View File

@ -103,7 +103,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) {
tstrncpy(pWal->path, path, sizeof(pWal->path)); tstrncpy(pWal->path, path, sizeof(pWal->path));
if (taosMkDir(pWal->path) != 0) { if (taosMkDir(pWal->path) != 0) {
wError("vgId:%d, path:%s, failed to create directory since %s", pWal->cfg.vgId, pWal->path, strerror(errno)); wError("vgId:%d, path:%s, failed to create directory since %s", pWal->cfg.vgId, pWal->path, tstrerror(terrno));
goto _err; goto _err;
} }

View File

@ -109,7 +109,7 @@ void taosRemoveDir(const char *dirname) {
} }
} }
taosCloseDir(&pDir); (void)taosCloseDir(&pDir);
(void)rmdir(dirname); (void)rmdir(dirname);
// printf("dir:%s is removed\n", dirname); // printf("dir:%s is removed\n", dirname);
@ -127,8 +127,13 @@ int32_t taosMkDir(const char *dirname) {
#else #else
int32_t code = mkdir(dirname, 0755); int32_t code = mkdir(dirname, 0755);
#endif #endif
if (code < 0 && errno == EEXIST) { if (-1 == code) {
if (errno == EEXIST) {
return 0; return 0;
} else {
terrno = TAOS_SYSTEM_ERROR(errno);
code = terrno;
}
} }
return code; return code;
@ -143,7 +148,7 @@ int32_t taosMulMkDir(const char *dirname) {
taosRealPath(dirname, temp, sizeof(temp)); taosRealPath(dirname, temp, sizeof(temp));
if (temp[1] == ':') pos += 3; if (temp[1] == ':') pos += 3;
#else #else
strcpy(temp, dirname); (void)strcpy(temp, dirname);
#endif #endif
if (taosDirExist(temp)) return code; if (taosDirExist(temp)) return code;
@ -165,7 +170,7 @@ int32_t taosMulMkDir(const char *dirname) {
code = mkdir(temp, 0755); code = mkdir(temp, 0755);
#endif #endif
if (code < 0 && errno != EEXIST) { if (code < 0 && errno != EEXIST) {
// terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
return code; return code;
} }
*pos = TD_DIRSEP[0]; *pos = TD_DIRSEP[0];
@ -181,7 +186,7 @@ int32_t taosMulMkDir(const char *dirname) {
code = mkdir(temp, 0755); code = mkdir(temp, 0755);
#endif #endif
if (code < 0 && errno != EEXIST) { if (code < 0 && errno != EEXIST) {
// terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
return code; return code;
} }
} }
@ -194,7 +199,10 @@ int32_t taosMulMkDir(const char *dirname) {
} }
int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) { int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) {
if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return -1; if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
char temp[TDDIRMAXLEN]; char temp[TDDIRMAXLEN];
char *pos = temp; char *pos = temp;
int32_t code = 0; int32_t code = 0;
@ -202,14 +210,18 @@ int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) {
taosRealPath(dirname, temp, sizeof(temp)); taosRealPath(dirname, temp, sizeof(temp));
if (temp[1] == ':') pos += 3; if (temp[1] == ':') pos += 3;
#else #else
strcpy(temp, dirname); (void)strcpy(temp, dirname);
#endif #endif
if (taosDirExist(temp)) { if (taosDirExist(temp)) {
if (checkAccess && taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) { if (checkAccess && taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) {
return 0; return 0;
} }
return chmod(temp, mode); code = chmod(temp, mode);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
} }
if (strncmp(temp, TD_DIRSEP, 1) == 0) { if (strncmp(temp, TD_DIRSEP, 1) == 0) {
@ -229,8 +241,8 @@ int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) {
code = mkdir(temp, mode); code = mkdir(temp, mode);
#endif #endif
if (code < 0 && errno != EEXIST) { if (code < 0 && errno != EEXIST) {
// terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
return code; return terrno;
} }
*pos = TD_DIRSEP[0]; *pos = TD_DIRSEP[0];
} }
@ -245,8 +257,8 @@ int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) {
code = mkdir(temp, mode); code = mkdir(temp, mode);
#endif #endif
if (code < 0 && errno != EEXIST) { if (code < 0 && errno != EEXIST) {
// terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
return code; return terrno;
} }
} }
@ -254,10 +266,14 @@ int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) {
if (checkAccess && taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) { if (checkAccess && taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) {
return 0; return 0;
} }
return chmod(temp, mode);
} }
return chmod(temp, mode); code = chmod(temp, mode);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return code;
} }
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) { void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
@ -271,7 +287,7 @@ void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
if (strcmp(taosGetDirEntryName(de), ".") == 0 || strcmp(taosGetDirEntryName(de), "..") == 0) continue; if (strcmp(taosGetDirEntryName(de), ".") == 0 || strcmp(taosGetDirEntryName(de), "..") == 0) continue;
char filename[1024]; char filename[1024];
snprintf(filename, sizeof(filename), "%s/%s", dirname, taosGetDirEntryName(de)); (void)snprintf(filename, sizeof(filename), "%s/%s", dirname, taosGetDirEntryName(de));
if (taosDirEntryIsDir(de)) { if (taosDirEntryIsDir(de)) {
continue; continue;
} else { } else {
@ -299,34 +315,25 @@ void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
} }
} }
taosCloseDir(&pDir); (void)taosCloseDir(&pDir);
rmdir(dirname); (void)rmdir(dirname);
} }
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) { int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
wordexp_t full_path; wordexp_t full_path;
switch (wordexp(dirname, &full_path, 0)) { int32_t code = wordexp(dirname, &full_path, 0);
switch (code) {
case 0: case 0:
break; break;
case WRDE_NOSPACE: case WRDE_NOSPACE:
wordfree(&full_path); wordfree(&full_path);
// printf("failed to expand path:%s since Out of memory\n", dirname); // FALL THROUGH
return -1;
case WRDE_BADCHAR:
// printf("failed to expand path:%s since illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }\n",
// dirname);
return -1;
case WRDE_SYNTAX:
// printf("failed to expand path:%s since Shell syntax error, such as unbalanced parentheses or unmatched
// quotes\n", dirname);
return -1;
default: default:
// printf("failed to expand path:%s since %s\n", dirname, strerror(errno)); return code;
return -1;
} }
if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) { if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) {
strncpy(outname, full_path.we_wordv[0], maxlen); (void)strncpy(outname, full_path.we_wordv[0], maxlen);
} }
wordfree(&full_path); wordfree(&full_path);
@ -343,21 +350,23 @@ int32_t taosRealPath(char *dirname, char *realPath, int32_t maxlen) {
#endif #endif
if (strlen(tmp) < maxlen) { if (strlen(tmp) < maxlen) {
if (realPath == NULL) { if (realPath == NULL) {
strncpy(dirname, tmp, maxlen); (void)strncpy(dirname, tmp, maxlen);
} else { } else {
strncpy(realPath, tmp, maxlen); (void)strncpy(realPath, tmp, maxlen);
} }
return 0; return 0;
} }
} }
return -1; terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
} }
bool taosIsDir(const char *dirname) { bool taosIsDir(const char *dirname) {
TdDirPtr pDir = taosOpenDir(dirname); TdDirPtr pDir = taosOpenDir(dirname);
if (pDir != NULL) { if (pDir != NULL) {
taosCloseDir(&pDir); (void)taosCloseDir(&pDir);
return true; return true;
} }
return false; return false;
@ -410,6 +419,7 @@ char *taosDirEntryBaseName(char *name) {
TdDirPtr taosOpenDir(const char *dirname) { TdDirPtr taosOpenDir(const char *dirname) {
if (dirname == NULL) { if (dirname == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL; return NULL;
} }
@ -436,12 +446,17 @@ TdDirPtr taosOpenDir(const char *dirname) {
dirPtr->pDir = pDir; dirPtr->pDir = pDir;
return dirPtr; return dirPtr;
#else #else
return (TdDirPtr)opendir(dirname); TdDirPtr ptr = (TdDirPtr)opendir(dirname);
if (NULL == ptr) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return ptr;
#endif #endif
} }
TdDirEntryPtr taosReadDir(TdDirPtr pDir) { TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
if (pDir == NULL) { if (pDir == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL; return NULL;
} }
#ifdef WINDOWS #ifdef WINDOWS
@ -456,7 +471,13 @@ TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
return NULL; return NULL;
} }
#else #else
return (TdDirEntryPtr)readdir((DIR *)pDir); errno = 0;
terrno = 0;
TdDirEntryPtr p = (TdDirEntryPtr)readdir((DIR *)pDir);
if (NULL == p && errno) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return p;
#endif #endif
} }
@ -484,7 +505,8 @@ char *taosGetDirEntryName(TdDirEntryPtr pDirEntry) {
int32_t taosCloseDir(TdDirPtr *ppDir) { int32_t taosCloseDir(TdDirPtr *ppDir) {
if (ppDir == NULL || *ppDir == NULL) { if (ppDir == NULL || *ppDir == NULL) {
return -1; terrno = TSDB_CODE_INVALID_PARA;
return terrno;
} }
#ifdef WINDOWS #ifdef WINDOWS
FindClose((*ppDir)->hFind); FindClose((*ppDir)->hFind);
@ -497,8 +519,12 @@ int32_t taosCloseDir(TdDirPtr *ppDir) {
*ppDir = NULL; *ppDir = NULL;
return 0; return 0;
#else #else
closedir((DIR *)*ppDir); int32_t code = closedir((DIR *)*ppDir);
*ppDir = NULL; *ppDir = NULL;
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return 0; return 0;
#endif #endif
} }

View File

@ -45,11 +45,16 @@ char tsAVX2Supported = 0;
char tsFMASupported = 0; char tsFMASupported = 0;
char tsAVX512Supported = 0; char tsAVX512Supported = 0;
void osDefaultInit() { int32_t osDefaultInit() {
int32_t code = TSDB_CODE_SUCCESS;
taosSeedRand(taosSafeRand()); taosSeedRand(taosSafeRand());
taosGetSystemLocale(tsLocale, tsCharset); taosGetSystemLocale(tsLocale, tsCharset);
taosGetSystemTimezone(tsTimezoneStr, &tsTimezone); taosGetSystemTimezone(tsTimezoneStr, &tsTimezone);
taosSetSystemTimezone(tsTimezoneStr, tsTimezoneStr, &tsDaylight, &tsTimezone); code = taosSetSystemTimezone(tsTimezoneStr, tsTimezoneStr, &tsDaylight, &tsTimezone);
if (code) {
return code;
}
taosGetSystemInfo(); taosGetSystemInfo();
// deadlock in query // deadlock in query
@ -65,33 +70,35 @@ void osDefaultInit() {
tmpDir = getenv("temp"); tmpDir = getenv("temp");
} }
if (tmpDir != NULL) { if (tmpDir != NULL) {
strcpy(tsTempDir, tmpDir); (void)strcpy(tsTempDir, tmpDir);
} }
strcpy(tsOsName, "Windows"); (void)strcpy(tsOsName, "Windows");
#elif defined(_TD_DARWIN_64) #elif defined(_TD_DARWIN_64)
strcpy(tsOsName, "Darwin"); (void)strcpy(tsOsName, "Darwin");
#else #else
strcpy(tsOsName, "Linux"); (void)strcpy(tsOsName, "Linux");
#endif #endif
if (configDir[0] == 0) { if (configDir[0] == 0) {
strcpy(configDir, TD_CFG_DIR_PATH); (void)strcpy(configDir, TD_CFG_DIR_PATH);
} }
strcpy(tsDataDir, TD_DATA_DIR_PATH); (void)strcpy(tsDataDir, TD_DATA_DIR_PATH);
strcpy(tsLogDir, TD_LOG_DIR_PATH); (void)strcpy(tsLogDir, TD_LOG_DIR_PATH);
if(strlen(tsTempDir) == 0){ if(strlen(tsTempDir) == 0){
strcpy(tsTempDir, TD_TMP_DIR_PATH); (void)strcpy(tsTempDir, TD_TMP_DIR_PATH);
} }
return code;
} }
void osUpdate() { void osUpdate() {
if (tsLogDir[0] != 0) { if (tsLogDir[0] != 0) {
taosGetDiskSize(tsLogDir, &tsLogSpace.size); (void)taosGetDiskSize(tsLogDir, &tsLogSpace.size);
} }
if (tsDataDir[0] != 0) { if (tsDataDir[0] != 0) {
taosGetDiskSize(tsDataDir, &tsDataSpace.size); (void)taosGetDiskSize(tsDataDir, &tsDataSpace.size);
} }
if (tsTempDir[0] != 0) { if (tsTempDir[0] != 0) {
taosGetDiskSize(tsTempDir, &tsTempSpace.size); (void)taosGetDiskSize(tsTempDir, &tsTempSpace.size);
} }
} }
@ -109,11 +116,11 @@ bool osDataSpaceSufficient() { return tsDataSpace.size.avail > tsDataSpace.reser
bool osTempSpaceSufficient() { return tsTempSpace.size.avail > tsTempSpace.reserved; } bool osTempSpaceSufficient() { return tsTempSpace.size.avail > tsTempSpace.reserved; }
void osSetTimezone(const char *tz) { taosSetSystemTimezone(tz, tsTimezoneStr, &tsDaylight, &tsTimezone); } int32_t osSetTimezone(const char *tz) { return taosSetSystemTimezone(tz, tsTimezoneStr, &tsDaylight, &tsTimezone); }
void osSetSystemLocale(const char *inLocale, const char *inCharSet) { void osSetSystemLocale(const char *inLocale, const char *inCharSet) {
memcpy(tsLocale, inLocale, strlen(inLocale) + 1); (void)memcpy(tsLocale, inLocale, strlen(inLocale) + 1);
memcpy(tsCharset, inCharSet, strlen(inCharSet) + 1); (void)memcpy(tsCharset, inCharSet, strlen(inCharSet) + 1);
} }
void osSetProcPath(int32_t argc, char **argv) { tsProcPath = argv[0]; } void osSetProcPath(int32_t argc, char **argv) { tsProcPath = argv[0]; }

View File

@ -90,24 +90,24 @@ void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, cha
char tmpPath[PATH_MAX]; char tmpPath[PATH_MAX];
int32_t len = strlen(inputTmpDir); int32_t len = strlen(inputTmpDir);
memcpy(tmpPath, inputTmpDir, len); (void)memcpy(tmpPath, inputTmpDir, len);
static uint64_t seqId = 0; static uint64_t seqId = 0;
if (tmpPath[len - 1] != '/') { if (tmpPath[len - 1] != '/') {
tmpPath[len++] = '/'; tmpPath[len++] = '/';
} }
strcpy(tmpPath + len, TD_TMP_FILE_PREFIX); (void)strcpy(tmpPath + len, TD_TMP_FILE_PREFIX);
if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) { if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) {
strcat(tmpPath, fileNamePrefix); (void)strcat(tmpPath, fileNamePrefix);
strcat(tmpPath, "-%d-%s"); (void)strcat(tmpPath, "-%d-%s");
} }
char rand[32] = {0}; char rand[32] = {0};
sprintf(rand, "%" PRIu64, atomic_add_fetch_64(&seqId, 1)); (void)sprintf(rand, "%" PRIu64, atomic_add_fetch_64(&seqId, 1));
snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand); (void)snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand);
#endif #endif
} }
@ -123,6 +123,7 @@ int64_t taosCopyFile(const char *from, const char *to) {
char buffer[4096]; char buffer[4096];
int64_t size = 0; int64_t size = 0;
int64_t bytes; int64_t bytes;
int32_t code = TSDB_CODE_SUCCESS;
// fidfrom = open(from, O_RDONLY); // fidfrom = open(from, O_RDONLY);
TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ); TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ);
@ -143,7 +144,7 @@ int64_t taosCopyFile(const char *from, const char *to) {
if (bytes < sizeof(buffer)) break; if (bytes < sizeof(buffer)) break;
} }
int code = taosFsyncFile(pFileTo); code = taosFsyncFile(pFileTo);
taosCloseFile(&pFileFrom); taosCloseFile(&pFileFrom);
taosCloseFile(&pFileTo); taosCloseFile(&pFileTo);
@ -154,10 +155,12 @@ int64_t taosCopyFile(const char *from, const char *to) {
return size; return size;
_err: _err:
if (pFileFrom != NULL) taosCloseFile(&pFileFrom); if (pFileFrom != NULL) taosCloseFile(&pFileFrom);
if (pFileTo != NULL) taosCloseFile(&pFileTo); if (pFileTo != NULL) taosCloseFile(&pFileTo);
/* coverity[+retval] */ /* coverity[+retval] */
taosRemoveFile(to); taosRemoveFile(to);
return -1; return -1;
#endif #endif
} }
@ -301,11 +304,15 @@ FILE *taosOpenFileForStream(const char *path, int32_t tdFileOptions) {
} else { } else {
mode = (tdFileOptions & TD_FILE_TEXT) ? "rt+" : "rb+"; mode = (tdFileOptions & TD_FILE_TEXT) ? "rt+" : "rb+";
} }
ASSERT(!(tdFileOptions & TD_FILE_EXCL));
if (tdFileOptions & TD_FILE_EXCL) { if (tdFileOptions & TD_FILE_EXCL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL; return NULL;
} }
return fopen(path, mode); FILE* f = fopen(path, mode);
if (NULL == f) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return f;
} }
#ifdef WINDOWS #ifdef WINDOWS
@ -653,6 +660,9 @@ int taosOpenFileNotStream(const char *path, int32_t tdFileOptions) {
access |= (tdFileOptions & TD_FILE_CLOEXEC) ? O_CLOEXEC : 0; access |= (tdFileOptions & TD_FILE_CLOEXEC) ? O_CLOEXEC : 0;
int fd = open(path, access, S_IRWXU | S_IRWXG | S_IRWXO); int fd = open(path, access, S_IRWXU | S_IRWXG | S_IRWXO);
if (-1 == fd) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return fd; return fd;
} }
@ -1060,14 +1070,14 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
#ifdef WINDOWS #ifdef WINDOWS
if (hFile != NULL) CloseHandle(hFile); if (hFile != NULL) CloseHandle(hFile);
#else #else
if (fd >= 0) close(fd); if (fd >= 0) (void)close(fd);
#endif #endif
if (fp != NULL) fclose(fp); if (fp != NULL) (void)fclose(fp);
return NULL; return NULL;
} }
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
taosThreadRwlockInit(&(pFile->rwlock), NULL); (void)taosThreadRwlockInit(&(pFile->rwlock), NULL);
#endif #endif
pFile->fp = fp; pFile->fp = fp;
pFile->refId = 0; pFile->refId = 0;
@ -1082,9 +1092,15 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
// Remove it instantly, so when the program exits normally/abnormally, the file // Remove it instantly, so when the program exits normally/abnormally, the file
// will be automatically remove by OS. // will be automatically remove by OS.
if (tdFileOptions & TD_FILE_AUTO_DEL) { if (tdFileOptions & TD_FILE_AUTO_DEL) {
unlink(path); if (-1 == unlink(path)) {
terrno = TAOS_SYSTEM_ERROR(errno);
(void)close(fd);
taosMemoryFree(pFile);
return NULL;
}
} }
#endif #endif
return pFile; return pFile;
} }
@ -1094,11 +1110,11 @@ int32_t taosCloseFile(TdFilePtr *ppFile) {
return 0; return 0;
} }
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
taosThreadRwlockWrlock(&((*ppFile)->rwlock)); (void)taosThreadRwlockWrlock(&((*ppFile)->rwlock));
#endif #endif
if ((*ppFile)->fp != NULL) { if ((*ppFile)->fp != NULL) {
fflush((*ppFile)->fp); (void)fflush((*ppFile)->fp);
fclose((*ppFile)->fp); (void)fclose((*ppFile)->fp);
(*ppFile)->fp = NULL; (*ppFile)->fp = NULL;
} }
#ifdef WINDOWS #ifdef WINDOWS
@ -1113,16 +1129,20 @@ int32_t taosCloseFile(TdFilePtr *ppFile) {
// warning: never fsync silently in base lib // warning: never fsync silently in base lib
/*fsync((*ppFile)->fd);*/ /*fsync((*ppFile)->fd);*/
code = close((*ppFile)->fd); code = close((*ppFile)->fd);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
(*ppFile)->fd = -1; (*ppFile)->fd = -1;
#endif #endif
} }
(*ppFile)->refId = 0; (*ppFile)->refId = 0;
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
taosThreadRwlockUnlock(&((*ppFile)->rwlock)); (void)taosThreadRwlockUnlock(&((*ppFile)->rwlock));
taosThreadRwlockDestroy(&((*ppFile)->rwlock)); (void)taosThreadRwlockDestroy(&((*ppFile)->rwlock));
#endif #endif
taosMemoryFree(*ppFile); taosMemoryFree(*ppFile);
*ppFile = NULL; *ppFile = NULL;
return code; return code;
} }
@ -1155,20 +1175,28 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset)
} }
#else #else
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
taosThreadRwlockRdlock(&(pFile->rwlock)); (void)taosThreadRwlockRdlock(&(pFile->rwlock));
#endif #endif
ASSERT(pFile->fd >= 0); // Please check if you have closed the file. ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
if (pFile->fd < 0) { if (pFile->fd < 0) {
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
taosThreadRwlockUnlock(&(pFile->rwlock)); (void)taosThreadRwlockUnlock(&(pFile->rwlock));
#endif #endif
terrno = TSDB_CODE_INVALID_PARA;
return -1; return -1;
} }
int32_t code = 0;
int64_t ret = pread(pFile->fd, buf, count, offset); int64_t ret = pread(pFile->fd, buf, count, offset);
if (-1 == ret) {
code = TAOS_SYSTEM_ERROR(errno);
}
#endif #endif
#if FILE_WITH_LOCK #if FILE_WITH_LOCK
taosThreadRwlockUnlock(&(pFile->rwlock)); (void)taosThreadRwlockUnlock(&(pFile->rwlock));
#endif #endif
terrno = code;
return ret; return ret;
} }

View File

@ -26,7 +26,9 @@ void taosSsleep(int32_t s) {
#ifdef WINDOWS #ifdef WINDOWS
Sleep(1000 * s); Sleep(1000 * s);
#else #else
sleep(s); while (s > 0) {
s = sleep(s);
}
#endif #endif
} }
@ -35,7 +37,7 @@ void taosMsleep(int32_t ms) {
#ifdef WINDOWS #ifdef WINDOWS
Sleep(ms); Sleep(ms);
#else #else
usleep(ms * 1000); (void)usleep(ms * 1000);
#endif #endif
} }
@ -51,6 +53,6 @@ void taosUsleep(int32_t us) {
WaitForSingleObject(timer, INFINITE); WaitForSingleObject(timer, INFINITE);
CloseHandle(timer); CloseHandle(timer);
#else #else
usleep(us); (void)usleep(us);
#endif #endif
} }

View File

@ -80,7 +80,7 @@ void taos_block_sigalrm(void) {
static void taosDeleteTimer(void *tharg) { static void taosDeleteTimer(void *tharg) {
timer_t *pTimer = tharg; timer_t *pTimer = tharg;
timer_delete(*pTimer); (void)timer_delete(*pTimer);
} }
static TdThread timerThread; static TdThread timerThread;
@ -90,9 +90,9 @@ static volatile bool stopTimer = false;
static void *taosProcessAlarmSignal(void *tharg) { static void *taosProcessAlarmSignal(void *tharg) {
// Block the signal // Block the signal
sigset_t sigset; sigset_t sigset;
sigemptyset(&sigset); (void)sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM); (void)sigaddset(&sigset, SIGALRM);
sigprocmask(SIG_BLOCK, &sigset, NULL); (void)sigprocmask(SIG_BLOCK, &sigset, NULL);
void (*callback)(int) = tharg; void (*callback)(int) = tharg;
struct sigevent sevent = {{0}}; struct sigevent sevent = {{0}};
@ -110,7 +110,8 @@ static void *taosProcessAlarmSignal(void *tharg) {
sevent.sigev_signo = SIGALRM; sevent.sigev_signo = SIGALRM;
if (timer_create(CLOCK_REALTIME, &sevent, &timerId) == -1) { if (timer_create(CLOCK_REALTIME, &sevent, &timerId) == -1) {
// printf("Failed to create timer"); terrno = TAOS_SYSTEM_ERROR(errno);
return NULL;
} }
taosThreadCleanupPush(taosDeleteTimer, &timerId); taosThreadCleanupPush(taosDeleteTimer, &timerId);
@ -122,15 +123,17 @@ static void *taosProcessAlarmSignal(void *tharg) {
ts.it_interval.tv_sec = 0; ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 1000000 * MSECONDS_PER_TICK; ts.it_interval.tv_nsec = 1000000 * MSECONDS_PER_TICK;
if (timer_settime(timerId, 0, &ts, NULL)) { if (-1 == timer_settime(timerId, 0, &ts, NULL)) {
// printf("Failed to init timer"); terrno = TAOS_SYSTEM_ERROR(errno);
break; break;
} }
int signo; int signo;
int32_t code = 0;
while (!stopTimer) { while (!stopTimer) {
if (sigwait(&sigset, &signo)) { code = sigwait(&sigset, &signo);
// printf("Failed to wait signal: number %d", signo); if (code) {
terrno = TAOS_SYSTEM_ERROR(code);
continue; continue;
} }
/* //printf("Signal handling: number %d ......\n", signo); */ /* //printf("Signal handling: number %d ......\n", signo); */
@ -178,14 +181,12 @@ int taosInitTimer(void (*callback)(int), int ms) {
#else #else
stopTimer = false; stopTimer = false;
TdThreadAttr tattr; TdThreadAttr tattr;
taosThreadAttrInit(&tattr); (void)taosThreadAttrInit(&tattr);
int code = taosThreadCreate(&timerThread, &tattr, taosProcessAlarmSignal, callback); int code = taosThreadCreate(&timerThread, &tattr, taosProcessAlarmSignal, callback);
taosThreadAttrDestroy(&tattr); (void)taosThreadAttrDestroy(&tattr);
if (code != 0) { if (code != 0) {
// printf("failed to create timer thread"); // printf("failed to create timer thread");
return -1; return TAOS_SYSTEM_ERROR(code);
} else {
// printf("timer thread:0x%08" PRIx64 " is created", taosGetPthreadId(timerThread));
} }
return 0; return 0;
@ -210,7 +211,7 @@ void taosUninitTimer() {
stopTimer = true; stopTimer = true;
// printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread)); // printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread));
taosThreadJoin(timerThread, NULL); (void)taosThreadJoin(timerThread, NULL);
#endif #endif
} }

View File

@ -742,12 +742,20 @@ char *tz_win[554][2] = {{"Asia/Shanghai", "China Standard Time"},
static int isdst_now = 0; static int isdst_now = 0;
void taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, int8_t *outDaylight, int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, int8_t *outDaylight,
enum TdTimezone *tsTimezone) { enum TdTimezone *tsTimezone) {
if (inTimezoneStr == NULL || inTimezoneStr[0] == 0) return; if (inTimezoneStr == NULL || inTimezoneStr[0] == 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
int32_t code = TSDB_CODE_SUCCESS;
size_t len = strlen(inTimezoneStr); size_t len = strlen(inTimezoneStr);
char *buf = taosMemoryCalloc(len + 1, 1); if (len >= TD_TIMEZONE_LEN) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
char buf[TD_TIMEZONE_LEN] = {0};
for (int32_t i = 0; i < len; i++) { for (int32_t i = 0; i < len; i++) {
if (inTimezoneStr[i] == ' ' || inTimezoneStr[i] == '(') { if (inTimezoneStr[i] == ' ' || inTimezoneStr[i] == '(') {
buf[i] = 0; buf[i] = 0;
@ -813,17 +821,22 @@ void taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, int8
*outDaylight = isdst_now; *outDaylight = isdst_now;
#else #else
setenv("TZ", buf, 1); code = setenv("TZ", buf, 1);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
tzset(); tzset();
int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR); int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR);
*tsTimezone = tz; *tsTimezone = tz;
tz += isdst_now; tz += isdst_now;
sprintf(outTimezoneStr, "%s (%s, %s%02d00)", buf, tzname[isdst_now], tz >= 0 ? "+" : "-", abs(tz)); (void)sprintf(outTimezoneStr, "%s (%s, %s%02d00)", buf, tzname[isdst_now], tz >= 0 ? "+" : "-", abs(tz));
*outDaylight = isdst_now; *outDaylight = isdst_now;
#endif #endif
taosMemoryFree(buf); return code;
} }
void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) { void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
@ -914,7 +927,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
{ {
int n = readlink("/etc/localtime", buf, sizeof(buf)-1); int n = readlink("/etc/localtime", buf, sizeof(buf)-1);
if (n < 0) { if (n < 0) {
printf("read /etc/localtime error, reason:%s", strerror(errno)); (void)printf("read /etc/localtime error, reason:%s", strerror(errno));
if (taosCheckExistFile("/etc/timezone")) { if (taosCheckExistFile("/etc/timezone")) {
/* /*
@ -924,7 +937,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/ */
time_t tx1 = taosGetTimestampSec(); time_t tx1 = taosGetTimestampSec();
struct tm tm1; struct tm tm1;
taosLocalTime(&tx1, &tm1, NULL); (void)taosLocalTime(&tx1, &tm1, NULL);
/* load time zone string from /etc/timezone */ /* load time zone string from /etc/timezone */
// FILE *f = fopen("/etc/timezone", "r"); // FILE *f = fopen("/etc/timezone", "r");
errno = 0; errno = 0;
@ -933,12 +946,12 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
if (pFile != NULL) { if (pFile != NULL) {
int len = taosReadFile(pFile, buf, 64); int len = taosReadFile(pFile, buf, 64);
if (len < 64 && taosGetErrorFile(pFile)) { if (len < 64 && taosGetErrorFile(pFile)) {
taosCloseFile(&pFile); (void)taosCloseFile(&pFile);
printf("read /etc/timezone error, reason:%s", strerror(errno)); (void)printf("read /etc/timezone error, reason:%s", strerror(errno));
return; return;
} }
taosCloseFile(&pFile); (void)taosCloseFile(&pFile);
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
char *lineEnd = strstr(buf, "\n"); char *lineEnd = strstr(buf, "\n");
@ -948,7 +961,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
// for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables // for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables
if (strlen(buf) > 0) { if (strlen(buf) > 0) {
setenv("TZ", buf, 1); (void)setenv("TZ", buf, 1);
} }
} }
// get and set default timezone // get and set default timezone
@ -970,10 +983,10 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
* Asia/Shanghai (CST, +0800) * Asia/Shanghai (CST, +0800)
* Europe/London (BST, +0100) * Europe/London (BST, +0100)
*/ */
snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", (void)snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-",
abs(tz)); abs(tz));
} else { } else {
printf("There is not /etc/timezone.\n"); (void)printf("There is not /etc/timezone.\n");
} }
return; return;
} }
@ -981,7 +994,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
char *zi = strstr(buf, "zoneinfo"); char *zi = strstr(buf, "zoneinfo");
if (!zi) { if (!zi) {
printf("parsing /etc/localtime failed"); (void)printf("parsing /etc/localtime failed");
return; return;
} }
tz = zi + strlen("zoneinfo") + 1; tz = zi + strlen("zoneinfo") + 1;
@ -1000,7 +1013,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
// return; // return;
// } // }
setenv("TZ", tz, 1); (void)setenv("TZ", tz, 1);
tzset(); tzset();
} }
@ -1011,7 +1024,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/ */
time_t tx1 = taosGetTimestampSec(); time_t tx1 = taosGetTimestampSec();
struct tm tm1; struct tm tm1;
taosLocalTime(&tx1, &tm1, NULL); (void)taosLocalTime(&tx1, &tm1, NULL);
isdst_now = tm1.tm_isdst; isdst_now = tm1.tm_isdst;
/* /*
@ -1020,7 +1033,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
* Asia/Shanghai (CST, +0800) * Asia/Shanghai (CST, +0800)
* Europe/London (BST, +0100) * Europe/London (BST, +0100)
*/ */
snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0], (void)snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0],
-timezone / 3600); -timezone / 3600);
#endif #endif
} }

View File

@ -136,8 +136,7 @@ static int32_t cfgCheckAndSetConf(SConfigItem *pItem, const char *conf) {
static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) { static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
char fullDir[PATH_MAX] = {0}; char fullDir[PATH_MAX] = {0};
if (taosExpandDir(inputDir, fullDir, PATH_MAX) != 0) { if (taosExpandDir(inputDir, fullDir, PATH_MAX) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno); uError("failed to expand dir:%s", inputDir);
uError("failed to expand dir:%s since %s", inputDir, terrstr());
return -1; return -1;
} }