diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index d4f3ba9bed664db72350fe6e0f3c9ff53e65e4d3..6917b6e0149c9930d18e2e1659a09d54b60c73a7 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "common/utils.h" #include "backend_manager.h" @@ -342,44 +343,41 @@ OH_NN_ReturnCode NNCompiledCache::ReadCacheModelFile(const std::string& filePath OHOS::NeuralNetworkRuntime::Buffer& cache) const { // filePath is validate in NNCompiledCache::Restore, no need to check again. - std::ifstream ifs(filePath.c_str(), std::ios::in | std::ios::binary); - if (!ifs) { - LOGE("[NNCompiledCache] ReadCacheModelFile failed, file is invalid."); + FILE* pFile = fopen(filePath.c_str(), "rb"); + if (pFile == NULL) { + LOGE("[NNCompiledCache] ReadCacheModelFile failed, file fopen failed."); return OH_NN_INVALID_FILE; } - int fsize{-1}; - OH_NN_ReturnCode ret = GetCacheFileLength(ifs, fsize); + long fsize{-1}; + OH_NN_ReturnCode ret = GetCacheFileLength(pFile, fsize); if (ret != OH_NN_SUCCESS) { - ifs.close(); + fclose(pFile); LOGE("[NNCompiledCache] ReadCacheModelFile failed, get file %{public}s length fialed.", filePath.c_str()); return ret; } - ifs.seekg(0, std::ios::beg); - if (!ifs.good()) { - LOGE("[NNCompiledCache] ReadCacheModelFile failed, file is invalid."); - ifs.close(); - return OH_NN_INVALID_FILE; - } + rewind(pFile); char* ptr = static_cast(m_device->AllocateBuffer(fsize)); if (ptr == nullptr) { LOGE("[NNCompiledCache] ReadCacheModelFile failed, failed to allocate memory."); - ifs.close(); + fclose(pFile); return OH_NN_MEMORY_ERROR; } - ifs.read(ptr, fsize); - if (!ifs.good()) { + LOGI("ReadCacheModelFile read start."); + size_t result = fread(ptr, 1, fsize, pFile); // size of each object in bytes is 1 + LOGI("ReadCacheModelFile read end."); + if (result != static_cast(fsize)) { LOGE("[NNCompiledCache] ReadCacheModelFile failed, failed to read file."); - ifs.close(); + fclose(pFile); m_device->ReleaseBuffer(ptr); ptr = nullptr; return OH_NN_INVALID_FILE; } - ifs.close(); + fclose(pFile); cache.data = ptr; cache.length = static_cast(fsize); // fsize should be non-negative, safe to cast. return OH_NN_SUCCESS; @@ -405,16 +403,16 @@ unsigned short NNCompiledCache::GetCrc16(char* buffer, size_t length) const return static_cast(~sum); } -OH_NN_ReturnCode NNCompiledCache::GetCacheFileLength(std::ifstream& ifs, int& fileSize) const +OH_NN_ReturnCode NNCompiledCache::GetCacheFileLength(FILE* pFile, long& fileSize) const { - ifs.seekg(0, std::ios::end); - if (!ifs.good()) { + int ret = fseek(pFile, 0L, SEEK_END); + if (ret != 0) { LOGE("[NNCompiledCache] GetCacheFileLength failed, fail to set the position of the next character " "to be extracted from the input stream."); return OH_NN_FAILED; } - int handleValue = ifs.tellg(); + long handleValue = ftell(pFile); if (handleValue == -1) { LOGE("[NNCompiledCache] GetCacheFileLength failed, fail to get position of the input stream."); return OH_NN_INVALID_FILE; @@ -422,7 +420,7 @@ OH_NN_ReturnCode NNCompiledCache::GetCacheFileLength(std::ifstream& ifs, int& fi if ((handleValue > MAX_MODEL_SIZE) || (handleValue == NULL_PTR_LENGTH)) { LOGE("[NNCompiledCache] GetCacheFileLength failed, unable to read huge or empty input stream, " - "get cache file size=%{public}d", + "get cache file size=%{public}ld", handleValue); return OH_NN_INVALID_FILE; } diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.h b/frameworks/native/neural_network_runtime/nncompiled_cache.h index 45897e918117790dbe5996bf8cd05c41ce1243ac..df84886a564a9bf78200ca8afa4699dbb2ae21e5 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.h +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.h @@ -65,7 +65,7 @@ private: uint32_t version) const; OH_NN_ReturnCode ReadCacheModelFile(const std::string& file, Buffer& cache) const; unsigned short GetCrc16(char* buffer, size_t length) const; - OH_NN_ReturnCode GetCacheFileLength(std::ifstream& ifs, int& fileSize) const; + OH_NN_ReturnCode GetCacheFileLength(FILE* pFile, long& fileSize) const; OH_NN_ReturnCode VerifyCachePath(const std::string& cachePath) const; private: