fix: windows compile issue

This commit is contained in:
dapan1121 2024-07-23 18:47:20 +08:00
parent ae2210f7cc
commit 9735c069c8
1 changed files with 72 additions and 28 deletions

View File

@ -127,40 +127,56 @@ int64_t taosCopyFile(const char *from, const char *to) {
// fidfrom = open(from, O_RDONLY); // fidfrom = open(from, O_RDONLY);
TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ); 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); // fidto = open(to, O_WRONLY | O_CREAT | O_EXCL, 0755);
TdFilePtr pFileTo = taosOpenFile(to, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_EXCL); 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) { while (true) {
bytes = taosReadFile(pFileFrom, buffer, sizeof(buffer)); bytes = taosReadFile(pFileFrom, buffer, sizeof(buffer));
if (bytes < 0) goto _err; if (bytes < 0) {
code = terrno;
goto _err;
}
if (bytes == 0) break; if (bytes == 0) break;
size += bytes; 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; if (bytes < sizeof(buffer)) break;
} }
code = taosFsyncFile(pFileTo); code = taosFsyncFile(pFileTo);
taosCloseFile(&pFileFrom); (void)taosCloseFile(&pFileFrom);
taosCloseFile(&pFileTo); (void)taosCloseFile(&pFileTo);
if (code != 0) { if (code != 0) {
terrno = code;
return -1; return -1;
} }
return size; return size;
_err: _err:
if (pFileFrom != NULL) taosCloseFile(&pFileFrom); if (pFileFrom != NULL) (void)taosCloseFile(&pFileFrom);
if (pFileTo != NULL) taosCloseFile(&pFileTo); if (pFileTo != NULL) (void)taosCloseFile(&pFileTo);
/* coverity[+retval] */ /* coverity[+retval] */
taosRemoveFile(to); (void)taosRemoveFile(to);
terrno = code;
return -1; return -1;
#endif #endif
} }
@ -168,24 +184,31 @@ _err:
TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) { TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) {
TdFilePtr fp = taosOpenFile(path, tdFileOptions); TdFilePtr fp = taosOpenFile(path, tdFileOptions);
if (!fp) { if (!fp) {
if (errno == ENOENT) { if (terrno == TAOS_SYSTEM_ERROR(ENOENT)) {
// Try to create directory recursively // Try to create directory recursively
char *s = taosStrdup(path); char s[PATH_MAX];
tstrncpy(s, path, sizeof(s));
if (taosMulMkDir(taosDirName(s)) != 0) { if (taosMulMkDir(taosDirName(s)) != 0) {
taosMemoryFree(s);
return NULL; return NULL;
} }
taosMemoryFree(s);
fp = taosOpenFile(path, tdFileOptions); fp = taosOpenFile(path, tdFileOptions);
if (!fp) { if (!fp) {
return NULL; return NULL;
} }
} }
} }
return fp; 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) { int32_t taosRenameFile(const char *oldName, const char *newName) {
#ifdef WINDOWS #ifdef WINDOWS
@ -217,8 +240,9 @@ int32_t taosRenameFile(const char *oldName, const char *newName) {
return finished ? 0 : -1; return finished ? 0 : -1;
#else #else
int32_t code = rename(oldName, newName); int32_t code = rename(oldName, newName);
if (code < 0) { if (-1 == code) {
printf("failed to rename file %s to %s, reason:%s\n", oldName, newName, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
} }
return code; return code;
@ -233,8 +257,9 @@ int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime, int32_t *a
struct stat fileStat; struct stat fileStat;
int32_t code = stat(path, &fileStat); int32_t code = stat(path, &fileStat);
#endif #endif
if (code < 0) { if (-1 == code) {
return code; terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
} }
if (size != NULL) { if (size != NULL) {
@ -272,13 +297,15 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) {
#else #else
if (pFile == NULL || pFile->fd < 0) { if (pFile == NULL || pFile->fd < 0) {
return -1; terrno = TSDB_CODE_INVALID_PARA;
return terrno;
} }
struct stat fileStat; struct stat fileStat;
int32_t code = fstat(pFile->fd, &fileStat); int32_t code = fstat(pFile->fd, &fileStat);
if (code < 0) { if (-1 == code) {
printf("taosFStatFile run fstat fail."); terrno = TAOS_SYSTEM_ERROR(errno);
return code; return terrno;
} }
if (stDev != NULL) { if (stDev != NULL) {
@ -1150,6 +1177,8 @@ int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset)
if (pFile == NULL) { if (pFile == NULL) {
return 0; return 0;
} }
int32_t code = 0;
#ifdef WINDOWS #ifdef WINDOWS
#if FILE_WITH_LOCK #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; terrno = TSDB_CODE_INVALID_PARA;
return -1; return -1;
} }
int32_t code = 0;
int64_t ret = pread(pFile->fd, buf, count, offset); int64_t ret = pread(pFile->fd, buf, count, offset);
if (-1 == ret) { if (-1 == ret) {
code = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
@ -1205,20 +1233,36 @@ int32_t taosFsyncFile(TdFilePtr pFile) {
return 0; return 0;
} }
int32_t code = 0;
// this implementation is WRONG // this implementation is WRONG
// fflush is not a replacement of fsync // 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 #ifdef WINDOWS
if (pFile->hFile != NULL) { if (pFile->hFile != NULL) {
if (pFile->tdFileOptions & TD_FILE_WRITE_THROUGH) { if (pFile->tdFileOptions & TD_FILE_WRITE_THROUGH) {
return 0; return 0;
} }
return !FlushFileBuffers(pFile->hFile); return !FlushFileBuffers(pFile->hFile);
}
#else #else
if (pFile->fd >= 0) { if (pFile->fd >= 0) {
return fsync(pFile->fd); code = fsync(pFile->fd);
#endif if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
} }
#endif
return 0; return 0;
} }
@ -1471,4 +1515,4 @@ int taosSetAutoDelFile(char* path) {
#else #else
return unlink(path); return unlink(path);
#endif #endif
} }