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 "bundle_data_storage.h"
17
18 #include <fstream>
19 #include <iomanip>
20
21 #include "string_ex.h"
22 #include "app_log_wrapper.h"
23 #include "bundle_constants.h"
24 #include "bundle_profile.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28
KeyToDeviceAndName(const std::string & key,std::string & deviceId,std::string & bundleName) const29 bool BundleDataStorage::KeyToDeviceAndName(const std::string &key, std::string &deviceId, std::string &bundleName) const
30 {
31 bool ret = false;
32 std::vector<std::string> splitStrs;
33 const std::string::size_type EXPECT_SPLIT_SIZE = 2;
34 OHOS::SplitStr(key, Constants::FILE_UNDERLINE, splitStrs);
35 // the expect split size should be 2.
36 // key rule is <deviceId>_<bundleName>
37 if (splitStrs.size() == EXPECT_SPLIT_SIZE) {
38 deviceId = splitStrs[0];
39 bundleName = splitStrs[1];
40 ret = true;
41 }
42 APP_LOGD("bundleName = %{public}s", bundleName.c_str());
43 return ret;
44 }
45
DeviceAndNameToKey(const std::string & deviceId,const std::string & bundleName,std::string & key) const46 void BundleDataStorage::DeviceAndNameToKey(
47 const std::string &deviceId, const std::string &bundleName, std::string &key) const
48 {
49 key.append(deviceId);
50 key.append(Constants::FILE_UNDERLINE);
51 key.append(bundleName);
52 APP_LOGD("bundleName = %{public}s", bundleName.c_str());
53 }
54
LoadAllData(std::map<std::string,std::map<std::string,InnerBundleInfo>> & infos)55 bool BundleDataStorage::LoadAllData(std::map<std::string, std::map<std::string, InnerBundleInfo>> &infos)
56 {
57 APP_LOGI("load all installed bundle data to map");
58 std::fstream i(Constants::BUNDLE_DATA_BASE_FILE);
59 nlohmann::json jParse;
60 if (!i.is_open()) {
61 APP_LOGE("failed to open bundle database file");
62 // if file not exist, should create file here
63 std::ofstream o(Constants::BUNDLE_DATA_BASE_FILE);
64 o.close();
65 return false;
66 }
67 APP_LOGI("open bundle database file success");
68 i.seekg(0, std::ios::end);
69 int len = static_cast<int>(i.tellg());
70 if (len > 0) {
71 i.seekg(0, std::ios::beg);
72 jParse = nlohmann::json::parse(i, nullptr, false);
73 if (jParse.is_discarded()) {
74 APP_LOGE("bad bundle database file");
75 return false;
76 }
77 for (auto &app : jParse.items()) {
78 std::map<std::string, InnerBundleInfo> deviceMap;
79 for (auto &device : app.value().items()) {
80 InnerBundleInfo innerBundleInfo;
81 if (!innerBundleInfo.FromJson(device.value())) {
82 deviceMap.try_emplace(device.key(), innerBundleInfo);
83 }
84 }
85 infos.try_emplace(app.key(), deviceMap);
86 }
87 }
88 i.close();
89 return true;
90 }
91
SaveStorageBundleInfo(const std::string & deviceId,const InnerBundleInfo & innerBundleInfo)92 bool BundleDataStorage::SaveStorageBundleInfo(const std::string &deviceId, const InnerBundleInfo &innerBundleInfo)
93 {
94 APP_LOGI("save bundle data");
95 bool ret = true;
96 std::string appName = innerBundleInfo.GetApplicationName();
97 std::fstream f(Constants::BUNDLE_DATA_BASE_FILE);
98 bool isExist = f.good();
99
100 if (isExist) {
101 nlohmann::json innerInfo;
102 innerBundleInfo.ToJson(innerInfo);
103 f.seekg(0, std::ios::end);
104 int len = static_cast<int>(f.tellg());
105 if (len == 0) {
106 nlohmann::json appRoot;
107 nlohmann::json app;
108 app[deviceId] = innerInfo;
109 appRoot[appName] = app;
110 f << std::setw(Constants::DUMP_INDENT) << appRoot << std::endl;
111 } else {
112 f.seekg(0, std::ios::beg);
113 nlohmann::json jsonFile;
114 f >> jsonFile;
115 if (jsonFile.find(appName) != jsonFile.end()) {
116 if (jsonFile[appName].find(deviceId) != jsonFile[appName].end()) {
117 // appName and device id is exist
118 APP_LOGE("appName = %{public}s is exist", appName.c_str());
119 ret = false;
120 } else {
121 jsonFile[appName][deviceId] = innerInfo;
122 f.seekp(0, std::ios::beg);
123 f << std::setw(Constants::DUMP_INDENT) << jsonFile << std::endl;
124 }
125 } else {
126 nlohmann::json app;
127 app[deviceId] = innerInfo;
128 jsonFile[appName] = app;
129 f.seekp(0, std::ios::beg);
130 f << std::setw(Constants::DUMP_INDENT) << jsonFile << std::endl;
131 }
132 }
133 } else {
134 APP_LOGI("bundle database file not exist");
135 ret = false;
136 }
137 f.close();
138 return ret;
139 }
140
DeleteStorageBundleInfo(const std::string & deviceId,const InnerBundleInfo & innerBundleInfo)141 bool BundleDataStorage::DeleteStorageBundleInfo(const std::string &deviceId, const InnerBundleInfo &innerBundleInfo)
142 {
143 APP_LOGI("delete bundle data");
144 bool ret = false;
145 bool isEmpty = false;
146 std::string appName = innerBundleInfo.GetApplicationName();
147 std::ifstream i(Constants::BUNDLE_DATA_BASE_FILE);
148 nlohmann::json jParse;
149 if (!i.is_open()) {
150 APP_LOGE("failed to open bundle database file");
151 return false;
152 } else {
153 i.seekg(0, std::ios::end);
154 int len = static_cast<int>(i.tellg());
155 if (len != 0) {
156 i.seekg(0, std::ios::beg);
157 i >> jParse;
158 if (jParse.find(appName) != jParse.end()) {
159 auto it = jParse[appName].find(deviceId);
160 if (it != jParse[appName].end()) {
161 jParse[appName].erase(it);
162 if (jParse[appName].size() == 0) {
163 jParse.erase(appName);
164 if (jParse.size() == 0) {
165 isEmpty = true;
166 }
167 }
168 ret = true;
169 }
170 } else {
171 APP_LOGE("not find appName = %{public}s", appName.c_str());
172 }
173 } else {
174 APP_LOGE("file is empty appName = %{private}s", appName.c_str());
175 }
176 }
177 i.close();
178
179 std::ofstream o(Constants::BUNDLE_DATA_BASE_FILE);
180 if (!o.is_open()) {
181 APP_LOGE("failed to open bundle database file");
182 ret = false;
183 } else {
184 if (!isEmpty) {
185 o << std::setw(Constants::DUMP_INDENT) << jParse;
186 }
187 }
188 o.close();
189 return ret;
190 }
191
192 } // namespace AppExecFwk
193 } // namespace OHOS
194