• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "data_share_profile_info.h"
17 
18 #include <algorithm>
19 #include <cerrno>
20 #include <fstream>
21 #include <sstream>
22 #include <unistd.h>
23 
24 #include "logger.h"
25 #include "bundle_info.h"
26 #include "hilog/log.h"
27 namespace OHOS::RdbBMSAdapter {
28 using namespace OHOS::Rdb;
29 
30 std::mutex DataShareProfileInfo::infosMutex_;
31 
32 constexpr const char *DATA_SHARE_PROFILE_META = "ohos.extension.dataShare";
33 constexpr const char *PROFILE_FILE_PREFIX = "$profile:";
34 const size_t PROFILE_PREFIX_LEN = strlen(PROFILE_FILE_PREFIX);
Marshal(json & node) const35 bool Config::Marshal(json &node) const
36 {
37     SetValue(node[GET_NAME(uri)], uri);
38     SetValue(node[GET_NAME(crossUserMode)], crossUserMode);
39     SetValue(node[GET_NAME(readPermission)], readPermission);
40     SetValue(node[GET_NAME(writePermission)], writePermission);
41     return true;
42 }
43 
Unmarshal(const json & node)44 bool Config::Unmarshal(const json &node)
45 {
46     bool ret = GetValue(node, GET_NAME(uri), uri);
47     GetValue(node, GET_NAME(crossUserMode), crossUserMode);
48     GetValue(node, GET_NAME(readPermission), readPermission);
49     GetValue(node, GET_NAME(writePermission), writePermission);
50     return ret;
51 }
52 
Marshal(json & node) const53 bool ProfileInfo::Marshal(json &node) const
54 {
55     SetValue(node[GET_NAME(tableConfig)], tableConfig);
56     return true;
57 }
58 
Unmarshal(const json & node)59 bool ProfileInfo::Unmarshal(const json &node)
60 {
61     return GetValue(node, GET_NAME(tableConfig), tableConfig);
62 }
63 
GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo & extensionInfo,std::vector<std::string> & profileInfos)64 bool DataShareProfileInfo::GetResConfigFile(
65     const AppExecFwk::ExtensionAbilityInfo &extensionInfo, std::vector<std::string> &profileInfos)
66 {
67     bool isCompressed = !extensionInfo.hapPath.empty();
68     std::string resourcePath = isCompressed ? extensionInfo.hapPath : extensionInfo.resourcePath;
69     profileInfos = GetResProfileByMetadata(extensionInfo.metadata, resourcePath, isCompressed);
70     if (profileInfos.empty()) {
71         return false;
72     }
73     return true;
74 }
75 
GetDataPropertiesFromProxyDatas(const OHOS::AppExecFwk::ProxyData & proxyData,const std::string & resourcePath,bool isCompressed,DataProperties & dataProperties)76 bool DataShareProfileInfo::GetDataPropertiesFromProxyDatas(const OHOS::AppExecFwk::ProxyData &proxyData,
77     const std::string &resourcePath, bool isCompressed, DataProperties &dataProperties)
78 {
79     std::vector<std::string> infos;
80     infos = GetResProfileByMetadata(proxyData.metadata, resourcePath, isCompressed);
81     if (infos.empty()) {
82         return false;
83     }
84     return dataProperties.Unmarshall(infos[0]);
85 }
86 
GetResProfileByMetadata(const AppExecFwk::Metadata & metadata,const std::string & resourcePath,bool isCompressed)87 std::vector<std::string> DataShareProfileInfo::GetResProfileByMetadata(
88     const AppExecFwk::Metadata &metadata, const std::string &resourcePath, bool isCompressed)
89 {
90     std::vector<std::string> infos;
91     if (metadata.name.empty() || resourcePath.empty()) {
92         return infos;
93     }
94     std::shared_ptr<ResourceManager> resMgr = InitResMgr(resourcePath);
95     if (resMgr == nullptr) {
96         return infos;
97     }
98     if (metadata.name == "dataProperties") {
99         infos = GetResFromResMgr(metadata.resource, *resMgr, isCompressed);
100     }
101     return infos;
102 }
103 
GetResProfileByMetadata(const std::vector<AppExecFwk::Metadata> & metadata,const std::string & resourcePath,bool isCompressed)104 std::vector<std::string> DataShareProfileInfo::GetResProfileByMetadata(
105     const std::vector<AppExecFwk::Metadata> &metadata, const std::string &resourcePath, bool isCompressed)
106 {
107     std::vector<std::string> profileInfos;
108     if (metadata.empty() || resourcePath.empty()) {
109         return profileInfos;
110     }
111     std::shared_ptr<ResourceManager> resMgr = InitResMgr(resourcePath);
112     if (resMgr == nullptr) {
113         return profileInfos;
114     }
115 
116     auto it = std::find_if(metadata.begin(), metadata.end(), [](AppExecFwk::Metadata meta) {
117         return meta.name == DATA_SHARE_PROFILE_META;
118     });
119     if (it != metadata.end()) {
120         return GetResFromResMgr((*it).resource, *resMgr, isCompressed);
121     }
122 
123     return profileInfos;
124 }
125 
InitResMgr(const std::string & resourcePath)126 std::shared_ptr<ResourceManager> DataShareProfileInfo::InitResMgr(const std::string &resourcePath)
127 {
128     std::lock_guard<std::mutex> lock(infosMutex_);
129     static std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
130     if (resMgr == nullptr) {
131         return nullptr;
132     }
133 
134     std::unique_ptr<ResConfig> resConfig(CreateResConfig());
135     if (resConfig == nullptr) {
136         return nullptr;
137     }
138     resMgr->UpdateResConfig(*resConfig);
139     resMgr->AddResource(resourcePath.c_str());
140     return resMgr;
141 }
142 
GetResFromResMgr(const std::string & resName,ResourceManager & resMgr,bool isCompressed)143 std::vector<std::string> DataShareProfileInfo::GetResFromResMgr(
144     const std::string &resName, ResourceManager &resMgr, bool isCompressed)
145 {
146     std::vector<std::string> profileInfos;
147     if (resName.empty()) {
148         return profileInfos;
149     }
150 
151     size_t pos = resName.rfind(PROFILE_FILE_PREFIX);
152     if ((pos == std::string::npos) || (pos == resName.length() - PROFILE_PREFIX_LEN)) {
153         LOG_ERROR("res name invalid, resName is %{public}s", resName.c_str());
154         return profileInfos;
155     }
156     std::string profileName = resName.substr(pos + PROFILE_PREFIX_LEN);
157     // hap is compressed status, get file content.
158     if (isCompressed) {
159         LOG_DEBUG("compressed status.");
160         std::unique_ptr<uint8_t[]> fileContent = nullptr;
161         size_t len = 0;
162         RState ret = resMgr.GetProfileDataByName(profileName.c_str(), len, fileContent);
163         if (ret != SUCCESS || fileContent == nullptr) {
164             LOG_ERROR("failed, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str());
165             return profileInfos;
166         }
167         if (len == 0) {
168             LOG_ERROR("fileContent is empty, profileName is %{public}s", profileName.c_str());
169             return profileInfos;
170         }
171         std::string rawData(fileContent.get(), fileContent.get() + len);
172         if (!Config::IsJson(rawData)) {
173             LOG_ERROR("rawData is not json, profileName is %{public}s", profileName.c_str());
174             return profileInfos;
175         }
176         profileInfos.push_back(std::move(rawData));
177         return profileInfos;
178     }
179     // hap is decompressed status, get file path then read file.
180     std::string resPath;
181     RState ret = resMgr.GetProfileByName(profileName.c_str(), resPath);
182     if (ret != SUCCESS) {
183         LOG_ERROR("profileName not found, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str());
184         return profileInfos;
185     }
186     std::string profile = ReadProfile(resPath);
187     if (profile.empty()) {
188         LOG_ERROR("Read profile failed, resPath is %{public}s", resPath.c_str());
189         return profileInfos;
190     }
191     profileInfos.push_back(std::move(profile));
192     return profileInfos;
193 }
194 
IsFileExisted(const std::string & filePath)195 bool DataShareProfileInfo::IsFileExisted(const std::string &filePath)
196 {
197     if (filePath.empty()) {
198         return false;
199     }
200     if (access(filePath.c_str(), F_OK) != 0) {
201         LOG_ERROR("can not access file, errno is %{public}d, filePath is %{public}s", errno, filePath.c_str());
202         return false;
203     }
204     return true;
205 }
206 
ReadProfile(const std::string & resPath)207 std::string DataShareProfileInfo::ReadProfile(const std::string &resPath)
208 {
209     if (!IsFileExisted(resPath)) {
210         return "";
211     }
212     std::fstream in;
213     in.open(resPath, std::ios_base::in | std::ios_base::binary);
214     if (!in.is_open()) {
215         LOG_ERROR("the file can not open, errno is %{public}d", errno);
216         return "";
217     }
218     in.seekg(0, std::ios::end);
219     int64_t size = in.tellg();
220     if (size <= 0) {
221         LOG_ERROR("the file is empty, resPath is %{public}s", resPath.c_str());
222         return "";
223     }
224     in.seekg(0, std::ios::beg);
225     std::ostringstream tmp;
226     tmp << in.rdbuf();
227     return tmp.str();
228 }
229 } // namespace OHOS::RdbBMSAdapter