• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "json_utils.h"
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include "config_policy_utils.h"
20 #include "ui_appearance_log.h"
21 
22 namespace OHOS::ArkUi::UiAppearance {
LoadConfiguration(const std::string & path,nlohmann::json & jsonBuf,const std::string & defaultPath)23 bool JsonUtils::LoadConfiguration(const std::string& path, nlohmann::json& jsonBuf,
24     const std::string& defaultPath)
25 {
26     std::string configPath = GetConfigPath(path, defaultPath);
27     LOGD("config path is: %{public}s", configPath.c_str());
28     if (!ReadFileInfoJson(configPath, jsonBuf)) {
29         return false;
30     }
31     return true;
32 }
33 
GetConfigPath(const std::string & path,const std::string & defaultPath)34 std::string JsonUtils::GetConfigPath(const std::string& path, const std::string& defaultPath)
35 {
36     char buf[MAX_PATH_LEN] = { 0 };
37     char *configPath = GetOneCfgFile(path.c_str(), buf, MAX_PATH_LEN);
38     if (configPath == nullptr || configPath[0] == '\0' || strlen(configPath) > MAX_PATH_LEN) {
39         return defaultPath;
40     }
41     return configPath;
42 }
43 
ReadFileInfoJson(const std::string & filePath,nlohmann::json & jsonBuf)44 bool JsonUtils::ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf)
45 {
46     if (filePath.empty()) {
47         LOGE("filePath empty");
48         return false;
49     }
50 
51     if (access(filePath.c_str(), F_OK) != 0) {
52         LOGE("deepLink config not exist");
53         return false;
54     }
55 
56     char path[PATH_MAX] = {0};
57     if (realpath(filePath.c_str(), path) == nullptr) {
58         LOGE("realpath error, errno: %{public}d", errno);
59         return false;
60     }
61 
62     std::string dataBuffer;
63     bool res = ReadFileToBuffer(path, dataBuffer);
64     if (!res) {
65         LOGE("ReadFileToBuffer failed");
66         return false;
67     }
68     jsonBuf = nlohmann::json::parse(dataBuffer, nullptr, false);
69     if (jsonBuf.is_discarded()) {
70         LOGE("bad profile file");
71         return false;
72     }
73 
74     return true;
75 }
76 
ReadFileToBuffer(const std::string & filePath,std::string & dataBuffer)77 bool JsonUtils::ReadFileToBuffer(const std::string &filePath, std::string &dataBuffer)
78 {
79     struct stat statbuf;
80     int ret = stat(filePath.c_str(), &statbuf);
81     if (ret != 0) {
82         LOGE("fail, ret:%{public}d", ret);
83         return false;
84     }
85     size_t bufferSize = static_cast<size_t>(statbuf.st_size);
86 
87     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(bufferSize);
88     if (buffer == nullptr) {
89         LOGE("buffer null");
90         return false;
91     }
92 
93     FILE *fp = fopen(filePath.c_str(), "rb");
94     if (fp == nullptr) {
95         LOGE("fail, real path=%{public}s", filePath.c_str());
96         return false;
97     }
98 
99     ret = fseek(fp, 0, SEEK_END);
100     if (ret != 0) {
101         LOGE("fseek fail, ret=%{public}d", ret);
102         fclose(fp);
103         return false;
104     }
105 
106     size_t fileSize = static_cast<size_t>(ftell(fp));
107 
108     ret = fseek(fp, 0, SEEK_SET);
109     if (ret != 0) {
110         LOGE("fseek fail, ret=%{public}d", ret);
111         fclose(fp);
112         return false;
113     }
114 
115     if (bufferSize < fileSize) {
116         LOGE("buffer size:(%{public}zu) is smaller than file size:(%{public}zu)", bufferSize,
117             fileSize);
118         fclose(fp);
119         return false;
120     }
121     size_t retSize = std::fread(buffer.get(), 1, fileSize, fp);
122     if (retSize != fileSize) {
123         LOGE("read file result size = %{public}zu, size = %{public}zu",
124             retSize, fileSize);
125         fclose(fp);
126         return false;
127     }
128     (void)fclose(fp);
129     dataBuffer = std::string(reinterpret_cast<char *>(buffer.get()), bufferSize);
130     return true;
131 }
132 } // namespace OHOS::ArkUi::UiAppearance
133