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 #define LOG_TAG "PreProcessUtils"
16
17 #include "preprocess_utils.h"
18
19 #include <sstream>
20
21 #include "bundle_info.h"
22 #include "dds_trace.h"
23 #include "udmf_radar_reporter.h"
24 #include "accesstoken_kit.h"
25 #include "checker/checker_manager.h"
26 #include "device_manager_adapter.h"
27 #include "file_mount_manager.h"
28 #include "iservice_registry.h"
29 #include "log_print.h"
30 #include "system_ability_definition.h"
31 #include "udmf_radar_reporter.h"
32 #include "udmf_utils.h"
33 #include "utils/crypto.h"
34 #include "uri_permission_manager_client.h"
35 #include "ipc_skeleton.h"
36 namespace OHOS {
37 namespace UDMF {
38 static constexpr int ID_LEN = 32;
39 static constexpr int MINIMUM = 48;
40 static constexpr int MAXIMUM = 121;
41 constexpr char SPECIAL = '^';
42 constexpr const char *FILE_SCHEME = "file";
43 constexpr const char *TAG = "PreProcessUtils::";
44 constexpr const char *FILE_SCHEME_PREFIX = "file://";
45 constexpr const char *DOCS_LOCAL_TAG = "/docs/";
46 static constexpr uint32_t DOCS_LOCAL_PATH_SUBSTR_START_INDEX = 1;
47 static constexpr uint32_t VERIFY_URI_PERMISSION_MAX_SIZE = 500;
48 constexpr const char *TEMP_UNIFIED_DATA_FLAG = "temp_udmf_file_flag";
49 static constexpr size_t TEMP_UDATA_RECORD_SIZE = 1;
50 static constexpr uint32_t PREFIX_LEN = 24;
51 static constexpr uint32_t INDEX_LEN = 8;
52 static constexpr const char PLACE_HOLDER = '0';
53 using namespace OHOS::DistributedDataDfx;
54 using namespace Security::AccessToken;
55 using namespace OHOS::AppFileService::ModuleRemoteFileShare;
56 using namespace RadarReporter;
57
FillRuntimeInfo(UnifiedData & data,CustomOption & option)58 int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option)
59 {
60 auto it = UD_INTENTION_MAP.find(option.intention);
61 if (it == UD_INTENTION_MAP.end()) {
62 return E_ERROR;
63 }
64 std::string bundleName;
65 std::string specificBundleName;
66 if (!GetSpecificBundleNameByTokenId(option.tokenId, specificBundleName, bundleName)) {
67 ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", option.tokenId);
68 return E_ERROR;
69 }
70 std::string intention = it->second;
71 UnifiedKey key(intention, specificBundleName, GenerateId());
72 Privilege privilege;
73 privilege.tokenId = option.tokenId;
74 std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId(
75 { IPCSkeleton::GetCallingUid(), option.tokenId, bundleName });
76 Runtime runtime;
77 runtime.key = key;
78 runtime.privileges.emplace_back(privilege);
79 runtime.createTime = GetTimestamp();
80 runtime.sourcePackage = bundleName;
81 runtime.createPackage = bundleName;
82 runtime.deviceId = GetLocalDeviceId();
83 runtime.recordTotalNum = static_cast<uint32_t>(data.GetRecords().size());
84 runtime.tokenId = option.tokenId;
85 runtime.sdkVersion = GetSdkVersionByToken(option.tokenId);
86 runtime.visibility = option.visibility;
87 runtime.appId = appId;
88 data.SetRuntime(runtime);
89 return E_OK;
90 }
91
GenerateId()92 std::string PreProcessUtils::GenerateId()
93 {
94 std::vector<uint8_t> randomDevices = DistributedData::Crypto::Random(ID_LEN, MINIMUM, MAXIMUM);
95 std::stringstream idStr;
96 for (auto &randomDevice : randomDevices) {
97 auto asc = randomDevice;
98 asc = asc >= SPECIAL ? asc + 1 : asc;
99 idStr << static_cast<uint8_t>(asc);
100 }
101 return idStr.str();
102 }
103
GetTimestamp()104 time_t PreProcessUtils::GetTimestamp()
105 {
106 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
107 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
108 time_t timestamp = tp.time_since_epoch().count();
109 return timestamp;
110 }
111
GetHapUidByToken(uint32_t tokenId,int & userId)112 int32_t PreProcessUtils::GetHapUidByToken(uint32_t tokenId, int &userId)
113 {
114 if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
115 ZLOGE("TokenType is not TOKEN_HAP");
116 return E_ERROR;
117 }
118 Security::AccessToken::HapTokenInfo tokenInfo;
119 auto result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo);
120 if (result != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
121 ZLOGE("GetHapUidByToken failed, result = %{public}d.", result);
122 return E_ERROR;
123 }
124 userId = tokenInfo.userID;
125 return E_OK;
126 }
127
GetHapBundleNameByToken(uint32_t tokenId,std::string & bundleName)128 bool PreProcessUtils::GetHapBundleNameByToken(uint32_t tokenId, std::string &bundleName)
129 {
130 if (UTILS::IsTokenNative()) {
131 ZLOGD("TypeATokenTypeEnum is TOKEN_HAP");
132 std::string processName;
133 if (GetNativeProcessNameByToken(tokenId, processName)) {
134 bundleName = processName;
135 return true;
136 }
137 }
138 Security::AccessToken::HapTokenInfo hapInfo;
139 if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo)
140 == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
141 bundleName = hapInfo.bundleName;
142 return true;
143 }
144 ZLOGE("Get bundle name faild");
145 return false;
146 }
147
GetNativeProcessNameByToken(uint32_t tokenId,std::string & processName)148 bool PreProcessUtils::GetNativeProcessNameByToken(uint32_t tokenId, std::string &processName)
149 {
150 Security::AccessToken::NativeTokenInfo nativeInfo;
151 if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, nativeInfo)
152 != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
153 return false;
154 }
155 processName = nativeInfo.processName;
156 return true;
157 }
158
GetLocalDeviceId()159 std::string PreProcessUtils::GetLocalDeviceId()
160 {
161 auto info = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice();
162 std::string encryptedUuid = DistributedData::DeviceManagerAdapter::GetInstance().CalcClientUuid(" ", info.uuid);
163 return encryptedUuid;
164 }
165
SetRemoteData(UnifiedData & data)166 void PreProcessUtils::SetRemoteData(UnifiedData &data)
167 {
168 if (data.IsEmpty()) {
169 ZLOGD("invalid data.");
170 return;
171 }
172 std::shared_ptr<Runtime> runtime = data.GetRuntime();
173 if (runtime->deviceId == GetLocalDeviceId()) {
174 ZLOGD("not remote data.");
175 return;
176 }
177 ZLOGD("is remote data.");
178 auto records = data.GetRecords();
179 ProcessFileType(records, [] (std::shared_ptr<Object> obj) {
180 std::shared_ptr<Object> detailObj;
181 obj->GetValue(DETAILS, detailObj);
182 if (detailObj == nullptr) {
183 ZLOGE("No details for object");
184 return false;
185 }
186 UDDetails details = ObjectUtils::ConvertToUDDetails(detailObj);
187 details.insert({ "isRemote", "true" });
188 obj->value_[DETAILS] = ObjectUtils::ConvertToObject(details);
189 return true;
190 });
191 }
192
SetRemoteUri(uint32_t tokenId,UnifiedData & data)193 int32_t PreProcessUtils::SetRemoteUri(uint32_t tokenId, UnifiedData &data)
194 {
195 std::vector<std::string> uris;
196 ProcessFileType(data.GetRecords(), [&uris](std::shared_ptr<Object> obj) {
197 std::string oriUri;
198 obj->GetValue(ORI_URI, oriUri);
199 if (oriUri.empty()) {
200 ZLOGW("URI is empty, please check");
201 return false;
202 }
203 Uri uri(oriUri);
204 std::string scheme = uri.GetScheme();
205 std::transform(scheme.begin(), scheme.end(), scheme.begin(), ::tolower);
206 if (uri.GetAuthority().empty() || scheme != FILE_SCHEME) {
207 ZLOGW("Empty URI authority or scheme not file");
208 return false;
209 }
210 uris.push_back(oriUri);
211 return true;
212 });
213 GetHtmlFileUris(tokenId, data, true, uris);
214 if (!uris.empty()) {
215 ZLOGI("Read to check uri authorization");
216 if (!CheckUriAuthorization(uris, tokenId)) {
217 ZLOGE("UriAuth check failed:bundleName:%{public}s,tokenId:%{public}d,uris size:%{public}zu",
218 data.GetRuntime()->createPackage.c_str(), tokenId, uris.size());
219 RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
220 BizScene::SET_DATA, SetDataStage::VERIFY_SHARE_PERMISSIONS, StageRes::FAILED, E_NO_PERMISSION);
221 return E_NO_PERMISSION;
222 }
223 if (!IsNetworkingEnabled()) {
224 return E_OK;
225 }
226 int32_t userId;
227 if (GetHapUidByToken(tokenId, userId) == E_OK) {
228 GetDfsUrisFromLocal(uris, userId, data);
229 }
230 }
231 return E_OK;
232 }
233
GetDfsUrisFromLocal(const std::vector<std::string> & uris,int32_t userId,UnifiedData & data)234 int32_t PreProcessUtils::GetDfsUrisFromLocal(const std::vector<std::string> &uris, int32_t userId, UnifiedData &data)
235 {
236 DdsTrace trace(
237 std::string(TAG) + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON | TraceSwitch::TRACE_CHAIN_ON);
238 RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
239 BizScene::SET_DATA, SetDataStage::GERERATE_DFS_URI, StageRes::IDLE);
240 std::unordered_map<std::string, HmdfsUriInfo> dfsUris;
241 int32_t ret = Storage::DistributedFile::FileMountManager::GetDfsUrisDirFromLocal(uris, userId, dfsUris);
242 if (ret != 0 || dfsUris.empty()) {
243 RadarReporterAdapter::ReportFail(std::string(__FUNCTION__),
244 BizScene::SET_DATA, SetDataStage::GERERATE_DFS_URI, StageRes::FAILED, E_FS_ERROR, BizState::DFX_END);
245 ZLOGE("Get remoteUri failed, ret = %{public}d, userId: %{public}d, uri size:%{public}zu.",
246 ret, userId, uris.size());
247 return E_FS_ERROR;
248 }
249 RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
250 BizScene::SET_DATA, SetDataStage::GERERATE_DFS_URI, StageRes::SUCCESS);
251 ProcessFileType(data.GetRecords(), [&dfsUris] (std::shared_ptr<Object> obj) {
252 std::string oriUri;
253 obj->GetValue(ORI_URI, oriUri);
254 auto iter = dfsUris.find(oriUri);
255 if (iter != dfsUris.end()) {
256 obj->value_[REMOTE_URI] = (iter->second).uriStr;
257 }
258 return true;
259 });
260 for (auto &record : data.GetRecords()) {
261 if (record == nullptr) {
262 continue;
263 }
264 record->ComputeUris([&dfsUris] (UriInfo &uriInfo) {
265 auto iter = dfsUris.find(uriInfo.authUri);
266 if (iter != dfsUris.end()) {
267 uriInfo.dfsUri = (iter->second).uriStr;
268 }
269 return true;
270 });
271 }
272 RadarReporterAdapter::ReportNormal(std::string(__FUNCTION__),
273 BizScene::SET_DATA, SetDataStage::GERERATE_DFS_URI, StageRes::SUCCESS);
274 return E_OK;
275 }
276
CheckUriAuthorization(const std::vector<std::string> & uris,uint32_t tokenId)277 bool PreProcessUtils::CheckUriAuthorization(const std::vector<std::string>& uris, uint32_t tokenId)
278 {
279 for (size_t index = 0; index < uris.size(); index += VERIFY_URI_PERMISSION_MAX_SIZE) {
280 std::vector<std::string> urisToBeChecked(
281 uris.begin() + index, uris.begin() + std::min(index + VERIFY_URI_PERMISSION_MAX_SIZE, uris.size()));
282 auto checkResults = AAFwk::UriPermissionManagerClient::GetInstance().CheckUriAuthorization(
283 urisToBeChecked, AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION, tokenId);
284 auto iter = find(checkResults.begin(), checkResults.end(), false);
285 if (iter != checkResults.end()) {
286 return false;
287 }
288 }
289 return true;
290 }
291
GetInstIndex(uint32_t tokenId,int32_t & instIndex)292 bool PreProcessUtils::GetInstIndex(uint32_t tokenId, int32_t &instIndex)
293 {
294 if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
295 instIndex = 0;
296 return true;
297 }
298 HapTokenInfo tokenInfo;
299 int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo);
300 if (errCode != RET_SUCCESS) {
301 ZLOGE("Get Hap TokenInfo error:%{public}d, tokenId:0x%{public}x", errCode, tokenId);
302 return false;
303 }
304 instIndex = tokenInfo.instIndex;
305 return true;
306 }
307
IsNetworkingEnabled()308 bool PreProcessUtils::IsNetworkingEnabled()
309 {
310 std::vector<AppDistributedKv::DeviceInfo> devInfos =
311 DistributedData::DeviceManagerAdapter::GetInstance().GetRemoteDevices();
312 ZLOGI("Remote devices count:%{public}u", static_cast<uint32_t>(devInfos.size()));
313 if (devInfos.empty()) {
314 return false;
315 }
316 return true;
317 }
318
ProcessFileType(std::vector<std::shared_ptr<UnifiedRecord>> records,std::function<bool (std::shared_ptr<Object>)> callback)319 void PreProcessUtils::ProcessFileType(std::vector<std::shared_ptr<UnifiedRecord>> records,
320 std::function<bool(std::shared_ptr<Object>)> callback)
321 {
322 for (auto record : records) {
323 if (record == nullptr) {
324 ZLOGW("Record is empty!");
325 continue;
326 }
327 auto entries = record->GetEntries();
328 if (entries == nullptr) {
329 ZLOGW("GetEntries returns empty!");
330 continue;
331 }
332 auto entry = entries->find(GENERAL_FILE_URI);
333 if (entry == entries->end()) {
334 continue;
335 }
336 auto value = entry->second;
337 if (!std::holds_alternative<std::shared_ptr<Object>>(value)) {
338 continue;
339 }
340 auto obj = std::get<std::shared_ptr<Object>>(value);
341 if (obj == nullptr) {
342 ZLOGE("ValueType is not Object, Not convert to remote uri!");
343 continue;
344 }
345 std::string dataType;
346 if (obj->GetValue(UNIFORM_DATA_TYPE, dataType) && dataType == GENERAL_FILE_URI) {
347 callback(obj);
348 }
349 }
350 }
351
ProcessRecord(std::shared_ptr<UnifiedRecord> record,uint32_t tokenId,bool isLocal,std::vector<std::string> & uris)352 void PreProcessUtils::ProcessRecord(std::shared_ptr<UnifiedRecord> record, uint32_t tokenId,
353 bool isLocal, std::vector<std::string> &uris)
354 {
355 record->ComputeUris([&uris, &isLocal, &tokenId] (UriInfo &uriInfo) {
356 std::string newUriStr = "";
357 if (isLocal && uriInfo.authUri.empty()) {
358 Uri tmpUri(uriInfo.oriUri);
359 std::string path = tmpUri.GetPath();
360 std::string bundleName;
361 if (!GetHapBundleNameByToken(tokenId, bundleName)) {
362 return true;
363 }
364 if (path.substr(0, strlen(DOCS_LOCAL_TAG)) == DOCS_LOCAL_TAG) {
365 newUriStr = FILE_SCHEME_PREFIX;
366 newUriStr += path.substr(DOCS_LOCAL_PATH_SUBSTR_START_INDEX);
367 } else {
368 newUriStr = FILE_SCHEME_PREFIX;
369 newUriStr += bundleName + path;
370 }
371 uriInfo.authUri = newUriStr;
372 } else {
373 newUriStr = isLocal ? uriInfo.authUri : uriInfo.dfsUri;
374 }
375 Uri uri(newUriStr);
376 if (uri.GetAuthority().empty()) {
377 return true;
378 }
379 std::string scheme = uri.GetScheme();
380 std::transform(scheme.begin(), scheme.end(), scheme.begin(), ::tolower);
381 if (scheme != FILE_SCHEME) {
382 return true;
383 }
384 auto iter = std::find(uris.begin(), uris.end(), newUriStr);
385 if (iter == uris.end()) {
386 uris.push_back(std::move(newUriStr));
387 }
388 return true;
389 });
390 }
391
GetHtmlFileUris(uint32_t tokenId,UnifiedData & data,bool isLocal,std::vector<std::string> & uris)392 void PreProcessUtils::GetHtmlFileUris(uint32_t tokenId, UnifiedData &data,
393 bool isLocal, std::vector<std::string> &uris)
394 {
395 for (auto &record : data.GetRecords()) {
396 if (record == nullptr) {
397 continue;
398 }
399 PreProcessUtils::ProcessRecord(record, tokenId, isLocal, uris);
400 }
401 }
402
ClearHtmlDfsUris(UnifiedData & data)403 void PreProcessUtils::ClearHtmlDfsUris(UnifiedData &data)
404 {
405 for (auto &record : data.GetRecords()) {
406 if (record == nullptr) {
407 continue;
408 }
409 record->ComputeUris([] (UriInfo &uriInfo) {
410 uriInfo.dfsUri = "";
411 return true;
412 });
413 }
414 }
415
ProcessHtmlFileUris(uint32_t tokenId,UnifiedData & data,bool isLocal,std::vector<Uri> & uris)416 void PreProcessUtils::ProcessHtmlFileUris(uint32_t tokenId, UnifiedData &data, bool isLocal, std::vector<Uri> &uris)
417 {
418 std::vector<std::string> strUris;
419 PreProcessUtils::GetHtmlFileUris(tokenId, data, isLocal, strUris);
420 for (auto &uri : strUris) {
421 uris.emplace_back(Uri(uri));
422 }
423 if (isLocal) {
424 PreProcessUtils::ClearHtmlDfsUris(data);
425 }
426 }
427
SetRecordUid(UnifiedData & data)428 void PreProcessUtils::SetRecordUid(UnifiedData &data)
429 {
430 uint32_t index = 0;
431 auto prefix = PreProcessUtils::GenerateId().substr(0, PREFIX_LEN);
432 for (const auto &record : data.GetRecords()) {
433 std::ostringstream oss;
434 oss << std::setw(INDEX_LEN) << std::setfill(PLACE_HOLDER) << index;
435 record->SetUid(prefix + oss.str());
436 index++;
437 }
438 }
439
GetDetailsFromUData(const UnifiedData & data,UDDetails & details)440 bool PreProcessUtils::GetDetailsFromUData(const UnifiedData &data, UDDetails &details)
441 {
442 auto records = data.GetRecords();
443 if (records.size() != TEMP_UDATA_RECORD_SIZE) {
444 ZLOGI("Records size:%{public}zu", records.size());
445 return false;
446 }
447 if (records[0] == nullptr) {
448 ZLOGE("First record is null.");
449 return false;
450 }
451 if (records[0]->GetType() != UDType::FILE) {
452 ZLOGE("First record is not file.");
453 return false;
454 }
455 auto value = records[0]->GetOriginValue();
456 auto obj = std::get_if<std::shared_ptr<Object>>(&value);
457 if (obj == nullptr || *obj == nullptr) {
458 ZLOGE("ValueType is not Object!");
459 return false;
460 }
461 std::shared_ptr<Object> detailObj;
462 (*obj)->GetValue(DETAILS, detailObj);
463 if (detailObj == nullptr) {
464 ZLOGE("Not contain details for object!");
465 return false;
466 }
467 auto result = ObjectUtils::ConvertToUDDetails(detailObj);
468 if (result.find(TEMP_UNIFIED_DATA_FLAG) == result.end()) {
469 ZLOGE("Not find temp file.");
470 return false;
471 }
472 details = std::move(result);
473 return true;
474 }
475
GetSummaryFromDetails(const UDDetails & details,Summary & summary)476 Status PreProcessUtils::GetSummaryFromDetails(const UDDetails &details, Summary &summary)
477 {
478 for (auto &item : details) {
479 if (item.first == TEMP_UNIFIED_DATA_FLAG) {
480 continue;
481 }
482 auto int64Value = std::get_if<int64_t>(&item.second);
483 if (int64Value != nullptr) {
484 summary.summary[item.first] = *int64Value;
485 summary.totalSize += *int64Value;
486 }
487 }
488 return E_OK;
489 }
490
GetSdkVersionByToken(uint32_t tokenId)491 std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId)
492 {
493 if (Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) !=
494 Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
495 ZLOGE("Caller is not application, tokenid is %{public}u", tokenId);
496 return "";
497 }
498 Security::AccessToken::HapTokenInfo hapTokenInfo;
499 auto ret = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapTokenInfo);
500 if (ret != 0) {
501 ZLOGE("GetHapTokenInfo fail, tokenid is %{public}u, ret is %{public}d.", tokenId, ret);
502 return "";
503 }
504 return std::to_string(hapTokenInfo.apiVersion);
505 }
506
GetSpecificBundleNameByTokenId(uint32_t tokenId,std::string & specificBundleName,std::string & bundleName)507 bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName,
508 std::string &bundleName)
509 {
510 if (UTILS::IsTokenNative()) {
511 ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE");
512 std::string processName;
513 if (GetNativeProcessNameByToken(tokenId, processName)) {
514 bundleName = std::move(processName);
515 specificBundleName = bundleName;
516 return true;
517 }
518 }
519 Security::AccessToken::HapTokenInfo hapInfo;
520 if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo)
521 == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
522 bundleName = hapInfo.bundleName;
523 return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, specificBundleName);
524 }
525 ZLOGE("Get bundle name faild, tokenid:%{public}u", tokenId);
526 return false;
527 }
528
GetBundleMgr()529 sptr<AppExecFwk::IBundleMgr> PreProcessUtils::GetBundleMgr()
530 {
531 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
532 if (samgrProxy == nullptr) {
533 ZLOGE("Failed to get system ability mgr.");
534 return nullptr;
535 }
536 auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
537 if (bundleMgrProxy == nullptr) {
538 ZLOGE("Failed to Get BMS SA.");
539 return nullptr;
540 }
541 auto bundleManager = iface_cast<AppExecFwk::IBundleMgr>(bundleMgrProxy);
542 if (bundleManager == nullptr) {
543 ZLOGE("Failed to get bundle manager");
544 return nullptr;
545 }
546 return bundleManager;
547 }
548
GetSpecificBundleName(const std::string & bundleName,int32_t appIndex,std::string & specificBundleName)549 bool PreProcessUtils::GetSpecificBundleName(const std::string &bundleName, int32_t appIndex,
550 std::string &specificBundleName)
551 {
552 auto bundleManager = GetBundleMgr();
553 if (bundleManager == nullptr) {
554 ZLOGE("Failed to get bundle manager, bundleName:%{public}s, appIndex:%{public}d", bundleName.c_str(), appIndex);
555 return false;
556 }
557 auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, specificBundleName);
558 if (ret != ERR_OK) {
559 ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d, bundleName:%{public}s, appIndex:%{public}d",
560 ret, bundleName.c_str(), appIndex);
561 return false;
562 }
563 return true;
564 }
565 } // namespace UDMF
566 } // namespace OHOS