diff --git a/include/os/osSocket.h b/include/os/osSocket.h index 0427c6b22f..48478c8f49 100644 --- a/include/os/osSocket.h +++ b/include/os/osSocket.h @@ -143,7 +143,7 @@ int32_t taosWriteMsg(TdSocketPtr pSocket, void *ptr, int32_t nbytes); int32_t taosReadMsg(TdSocketPtr pSocket, void *ptr, int32_t nbytes); int32_t taosNonblockwrite(TdSocketPtr pSocket, char *ptr, int32_t nbytes); int64_t taosCopyFds(TdSocketPtr pSrcSocket, TdSocketPtr pDestSocket, int64_t len); -void taosWinSocketInit(); +int32_t taosWinSocketInit(); /* * set timeout(ms) diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 4591c7fbcc..4e909b0a1c 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -35,6 +35,8 @@ extern STaosError errors[]; #define TAOS_DEF_ERROR_CODE(mod, code) ((int32_t)((0x80000000 | ((mod)<<16) | (code)))) #define TAOS_SYSTEM_ERROR(code) (0x80ff0000 | (code)) +#define TAOS_SYSTEM_WINAPI_ERROR(code) (0x81ff0000 | (code)) +#define TAOS_SYSTEM_WINSOCKET_ERROR(code) (0x82ff0000 | (code)) #define TAOS_SUCCEEDED(err) ((err) >= 0) #define TAOS_FAILED(err) ((err) < 0) @@ -153,6 +155,7 @@ int32_t taosGetErrSize(); #define TSDB_CODE_MSG_PREPROCESSED TAOS_DEF_ERROR_CODE(0, 0x0136) // internal #define TSDB_CODE_OUT_OF_BUFFER TAOS_DEF_ERROR_CODE(0, 0x0137) #define TSDB_CODE_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x0138) +#define TSDB_CODE_SOCKET_ERROR TAOS_DEF_ERROR_CODE(0, 0x0139) //client #define TSDB_CODE_TSC_INVALID_OPERATION TAOS_DEF_ERROR_CODE(0, 0x0200) diff --git a/source/common/src/cos_cp.c b/source/common/src/cos_cp.c index e9af9c0306..793433bba7 100644 --- a/source/common/src/cos_cp.c +++ b/source/common/src/cos_cp.c @@ -175,7 +175,7 @@ int32_t cos_cp_load(char const* filepath, SCheckpoint* checkpoint) { int64_t n = taosReadFile(fd, cp_body, size); if (n < 0) { - TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), &lino, _exit); + TAOS_CHECK_GOTO(terrno, &lino, _exit); } else if (n != size) { TAOS_CHECK_GOTO(TSDB_CODE_FILE_CORRUPTED, &lino, _exit); } diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c index 563d123b83..d9f6920fcb 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c @@ -96,7 +96,7 @@ int32_t mmReadFile(const char *path, SMnodeOpt *pOption) { } if (taosReadFile(pFile, pData, size) != size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; dError("failed to read mnode file:%s since %s", file, tstrerror(code)); goto _OVER; } @@ -215,10 +215,7 @@ int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) { code = TAOS_SYSTEM_ERROR(errno); goto _OVER; } - if (taosRenameFile(file, realfile) != 0) { - code = TAOS_SYSTEM_ERROR(errno); - goto _OVER; - } + TAOS_CHECK_GOTO(taosRenameFile(file, realfile), NULL, _OVER); dInfo("succeed to write mnode file:%s, deloyed:%d", realfile, pOption->deploy); diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmFile.c b/source/dnode/mgmt/mgmt_vnode/src/vmFile.c index 8513d31695..d5b1e61aed 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmFile.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmFile.c @@ -133,7 +133,7 @@ int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t } if (taosReadFile(pFile, pData, size) != size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; dError("failed to read vnode file:%s since %s", file, tstrerror(code)); goto _OVER; } @@ -253,12 +253,8 @@ int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) { code = TAOS_SYSTEM_ERROR(errno); goto _OVER; } - if (taosRenameFile(file, realfile) != 0) { - code = TAOS_SYSTEM_ERROR(errno); - goto _OVER; - } + TAOS_CHECK_GOTO(taosRenameFile(file, realfile), NULL, _OVER); - code = 0; dInfo("succeed to write vnodes file:%s, vnodes:%d", realfile, numOfVnodes); _OVER: diff --git a/source/dnode/mgmt/node_util/src/dmEps.c b/source/dnode/mgmt/node_util/src/dmEps.c index 315c4d7430..7c0272965a 100644 --- a/source/dnode/mgmt/node_util/src/dmEps.c +++ b/source/dnode/mgmt/node_util/src/dmEps.c @@ -222,7 +222,7 @@ int32_t dmReadEps(SDnodeData *pData) { } if (taosReadFile(pFile, content, size) != size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; dError("failed to read dnode file:%s since %s", file, terrstr()); goto _OVER; } @@ -340,9 +340,8 @@ int32_t dmWriteEps(SDnodeData *pData) { if (taosFsyncFile(pFile) < 0) TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), NULL, _OVER); (void)taosCloseFile(&pFile); - if (taosRenameFile(file, realfile) != 0) TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), NULL, _OVER); + TAOS_CHECK_GOTO(taosRenameFile(file, realfile), NULL, _OVER); - code = 0; pData->updateTime = taosGetTimestampMs(); dInfo("succeed to write dnode file:%s, num:%d ver:%" PRId64, realfile, (int32_t)taosArrayGetSize(pData->dnodeEps), pData->dnodeVer); @@ -626,7 +625,7 @@ static int32_t dmReadDnodePairs(SDnodeData *pData) { } if (taosReadFile(pFile, content, size) != size) { - terrno = TAOS_SYSTEM_ERROR(errno); + terrno = terrno; dError("failed to read dnode file:%s since %s", file, terrstr()); goto _OVER; } diff --git a/source/dnode/mgmt/node_util/src/dmFile.c b/source/dnode/mgmt/node_util/src/dmFile.c index c8b24d5469..3412d5ed3a 100644 --- a/source/dnode/mgmt/node_util/src/dmFile.c +++ b/source/dnode/mgmt/node_util/src/dmFile.c @@ -75,7 +75,7 @@ int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) { } if (taosReadFile(pFile, content, size) != size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; dError("failed to read file:%s since %s", file, tstrerror(code)); goto _OVER; } @@ -166,12 +166,8 @@ int32_t dmWriteFile(const char *path, const char *name, bool deployed) { code = TAOS_SYSTEM_ERROR(errno); goto _OVER; } - if (taosRenameFile(file, realfile) != 0) { - code = TAOS_SYSTEM_ERROR(errno); - goto _OVER; - } + TAOS_CHECK_GOTO(taosRenameFile(file, realfile), NULL, _OVER); - code = 0; dInfo("succeed to write file:%s, deloyed:%d", realfile, deployed); _OVER: @@ -262,10 +258,7 @@ static int32_t dmWriteCheckCodeFile(char *file, char *realfile, char *key, bool goto _OVER; } - if (taosRenameFile(file, realfile) != 0) { - code = TAOS_SYSTEM_ERROR(errno); - goto _OVER; - } + TAOS_CHECK_GOTO(taosRenameFile(file, realfile), NULL, _OVER); encryptDebug("succeed to write checkCode file:%s", realfile); @@ -302,10 +295,7 @@ static int32_t dmWriteEncryptCodeFile(char *file, char *realfile, char *encryptC goto _OVER; } - if (taosRenameFile(file, realfile) != 0) { - code = TAOS_SYSTEM_ERROR(errno); - goto _OVER; - } + TAOS_CHECK_GOTO(taosRenameFile(file, realfile), NULL, _OVER); encryptDebug("succeed to write encryptCode file:%s", realfile); @@ -338,12 +328,12 @@ static int32_t dmCompareEncryptKey(char *file, char *key, bool toLogFile) { content = taosMemoryMalloc(size); if (content == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; goto _OVER; } if (taosReadFile(pFile, content, size) != size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; encryptError("failed to read dnode file:%s since %s", file, tstrerror(code)); goto _OVER; } @@ -484,7 +474,7 @@ static int32_t dmReadEncryptCodeFile(char *file, char **output) { } if (taosReadFile(pFile, content, size) != size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; dError("failed to read dnode file:%s since %s", file, tstrerror(code)); goto _OVER; } diff --git a/source/dnode/mnode/sdb/src/sdbFile.c b/source/dnode/mnode/sdb/src/sdbFile.c index 94826405dc..813579ac26 100644 --- a/source/dnode/mnode/sdb/src/sdbFile.c +++ b/source/dnode/mnode/sdb/src/sdbFile.c @@ -86,8 +86,7 @@ static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) { int64_t sver = 0; int32_t ret = taosReadFile(pFile, &sver, sizeof(int64_t)); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TAOS_RETURN(code); + return terrno; } if (ret != sizeof(int64_t)) { code = TSDB_CODE_FILE_CORRUPTED; @@ -100,8 +99,7 @@ static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) { ret = taosReadFile(pFile, &pSdb->applyIndex, sizeof(int64_t)); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TAOS_RETURN(code); + return terrno; } if (ret != sizeof(int64_t)) { code = TSDB_CODE_FILE_CORRUPTED; @@ -110,8 +108,7 @@ static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) { ret = taosReadFile(pFile, &pSdb->applyTerm, sizeof(int64_t)); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TAOS_RETURN(code); + return terrno; } if (ret != sizeof(int64_t)) { code = TSDB_CODE_FILE_CORRUPTED; @@ -120,8 +117,7 @@ static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) { ret = taosReadFile(pFile, &pSdb->applyConfig, sizeof(int64_t)); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TAOS_RETURN(code); + return terrno; } if (ret != sizeof(int64_t)) { code = TSDB_CODE_FILE_CORRUPTED; @@ -132,8 +128,7 @@ static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) { int64_t maxId = 0; ret = taosReadFile(pFile, &maxId, sizeof(int64_t)); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TAOS_RETURN(code); + return terrno; } if (ret != sizeof(int64_t)) { code = TSDB_CODE_FILE_CORRUPTED; @@ -148,8 +143,7 @@ static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) { int64_t ver = 0; ret = taosReadFile(pFile, &ver, sizeof(int64_t)); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TAOS_RETURN(code); + return terrno; } if (ret != sizeof(int64_t)) { code = TSDB_CODE_FILE_CORRUPTED; @@ -163,8 +157,7 @@ static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) { char reserve[SDB_RESERVE_SIZE] = {0}; ret = taosReadFile(pFile, reserve, sizeof(reserve)); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TAOS_RETURN(code); + return terrno; } if (ret != sizeof(reserve)) { code = TSDB_CODE_FILE_CORRUPTED; @@ -274,7 +267,7 @@ static int32_t sdbReadFileImp(SSdb *pSdb) { if (ret == 0) break; if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; mError("failed to read sdb file:%s since %s", file, tstrerror(code)); goto _OVER; } @@ -305,7 +298,7 @@ static int32_t sdbReadFileImp(SSdb *pSdb) { ret = taosReadFile(pFile, pRaw->pData, readLen); if (ret < 0) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; mError("failed to read sdb file:%s since %s, ret:%" PRId64 " readLen:%d", file, tstrerror(code), ret, readLen); goto _OVER; } @@ -523,7 +516,6 @@ static int32_t sdbWriteFileImp(SSdb *pSdb, int32_t skip_type) { if (code == 0) { code = taosRenameFile(tmpfile, curfile); if (code != 0) { - code = TAOS_SYSTEM_ERROR(errno); mError("failed to write sdb file:%s since %s", curfile, tstrerror(code)); } } @@ -693,7 +685,7 @@ int32_t sdbDoRead(SSdb *pSdb, SSdbIter *pIter, void **ppBuf, int32_t *len) { int32_t readlen = taosReadFile(pIter->file, pBuf, maxlen); if (readlen < 0 || readlen > maxlen) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; mError("sdbiter:%p, failed to read snapshot since %s, total:%" PRId64, pIter, tstrerror(code), pIter->total); *ppBuf = NULL; *len = 0; @@ -758,8 +750,8 @@ int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, i char datafile[PATH_MAX] = {0}; snprintf(datafile, sizeof(datafile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP); - if (taosRenameFile(pIter->name, datafile) != 0) { - code = TAOS_SYSTEM_ERROR(errno); + code = taosRenameFile(pIter->name, datafile); + if (code != 0) { mError("sdbiter:%p, failed to rename file %s to %s since %s", pIter, pIter->name, datafile, tstrerror(code)); goto _OVER; } diff --git a/source/dnode/vnode/src/tsdb/tsdbFS.c b/source/dnode/vnode/src/tsdb/tsdbFS.c index ed80b5d412..cde4d7ded8 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS.c @@ -310,7 +310,7 @@ static int32_t load_fs(const char *fname, STsdbFS *pFS) { } if (taosReadFile(pFD, pData, size) < 0) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; (void)taosCloseFile(&pFD); TSDB_CHECK_CODE(code, lino, _exit); } @@ -711,10 +711,7 @@ static int32_t tsdbFSCommit(STsdb *pTsdb) { if (!taosCheckExistFile(current_t)) goto _exit; // rename the file - if (taosRenameFile(current_t, current) < 0) { - code = TAOS_SYSTEM_ERROR(errno); - TSDB_CHECK_CODE(code, lino, _exit); - } + TSDB_CHECK_CODE(taosRenameFile(current_t, current), lino, _exit); // Load the new FS code = tsdbFSCreate(&fs); diff --git a/source/dnode/vnode/src/tsdb/tsdbFS2.c b/source/dnode/vnode/src/tsdb/tsdbFS2.c index 24916bf47c..843e700fd3 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS2.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS2.c @@ -130,7 +130,7 @@ static int32_t load_json(const char *fname, cJSON **json) { } if (taosReadFile(fp, data, size) < 0) { - TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit); + TSDB_CHECK_CODE(code = terrno, lino, _exit); } data[size] = '\0'; @@ -309,10 +309,7 @@ static int32_t commit_edit(STFileSystem *fs) { int32_t code; int32_t lino; - if ((code = taosRenameFile(current_t, current))) { - code = TAOS_SYSTEM_ERROR(code); - TSDB_CHECK_CODE(code, lino, _exit); - } + TSDB_CHECK_CODE(taosRenameFile(current_t, current), lino, _exit); code = apply_commit(fs); TSDB_CHECK_CODE(code, lino, _exit); diff --git a/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c b/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c index e044743bea..c34b29f97d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c +++ b/source/dnode/vnode/src/tsdb/tsdbReaderWriter.c @@ -226,7 +226,7 @@ static int32_t tsdbReadFilePage(STsdbFD *pFD, int64_t pgno, int32_t encryptAlgor // read n = taosReadFile(pFD->pFD, pFD->pBuf, pFD->szPage); if (n < 0) { - TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(errno), lino, _exit); + TSDB_CHECK_CODE(code = terrno, lino, _exit); } else if (n < pFD->szPage) { TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit); } @@ -378,7 +378,7 @@ static int32_t tsdbReadFileBlock(STsdbFD *pFD, int64_t offset, int64_t size, boo ret = taosReadFile(pFD->pFD, buf + n, nRead); if (ret < 0) { - TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(errno), lino, _exit); + TSDB_CHECK_CODE(code = terrno, lino, _exit); } else if (ret < nRead) { TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit); } diff --git a/source/dnode/vnode/src/vnd/vnodeCommit.c b/source/dnode/vnode/src/vnd/vnodeCommit.c index ddd1ac9d4a..c272e540ba 100644 --- a/source/dnode/vnode/src/vnd/vnodeCommit.c +++ b/source/dnode/vnode/src/vnd/vnodeCommit.c @@ -211,8 +211,9 @@ int vnodeCommitInfo(const char *dir) { snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME); snprintf(tfname, TSDB_FILENAME_LEN, "%s%s%s", dir, TD_DIRSEP, VND_INFO_FNAME_TMP); - if (taosRenameFile(tfname, fname) < 0) { - return terrno = TAOS_SYSTEM_ERROR(errno); + int32_t code = taosRenameFile(tfname, fname); + if (code < 0) { + return code; } vInfo("vnode info is committed, dir:%s", dir); @@ -245,7 +246,7 @@ int vnodeLoadInfo(const char *dir, SVnodeInfo *pInfo) { } if (taosReadFile(pFile, pData, size) < 0) { - TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(errno), lino, _exit); + TSDB_CHECK_CODE(code = terrno, lino, _exit); } pData[size] = '\0'; diff --git a/source/dnode/vnode/src/vnd/vnodeSnapshot.c b/source/dnode/vnode/src/vnd/vnodeSnapshot.c index 86c25009bc..7d8b5d4eda 100644 --- a/source/dnode/vnode/src/vnd/vnodeSnapshot.c +++ b/source/dnode/vnode/src/vnd/vnodeSnapshot.c @@ -289,7 +289,7 @@ int32_t vnodeSnapRead(SVSnapReader *pReader, uint8_t **ppData, uint32_t *nData) if (taosReadFile(pFile, ((SSnapDataHdr *)(*ppData))->data, size) < 0) { taosMemoryFree(*ppData); (void)taosCloseFile(&pFile); - TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(errno), lino, _exit); + TSDB_CHECK_CODE(code = terrno, lino, _exit); } (void)taosCloseFile(&pFile); diff --git a/source/libs/executor/src/groupcacheoperator.c b/source/libs/executor/src/groupcacheoperator.c index 1796aa7b64..576f3932b6 100644 --- a/source/libs/executor/src/groupcacheoperator.c +++ b/source/libs/executor/src/groupcacheoperator.c @@ -562,7 +562,7 @@ static int32_t readBlockFromDisk(SGroupCacheOperatorInfo* pGCache, SGroupCacheDa ret = (int32_t)taosReadFile(pFileFd->fd, *ppBuf, pBasic->bufSize); if (ret != pBasic->bufSize) { taosMemoryFreeClear(*ppBuf); - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; goto _return; } diff --git a/source/libs/stream/src/streamBackendRocksdb.c b/source/libs/stream/src/streamBackendRocksdb.c index 465968dc50..4e14df9a40 100644 --- a/source/libs/stream/src/streamBackendRocksdb.c +++ b/source/libs/stream/src/streamBackendRocksdb.c @@ -296,7 +296,7 @@ int32_t remoteChkp_readMetaData(char* path, SSChkpMetaOnS3** pMeta) { char buf[256] = {0}; if (taosReadFile(pFile, buf, sizeof(buf)) <= 0) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; goto _EXIT; } @@ -367,8 +367,8 @@ int32_t remoteChkp_validAndCvtMeta(char* path, SSChkpMetaOnS3* pMeta, int64_t ch goto _EXIT; } - if (taosRenameFile(src, dst) != 0) { - code = TAOS_SYSTEM_ERROR(errno); + code = taosRenameFile(src, dst); + if (code != 0) { goto _EXIT; } @@ -507,7 +507,6 @@ int32_t rebuildFromRemoteChkp_s3(const char* key, char* chkpPath, int64_t chkpId if (taosIsDir(defaultPath)) { code = taosRenameFile(defaultPath, defaultTmp); if (code != 0) { - code = TAOS_SYSTEM_ERROR(errno); goto _EXIT; } else { rename = 1; @@ -1609,7 +1608,7 @@ int32_t chkpLoadExtraInfo(char* pChkpIdDir, int64_t* chkpId, int64_t* processId) } if (taosReadFile(pFile, buf, sizeof(buf)) <= 0) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; stError("failed to read file to load extra info, file:%s, reason:%s", pDst, tstrerror(code)); goto _EXIT; } diff --git a/source/libs/stream/src/streamSnapshot.c b/source/libs/stream/src/streamSnapshot.c index eb222efce6..2b69da6007 100644 --- a/source/libs/stream/src/streamSnapshot.c +++ b/source/libs/stream/src/streamSnapshot.c @@ -696,7 +696,7 @@ int32_t streamSnapWriteImpl(SStreamSnapWriter* pWriter, uint8_t* pData, uint32_t if (strlen(pHdr->name) == strlen(pItem->name) && strcmp(pHdr->name, pItem->name) == 0) { int64_t bytes = taosPWriteFile(pSnapFile->fd, pHdr->data, pHdr->size, pSnapFile->offset); if (bytes != pHdr->size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; stError("%s failed to write snap, file name:%s, reason:%s", STREAM_STATE_TRANSFER, pHdr->name, tstrerror(code)); goto _err; } else { @@ -735,7 +735,7 @@ int32_t streamSnapWriteImpl(SStreamSnapWriter* pWriter, uint8_t* pData, uint32_t // open fd again, let's close fd during handle errors. if (taosPWriteFile(pSnapFile->fd, pHdr->data, pHdr->size, pSnapFile->offset) != pHdr->size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; stError("%s failed to write snap, file name:%s, reason:%s", STREAM_STATE_TRANSFER, pHdr->name, tstrerror(code)); goto _err; } diff --git a/source/libs/sync/src/syncRaftCfg.c b/source/libs/sync/src/syncRaftCfg.c index 82cc86ed86..ac3bba8c42 100644 --- a/source/libs/sync/src/syncRaftCfg.c +++ b/source/libs/sync/src/syncRaftCfg.c @@ -164,9 +164,7 @@ int32_t syncWriteCfgFile(SSyncNode *pNode) { } (void)taosCloseFile(&pFile); - if (taosRenameFile(file, realfile) != 0) { - TAOS_CHECK_EXIT(TAOS_SYSTEM_ERROR(errno)); - } + TAOS_CHECK_EXIT(taosRenameFile(file, realfile)); sInfo("vgId:%d, succeed to write sync cfg file:%s, len:%d, lastConfigIndex:%" PRId64 ", changeVersion:%d", pNode->vgId, realfile, len, pNode->raftCfg.lastConfigIndex, pNode->raftCfg.cfg.changeVersion); @@ -280,7 +278,7 @@ int32_t syncReadCfgFile(SSyncNode *pNode) { } if (taosReadFile(pFile, pData, size) != size) { - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; sError("vgId:%d, failed to read sync cfg file:%s since %s", pNode->vgId, file, tstrerror(code)); goto _OVER; } diff --git a/source/libs/sync/src/syncRaftStore.c b/source/libs/sync/src/syncRaftStore.c index cc2aa7d91e..d6482ba2da 100644 --- a/source/libs/sync/src/syncRaftStore.c +++ b/source/libs/sync/src/syncRaftStore.c @@ -72,7 +72,7 @@ int32_t raftStoreReadFile(SSyncNode *pNode) { if (taosReadFile(pFile, pData, size) != size) { sError("vgId:%d, failed to read raft store file:%s since %s", pNode->vgId, file, terrstr()); - TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), &lino, _OVER); + TAOS_CHECK_GOTO(terrno, &lino, _OVER); } pData[size] = '\0'; diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index d6d33bc7d8..2249123ef5 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -39,7 +39,9 @@ int tdbGnrtFileID(tdb_fd_t fd, uint8_t *fileid, bool unique) { int64_t stDev = 0, stIno = 0; int32_t code = taosDevInoFile(fd, &stDev, &stIno); - return code; + if (TSDB_CODE_SUCCESS != code) { + return code; + } memset(fileid, 0, TDB_FILE_ID_LEN); diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index fb30dd3376..378e9d1903 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -112,7 +112,7 @@ static FORCE_INLINE int32_t walScanLogGetLastVer(SWal* pWal, int32_t fileIdx, in if (readSize != taosReadFile(pFile, buf, readSize)) { wError("vgId:%d, failed to read file due to %s. readSize:%" PRId64 ", file:%s", pWal->cfg.vgId, strerror(errno), readSize, fnameStr); - TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), &lino, _err); + TAOS_CHECK_GOTO(terrno, &lino, _err); } char* candidate = NULL; @@ -177,7 +177,7 @@ static FORCE_INLINE int32_t walScanLogGetLastVer(SWal* pWal, int32_t fileIdx, in if (extraSize != taosReadFile(pFile, buf + readSize, extraSize)) { wError("vgId:%d, failed to read file due to %s. offset:%" PRId64 ", extraSize:%" PRId64 ", file:%s", pWal->cfg.vgId, strerror(errno), offset + readSize, extraSize, fnameStr); - code = TAOS_SYSTEM_ERROR(errno); + code = terrno; break; } } @@ -522,7 +522,7 @@ static int32_t walReadLogHead(TdFilePtr pLogFile, int64_t offset, SWalCkHead* pC } if (taosReadFile(pLogFile, pCkHead, sizeof(SWalCkHead)) != sizeof(SWalCkHead)) { - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } if (walValidHeadCksum(pCkHead) != 0) { @@ -588,7 +588,7 @@ static int32_t walCheckAndRepairIdxFile(SWal* pWal, int32_t fileIdx) { wError("vgId:%d, failed to read file due to %s. offset:%" PRId64 ", file:%s", pWal->cfg.vgId, strerror(errno), offset, fnameStr); - TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), &lino, _err); + TAOS_CHECK_GOTO(terrno, &lino, _err); } if (idxEntry.ver > pFileInfo->lastVer) { @@ -1028,7 +1028,7 @@ int32_t walLoadMeta(SWal* pWal) { (void)taosCloseFile(&pFile); taosMemoryFree(buf); - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } // load into fileInfoSet code = walMetaDeserialize(pWal, buf); diff --git a/source/libs/wal/src/walRead.c b/source/libs/wal/src/walRead.c index deb5a07672..6476cd39ac 100644 --- a/source/libs/wal/src/walRead.c +++ b/source/libs/wal/src/walRead.c @@ -144,7 +144,7 @@ static int32_t walReadSeekFilePos(SWalReader *pReader, int64_t fileFirstVer, int if (ret < 0) { wError("vgId:%d, failed to read idx file, since %s", pReader->pWal->cfg.vgId, terrstr()); - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } else { wError("vgId:%d, read idx file incompletely, read bytes %" PRId64 ", bytes should be %ld", pReader->pWal->cfg.vgId, ret, sizeof(SWalIdxEntry)); @@ -270,7 +270,7 @@ int32_t walFetchHead(SWalReader *pRead, int64_t ver) { continue; } else { if (contLen < 0) { - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } else { TAOS_RETURN(TSDB_CODE_WAL_FILE_CORRUPTED); } @@ -345,7 +345,7 @@ int32_t walFetchBody(SWalReader *pRead) { wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since %s, 0x%" PRIx64, vgId, pReadHead->version, ver, tstrerror(terrno), id); - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } else { wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since file corrupted, 0x%" PRIx64, @@ -426,7 +426,7 @@ int32_t walReadVer(SWalReader *pReader, int64_t ver) { TAOS_UNUSED(taosThreadMutexUnlock(&pReader->mutex)); if (contLen < 0) { - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } else { TAOS_RETURN(TSDB_CODE_WAL_FILE_CORRUPTED); } @@ -467,7 +467,7 @@ int32_t walReadVer(SWalReader *pReader, int64_t ver) { TAOS_UNUSED(taosThreadMutexUnlock(&pReader->mutex)); if (contLen < 0) { - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } else { TAOS_RETURN(TSDB_CODE_WAL_FILE_CORRUPTED); } diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index 52af3e8528..a11831c437 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -214,7 +214,7 @@ int32_t walRollback(SWal *pWal, int64_t ver) { if (taosReadFile(pIdxFile, &entry, sizeof(SWalIdxEntry)) != sizeof(SWalIdxEntry)) { TAOS_UNUSED(taosThreadMutexUnlock(&pWal->mutex)); - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } walBuildLogName(pWal, walGetCurFileFirstVer(pWal), fnameStr); @@ -240,7 +240,7 @@ int32_t walRollback(SWal *pWal, int64_t ver) { if (size != sizeof(SWalCkHead)) { TAOS_UNUSED(taosThreadMutexUnlock(&pWal->mutex)); - TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + TAOS_RETURN(terrno); } code = walValidHeadCksum(&head); diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index d233a4fe0e..dcce5543b2 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -447,7 +447,7 @@ TdDirPtr taosOpenDir(const char *dirname) { if (INVALID_HANDLE_VALUE == pDir->hFind) { taosMemoryFree(pDir); DWORD errorCode = GetLastError(); - terrno = TAOS_SYSTEM_ERROR(errorCode); + terrno = TAOS_SYSTEM_WINAPI_ERROR(errorCode); return NULL; } return pDir; @@ -529,7 +529,7 @@ int32_t taosCloseDir(TdDirPtr *ppDir) { } #ifdef WINDOWS if(!FindClose((*ppDir)->hFind)) { - terrno = TAOS_SYSTEM_ERROR(GetLastError()); + terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError()); return terrno; } taosMemoryFree(*ppDir); diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index c0ea49e2cb..8334d609b1 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -64,7 +64,10 @@ int32_t osDefaultInit() { } #ifdef WINDOWS - taosWinSocketInit(); + code = taosWinSocketInit(); + if (code != TSDB_CODE_SUCCESS) { + return code; + } const char *tmpDir = getenv("tmp"); if (tmpDir == NULL) { diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 144baeb148..f4afaeabd1 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -132,6 +132,7 @@ int64_t taosCopyFile(const char *from, const char *to) { if (CopyFile(from, to, 0)) { return 1; } else { + terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError()); return -1; } #else @@ -231,8 +232,9 @@ int32_t taosRenameFile(const char *oldName, const char *newName) { HANDLE transactionHandle = CreateTransaction(NULL, NULL, 0, 0, 0, INFINITE, NULL); if (transactionHandle == INVALID_HANDLE_VALUE) { - printf("failed to rename file %s to %s, reason: CreateTransaction failed.\n", oldName, newName); - return -1; + DWORD error = GetLastError(); + terrno = TAOS_SYSTEM_WINAPI_ERROR(error); + return terrno; } BOOL result = MoveFileTransacted(oldName, newName, NULL, NULL, MOVEFILE_REPLACE_EXISTING, transactionHandle); @@ -241,18 +243,18 @@ int32_t taosRenameFile(const char *oldName, const char *newName) { finished = CommitTransaction(transactionHandle); if (!finished) { DWORD error = GetLastError(); - printf("failed to rename file %s to %s, reason: CommitTransaction errcode %d.\n", oldName, newName, error); + terrno = TAOS_SYSTEM_WINAPI_ERROR(error); } } else { RollbackTransaction(transactionHandle); DWORD error = GetLastError(); + terrno = TAOS_SYSTEM_WINAPI_ERROR(error); finished = false; - printf("failed to rename file %s to %s, reason: MoveFileTransacted errcode %d.\n", oldName, newName, error); } CloseHandle(transactionHandle); - return finished ? 0 : -1; + return finished ? 0 : terrno; #else int32_t code = rename(oldName, newName); if (-1 == code) { @@ -260,7 +262,7 @@ int32_t taosRenameFile(const char *oldName, const char *newName) { return terrno; } - return code; + return TSDB_CODE_SUCCESS; #endif } @@ -294,12 +296,14 @@ int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime, int32_t *a int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) { #ifdef WINDOWS if (pFile == NULL || pFile->hFile == NULL) { - return -1; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } BY_HANDLE_FILE_INFORMATION bhfi; if (GetFileInformationByHandle(pFile->hFile, &bhfi) == FALSE) { - printf("taosFStatFile get file info fail."); - return -1; + DWORD error = GetLastError(); + terrno = TAOS_SYSTEM_WINAPI_ERROR(error); + return terrno; } if (stDev != NULL) { @@ -395,35 +399,38 @@ HANDLE taosOpenFileNotStream(const char *path, int32_t tdFileOptions) { if (h != INVALID_HANDLE_VALUE && (tdFileOptions & TD_FILE_APPEND) && (tdFileOptions & TD_FILE_WRITE)) { SetFilePointer(h, 0, NULL, FILE_END); } - // if (h == INVALID_HANDLE_VALUE) { - // DWORD dwError = GetLastError(); - // LPVOID lpMsgBuf; - // FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, - // 0, - // (LPTSTR)&lpMsgBuf, 0, NULL); - // printf("CreateFile failed with error %d: %s", dwError, (char*)lpMsgBuf); - // LocalFree(lpMsgBuf); - // } + if (h == INVALID_HANDLE_VALUE) { + DWORD dwError = GetLastError(); + terrno = TAOS_SYSTEM_WINAPI_ERROR(error); + // LPVOID lpMsgBuf; + // FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, 0, (LPTSTR)&lpMsgBuf, 0, + // NULL); + // printf("CreateFile failed with error %d: %s", dwError, (char *)lpMsgBuf); + // LocalFree(lpMsgBuf); + } return h; } int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { #if FILE_WITH_LOCK - taosThreadRwlockRdlock(&(pFile->rwlock)); + (void)taosThreadRwlockRdlock(&(pFile->rwlock)); #endif if (pFile->hFile == NULL) { #if FILE_WITH_LOCK - taosThreadRwlockUnlock(&(pFile->rwlock)); + (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif - return -1; + terrno = TSDB_CODE_INVALID_PARA; + return terrno; } DWORD bytesRead; if (!ReadFile(pFile->hFile, buf, count, &bytesRead, NULL)) { + DWORD errCode = GetLastError(); + terrno = TAOS_SYSTEM_WINAPI_ERROR(errCode); bytesRead = -1; } #if FILE_WITH_LOCK - taosThreadRwlockUnlock(&(pFile->rwlock)); + (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return bytesRead; } @@ -449,14 +456,15 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t offset) { if (pFile == NULL) { + terrno = TSDB_CODE_INVALID_PARA; return 0; } #if FILE_WITH_LOCK - taosThreadRwlockWrlock(&(pFile->rwlock)); + (void)taosThreadRwlockWrlock(&(pFile->rwlock)); #endif if (pFile->hFile == NULL) { #if FILE_WITH_LOCK - taosThreadRwlockUnlock(&(pFile->rwlock)); + (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return 0; } @@ -470,11 +478,12 @@ int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t BOOL result = WriteFile(pFile->hFile, buf, count, &ret, &ol); if (!result) { errno = GetLastError(); + terrno = TAOS_SYSTEM_WINAPI_ERROR(errno); ret = -1; } #if FILE_WITH_LOCK - taosThreadRwlockUnlock(&(pFile->rwlock)); + (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return ret; } @@ -484,7 +493,7 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { return -1; } #if FILE_WITH_LOCK - taosThreadRwlockRdlock(&(pFile->rwlock)); + (void)taosThreadRwlockRdlock(&(pFile->rwlock)); #endif LARGE_INTEGER liOffset; @@ -498,7 +507,7 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { return -1; } #if FILE_WITH_LOCK - taosThreadRwlockUnlock(&(pFile->rwlock)); + (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif return liOffset.QuadPart; } @@ -826,25 +835,11 @@ int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t } #endif -#ifdef WINDOWS - DWORD ret = 0; - OVERLAPPED ol = {0}; - ol.OffsetHigh = (uint32_t)((offset & 0xFFFFFFFF00000000LL) >> 0x20); - ol.Offset = (uint32_t)(offset & 0xFFFFFFFFLL); - - HANDLE handle = (HANDLE)_get_osfhandle(pFile->fd); - SetLastError(0); - BOOL result = WriteFile(handle, buf, count, &ret, &ol); - if (!result) { - errno = GetLastError(); - ret = -1; - } -#else int64_t ret = pwrite(pFile->fd, buf, count, offset); if (-1 == ret) { code = TAOS_SYSTEM_ERROR(errno); } -#endif + #if FILE_WITH_LOCK (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif @@ -867,14 +862,10 @@ int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) { int32_t code = 0; -#ifdef WINDOWS - int64_t ret = _lseeki64(pFile->fd, offset, whence); -#else int64_t ret = lseek(pFile->fd, offset, whence); if (-1 == ret) { code = TAOS_SYSTEM_ERROR(errno); } -#endif #if FILE_WITH_LOCK (void)taosThreadRwlockUnlock(&(pFile->rwlock)); diff --git a/source/os/src/osSocket.c b/source/os/src/osSocket.c index 2d160b277b..48ca36b5e5 100644 --- a/source/os/src/osSocket.c +++ b/source/os/src/osSocket.c @@ -1170,19 +1170,23 @@ int32_t taosCreateSocketWithTimeout(uint32_t timeout) { return (int)fd; } -void taosWinSocketInit() { +int32_t taosWinSocketInit() { #ifdef WINDOWS - static char flag = 0; - if (flag == 0) { + static int8_t flag = 0; + if (atomic_val_compare_exchange_8(&flag, 0, 1) == 0) { WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(1, 1); - if (WSAStartup(wVersionRequested, &wsaData) == 0) { - flag = 1; + if (WSAStartup(wVersionRequested, &wsaData) != 0) { + atomic_store_8(&flag, 0) + int errorCode = WSAGetLastError(); + return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode); } } + return 0; #else #endif + return 0; } uint64_t taosHton64(uint64_t val) { diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 9a4f0f8a98..137bb8f0db 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -300,7 +300,7 @@ int32_t taosGetEmail(char *email, int32_t maxLen) { if (taosReadFile(pFile, (void *)email, maxLen) < 0) { taosCloseFile(&pFile); - return -1; + return terrno; } taosCloseFile(&pFile); diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 58dde5cd23..5ceec33831 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -850,6 +850,7 @@ static void tsSortError(void) { taosSort(errors, sizeof(errors) / sizeof(errors[0]), sizeof(errors[0]), taosCompareTaosError); } +static char WinAPIErrDesc[256] = {0}; const char* tstrerror(int32_t err) { (void)taosThreadOnce(&tsErrorInit, tsSortError); @@ -860,6 +861,15 @@ const char* tstrerror(int32_t err) { // invalid code return Unknown error return strerror(code); } + #ifdef WINDOWS + if ((err & 0x01ff0000) == 0x01ff0000) { + snprintf(WinAPIErrDesc, 256, "windows api error, code: 0x%08x", err & 0x0000ffff); + return WinAPIErrDesc; + } else if ((err & 0x02ff0000) == 0x02ff0000) { + snprintf(WinAPIErrDesc, 256, "windows socket error, code: 0x%08x", err & 0x0000ffff); + return WinAPIErrDesc; + } + #endif int32_t s = 0; int32_t e = sizeof(errors) / sizeof(errors[0]); diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index 32b9b6e18d..de27cf4df9 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -256,7 +256,7 @@ static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { void* pPage = (void*)GET_PAYLOAD_DATA(pg); ret = (int32_t)taosReadFile(pBuf->pFile, pPage, pg->length); if (ret != pg->length) { - ret = TAOS_SYSTEM_ERROR(errno); + ret = terrno; return ret; }