From ffe2973ade468cef70cab78350ae6a9eaf8d92a5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 19 Jul 2024 13:19:01 +0800 Subject: [PATCH 1/5] refactor return code --- source/dnode/vnode/src/inc/tsdb.h | 3 +- .../dnode/vnode/src/tsdb/tsdbReaderWriter.c | 8 +- source/dnode/vnode/src/tsdb/tsdbSnapInfo.c | 199 ++++++++++-------- source/dnode/vnode/src/tsdb/tsdbUtil.c | 83 +++++--- source/libs/sync/src/syncUtil.c | 16 +- 5 files changed, 175 insertions(+), 134 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 3d26104722..d496853cd5 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -226,7 +226,7 @@ void tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32 int32_t tMapDataSearch(SMapData *pMapData, void *pSearchItem, int32_t (*tGetItemFn)(uint8_t *, void *), int32_t (*tItemCmprFn)(const void *, const void *), void *pItem); int32_t tPutMapData(uint8_t *p, SMapData *pMapData); -int32_t tGetMapData(uint8_t *p, SMapData *pMapData); +int32_t tGetMapData(uint8_t *p, SMapData *pMapData, int32_t *decodedSize); int32_t tMapDataToArray(SMapData *pMapData, int32_t itemSize, int32_t (*tGetItemFn)(uint8_t *, void *), SArray **ppArray); // other @@ -706,7 +706,6 @@ typedef TARRAY2(STsdbFSetPartition *) STsdbFSetPartList; STsdbFSetPartList *tsdbFSetPartListCreate(); void tsdbFSetPartListDestroy(STsdbFSetPartList **ppList); -int32_t tSerializeTsdbFSetPartList(void *buf, int32_t bufLen, STsdbFSetPartList *pList); int32_t tDeserializeTsdbFSetPartList(void *buf, int32_t bufLen, STsdbFSetPartList *pList); int32_t tsdbFSetPartListToRangeDiff(STsdbFSetPartList *pList, TFileSetRangeArray **ppRanges); diff --git a/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c b/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c index 932bf2d92c..dfc7dd8c7c 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c +++ b/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c @@ -803,11 +803,9 @@ int32_t tsdbReadDataBlk(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *m if (code) goto _err; // decode - int64_t n = tGetMapData(pReader->aBuf[0], mDataBlk); - if (n < 0) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto _err; - } + int32_t n; + code = tGetMapData(pReader->aBuf[0], mDataBlk, &n); + if (code) goto _err; ASSERT(n == size); return code; diff --git a/source/dnode/vnode/src/tsdb/tsdbSnapInfo.c b/source/dnode/vnode/src/tsdb/tsdbSnapInfo.c index bc7bf5449e..f515cd5352 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSnapInfo.c +++ b/source/dnode/vnode/src/tsdb/tsdbSnapInfo.c @@ -81,7 +81,7 @@ static int32_t tsdbFTypeToFRangeType(tsdb_ftype_t ftype) { static int32_t tsdbTFileSetToFSetPartition(STFileSet* fset, STsdbFSetPartition** ppSP) { STsdbFSetPartition* p = tsdbFSetPartitionCreate(); if (p == NULL) { - goto _err; + return terrno; } p->fid = fset->fid; @@ -134,10 +134,6 @@ static int32_t tsdbTFileSetToFSetPartition(STFileSet* fset, STsdbFSetPartition** } ppSP[0] = p; return 0; - -_err: - tsdbFSetPartitionClear(&p); - return -1; } // fset partition list @@ -160,9 +156,11 @@ void tsdbFSetPartListDestroy(STsdbFSetPartList** ppList) { } int32_t tsdbFSetPartListToRangeDiff(STsdbFSetPartList* pList, TFileSetRangeArray** ppRanges) { + int32_t code = 0; + TFileSetRangeArray* pDiff = taosMemoryCalloc(1, sizeof(TFileSetRangeArray)); if (pDiff == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + code = TSDB_CODE_OUT_OF_MEMORY; goto _err; } TARRAY2_INIT(pDiff); @@ -171,7 +169,7 @@ int32_t tsdbFSetPartListToRangeDiff(STsdbFSetPartList* pList, TFileSetRangeArray TARRAY2_FOREACH(pList, part) { STFileSetRange* r = taosMemoryCalloc(1, sizeof(STFileSetRange)); if (r == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + code = TSDB_CODE_OUT_OF_MEMORY; goto _err; } int64_t maxVerValid = -1; @@ -202,7 +200,7 @@ _err: if (pDiff) { tsdbTFileSetRangeArrayDestroy(&pDiff); } - return -1; + return code; } // serialization @@ -235,96 +233,103 @@ int32_t tTsdbFSetPartListDataLenCalc(STsdbFSetPartList* pList) { return datLen; } -int32_t tSerializeTsdbFSetPartList(void* buf, int32_t bufLen, STsdbFSetPartList* pList) { +static int32_t tSerializeTsdbFSetPartList(void* buf, int32_t bufLen, STsdbFSetPartList* pList, int32_t* encodeSize) { SEncoder encoder = {0}; + int8_t reserved8 = 0; + int16_t reserved16 = 0; + int64_t reserved64 = 0; + int8_t msgVer = TSDB_SNAP_MSG_VER; + int32_t len = TARRAY2_SIZE(pList); + int32_t code = 0; + tEncoderInit(&encoder, buf, bufLen); - - int8_t reserved8 = 0; - int16_t reserved16 = 0; - int64_t reserved64 = 0; - - int8_t msgVer = TSDB_SNAP_MSG_VER; - int32_t len = TARRAY2_SIZE(pList); - - if (tStartEncode(&encoder) < 0) goto _err; - if (tEncodeI8(&encoder, msgVer) < 0) goto _err; - if (tEncodeI32(&encoder, len) < 0) goto _err; + if ((code = tStartEncode(&encoder))) goto _exit; + if ((code = tEncodeI8(&encoder, msgVer))) goto _exit; + if ((code = tEncodeI32(&encoder, len))) goto _exit; for (int32_t u = 0; u < len; u++) { STsdbFSetPartition* p = TARRAY2_GET(pList, u); - if (tEncodeI64(&encoder, p->fid) < 0) goto _err; - if (tEncodeI8(&encoder, p->stat) < 0) goto _err; - if (tEncodeI8(&encoder, reserved8) < 0) goto _err; - if (tEncodeI16(&encoder, reserved16) < 0) goto _err; + if ((code = tEncodeI64(&encoder, p->fid))) goto _exit; + if ((code = tEncodeI8(&encoder, p->stat))) goto _exit; + if ((code = tEncodeI8(&encoder, reserved8))) goto _exit; + if ((code = tEncodeI16(&encoder, reserved16))) goto _exit; int32_t typMax = TSDB_FSET_RANGE_TYP_MAX; - if (tEncodeI32(&encoder, typMax) < 0) goto _err; + if ((code = tEncodeI32(&encoder, typMax))) goto _exit; for (int32_t i = 0; i < typMax; i++) { SVerRangeList* iList = &p->verRanges[i]; int32_t iLen = TARRAY2_SIZE(iList); - if (tEncodeI32(&encoder, iLen) < 0) goto _err; + if ((code = tEncodeI32(&encoder, iLen))) goto _exit; for (int32_t j = 0; j < iLen; j++) { SVersionRange r = TARRAY2_GET(iList, j); - if (tEncodeI64(&encoder, r.minVer) < 0) goto _err; - if (tEncodeI64(&encoder, r.maxVer) < 0) goto _err; - if (tEncodeI64(&encoder, reserved64) < 0) goto _err; + if ((code = tEncodeI64(&encoder, r.minVer))) goto _exit; + if ((code = tEncodeI64(&encoder, r.maxVer))) goto _exit; + if ((code = tEncodeI64(&encoder, reserved64))) goto _exit; } } } tEndEncode(&encoder); - int32_t tlen = encoder.pos; - tEncoderClear(&encoder); - return tlen; -_err: + if (encodeSize) { + encodeSize[0] = encoder.pos; + } + +_exit: tEncoderClear(&encoder); - return -1; + return code; } int32_t tDeserializeTsdbFSetPartList(void* buf, int32_t bufLen, STsdbFSetPartList* pList) { SDecoder decoder = {0}; - tDecoderInit(&decoder, buf, bufLen); - - int8_t reserved8 = 0; - int16_t reserved16 = 0; - int64_t reserved64 = 0; + int8_t reserved8 = 0; + int16_t reserved16 = 0; + int64_t reserved64 = 0; + int32_t code = 0; STsdbFSetPartition* p = NULL; + tDecoderInit(&decoder, buf, bufLen); int8_t msgVer = 0; int32_t len = 0; - if (tStartDecode(&decoder) < 0) goto _err; - if (tDecodeI8(&decoder, &msgVer) < 0) goto _err; - if (msgVer != TSDB_SNAP_MSG_VER) goto _err; - if (tDecodeI32(&decoder, &len) < 0) goto _err; + if ((code = tStartDecode(&decoder))) goto _err; + if ((code = tDecodeI8(&decoder, &msgVer))) goto _err; + if (msgVer != TSDB_SNAP_MSG_VER) { + code = TSDB_CODE_INVALID_MSG; + goto _err; + } + if ((code = tDecodeI32(&decoder, &len))) goto _err; for (int32_t u = 0; u < len; u++) { p = tsdbFSetPartitionCreate(); - if (p == NULL) goto _err; - if (tDecodeI64(&decoder, &p->fid) < 0) goto _err; - if (tDecodeI8(&decoder, &p->stat) < 0) goto _err; - if (tDecodeI8(&decoder, &reserved8) < 0) goto _err; - if (tDecodeI16(&decoder, &reserved16) < 0) goto _err; + if (p == NULL) { + code = terrno; + goto _err; + } + + if ((code = tDecodeI64(&decoder, &p->fid))) goto _err; + if ((code = tDecodeI8(&decoder, &p->stat))) goto _err; + if ((code = tDecodeI8(&decoder, &reserved8))) goto _err; + if ((code = tDecodeI16(&decoder, &reserved16))) goto _err; int32_t typMax = 0; - if (tDecodeI32(&decoder, &typMax) < 0) goto _err; + if ((code = tDecodeI32(&decoder, &typMax))) goto _err; for (int32_t i = 0; i < typMax; i++) { SVerRangeList* iList = &p->verRanges[i]; int32_t iLen = 0; - if (tDecodeI32(&decoder, &iLen) < 0) goto _err; + if ((code = tDecodeI32(&decoder, &iLen))) goto _err; for (int32_t j = 0; j < iLen; j++) { SVersionRange r = {0}; - if (tDecodeI64(&decoder, &r.minVer) < 0) goto _err; - if (tDecodeI64(&decoder, &r.maxVer) < 0) goto _err; - if (tDecodeI64(&decoder, &reserved64) < 0) goto _err; - if (TARRAY2_APPEND(iList, r)) goto _err; + if ((code = tDecodeI64(&decoder, &r.minVer))) goto _err; + if ((code = tDecodeI64(&decoder, &r.maxVer))) goto _err; + if ((code = tDecodeI64(&decoder, &reserved64))) goto _err; + if ((code = TARRAY2_APPEND(iList, r))) goto _err; } } - if (TARRAY2_APPEND(pList, p)) goto _err; + if ((code = TARRAY2_APPEND(pList, p))) goto _err; p = NULL; } @@ -337,7 +342,7 @@ _err: tsdbFSetPartitionClear(&p); } tDecoderClear(&decoder); - return -1; + return code; } // fs state @@ -352,8 +357,9 @@ static STsdbFSetPartList* tsdbSnapGetFSetPartList(STFileSystem* fs) { STFileSet* fset; TARRAY2_FOREACH(fs->fSetArr, fset) { STsdbFSetPartition* pItem = NULL; - if (tsdbTFileSetToFSetPartition(fset, &pItem) < 0) { - code = -1; + code = tsdbTFileSetToFSetPartition(fset, &pItem); + if (code) { + terrno = code; break; } ASSERT(pItem != NULL); @@ -404,7 +410,9 @@ static int32_t tsdbPartitionInfoInit(SVnode* pVnode, STsdbPartitionInfo* pInfo) for (int32_t j = 0; j < pInfo->tsdbMaxCnt; ++j) { STsdb* pTsdb = SMA_RSMA_GET_TSDB(pVnode, j); pInfo->pLists[j] = tsdbSnapGetFSetPartList(pTsdb->pFS); - if (pInfo->pLists[j] == NULL) return -1; + if (pInfo->pLists[j] == NULL) { + return terrno; + } } return 0; } @@ -432,9 +440,10 @@ static int32_t tsdbPartitionInfoSerialize(STsdbPartitionInfo* pInfo, uint8_t* bu SSyncTLV* pSubHead = (void*)((char*)buf + offset); int32_t valOffset = offset + sizeof(*pSubHead); ASSERT(pSubHead->val == (char*)buf + valOffset); - if ((tlen = tSerializeTsdbFSetPartList(pSubHead->val, bufLen - valOffset, pInfo->pLists[j])) < 0) { + int32_t code = tSerializeTsdbFSetPartList(pSubHead->val, bufLen - valOffset, pInfo->pLists[j], &tlen); + if (code) { tsdbError("vgId:%d, failed to serialize fset partition list of tsdb %d since %s", pInfo->vgId, j, terrstr()); - return -1; + return code; } pSubHead->typ = pInfo->subTyps[j]; pSubHead->len = tlen; @@ -460,17 +469,18 @@ static int32_t tTsdbRepOptsDataLenCalc(STsdbRepOpts* pInfo) { } int32_t tSerializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) { + int32_t code = 0; SEncoder encoder = {0}; + int64_t reserved64 = 0; + int8_t msgVer = TSDB_SNAP_MSG_VER; + tEncoderInit(&encoder, buf, bufLen); - int64_t reserved64 = 0; - int8_t msgVer = TSDB_SNAP_MSG_VER; - - if (tStartEncode(&encoder) < 0) goto _err; - if (tEncodeI8(&encoder, msgVer) < 0) goto _err; + if ((code = tStartEncode(&encoder))) goto _err; + if ((code = tEncodeI8(&encoder, msgVer))) goto _err; int16_t format = pOpts->format; - if (tEncodeI16(&encoder, format) < 0) goto _err; - if (tEncodeI64(&encoder, reserved64) < 0) goto _err; + if ((code = tEncodeI16(&encoder, format))) goto _err; + if ((code = tEncodeI64(&encoder, reserved64))) goto _err; tEndEncode(&encoder); int32_t tlen = encoder.pos; @@ -479,23 +489,24 @@ int32_t tSerializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) { _err: tEncoderClear(&encoder); - return -1; + return code; } int32_t tDeserializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) { + int32_t code; SDecoder decoder = {0}; + int64_t reserved64 = 0; + int8_t msgVer = 0; + tDecoderInit(&decoder, buf, bufLen); - int64_t reserved64 = 0; - int8_t msgVer = 0; - - if (tStartDecode(&decoder) < 0) goto _err; - if (tDecodeI8(&decoder, &msgVer) < 0) goto _err; + if ((code = tStartDecode(&decoder))) goto _err; + if ((code = tDecodeI8(&decoder, &msgVer))) goto _err; if (msgVer != TSDB_SNAP_MSG_VER) goto _err; int16_t format = 0; - if (tDecodeI16(&decoder, &format) < 0) goto _err; + if ((code = tDecodeI16(&decoder, &format))) goto _err; pOpts->format = format; - if (tDecodeI64(&decoder, &reserved64) < 0) goto _err; + if ((code = tDecodeI64(&decoder, &reserved64))) goto _err; tEndDecode(&decoder); tDecoderClear(&decoder); @@ -503,7 +514,7 @@ int32_t tDeserializeTsdbRepOpts(void* buf, int32_t bufLen, STsdbRepOpts* pOpts) _err: tDecoderClear(&decoder); - return -1; + return code; } static int32_t tsdbRepOptsEstSize(STsdbRepOpts* pOpts) { @@ -518,7 +529,7 @@ static int32_t tsdbRepOptsSerialize(STsdbRepOpts* pOpts, void* buf, int32_t bufL int32_t offset = 0; int32_t tlen = 0; if ((tlen = tSerializeTsdbRepOpts(pSubHead->val, bufLen, pOpts)) < 0) { - return -1; + return tlen; } pSubHead->typ = SNAP_DATA_RAW; pSubHead->len = tlen; @@ -528,8 +539,10 @@ static int32_t tsdbRepOptsSerialize(STsdbRepOpts* pOpts, void* buf, int32_t bufL // snap info static int32_t tsdbSnapPrepDealWithSnapInfo(SVnode* pVnode, SSnapshot* pSnap, STsdbRepOpts* pInfo) { - if (!pSnap->data) return 0; - int32_t code = -1; + if (!pSnap->data) { + return 0; + } + int32_t code = 0; SSyncTLV* pHead = (void*)pSnap->data; int32_t offset = 0; @@ -546,30 +559,30 @@ static int32_t tsdbSnapPrepDealWithSnapInfo(SVnode* pVnode, SSnapshot* pSnap, ST case SNAP_DATA_RSMA2: { } break; case SNAP_DATA_RAW: { - if (tDeserializeTsdbRepOpts(buf, bufLen, pInfo) < 0) { - terrno = TSDB_CODE_INVALID_DATA_FMT; + code = tDeserializeTsdbRepOpts(buf, bufLen, pInfo); + if (code < 0) { tsdbError("vgId:%d, failed to deserialize tsdb rep opts since %s", TD_VID(pVnode), terrstr()); - goto _out; + return code; } } break; default: + code = TSDB_CODE_INVALID_MSG; tsdbError("vgId:%d, unexpected subfield type of snap info. typ:%d", TD_VID(pVnode), pField->typ); - goto _out; + return code; } } - code = 0; -_out: return code; } int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) { ASSERT(pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY); STsdbPartitionInfo partitionInfo = {0}; - int code = -1; + int code = 0; STsdbPartitionInfo* pInfo = &partitionInfo; - if (tsdbPartitionInfoInit(pVnode, pInfo) != 0) { + code = tsdbPartitionInfoInit(pVnode, pInfo); + if (code) { goto _out; } @@ -577,7 +590,7 @@ int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) { STsdbRepOpts opts = {.format = TSDB_SNAP_REP_FMT_RAW}; if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) { STsdbRepOpts leaderOpts = {0}; - if (tsdbSnapPrepDealWithSnapInfo(pVnode, pSnap, &leaderOpts) < 0) { + if ((code = tsdbSnapPrepDealWithSnapInfo(pVnode, pSnap, &leaderOpts)) < 0) { tsdbError("vgId:%d, failed to deal with snap info for reply since %s", TD_VID(pVnode), terrstr()); goto _out; } @@ -589,7 +602,7 @@ int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) { int32_t bufLen = headLen; bufLen += tsdbPartitionInfoEstSize(pInfo); bufLen += tsdbRepOptsEstSize(&opts); - if (syncSnapInfoDataRealloc(pSnap, bufLen) != 0) { + if ((code = syncSnapInfoDataRealloc(pSnap, bufLen)) != 0) { tsdbError("vgId:%d, failed to realloc memory for data of snap info. bytes:%d", TD_VID(pVnode), bufLen); goto _out; } @@ -599,7 +612,8 @@ int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) { int32_t offset = headLen; int32_t tlen = 0; - if ((tlen = tsdbPartitionInfoSerialize(pInfo, buf + offset, bufLen - offset)) < 0) { + if ((tlen = tsdbPartitionInfoSerialize(pInfo, (uint8_t*)(buf + offset), bufLen - offset)) < 0) { + code = tlen; tsdbError("vgId:%d, failed to serialize tsdb partition info since %s", TD_VID(pVnode), terrstr()); goto _out; } @@ -607,6 +621,7 @@ int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) { ASSERT(offset <= bufLen); if ((tlen = tsdbRepOptsSerialize(&opts, buf + offset, bufLen - offset)) < 0) { + code = tlen; tsdbError("vgId:%d, failed to serialize tsdb rep opts since %s", TD_VID(pVnode), terrstr()); goto _out; } @@ -620,7 +635,7 @@ int32_t tsdbSnapPrepDescription(SVnode* pVnode, SSnapshot* pSnap) { tsdbInfo("vgId:%d, tsdb snap info prepared. type:%s, val length:%d", TD_VID(pVnode), TMSG_INFO(pHead->typ), pHead->len); - code = 0; + _out: tsdbPartitionInfoClear(pInfo); return code; diff --git a/source/dnode/vnode/src/tsdb/tsdbUtil.c b/source/dnode/vnode/src/tsdb/tsdbUtil.c index 58075cf0ac..66db7baab9 100644 --- a/source/dnode/vnode/src/tsdb/tsdbUtil.c +++ b/source/dnode/vnode/src/tsdb/tsdbUtil.c @@ -152,7 +152,7 @@ int32_t tPutMapData(uint8_t *p, SMapData *pMapData) { } #endif -int32_t tGetMapData(uint8_t *p, SMapData *pMapData) { +int32_t tGetMapData(uint8_t *p, SMapData *pMapData, int32_t *decodedSize) { int32_t n = 0; int32_t offset; @@ -160,7 +160,9 @@ int32_t tGetMapData(uint8_t *p, SMapData *pMapData) { n += tGetI32v(p + n, &pMapData->nItem); if (pMapData->nItem) { - if (tRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem)) return -1; + if (tRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem)) { + return TSDB_CODE_OUT_OF_MEMORY; + } int32_t lOffset = 0; for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) { @@ -170,12 +172,18 @@ int32_t tGetMapData(uint8_t *p, SMapData *pMapData) { } n += tGetI32v(p + n, &pMapData->nData); - if (tRealloc(&pMapData->pData, pMapData->nData)) return -1; + if (tRealloc(&pMapData->pData, pMapData->nData)) { + return TSDB_CODE_OUT_OF_MEMORY; + } memcpy(pMapData->pData, p + n, pMapData->nData); n += pMapData->nData; } - return n; + if (decodedSize) { + *decodedSize = n; + } + + return 0; } #ifdef BUILD_NO_CALL @@ -680,20 +688,17 @@ int32_t tsdbRowCompareWithoutVersion(const void *p1, const void *p2) { // STSDBRowIter ====================================================== int32_t tsdbRowIterOpen(STSDBRowIter *pIter, TSDBROW *pRow, STSchema *pTSchema) { - int32_t code = 0; - pIter->pRow = pRow; if (pRow->type == TSDBROW_ROW_FMT) { - code = tRowIterOpen(pRow->pTSRow, pTSchema, &pIter->pIter); - if (code) goto _exit; + int32_t code = tRowIterOpen(pRow->pTSRow, pTSchema, &pIter->pIter); + if (code) return code; } else if (pRow->type == TSDBROW_COL_FMT) { pIter->iColData = 0; } else { ASSERT(0); } -_exit: - return code; + return 0; } void tsdbRowClose(STSDBRowIter *pIter) { @@ -760,7 +765,9 @@ int32_t tsdbRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) --iCol; continue; } else if (pTSchema->columns[jCol].colId > pTColumn->colId) { - taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type)); + if (taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type)) == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } continue; } @@ -780,14 +787,15 @@ int32_t tsdbRowMergerAdd(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) } if (taosArrayPush(pMerger->pArray, pColVal) == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; - return code; + return TSDB_CODE_OUT_OF_MEMORY; } } for (; iCol < pMerger->pTSchema->numOfCols; ++iCol) { pTColumn = &pMerger->pTSchema->columns[iCol]; - taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type)); + if (taosArrayPush(pMerger->pArray, &COL_VAL_NONE(pTColumn->colId, pTColumn->type)) == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } } pMerger->version = key.version; @@ -962,8 +970,7 @@ _exit: */ // delete skyline ====================================================== -static int32_t tsdbMergeSkyline(SArray *pSkyline1, SArray *pSkyline2, SArray *pSkyline) { - int32_t code = 0; +static void tsdbMergeSkyline(SArray *pSkyline1, SArray *pSkyline2, SArray *pSkyline) { int32_t i1 = 0; int32_t n1 = taosArrayGetSize(pSkyline1); int32_t i2 = 0; @@ -1017,7 +1024,6 @@ static int32_t tsdbMergeSkyline(SArray *pSkyline1, SArray *pSkyline2, SArray *pS } pSkyline->size = TARRAY_ELEM_IDX(pSkyline, pItem); - return code; } int32_t tsdbBuildDeleteSkylineImpl(SArray *aSkyline, int32_t sidx, int32_t eidx, SArray *pSkyline) { @@ -1029,8 +1035,13 @@ int32_t tsdbBuildDeleteSkylineImpl(SArray *aSkyline, int32_t sidx, int32_t eidx, if (sidx == eidx) { TSDBKEY *pItem1 = taosArrayGet(aSkyline, sidx * 2); TSDBKEY *pItem2 = taosArrayGet(aSkyline, sidx * 2 + 1); - taosArrayPush(pSkyline, &pItem1); - taosArrayPush(pSkyline, &pItem2); + if (taosArrayPush(pSkyline, &pItem1) == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + if (taosArrayPush(pSkyline, &pItem2) == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } } else { SArray *pSkyline1 = NULL; SArray *pSkyline2 = NULL; @@ -1049,7 +1060,7 @@ int32_t tsdbBuildDeleteSkylineImpl(SArray *aSkyline, int32_t sidx, int32_t eidx, code = tsdbBuildDeleteSkylineImpl(aSkyline, midx + 1, eidx, pSkyline2); if (code) goto _clear; - code = tsdbMergeSkyline(pSkyline1, pSkyline2, pSkyline); + tsdbMergeSkyline(pSkyline1, pSkyline2, pSkyline); _clear: taosArrayDestroy(pSkyline1); @@ -1064,13 +1075,28 @@ int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SAr int32_t code = 0; int32_t dataNum = eidx - sidx + 1; SArray *aTmpSkyline = taosArrayInit(dataNum * 2, sizeof(TSDBKEY)); - SArray *pSkyline = taosArrayInit(dataNum * 2, POINTER_BYTES); + if (aTmpSkyline == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SArray *pSkyline = taosArrayInit(dataNum * 2, POINTER_BYTES); + if (pSkyline == NULL) { + taosArrayDestroy(aTmpSkyline); + return TSDB_CODE_OUT_OF_MEMORY; + } taosArrayClear(aSkyline); for (int32_t i = sidx; i <= eidx; ++i) { pDelData = (SDelData *)taosArrayGet(aDelData, i); - taosArrayPush(aTmpSkyline, &(TSDBKEY){.ts = pDelData->sKey, .version = pDelData->version}); - taosArrayPush(aTmpSkyline, &(TSDBKEY){.ts = pDelData->eKey, .version = 0}); + if (taosArrayPush(aTmpSkyline, &(TSDBKEY){.ts = pDelData->sKey, .version = pDelData->version}) == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _clear; + } + + if (taosArrayPush(aTmpSkyline, &(TSDBKEY){.ts = pDelData->eKey, .version = 0}) == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _clear; + } } code = tsdbBuildDeleteSkylineImpl(aTmpSkyline, sidx, eidx, pSkyline); @@ -1079,7 +1105,10 @@ int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SAr int32_t skylineNum = taosArrayGetSize(pSkyline); for (int32_t i = 0; i < skylineNum; ++i) { TSDBKEY *p = taosArrayGetP(pSkyline, i); - taosArrayPush(aSkyline, p); + if (taosArrayPush(aSkyline, p) == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _clear; + } } _clear: @@ -1519,8 +1548,8 @@ int32_t tBlockDataDecompress(SBufferReader *br, SBlockData *blockData, SBuffer * SBlockCol blockCol; code = tGetBlockCol(&br2, &blockCol, hdr.fmtVer, hdr.cmprAlg); - if (blockCol.alg == 0) blockCol.alg = hdr.cmprAlg; TSDB_CHECK_CODE(code, lino, _exit); + if (blockCol.alg == 0) blockCol.alg = hdr.cmprAlg; code = tBlockDataDecompressColData(&hdr, &blockCol, br, blockData, assist); TSDB_CHECK_CODE(code, lino, _exit); } @@ -1812,7 +1841,9 @@ int32_t tsdbGetColCmprAlgFromSet(SHashObj *set, int16_t colId, uint32_t *alg) { if (set == NULL) return -1; uint32_t *ret = taosHashGet(set, &colId, sizeof(colId)); - if (ret == NULL) return -1; + if (ret == NULL) { + return TSDB_CODE_NOT_FOUND; + } *alg = *ret; return 0; diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 93a06ddf30..66b7938440 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -43,17 +43,16 @@ void syncUtilNodeInfo2EpSet(const SNodeInfo* pInfo, SEpSet* pEpSet) { bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId* raftId) { uint32_t ipv4 = 0xFFFFFFFF; - sDebug("vgId:%d, start to resolve sync addr fqdn in %d seconds, " - "dnode:%d cluster:%" PRId64 " fqdn:%s port:%u ", - vgId, tsResolveFQDNRetryTime, - pInfo->nodeId, pInfo->clusterId, pInfo->nodeFqdn, pInfo->nodePort); - for(int i = 0; i < tsResolveFQDNRetryTime; i++){ + sDebug( + "vgId:%d, start to resolve sync addr fqdn in %d seconds, " + "dnode:%d cluster:%" PRId64 " fqdn:%s port:%u ", + vgId, tsResolveFQDNRetryTime, pInfo->nodeId, pInfo->clusterId, pInfo->nodeFqdn, pInfo->nodePort); + for (int i = 0; i < tsResolveFQDNRetryTime; i++) { ipv4 = taosGetIpv4FromFqdn(pInfo->nodeFqdn); if (ipv4 == 0xFFFFFFFF || ipv4 == 1) { sError("failed to resolve ipv4 addr, fqdn:%s, wait one second", pInfo->nodeFqdn); taosSsleep(1); - } - else{ + } else { break; } } @@ -501,8 +500,7 @@ void syncLogSendRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteRepl int32_t syncSnapInfoDataRealloc(SSnapshot* pSnap, int32_t size) { void* data = taosMemoryRealloc(pSnap->data, size); if (data == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + return terrno = TSDB_CODE_OUT_OF_MEMORY; } pSnap->data = data; return 0; From 5734fe8778f8e7070c1cba44ccccb0af4d5ceb26 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 19 Jul 2024 16:07:09 +0800 Subject: [PATCH 2/5] fix compile error --- source/dnode/vnode/src/tsdb/tsdbReaderWriter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c b/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c index 16bacf90b3..28abed1ce7 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c +++ b/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c @@ -759,7 +759,7 @@ int32_t tsdbReadDataBlk(SDataFReader *pReader, SBlockIdx *pBlockIdx, SMapData *m // decode int32_t n; code = tGetMapData(pReader->aBuf[0], mDataBlk, &n); - if (code) goto _err; + if (code) goto _exit; ASSERT(n == size); _exit: From 7718d5e042162b4c93f795cf3a600cff523fe666 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Mon, 22 Jul 2024 18:47:41 +0800 Subject: [PATCH 3/5] fix: os return code --- include/os/os.h | 1 + include/os/osTime.h | 8 ++++---- source/os/src/osDir.c | 6 +++--- source/os/src/osMemory.c | 39 +++++++++++++++++++++++++++++++-------- source/os/src/osRand.c | 2 +- source/os/src/osTime.c | 26 ++++++++++++++++++++------ 6 files changed, 60 insertions(+), 22 deletions(-) diff --git a/include/os/os.h b/include/os/os.h index ac1a750b78..1749687d97 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -97,6 +97,7 @@ extern "C" { #include #endif + #include "osThread.h" #include "osAtomic.h" diff --git a/include/os/osTime.h b/include/os/osTime.h index 87df3a2650..c367416175 100644 --- a/include/os/osTime.h +++ b/include/os/osTime.h @@ -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; } diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index 667c3c146a..4348629467 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -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 { @@ -110,7 +110,7 @@ void taosRemoveDir(const char *dirname) { } taosCloseDir(&pDir); - rmdir(dirname); + (void)rmdir(dirname); // printf("dir:%s is removed\n", dirname); return; diff --git a/source/os/src/osMemory.c b/source/os/src/osMemory.c index d130ebe469..3e49808dad 100644 --- a/source/os/src/osMemory.c +++ b/source/os/src/osMemory.c @@ -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); diff --git a/source/os/src/osRand.c b/source/os/src/osRand.c index 0e58b7a8ec..da4e8dfb9d 100644 --- a/source/os/src/osRand.c +++ b/source/os/src/osRand.c @@ -69,7 +69,7 @@ uint32_t taosSafeRand(void) { if (len < 0) { seed = (int)taosGetTimestampSec(); } - taosCloseFile(&pFile); + (void)taosCloseFile(&pFile); } return (uint32_t)seed; diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 9cad1dd6ef..c650ac989d 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -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 } From 9e8d94a69a10812ac870a19e96c2e9204b643d8e Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Tue, 23 Jul 2024 15:03:24 +0800 Subject: [PATCH 4/5] fix: add function return code validation --- include/os/osEnv.h | 4 +- include/os/osTimezone.h | 2 +- source/client/src/clientMonitor.c | 3 +- source/common/src/tglobal.c | 1 - source/dnode/mgmt/node_mgmt/src/dmEnv.c | 2 - source/dnode/mgmt/node_mgmt/src/dmNodes.c | 1 - source/dnode/mgmt/node_util/src/dmFile.c | 1 - source/dnode/mnode/impl/src/mndMain.c | 1 - source/libs/tdb/src/db/tdbDb.c | 2 +- source/libs/wal/src/walMgmt.c | 2 +- source/os/src/osDir.c | 106 ++++++++++++++-------- source/os/src/osEnv.c | 39 ++++---- source/os/src/osFile.c | 70 +++++++++----- source/os/src/osSleep.c | 8 +- source/os/src/osTimer.c | 31 ++++--- source/os/src/osTimezone.c | 49 ++++++---- source/util/src/tconfig.c | 3 +- 17 files changed, 197 insertions(+), 128 deletions(-) diff --git a/include/os/osEnv.h b/include/os/osEnv.h index 4f4a58d4e8..5f7cdf44dc 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -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); diff --git a/include/os/osTimezone.h b/include/os/osTimezone.h index ce573d320b..fab42e8e11 100644 --- a/include/os/osTimezone.h +++ b/include/os/osTimezone.h @@ -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 } diff --git a/source/client/src/clientMonitor.c b/source/client/src/clientMonitor.c index b8b57160fe..e3b073dbc8 100644 --- a/source/client/src/clientMonitor.c +++ b/source/client/src/clientMonitor.c @@ -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) { diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 7538b504e3..c4905de898 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -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; diff --git a/source/dnode/mgmt/node_mgmt/src/dmEnv.c b/source/dnode/mgmt/node_mgmt/src/dmEnv.c index f636629b3a..98a8bccf0d 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmEnv.c +++ b/source/dnode/mgmt/node_mgmt/src/dmEnv.c @@ -244,7 +244,6 @@ static int32_t dmProcessCreateNodeReq(EDndNodeType ntype, SRpcMsg *pMsg) { pWrapper = &pDnode->wrappers[ntype]; if (taosMkDir(pWrapper->path) != 0) { dmReleaseWrapper(pWrapper); - terrno = TAOS_SYSTEM_ERROR(errno); dError("failed to create dir:%s since %s", pWrapper->path, terrstr()); return -1; } @@ -314,7 +313,6 @@ static int32_t dmProcessAlterNodeTypeReq(EDndNodeType ntype, SRpcMsg *pMsg) { pWrapper = &pDnode->wrappers[ntype]; if (taosMkDir(pWrapper->path) != 0) { taosThreadMutexUnlock(&pDnode->mutex); - terrno = TAOS_SYSTEM_ERROR(errno); dError("failed to create dir:%s since %s", pWrapper->path, terrstr()); return -1; } diff --git a/source/dnode/mgmt/node_mgmt/src/dmNodes.c b/source/dnode/mgmt/node_mgmt/src/dmNodes.c index a8bf5be3e2..f25bc03a4d 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmNodes.c +++ b/source/dnode/mgmt/node_mgmt/src/dmNodes.c @@ -20,7 +20,6 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) { SDnode *pDnode = pWrapper->pDnode; 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()); return -1; } diff --git a/source/dnode/mgmt/node_util/src/dmFile.c b/source/dnode/mgmt/node_util/src/dmFile.c index 752abb83a2..d86c345ce3 100644 --- a/source/dnode/mgmt/node_util/src/dmFile.c +++ b/source/dnode/mgmt/node_util/src/dmFile.c @@ -328,7 +328,6 @@ int32_t dmUpdateEncryptKey(char *key, bool toLogFile) { terrno = 0; if (taosMkDir(folder) != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); encryptError("failed to create dir:%s since %s", folder, terrstr()); goto _OVER; } diff --git a/source/dnode/mnode/impl/src/mndMain.c b/source/dnode/mnode/impl/src/mndMain.c index 723e3701a1..680cc7ab28 100644 --- a/source/dnode/mnode/impl/src/mndMain.c +++ b/source/dnode/mnode/impl/src/mndMain.c @@ -454,7 +454,6 @@ static int32_t mndCreateDir(SMnode *pMnode, const char *path) { } if (taosMkDir(pMnode->path) != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); return -1; } diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index 9a1d89eeac..1ac6c727ca 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -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 -1; + return terrno; } #ifdef USE_MAINDB diff --git a/source/libs/wal/src/walMgmt.c b/source/libs/wal/src/walMgmt.c index 3dbaed1bc7..88992ada2a 100644 --- a/source/libs/wal/src/walMgmt.c +++ b/source/libs/wal/src/walMgmt.c @@ -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; } diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index 4348629467..c04add2f42 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -109,7 +109,7 @@ void taosRemoveDir(const char *dirname) { } } - taosCloseDir(&pDir); + (void)taosCloseDir(&pDir); (void)rmdir(dirname); // printf("dir:%s is removed\n", dirname); @@ -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 } diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 948040ac76..72f0a41710 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -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]; } diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index f63339bcfc..0581df573a 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -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,6 +123,7 @@ 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); @@ -143,7 +144,7 @@ int64_t taosCopyFile(const char *from, const char *to) { if (bytes < sizeof(buffer)) break; } - int code = taosFsyncFile(pFileTo); + code = taosFsyncFile(pFileTo); taosCloseFile(&pFileFrom); taosCloseFile(&pFileTo); @@ -154,10 +155,12 @@ int64_t taosCopyFile(const char *from, const char *to) { return size; _err: + if (pFileFrom != NULL) taosCloseFile(&pFileFrom); if (pFileTo != NULL) taosCloseFile(&pFileTo); /* coverity[+retval] */ taosRemoveFile(to); + return -1; #endif } @@ -301,11 +304,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 +660,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 +1070,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 +1092,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 +1110,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 +1129,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; } @@ -1155,20 +1175,28 @@ 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; } + int32_t code = 0; 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; } diff --git a/source/os/src/osSleep.c b/source/os/src/osSleep.c index f6cc7d608d..f72805b15d 100644 --- a/source/os/src/osSleep.c +++ b/source/os/src/osSleep.c @@ -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 } diff --git a/source/os/src/osTimer.c b/source/os/src/osTimer.c index 36d364382a..6e5d9844a9 100644 --- a/source/os/src/osTimer.c +++ b/source/os/src/osTimer.c @@ -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 } diff --git a/source/os/src/osTimezone.c b/source/os/src/osTimezone.c index 72f7dda41c..8780fb2bc2 100644 --- a/source/os/src/osTimezone.c +++ b/source/os/src/osTimezone.c @@ -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 } diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 3ab0f83238..268b0b8497 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -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; } From 9735c069c85f670d458966e53b6abe799dc6326d Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Tue, 23 Jul 2024 18:47:20 +0800 Subject: [PATCH 5/5] fix: windows compile issue --- source/os/src/osFile.c | 100 +++++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 28 deletions(-) diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 0581df573a..167c141569 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -127,40 +127,56 @@ int64_t taosCopyFile(const char *from, const char *to) { // 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; } 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 } @@ -168,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 @@ -217,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; @@ -233,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) { @@ -272,13 +297,15 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) { #else if (pFile == NULL || pFile->fd < 0) { - return -1; + 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 code; + if (-1 == code) { + terrno = TAOS_SYSTEM_ERROR(errno); + return terrno; } if (stDev != NULL) { @@ -1150,6 +1177,8 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) if (pFile == NULL) { return 0; } + + int32_t code = 0; #ifdef WINDOWS #if FILE_WITH_LOCK @@ -1185,7 +1214,6 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) terrno = TSDB_CODE_INVALID_PARA; return -1; } - int32_t code = 0; int64_t ret = pread(pFile->fd, buf, count, offset); if (-1 == ret) { code = TAOS_SYSTEM_ERROR(errno); @@ -1205,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; } @@ -1471,4 +1515,4 @@ int taosSetAutoDelFile(char* path) { #else return unlink(path); #endif -} \ No newline at end of file +}