fix: file return code
This commit is contained in:
parent
ce76be7eda
commit
5a0147d4dd
|
@ -695,18 +695,21 @@ int taosOpenFileNotStream(const char *path, int32_t tdFileOptions) {
|
||||||
|
|
||||||
int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
|
int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockRdlock(&(pFile->rwlock));
|
(void)taosThreadRwlockRdlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
|
|
||||||
if (pFile->fd < 0) {
|
if (pFile->fd < 0) {
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t leftbytes = count;
|
int64_t leftbytes = count;
|
||||||
int64_t readbytes;
|
int64_t readbytes;
|
||||||
char *tbuf = (char *)buf;
|
char *tbuf = (char *)buf;
|
||||||
|
int32_t code = 0;
|
||||||
|
|
||||||
while (leftbytes > 0) {
|
while (leftbytes > 0) {
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
|
@ -718,14 +721,16 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
|
code = TAOS_SYSTEM_ERROR(errno);
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
terrno = code;
|
||||||
|
return terrno;
|
||||||
}
|
}
|
||||||
} else if (readbytes == 0) {
|
} else if (readbytes == 0) {
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
return (int64_t)(count - leftbytes);
|
return (int64_t)(count - leftbytes);
|
||||||
}
|
}
|
||||||
|
@ -735,28 +740,32 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
|
int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
|
||||||
if (pFile == NULL) {
|
if (pFile == NULL) {
|
||||||
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockWrlock(&(pFile->rwlock));
|
(void)taosThreadRwlockWrlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
if (pFile->fd < 0) {
|
if (pFile->fd < 0) {
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t nleft = count;
|
int64_t nleft = count;
|
||||||
int64_t nwritten = 0;
|
int64_t nwritten = 0;
|
||||||
char *tbuf = (char *)buf;
|
char *tbuf = (char *)buf;
|
||||||
|
int32_t code = 0;
|
||||||
|
|
||||||
while (nleft > 0) {
|
while (nleft > 0) {
|
||||||
nwritten = write(pFile->fd, (void *)tbuf, (uint32_t)nleft);
|
nwritten = write(pFile->fd, (void *)tbuf, (uint32_t)nleft);
|
||||||
|
@ -764,9 +773,11 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
code = TAOS_SYSTEM_ERROR(errno);
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
|
terrno = code;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
nleft -= nwritten;
|
nleft -= nwritten;
|
||||||
|
@ -774,25 +785,30 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t offset) {
|
int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t offset) {
|
||||||
if (pFile == NULL) {
|
if (pFile == NULL) {
|
||||||
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int32_t code = 0;
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockWrlock(&(pFile->rwlock));
|
(void)taosThreadRwlockWrlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
|
|
||||||
|
#if FILE_WITH_LOCK
|
||||||
if (pFile->fd < 0) {
|
if (pFile->fd < 0) {
|
||||||
#if FILE_WITH_LOCK
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
DWORD ret = 0;
|
DWORD ret = 0;
|
||||||
OVERLAPPED ol = {0};
|
OVERLAPPED ol = {0};
|
||||||
|
@ -808,39 +824,62 @@ int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int64_t ret = pwrite(pFile->fd, buf, count, offset);
|
int64_t ret = pwrite(pFile->fd, buf, count, offset);
|
||||||
|
if (-1 == ret) {
|
||||||
|
code = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (code) {
|
||||||
|
terrno = code;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) {
|
int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) {
|
||||||
if (pFile == NULL || pFile->fd < 0) {
|
if (pFile == NULL || pFile->fd < 0) {
|
||||||
return -1;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
|
return terrno;
|
||||||
}
|
}
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockRdlock(&(pFile->rwlock));
|
(void)taosThreadRwlockRdlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
int32_t code = 0;
|
||||||
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
|
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
int64_t ret = _lseeki64(pFile->fd, offset, whence);
|
int64_t ret = _lseeki64(pFile->fd, offset, whence);
|
||||||
#else
|
#else
|
||||||
int64_t ret = lseek(pFile->fd, offset, whence);
|
int64_t ret = lseek(pFile->fd, offset, whence);
|
||||||
|
if (-1 == ret) {
|
||||||
|
code = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if FILE_WITH_LOCK
|
#if FILE_WITH_LOCK
|
||||||
taosThreadRwlockUnlock(&(pFile->rwlock));
|
(void)taosThreadRwlockUnlock(&(pFile->rwlock));
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
|
||||||
|
if (code) {
|
||||||
|
terrno = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
|
int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
|
||||||
if (pFile == NULL) {
|
if (pFile == NULL) {
|
||||||
return 0;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
|
return terrno;
|
||||||
}
|
}
|
||||||
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
|
|
||||||
if (pFile->fd < 0) {
|
if (pFile->fd < 0) {
|
||||||
return -1;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
|
@ -850,7 +889,8 @@ int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
|
||||||
struct stat fileStat;
|
struct stat fileStat;
|
||||||
int32_t code = fstat(pFile->fd, &fileStat);
|
int32_t code = fstat(pFile->fd, &fileStat);
|
||||||
#endif
|
#endif
|
||||||
if (code < 0) {
|
if (-1 == code) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -866,10 +906,11 @@ int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t taosLockFile(TdFilePtr pFile) {
|
int32_t taosLockFile(TdFilePtr pFile) {
|
||||||
ASSERT(pFile->fd >= 0); // Please check if you have closed the file.
|
if (NULL == pFile || pFile->fd < 0) {
|
||||||
if (pFile->fd < 0) {
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return -1;
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
BOOL fSuccess = FALSE;
|
BOOL fSuccess = FALSE;
|
||||||
LARGE_INTEGER fileSize;
|
LARGE_INTEGER fileSize;
|
||||||
|
@ -888,15 +929,21 @@ int32_t taosLockFile(TdFilePtr pFile) {
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
return (int32_t)flock(pFile->fd, LOCK_EX | LOCK_NB);
|
int32_t code = (int32_t)flock(pFile->fd, LOCK_EX | LOCK_NB);
|
||||||
|
if (-1 == code) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
return terrno;
|
||||||
|
}
|
||||||
|
return code;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t taosUnLockFile(TdFilePtr pFile) {
|
int32_t taosUnLockFile(TdFilePtr pFile) {
|
||||||
ASSERT(pFile->fd >= 0);
|
if (NULL == pFile || pFile->fd < 0) {
|
||||||
if (pFile->fd < 0) {
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
return 0;
|
return terrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
BOOL fSuccess = FALSE;
|
BOOL fSuccess = FALSE;
|
||||||
OVERLAPPED overlapped = {0};
|
OVERLAPPED overlapped = {0};
|
||||||
|
@ -908,18 +955,21 @@ int32_t taosUnLockFile(TdFilePtr pFile) {
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
return (int32_t)flock(pFile->fd, LOCK_UN | LOCK_NB);
|
int32_t code = (int32_t)flock(pFile->fd, LOCK_UN | LOCK_NB);
|
||||||
|
if (-1 == code) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
return terrno;
|
||||||
|
}
|
||||||
|
return code;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) {
|
int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) {
|
||||||
if (pFile == NULL) {
|
if (NULL == pFile || pFile->fd < 0) {
|
||||||
return 0;
|
terrno = TSDB_CODE_INVALID_PARA;
|
||||||
}
|
return terrno;
|
||||||
if (pFile->fd < 0) {
|
|
||||||
printf("Ftruncate file error, fd arg was negative\n");
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
|
|
||||||
HANDLE h = (HANDLE)_get_osfhandle(pFile->fd);
|
HANDLE h = (HANDLE)_get_osfhandle(pFile->fd);
|
||||||
|
@ -965,7 +1015,12 @@ int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
return ftruncate(pFile->fd, l_size);
|
int32_t code = ftruncate(pFile->fd, l_size);
|
||||||
|
if (-1 == code) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
return terrno;
|
||||||
|
}
|
||||||
|
return code;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue