1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "file_cache.h"
17
FileCache(const std::string & path)18 FileCache::FileCache(const std::string& path) : path_(path), fp_(nullptr)
19 {
20 HILOG_INFO(LOG_CORE, "FileCache: path(%s)!", path_.c_str());
21 }
22
~FileCache()23 FileCache::~FileCache()
24 {
25 fp_ = nullptr;
26 return;
27 }
28
Open(const std::string & file)29 bool FileCache::Open(const std::string& file)
30 {
31 char realPath[PATH_MAX] = {0};
32 if (path_.empty() || (path_.length() >= PATH_MAX) || (realpath(path_.c_str(), realPath) == nullptr)) {
33 HILOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path_.c_str(), errno);
34 return false;
35 }
36 std::string targetFile = std::string(realPath) + std::string("/") + file;
37 fp_ = fopen(targetFile.c_str(), "wb+");
38 CHECK_NOTNULL(fp_, false, "FileCache: open(%s) Failed, errno(%d)", targetFile.c_str(), errno);
39 return true;
40 }
41
Write(char * bytes,int32_t len)42 long FileCache::Write(char* bytes, int32_t len)
43 {
44 CHECK_TRUE((len >= 0) && (bytes != nullptr), -1, "FileCache:%s param invalid!", __func__);
45 CHECK_NOTNULL(fp_, -1, "FileCache:%s fp_ invalid!", __func__);
46
47 // write data bytes
48 int32_t dataLen = len;
49 int32_t writedLen = 0;
50 while (writedLen < dataLen) {
51 size_t len = fwrite(bytes, sizeof(char), dataLen - writedLen, fp_);
52 CHECK_TRUE(len >= 0, -1, "FileCache: write failed, error(%d)!", errno);
53 writedLen += len;
54 }
55
56 return dataLen;
57 }
58
Read(char * content)59 long FileCache::Read(char* content)
60 {
61 CHECK_NOTNULL(content, -1, "FileCache:%s param invalid!", __func__);
62 CHECK_NOTNULL(fp_, -1, "FileCache:%s fp_ invalid!", __func__);
63 uint64_t readLen = 0;
64
65 // read data bytes
66 int ret = fseek(fp_, 0, SEEK_END);
67 CHECK_TRUE(ret == 0, -1, "FileCache:%s fseek_end failed, error(%d)!", __func__, errno);
68 uint64_t dataLen = static_cast<uint64_t>(ftell(fp_));
69 CHECK_TRUE(dataLen > 0, -1, "FileCache:%s ftell failed, error(%d)!", __func__, errno);
70 ret = fseek(fp_, 0, SEEK_SET);
71 CHECK_TRUE(ret == 0, -1, "FileCache:%s fseek_set failed, error(%d)!", __func__, errno);
72
73 while (readLen < dataLen) {
74 size_t len = static_cast<size_t>(fread(content, sizeof(char), dataLen - readLen, fp_));
75 CHECK_TRUE(len >= 0, -1, "FileCache:%s read failed, error(%d)!", __func__, errno);
76 readLen += len;
77 }
78
79 return dataLen;
80 }
81
Close()82 bool FileCache::Close()
83 {
84 CHECK_NOTNULL(fp_, false, "FileCache: %s fp is null", __func__);
85
86 fclose(fp_);
87 fp_ = nullptr;
88
89 return true;
90 }
91