diff --git a/file/src/file_impl_hal/file.c b/file/src/file_impl_hal/file.c index 980b50c1de9297b5656c09ce612ee65d8639c494..6bce29b29255a6f2e5c1c53f9d122adbc67228ee 100755 --- a/file/src/file_impl_hal/file.c +++ b/file/src/file_impl_hal/file.c @@ -19,6 +19,8 @@ #include "ohos_errno.h" #include "ohos_types.h" +#include + #define BUFFER_SIZE 128 int UtilsFileOpen(const char* path, int oflag, int mode) @@ -70,28 +72,26 @@ int UtilsFileCopy(const char* src, const char* dest) UtilsFileClose(fpSrc); return fpDest; } + bool copyFailed = true; char* dataBuf = (char *)malloc(BUFFER_SIZE); if (dataBuf == NULL) { - UtilsFileClose(fpSrc); - UtilsFileClose(fpDest); - UtilsFileDelete(dest); - return EC_FAILURE; + goto MALLOC_ERROR; } int nLen = UtilsFileRead(fpSrc, dataBuf, BUFFER_SIZE); while (nLen > 0) { if (UtilsFileWrite(fpDest, dataBuf, nLen) != nLen) { - free(dataBuf); - UtilsFileClose(fpSrc); - UtilsFileClose(fpDest); - UtilsFileDelete(dest); - return EC_FAILURE; + goto EXIT; } nLen = UtilsFileRead(fpSrc, dataBuf, BUFFER_SIZE); } + copyFailed = (nLen < 0); + +EXIT: free(dataBuf); +MALLOC_ERROR: UtilsFileClose(fpSrc); UtilsFileClose(fpDest); - if (nLen < 0) { + if (copyFailed) { UtilsFileDelete(dest); return EC_FAILURE; } diff --git a/js/builtin/filekit/src/nativeapi_fs_impl.c b/js/builtin/filekit/src/nativeapi_fs_impl.c index 9fa91e8a93fc9ea95b420aec68117950503b0d28..edd34b80f42f5402b8eb8bdb1744dab85a5c445a 100755 --- a/js/builtin/filekit/src/nativeapi_fs_impl.c +++ b/js/builtin/filekit/src/nativeapi_fs_impl.c @@ -69,24 +69,17 @@ static int RmdirRecursive(const char* fileName) struct stat info = { 0 }; char* fullPath = (char *)malloc(FILE_NAME_MAX_LEN + 1); if (fullPath == NULL) { - closedir(fileDir); - return ret; + goto MALLOC_ERROR; } while (dir != NULL) { if (memset_s(fullPath, FILE_NAME_MAX_LEN + 1, 0x0, FILE_NAME_MAX_LEN + 1) != EOK) { - free(fullPath); - closedir(fileDir); - return ret; + goto EXIT; } if (sprintf_s(fullPath, FILE_NAME_MAX_LEN + 1, "%s/%s", fileName, dir->d_name) < 0) { - free(fullPath); - closedir(fileDir); - return ret; + goto EXIT; } if (stat(fullPath, &info) != 0) { - free(fullPath); - closedir(fileDir); - return ret; + goto EXIT; } if (S_ISDIR(info.st_mode)) { ret = RmdirRecursive(fullPath); @@ -94,15 +87,17 @@ static int RmdirRecursive(const char* fileName) ret = unlink(fullPath); } if (ret != NATIVE_SUCCESS) { - free(fullPath); - closedir(fileDir); - return ret; + goto EXIT; } dir = readdir(fileDir); } + ret = rmdir(fileName); + +EXIT: free(fullPath); +MALLOC_ERROR: closedir(fileDir); - return rmdir(fileName); + return ret; } static int MakeParent(const char* path, char* firstPath, size_t fPathLen, int* dirNum) @@ -175,10 +170,7 @@ static int DoCopyFile(int fdSrc, int fdDest) nLen = read(fdSrc, dataBuf, BUFFER_SIZE); } free(dataBuf); - if (nLen < 0) { - return ERROR_CODE_IO; - } - return NATIVE_SUCCESS; + return (nLen < 0) ? ERROR_CODE_IO : NATIVE_SUCCESS; } int StatImpl(const char* path, struct stat* buf) @@ -186,8 +178,8 @@ int StatImpl(const char* path, struct stat* buf) if (!IsValidPath(path) || (buf == NULL)) { return ERROR_CODE_PARAM; } - int ret = stat(path, buf); - if (ret != NATIVE_SUCCESS) { + + if (stat(path, buf) != NATIVE_SUCCESS) { return (-errno); } return NATIVE_SUCCESS; @@ -198,8 +190,8 @@ int DeleteFileImpl(const char* src) if (!IsValidPath(src)) { return ERROR_CODE_PARAM; } - int ret = unlink(src); - if (ret != NATIVE_SUCCESS) { + + if (unlink(src) != NATIVE_SUCCESS) { return (-errno); } return NATIVE_SUCCESS; @@ -242,11 +234,11 @@ int CopyFileImpl(const char* src, const char* dest) return (-errno); } int ret = DoCopyFile(fdSrc, fdDest); + close(fdSrc); + close(fdDest); if (ret != NATIVE_SUCCESS) { unlink(dest); } - close(fdSrc); - close(fdDest); return ret; } @@ -273,12 +265,9 @@ int WriteTextFile(const char* fileName, const void* buf, size_t len, bool append if (fileHandle < 0) { return (-errno); } - if (write(fileHandle, buf, len) != len) { - close(fileHandle); - return ERROR_CODE_IO; - } + int writeLen = write(fileHandle, buf, len); close(fileHandle); - return NATIVE_SUCCESS; + return (writeLen != len) ? ERROR_CODE_IO : NATIVE_SUCCESS; } int WriteArrayFile(const char* fileName, const void* buf, size_t len, unsigned int position, bool append) @@ -314,12 +303,9 @@ int WriteArrayFile(const char* fileName, const void* buf, size_t len, unsigned i return ERROR_CODE_IO; } } - if (write(fileHandle, buf, len) != len) { - close(fileHandle); - return ERROR_CODE_IO; - } + int writeLen = write(fileHandle, buf, len); close(fileHandle); - return NATIVE_SUCCESS; + return (writeLen != len) ? ERROR_CODE_IO : NATIVE_SUCCESS; } int ReadFileImpl(const char* fileName, void* text, size_t len, unsigned int position, size_t* actualLen) @@ -358,13 +344,9 @@ int ReadFileImpl(const char* fileName, void* text, size_t len, unsigned int posi return ERROR_CODE_IO; } int readLen = read(fileHandle, text, len); - *actualLen = readLen; - if (readLen < 0) { - close(fileHandle); - return ERROR_CODE_IO; - } close(fileHandle); - return NATIVE_SUCCESS; + *actualLen = readLen; + return (readLen < 0) ? ERROR_CODE_IO : NATIVE_SUCCESS; } int GetFileListImpl(const char* dirName, FileMetaInfo* fileList, unsigned int listNum) @@ -427,10 +409,7 @@ int GetFileNum(const char* dirName) dir = readdir(fileDir); } closedir(fileDir); - if (sum == 0) { - return ERROR_CODE_IO; - } - return sum; + return (sum == 0) ? ERROR_CODE_IO : sum; } int AccessImpl(const char* fileName) @@ -455,8 +434,8 @@ int CreateDirImpl(const char* fileName, bool recursive) if (recursive) { return MkdirRecursive(fileName); } - int ret = mkdir(fileName, S_IRUSR | S_IWUSR | S_IXUSR); - if (ret != NATIVE_SUCCESS) { + + if (mkdir(fileName, S_IRUSR | S_IWUSR | S_IXUSR) != NATIVE_SUCCESS) { return (-errno); } return NATIVE_SUCCESS; @@ -471,8 +450,7 @@ int RemoveDirImpl(const char* fileName, bool recursive) if (recursive) { return RmdirRecursive(fileName); } - ret = rmdir(fileName); - if (ret != NATIVE_SUCCESS) { + if (rmdir(fileName) != NATIVE_SUCCESS) { return (-errno); } return NATIVE_SUCCESS; diff --git a/js/builtin/kvstorekit/src/nativeapi_kv.cpp b/js/builtin/kvstorekit/src/nativeapi_kv.cpp index fdf1ae1f7db0256b22244fee1a5937ab3c50beae..4f5d4abf47f908c1778a5c124f1a0882cf010cff 100755 --- a/js/builtin/kvstorekit/src/nativeapi_kv.cpp +++ b/js/builtin/kvstorekit/src/nativeapi_kv.cpp @@ -75,8 +75,7 @@ int GetValueInner(const char* dataPath, const char* key, char* value) if (ret != NATIVE_SUCCESS) { return ret; } - ret = GetValue(g_kvFullPath, value); - return ret; + return GetValue(g_kvFullPath, value); } int SetValueInner(const char* dataPath, const char* key, const char* value) @@ -85,8 +84,7 @@ int SetValueInner(const char* dataPath, const char* key, const char* value) if (ret != NATIVE_SUCCESS) { return ret; } - ret = SetValue(g_kvFullPath, value); - return ret; + return SetValue(g_kvFullPath, value); } int DeleteValueInner(const char* dataPath, const char* key) @@ -95,8 +93,7 @@ int DeleteValueInner(const char* dataPath, const char* key) if (ret != NATIVE_SUCCESS) { return ret; } - ret = DeleteValue(g_kvFullPath); - return ret; + return DeleteValue(g_kvFullPath); } JSIValue ExecuteAsyncWork(const JSIValue thisVal, const JSIValue* args, diff --git a/js/builtin/kvstorekit/src/nativeapi_kv_impl.c b/js/builtin/kvstorekit/src/nativeapi_kv_impl.c index 94ec96095341852d0ed54bc19128a0ced1310df0..1550d5011815bcbe89d977e0f0ad05caff667ef5 100755 --- a/js/builtin/kvstorekit/src/nativeapi_kv_impl.c +++ b/js/builtin/kvstorekit/src/nativeapi_kv_impl.c @@ -129,12 +129,9 @@ int SetValue(const char* key, const char* value) if (fd < 0) { return (-errno); } - if (write(fd, value, strlen(value)) < 0) { - close(fd); - return ERROR_CODE_IO; - } + int ret = write(fd, value, strlen(value)); close(fd); - return NATIVE_SUCCESS; + return (ret < 0) ? ERROR_CODE_IO : NATIVE_SUCCESS; } int DeleteValue(const char* key) @@ -142,8 +139,7 @@ int DeleteValue(const char* key) if (key == NULL) { return ERROR_CODE_PARAM; } - int ret = unlink(key); - if (ret != NATIVE_SUCCESS) { + if (unlink(key) != NATIVE_SUCCESS) { return (-errno); } return NATIVE_SUCCESS;