• 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 "bundle_data_storage.h"
17 
18 #include <fstream>
19 #include <iomanip>
20 
21 #include "app_log_wrapper.h"
22 #include "bundle_constants.h"
23 #include "bundle_profile.h"
24 #include "string_ex.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
LoadAllData(std::map<std::string,InnerBundleInfo> & infos)28 bool BundleDataStorage::LoadAllData(std::map<std::string, InnerBundleInfo> &infos)
29 {
30     APP_LOGI("load all installed bundle data to map");
31     std::fstream i(Constants::BUNDLE_DATA_BASE_FILE);
32     nlohmann::json jParse;
33     if (!i.is_open()) {
34         APP_LOGE("failed to open bundle database file");
35         // if file not exist, should create file here
36         std::ofstream o(Constants::BUNDLE_DATA_BASE_FILE);
37         o.close();
38         return false;
39     }
40     APP_LOGI("open bundle database file success");
41     i.seekg(0, std::ios::end);
42     int len = static_cast<int>(i.tellg());
43     if (len > 0) {
44         i.seekg(0, std::ios::beg);
45         jParse = nlohmann::json::parse(i, nullptr, false);
46         if (jParse.is_discarded()) {
47             APP_LOGE("bad bundle database file");
48             i.close();
49             return false;
50         }
51         for (auto &item : jParse.items()) {
52             InnerBundleInfo innerBundleInfo;
53             if (!innerBundleInfo.FromJson(item.value())) {
54                 infos.try_emplace(item.key(), innerBundleInfo);
55             }
56         }
57     }
58     i.close();
59     return true;
60 }
61 
SaveStorageBundleInfo(const InnerBundleInfo & innerBundleInfo)62 bool BundleDataStorage::SaveStorageBundleInfo(const InnerBundleInfo &innerBundleInfo)
63 {
64     APP_LOGI("save bundle data");
65     bool ret = true;
66     std::string appName = innerBundleInfo.GetBundleName();
67     std::fstream f(Constants::BUNDLE_DATA_BASE_FILE);
68     bool isExist = f.good();
69     if (isExist) {
70         nlohmann::json innerInfo;
71         innerBundleInfo.ToJson(innerInfo);
72         f.seekg(0, std::ios::end);
73         int len = static_cast<int>(f.tellg());
74         if (len == 0) {
75             nlohmann::json appRoot;
76             appRoot[appName] = innerInfo;
77             f << std::setw(Constants::DUMP_INDENT) << appRoot << std::endl;
78         } else {
79             f.seekg(0, std::ios::beg);
80             nlohmann::json jsonFile;
81             f >> jsonFile;
82             jsonFile[appName] = innerInfo;
83             f.seekp(0, std::ios::beg);
84             f << std::setw(Constants::DUMP_INDENT) << jsonFile << std::endl;
85         }
86     } else {
87         APP_LOGI("bundle database file not exist");
88         ret = false;
89     }
90     f.close();
91     return ret;
92 }
93 
DeleteStorageBundleInfo(const InnerBundleInfo & innerBundleInfo)94 bool BundleDataStorage::DeleteStorageBundleInfo(const InnerBundleInfo &innerBundleInfo)
95 {
96     APP_LOGI("delete bundle data");
97     bool ret = false;
98     bool isEmpty = false;
99     std::string appName = innerBundleInfo.GetBundleName();
100     std::ifstream i(Constants::BUNDLE_DATA_BASE_FILE);
101     nlohmann::json jParse;
102     if (!i.is_open()) {
103         APP_LOGE("failed to open bundle database file");
104         return false;
105     } else {
106         i.seekg(0, std::ios::end);
107         int len = static_cast<int>(i.tellg());
108         if (len != 0) {
109             i.seekg(0, std::ios::beg);
110             i >> jParse;
111             if (jParse.find(appName) != jParse.end()) {
112                 jParse.erase(appName);
113                 isEmpty = (jParse.size() == 0);
114                 ret = true;
115             } else {
116                 APP_LOGE("not find appName = %{public}s", appName.c_str());
117             }
118         } else {
119             APP_LOGE("file is empty appName = %{private}s", appName.c_str());
120         }
121     }
122     i.close();
123 
124     std::ofstream o(Constants::BUNDLE_DATA_BASE_FILE);
125     if (!o.is_open()) {
126         APP_LOGE("failed to open bundle database file");
127         ret = false;
128     } else {
129         if (!isEmpty) {
130             o << std::setw(Constants::DUMP_INDENT) << jParse;
131         }
132     }
133     o.close();
134     return ret;
135 }
136 }  // namespace AppExecFwk
137 }  // namespace OHOS
138