• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     CHECK_TRUE(!(path_.empty() || (path_.length() >= PATH_MAX) || (realpath(path_.c_str(), realPath) == nullptr)),
33                false, "%s:path is invalid: %s, errno=%d", __func__, path_.c_str(), errno);
34     std::string targetFile = std::string(realPath) + std::string("/") + file;
35     fp_ = fopen(targetFile.c_str(), "wb+");
36     CHECK_NOTNULL(fp_, false, "FileCache: open(%s) Failed, errno(%d)", targetFile.c_str(), errno);
37     return true;
38 }
39 
Write(char * bytes,int32_t len)40 long FileCache::Write(char* bytes, int32_t len)
41 {
42     CHECK_TRUE((len >= 0) && (bytes != nullptr), -1, "FileCache:%s param invalid!", __func__);
43     CHECK_NOTNULL(fp_, -1, "FileCache:%s fp_ invalid!", __func__);
44 
45     // write data bytes
46     int32_t dataLen = len;
47     int32_t writedLen = 0;
48     while (writedLen < dataLen) {
49         size_t len = fwrite(bytes, sizeof(char), dataLen - writedLen, fp_);
50         CHECK_TRUE(len >= 0, -1, "FileCache: write failed, error(%d)!", errno);
51         writedLen += len;
52     }
53 
54     return dataLen;
55 }
56 
Read(char * content)57 long FileCache::Read(char* content)
58 {
59     CHECK_NOTNULL(content, -1, "FileCache:%s param invalid!", __func__);
60     CHECK_NOTNULL(fp_, -1, "FileCache:%s fp_ invalid!", __func__);
61     uint64_t readLen = 0;
62 
63     // read data bytes
64     int ret = fseek(fp_, 0, SEEK_END);
65     CHECK_TRUE(ret == 0, -1, "FileCache:%s fseek_end failed, error(%d)!", __func__, errno);
66     uint64_t dataLen = static_cast<uint64_t>(ftell(fp_));
67     CHECK_TRUE(dataLen > 0, -1, "FileCache:%s ftell failed, error(%d)!", __func__, errno);
68     ret = fseek(fp_, 0, SEEK_SET);
69     CHECK_TRUE(ret == 0, -1, "FileCache:%s fseek_set failed, error(%d)!", __func__, errno);
70 
71     while (readLen < dataLen) {
72         size_t len = static_cast<size_t>(fread(content, sizeof(char), dataLen - readLen, fp_));
73         CHECK_TRUE(len >= 0, -1, "FileCache:%s read failed, error(%d)!", __func__, errno);
74         readLen += len;
75     }
76 
77     return dataLen;
78 }
79 
Close()80 bool FileCache::Close()
81 {
82     CHECK_NOTNULL(fp_, false, "FileCache: %s fp is null", __func__);
83 
84     fclose(fp_);
85     fp_ = nullptr;
86 
87     return true;
88 }
89