106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include "file_cache.h" 1706f6ba60Sopenharmony_ci 1806f6ba60Sopenharmony_ciFileCache::FileCache(const std::string& path) : path_(path), fp_(nullptr) 1906f6ba60Sopenharmony_ci{ 2006f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "FileCache: path(%s)!", path_.c_str()); 2106f6ba60Sopenharmony_ci} 2206f6ba60Sopenharmony_ci 2306f6ba60Sopenharmony_ciFileCache::~FileCache() 2406f6ba60Sopenharmony_ci{ 2506f6ba60Sopenharmony_ci fp_ = nullptr; 2606f6ba60Sopenharmony_ci return; 2706f6ba60Sopenharmony_ci} 2806f6ba60Sopenharmony_ci 2906f6ba60Sopenharmony_cibool FileCache::Open(const std::string& file) 3006f6ba60Sopenharmony_ci{ 3106f6ba60Sopenharmony_ci char realPath[PATH_MAX] = {0}; 3206f6ba60Sopenharmony_ci CHECK_TRUE(!(path_.empty() || (path_.length() >= PATH_MAX) || (realpath(path_.c_str(), realPath) == nullptr)), 3306f6ba60Sopenharmony_ci false, "%s:path is invalid: %s, errno=%d", __func__, path_.c_str(), errno); 3406f6ba60Sopenharmony_ci std::string targetFile = std::string(realPath) + std::string("/") + file; 3506f6ba60Sopenharmony_ci fp_ = fopen(targetFile.c_str(), "wb+"); 3606f6ba60Sopenharmony_ci CHECK_NOTNULL(fp_, false, "FileCache: open(%s) Failed, errno(%d)", targetFile.c_str(), errno); 3706f6ba60Sopenharmony_ci return true; 3806f6ba60Sopenharmony_ci} 3906f6ba60Sopenharmony_ci 4006f6ba60Sopenharmony_cilong FileCache::Write(char* bytes, int32_t len) 4106f6ba60Sopenharmony_ci{ 4206f6ba60Sopenharmony_ci CHECK_TRUE((len >= 0) && (bytes != nullptr), -1, "FileCache:%s param invalid!", __func__); 4306f6ba60Sopenharmony_ci CHECK_NOTNULL(fp_, -1, "FileCache:%s fp_ invalid!", __func__); 4406f6ba60Sopenharmony_ci 4506f6ba60Sopenharmony_ci // write data bytes 4606f6ba60Sopenharmony_ci int32_t dataLen = len; 4706f6ba60Sopenharmony_ci int32_t writedLen = 0; 4806f6ba60Sopenharmony_ci while (writedLen < dataLen) { 4906f6ba60Sopenharmony_ci size_t len = fwrite(bytes, sizeof(char), dataLen - writedLen, fp_); 5006f6ba60Sopenharmony_ci CHECK_TRUE(len >= 0, -1, "FileCache: write failed, error(%d)!", errno); 5106f6ba60Sopenharmony_ci writedLen += static_cast<int32_t>(len); 5206f6ba60Sopenharmony_ci } 5306f6ba60Sopenharmony_ci 5406f6ba60Sopenharmony_ci return dataLen; 5506f6ba60Sopenharmony_ci} 5606f6ba60Sopenharmony_ci 5706f6ba60Sopenharmony_cilong FileCache::Read(char* content) 5806f6ba60Sopenharmony_ci{ 5906f6ba60Sopenharmony_ci CHECK_NOTNULL(content, -1, "FileCache:%s param invalid!", __func__); 6006f6ba60Sopenharmony_ci CHECK_NOTNULL(fp_, -1, "FileCache:%s fp_ invalid!", __func__); 6106f6ba60Sopenharmony_ci uint64_t readLen = 0; 6206f6ba60Sopenharmony_ci 6306f6ba60Sopenharmony_ci // read data bytes 6406f6ba60Sopenharmony_ci int ret = fseek(fp_, 0, SEEK_END); 6506f6ba60Sopenharmony_ci CHECK_TRUE(ret == 0, -1, "FileCache:%s fseek_end failed, error(%d)!", __func__, errno); 6606f6ba60Sopenharmony_ci uint64_t dataLen = static_cast<uint64_t>(ftell(fp_)); 6706f6ba60Sopenharmony_ci CHECK_TRUE(dataLen > 0, -1, "FileCache:%s ftell failed, error(%d)!", __func__, errno); 6806f6ba60Sopenharmony_ci ret = fseek(fp_, 0, SEEK_SET); 6906f6ba60Sopenharmony_ci CHECK_TRUE(ret == 0, -1, "FileCache:%s fseek_set failed, error(%d)!", __func__, errno); 7006f6ba60Sopenharmony_ci 7106f6ba60Sopenharmony_ci while (readLen < dataLen) { 7206f6ba60Sopenharmony_ci size_t len = static_cast<size_t>(fread(content, sizeof(char), dataLen - readLen, fp_)); 7306f6ba60Sopenharmony_ci CHECK_TRUE(len >= 0, -1, "FileCache:%s read failed, error(%d)!", __func__, errno); 7406f6ba60Sopenharmony_ci readLen += len; 7506f6ba60Sopenharmony_ci } 7606f6ba60Sopenharmony_ci 7706f6ba60Sopenharmony_ci return dataLen; 7806f6ba60Sopenharmony_ci} 7906f6ba60Sopenharmony_ci 8006f6ba60Sopenharmony_cibool FileCache::Close() 8106f6ba60Sopenharmony_ci{ 8206f6ba60Sopenharmony_ci CHECK_NOTNULL(fp_, false, "FileCache: %s fp is null", __func__); 8306f6ba60Sopenharmony_ci 8406f6ba60Sopenharmony_ci fclose(fp_); 8506f6ba60Sopenharmony_ci fp_ = nullptr; 8606f6ba60Sopenharmony_ci 8706f6ba60Sopenharmony_ci return true; 8806f6ba60Sopenharmony_ci} 89