Merge pull request #26747 from taosdata/fix/TD-31073

fix: add function return code validation
This commit is contained in:
dapan1121 2024-07-24 08:35:29 +08:00 committed by GitHub
commit 02c90a2211
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 334 additions and 178 deletions

View File

@ -97,6 +97,7 @@ extern "C" {
#include <nmmintrin.h>
#endif
#include "osThread.h"
#include "osAtomic.h"

View File

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

View File

@ -65,28 +65,28 @@ int32_t taosGetTimestampSec();
//@return timestamp in millisecond
static FORCE_INLINE int64_t taosGetTimestampMs() {
struct timeval systemTime;
taosGetTimeOfDay(&systemTime);
(void)taosGetTimeOfDay(&systemTime);
return (int64_t)systemTime.tv_sec * 1000LL + (int64_t)systemTime.tv_usec / 1000;
}
//@return timestamp in microsecond
static FORCE_INLINE int64_t taosGetTimestampUs() {
struct timeval systemTime;
taosGetTimeOfDay(&systemTime);
(void)taosGetTimeOfDay(&systemTime);
return (int64_t)systemTime.tv_sec * 1000000LL + (int64_t)systemTime.tv_usec;
}
//@return timestamp in nanosecond
static FORCE_INLINE int64_t taosGetTimestampNs() {
struct timespec systemTime = {0};
taosClockGetTime(CLOCK_REALTIME, &systemTime);
(void)taosClockGetTime(CLOCK_REALTIME, &systemTime);
return (int64_t)systemTime.tv_sec * 1000000000LL + (int64_t)systemTime.tv_nsec;
}
//@return timestamp of monotonic clock in millisecond
static FORCE_INLINE int64_t taosGetMonoTimestampMs() {
struct timespec systemTime = {0};
taosClockGetTime(CLOCK_MONOTONIC, &systemTime);
(void)taosClockGetTime(CLOCK_MONOTONIC, &systemTime);
return (int64_t)systemTime.tv_sec * 1000LL + (int64_t)systemTime.tv_nsec / 1000000;
}

View File

@ -55,7 +55,7 @@ enum TdTimezone {
};
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
}

View File

@ -801,9 +801,8 @@ int32_t monitorInit() {
}
if (taosMulModeMkDir(tmpSlowLogPath, 0777, true) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
tscError("failed to create dir:%s since %s", tmpSlowLogPath, terrstr());
return -1;
return terrno;
}
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);
if (taosMulModeMkDir(tsLogDir, 0777, true) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
printf("failed to create dir:%s since %s", tsLogDir, terrstr());
cfgCleanup(pCfg);
return -1;

View File

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

View File

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

View File

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

View File

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

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);
if (ret < 0) {
return TAOS_SYSTEM_ERROR(ret);
return terrno;
}
#ifdef USE_MAINDB

View File

@ -103,7 +103,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) {
tstrncpy(pWal->path, path, sizeof(pWal->path));
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;
}

View File

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

View File

@ -45,11 +45,16 @@ char tsAVX2Supported = 0;
char tsFMASupported = 0;
char tsAVX512Supported = 0;
void osDefaultInit() {
int32_t osDefaultInit() {
int32_t code = TSDB_CODE_SUCCESS;
taosSeedRand(taosSafeRand());
taosGetSystemLocale(tsLocale, tsCharset);
taosGetSystemTimezone(tsTimezoneStr, &tsTimezone);
taosSetSystemTimezone(tsTimezoneStr, tsTimezoneStr, &tsDaylight, &tsTimezone);
code = taosSetSystemTimezone(tsTimezoneStr, tsTimezoneStr, &tsDaylight, &tsTimezone);
if (code) {
return code;
}
taosGetSystemInfo();
// deadlock in query
@ -65,33 +70,35 @@ void osDefaultInit() {
tmpDir = getenv("temp");
}
if (tmpDir != NULL) {
strcpy(tsTempDir, tmpDir);
(void)strcpy(tsTempDir, tmpDir);
}
strcpy(tsOsName, "Windows");
(void)strcpy(tsOsName, "Windows");
#elif defined(_TD_DARWIN_64)
strcpy(tsOsName, "Darwin");
(void)strcpy(tsOsName, "Darwin");
#else
strcpy(tsOsName, "Linux");
(void)strcpy(tsOsName, "Linux");
#endif
if (configDir[0] == 0) {
strcpy(configDir, TD_CFG_DIR_PATH);
(void)strcpy(configDir, TD_CFG_DIR_PATH);
}
strcpy(tsDataDir, TD_DATA_DIR_PATH);
strcpy(tsLogDir, TD_LOG_DIR_PATH);
(void)strcpy(tsDataDir, TD_DATA_DIR_PATH);
(void)strcpy(tsLogDir, TD_LOG_DIR_PATH);
if(strlen(tsTempDir) == 0){
strcpy(tsTempDir, TD_TMP_DIR_PATH);
(void)strcpy(tsTempDir, TD_TMP_DIR_PATH);
}
return code;
}
void osUpdate() {
if (tsLogDir[0] != 0) {
taosGetDiskSize(tsLogDir, &tsLogSpace.size);
(void)taosGetDiskSize(tsLogDir, &tsLogSpace.size);
}
if (tsDataDir[0] != 0) {
taosGetDiskSize(tsDataDir, &tsDataSpace.size);
(void)taosGetDiskSize(tsDataDir, &tsDataSpace.size);
}
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; }
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) {
memcpy(tsLocale, inLocale, strlen(inLocale) + 1);
memcpy(tsCharset, inCharSet, strlen(inCharSet) + 1);
(void)memcpy(tsLocale, inLocale, strlen(inLocale) + 1);
(void)memcpy(tsCharset, inCharSet, strlen(inCharSet) + 1);
}
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];
int32_t len = strlen(inputTmpDir);
memcpy(tmpPath, inputTmpDir, len);
(void)memcpy(tmpPath, inputTmpDir, len);
static uint64_t seqId = 0;
if (tmpPath[len - 1] != '/') {
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) {
strcat(tmpPath, fileNamePrefix);
strcat(tmpPath, "-%d-%s");
(void)strcat(tmpPath, fileNamePrefix);
(void)strcat(tmpPath, "-%d-%s");
}
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
}
@ -123,41 +123,60 @@ int64_t taosCopyFile(const char *from, const char *to) {
char buffer[4096];
int64_t size = 0;
int64_t bytes;
int32_t code = TSDB_CODE_SUCCESS;
// fidfrom = open(from, O_RDONLY);
TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ);
if (pFileFrom == NULL) goto _err;
if (pFileFrom == NULL) {
code = terrno;
goto _err;
}
// fidto = open(to, O_WRONLY | O_CREAT | O_EXCL, 0755);
TdFilePtr pFileTo = taosOpenFile(to, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_EXCL);
if (pFileTo == NULL) goto _err;
if (pFileTo == NULL) {
code = terrno;
goto _err;
}
while (true) {
bytes = taosReadFile(pFileFrom, buffer, sizeof(buffer));
if (bytes < 0) goto _err;
if (bytes < 0) {
code = terrno;
goto _err;
}
if (bytes == 0) break;
size += bytes;
if (taosWriteFile(pFileTo, (void *)buffer, bytes) < bytes) goto _err;
if (taosWriteFile(pFileTo, (void *)buffer, bytes) < bytes) {
code = terrno;
goto _err;
}
if (bytes < sizeof(buffer)) break;
}
int code = taosFsyncFile(pFileTo);
code = taosFsyncFile(pFileTo);
taosCloseFile(&pFileFrom);
taosCloseFile(&pFileTo);
(void)taosCloseFile(&pFileFrom);
(void)taosCloseFile(&pFileTo);
if (code != 0) {
terrno = code;
return -1;
}
return size;
_err:
if (pFileFrom != NULL) taosCloseFile(&pFileFrom);
if (pFileTo != NULL) taosCloseFile(&pFileTo);
if (pFileFrom != NULL) (void)taosCloseFile(&pFileFrom);
if (pFileTo != NULL) (void)taosCloseFile(&pFileTo);
/* coverity[+retval] */
taosRemoveFile(to);
(void)taosRemoveFile(to);
terrno = code;
return -1;
#endif
}
@ -165,24 +184,31 @@ _err:
TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) {
TdFilePtr fp = taosOpenFile(path, tdFileOptions);
if (!fp) {
if (errno == ENOENT) {
if (terrno == TAOS_SYSTEM_ERROR(ENOENT)) {
// Try to create directory recursively
char *s = taosStrdup(path);
char s[PATH_MAX];
tstrncpy(s, path, sizeof(s));
if (taosMulMkDir(taosDirName(s)) != 0) {
taosMemoryFree(s);
return NULL;
}
taosMemoryFree(s);
fp = taosOpenFile(path, tdFileOptions);
if (!fp) {
return NULL;
}
}
}
return fp;
}
int32_t taosRemoveFile(const char *path) { return remove(path); }
int32_t taosRemoveFile(const char *path) {
int32_t code = remove(path);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return code;
}
int32_t taosRenameFile(const char *oldName, const char *newName) {
#ifdef WINDOWS
@ -214,8 +240,9 @@ int32_t taosRenameFile(const char *oldName, const char *newName) {
return finished ? 0 : -1;
#else
int32_t code = rename(oldName, newName);
if (code < 0) {
printf("failed to rename file %s to %s, reason:%s\n", oldName, newName, strerror(errno));
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return code;
@ -230,8 +257,9 @@ int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime, int32_t *a
struct stat fileStat;
int32_t code = stat(path, &fileStat);
#endif
if (code < 0) {
return code;
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
if (size != NULL) {
@ -269,13 +297,15 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) {
#else
if (pFile == NULL || pFile->fd < 0) {
return TSDB_CODE_INVALID_PARA;
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
struct stat fileStat;
int32_t code = fstat(pFile->fd, &fileStat);
if (code < 0) {
printf("taosFStatFile run fstat fail.");
return TAOS_SYSTEM_ERROR(errno);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
if (stDev != NULL) {
@ -301,11 +331,15 @@ FILE *taosOpenFileForStream(const char *path, int32_t tdFileOptions) {
} else {
mode = (tdFileOptions & TD_FILE_TEXT) ? "rt+" : "rb+";
}
ASSERT(!(tdFileOptions & TD_FILE_EXCL));
if (tdFileOptions & TD_FILE_EXCL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL;
}
return fopen(path, mode);
FILE* f = fopen(path, mode);
if (NULL == f) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return f;
}
#ifdef WINDOWS
@ -653,6 +687,9 @@ int taosOpenFileNotStream(const char *path, int32_t tdFileOptions) {
access |= (tdFileOptions & TD_FILE_CLOEXEC) ? O_CLOEXEC : 0;
int fd = open(path, access, S_IRWXU | S_IRWXG | S_IRWXO);
if (-1 == fd) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return fd;
}
@ -1060,14 +1097,14 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
#ifdef WINDOWS
if (hFile != NULL) CloseHandle(hFile);
#else
if (fd >= 0) close(fd);
if (fd >= 0) (void)close(fd);
#endif
if (fp != NULL) fclose(fp);
if (fp != NULL) (void)fclose(fp);
return NULL;
}
#if FILE_WITH_LOCK
taosThreadRwlockInit(&(pFile->rwlock), NULL);
(void)taosThreadRwlockInit(&(pFile->rwlock), NULL);
#endif
pFile->fp = fp;
pFile->refId = 0;
@ -1082,9 +1119,15 @@ TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
// Remove it instantly, so when the program exits normally/abnormally, the file
// will be automatically remove by OS.
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
return pFile;
}
@ -1094,11 +1137,11 @@ int32_t taosCloseFile(TdFilePtr *ppFile) {
return 0;
}
#if FILE_WITH_LOCK
taosThreadRwlockWrlock(&((*ppFile)->rwlock));
(void)taosThreadRwlockWrlock(&((*ppFile)->rwlock));
#endif
if ((*ppFile)->fp != NULL) {
fflush((*ppFile)->fp);
fclose((*ppFile)->fp);
(void)fflush((*ppFile)->fp);
(void)fclose((*ppFile)->fp);
(*ppFile)->fp = NULL;
}
#ifdef WINDOWS
@ -1113,16 +1156,20 @@ int32_t taosCloseFile(TdFilePtr *ppFile) {
// warning: never fsync silently in base lib
/*fsync((*ppFile)->fd);*/
code = close((*ppFile)->fd);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
(*ppFile)->fd = -1;
#endif
}
(*ppFile)->refId = 0;
#if FILE_WITH_LOCK
taosThreadRwlockUnlock(&((*ppFile)->rwlock));
taosThreadRwlockDestroy(&((*ppFile)->rwlock));
(void)taosThreadRwlockUnlock(&((*ppFile)->rwlock));
(void)taosThreadRwlockDestroy(&((*ppFile)->rwlock));
#endif
taosMemoryFree(*ppFile);
*ppFile = NULL;
return code;
}
@ -1131,6 +1178,8 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset)
return 0;
}
int32_t code = 0;
#ifdef WINDOWS
#if FILE_WITH_LOCK
taosThreadRwlockRdlock(&(pFile->rwlock));
@ -1155,20 +1204,27 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset)
}
#else
#if FILE_WITH_LOCK
taosThreadRwlockRdlock(&(pFile->rwlock));
(void)taosThreadRwlockRdlock(&(pFile->rwlock));
#endif
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
if (pFile->fd < 0) {
#if FILE_WITH_LOCK
taosThreadRwlockUnlock(&(pFile->rwlock));
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
#endif
terrno = TSDB_CODE_INVALID_PARA;
return -1;
}
int64_t ret = pread(pFile->fd, buf, count, offset);
if (-1 == ret) {
code = TAOS_SYSTEM_ERROR(errno);
}
#endif
#if FILE_WITH_LOCK
taosThreadRwlockUnlock(&(pFile->rwlock));
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
#endif
terrno = code;
return ret;
}
@ -1177,20 +1233,36 @@ int32_t taosFsyncFile(TdFilePtr pFile) {
return 0;
}
int32_t code = 0;
// this implementation is WRONG
// fflush is not a replacement of fsync
if (pFile->fp != NULL) return fflush(pFile->fp);
if (pFile->fp != NULL) {
code = fflush(pFile->fp);
if (0 != code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return code;
}
#ifdef WINDOWS
if (pFile->hFile != NULL) {
if (pFile->tdFileOptions & TD_FILE_WRITE_THROUGH) {
return 0;
}
return !FlushFileBuffers(pFile->hFile);
}
#else
if (pFile->fd >= 0) {
return fsync(pFile->fd);
#endif
code = fsync(pFile->fd);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
}
#endif
return 0;
}

View File

@ -186,7 +186,7 @@ static void print_line(Dwarf_Debug dbg, Dwarf_Line line, Dwarf_Addr pc) {
dwarf_linesrc(line, &linesrc, NULL);
dwarf_lineno(line, &lineno, NULL);
}
printf("BackTrace %08" PRId64 " %s:%" DW_PR_DUu "\n", taosGetSelfPthreadId(), linesrc, lineno);
(void)printf("BackTrace %08" PRId64 " %s:%" DW_PR_DUu "\n", taosGetSelfPthreadId(), linesrc, lineno);
if (line) dwarf_dealloc(dbg, linesrc, DW_DLA_STRING);
}
void taosPrintBackTrace() {
@ -266,7 +266,11 @@ void *taosMemoryMalloc(int64_t size) {
return (char *)tmp + sizeof(TdMemoryInfo);
#else
return malloc(size);
void* p = malloc(size);
if (NULL == p) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
}
return p;
#endif
}
@ -283,7 +287,11 @@ void *taosMemoryCalloc(int64_t num, int64_t size) {
return (char *)tmp + sizeof(TdMemoryInfo);
#else
return calloc(num, size);
void* p = calloc(num, size);
if (NULL == p) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
}
return p;
#endif
}
@ -309,7 +317,11 @@ void *taosMemoryRealloc(void *ptr, int64_t size) {
return (char *)tmp + sizeof(TdMemoryInfo);
#else
return realloc(ptr, size);
void* p = realloc(ptr, size);
if (size > 0 && NULL == p) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
}
return p;
#endif
}
@ -330,7 +342,12 @@ char *taosStrdup(const char *ptr) {
return (char *)tmp + sizeof(TdMemoryInfo);
#else
return tstrdup(ptr);
char* p = tstrdup(ptr);
if (ptr != NULL && NULL == p) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
}
return p;
#endif
}
@ -357,8 +374,7 @@ int64_t taosMemorySize(void *ptr) {
TdMemoryInfoPtr pTdMemoryInfo = (TdMemoryInfoPtr)((char *)ptr - sizeof(TdMemoryInfo));
ASSERT(pTdMemoryInfo->symbol == TD_MEMORY_SYMBOL);
if (pTdMemoryInfo->symbol != TD_MEMORY_SYMBOL) {
+return NULL;
+
return NULL;
}
return pTdMemoryInfo->memorySize;
@ -378,7 +394,7 @@ void taosMemoryTrim(int32_t size) {
// do nothing
return;
#else
malloc_trim(size);
(void)malloc_trim(size);
#endif
}
@ -388,6 +404,13 @@ void *taosMemoryMallocAlign(uint32_t alignment, int64_t size) {
#else
#if defined(LINUX)
void *p = memalign(alignment, size);
if (NULL == p) {
if (ENOMEM == errno) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
} else {
terrno = TAOS_SYSTEM_ERROR(errno);
}
}
return p;
#else
return taosMemoryMalloc(size);

View File

@ -69,7 +69,7 @@ uint32_t taosSafeRand(void) {
if (len < 0) {
seed = (int)taosGetTimestampSec();
}
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
}
return (uint32_t)seed;

View File

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

View File

@ -345,6 +345,7 @@ char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) {
}
int32_t taosGetTimeOfDay(struct timeval *tv) {
int32_t code = 0;
#ifdef WINDOWS
LARGE_INTEGER t;
FILETIME f;
@ -359,11 +360,18 @@ int32_t taosGetTimeOfDay(struct timeval *tv) {
tv->tv_usec = (t.QuadPart % 10000000) / 10;
return 0;
#else
return gettimeofday(tv, NULL);
code = gettimeofday(tv, NULL);
return (-1 == code) ? (terrno = TAOS_SYSTEM_ERROR(errno)) : 0;
#endif
}
time_t taosTime(time_t *t) { return time(t); }
time_t taosTime(time_t *t) {
time_t r = time(t);
if (r == (time_t)-1) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return r;
}
/*
* mktime64 - Converts date to seconds.
@ -458,7 +466,11 @@ time_t taosMktime(struct tm *timep) {
timep->tm_sec, tz);
#endif
#else
return mktime(timep);
time_t r = mktime(timep);
if (r == (time_t)-1) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return r;
#endif
}
@ -470,7 +482,7 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) {
if (result == NULL) {
res = localtime(timep);
if (res == NULL && buf != NULL) {
sprintf(buf, "NaN");
(void)sprintf(buf, "NaN");
}
return res;
}
@ -528,7 +540,7 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) {
#else
res = localtime_r(timep, result);
if (res == NULL && buf != NULL) {
sprintf(buf, "NaN");
(void)sprintf(buf, "NaN");
}
#endif
return result;
@ -642,6 +654,7 @@ struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst)
int32_t taosGetTimestampSec() { return (int32_t)time(NULL); }
int32_t taosClockGetTime(int clock_id, struct timespec *pTS) {
int32_t code = 0;
#ifdef WINDOWS
LARGE_INTEGER t;
FILETIME f;
@ -656,6 +669,7 @@ int32_t taosClockGetTime(int clock_id, struct timespec *pTS) {
pTS->tv_nsec = (t.QuadPart % 10000000) * 100;
return (0);
#else
return clock_gettime(clock_id, pTS);
code = clock_gettime(clock_id, pTS);
return (-1 == code) ? (terrno = TAOS_SYSTEM_ERROR(errno)) : 0;
#endif
}

View File

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

View File

@ -742,12 +742,20 @@ char *tz_win[554][2] = {{"Asia/Shanghai", "China Standard Time"},
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) {
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);
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++) {
if (inTimezoneStr[i] == ' ' || inTimezoneStr[i] == '(') {
buf[i] = 0;
@ -813,17 +821,22 @@ void taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, int8
*outDaylight = isdst_now;
#else
setenv("TZ", buf, 1);
code = setenv("TZ", buf, 1);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
tzset();
int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR);
*tsTimezone = tz;
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;
#endif
taosMemoryFree(buf);
return code;
}
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);
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")) {
/*
@ -924,7 +937,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/
time_t tx1 = taosGetTimestampSec();
struct tm tm1;
taosLocalTime(&tx1, &tm1, NULL);
(void)taosLocalTime(&tx1, &tm1, NULL);
/* load time zone string from /etc/timezone */
// FILE *f = fopen("/etc/timezone", "r");
errno = 0;
@ -933,12 +946,12 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
if (pFile != NULL) {
int len = taosReadFile(pFile, buf, 64);
if (len < 64 && taosGetErrorFile(pFile)) {
taosCloseFile(&pFile);
printf("read /etc/timezone error, reason:%s", strerror(errno));
(void)taosCloseFile(&pFile);
(void)printf("read /etc/timezone error, reason:%s", strerror(errno));
return;
}
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
buf[sizeof(buf) - 1] = 0;
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
if (strlen(buf) > 0) {
setenv("TZ", buf, 1);
(void)setenv("TZ", buf, 1);
}
}
// get and set default timezone
@ -970,10 +983,10 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
* Asia/Shanghai (CST, +0800)
* 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));
} else {
printf("There is not /etc/timezone.\n");
(void)printf("There is not /etc/timezone.\n");
}
return;
}
@ -981,7 +994,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
char *zi = strstr(buf, "zoneinfo");
if (!zi) {
printf("parsing /etc/localtime failed");
(void)printf("parsing /etc/localtime failed");
return;
}
tz = zi + strlen("zoneinfo") + 1;
@ -1000,7 +1013,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
// return;
// }
setenv("TZ", tz, 1);
(void)setenv("TZ", tz, 1);
tzset();
}
@ -1011,7 +1024,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/
time_t tx1 = taosGetTimestampSec();
struct tm tm1;
taosLocalTime(&tx1, &tm1, NULL);
(void)taosLocalTime(&tx1, &tm1, NULL);
isdst_now = tm1.tm_isdst;
/*
@ -1020,7 +1033,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
* Asia/Shanghai (CST, +0800)
* 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);
#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) {
char fullDir[PATH_MAX] = {0};
if (taosExpandDir(inputDir, fullDir, PATH_MAX) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
uError("failed to expand dir:%s since %s", inputDir, terrstr());
uError("failed to expand dir:%s", inputDir);
return -1;
}