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 "common_func.h" 17 18#include <mutex> 19#include <vector> 20 21#include <singleton.h> 22#include "bundle_mgr_client.h" 23#include "bundle_mgr_proxy.h" 24#include "ipc_skeleton.h" 25#include "iservice_registry.h" 26#include "system_ability_definition.h" 27 28#include "log.h" 29#include "sandbox_helper.h" 30 31using namespace std; 32 33namespace OHOS { 34namespace AppFileService { 35using namespace OHOS::AppExecFwk; 36namespace { 37 const std::string FILE_SCHEME_PREFIX = "file://"; 38 const char BACKFLASH = '/'; 39 const std::string FILE_MANAGER_URI_HEAD = "/storage/"; 40 const std::string FILE_MANAGER_AUTHORITY = "docs"; 41 std::string g_bundleName = ""; 42 std::mutex g_globalMutex; 43} 44static sptr<BundleMgrProxy> GetBundleMgrProxy() 45{ 46 sptr<ISystemAbilityManager> systemAbilityManager = 47 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 48 if (!systemAbilityManager) { 49 LOGE("fail to get system ability mgr."); 50 return nullptr; 51 } 52 53 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); 54 if (!remoteObject) { 55 LOGE("fail to get bundle manager proxy."); 56 return nullptr; 57 } 58 59 return iface_cast<BundleMgrProxy>(remoteObject); 60} 61 62bool CommonFunc::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName) 63{ 64 auto bmsClient = DelayedSingleton<AppExecFwk::BundleMgrClient>::GetInstance(); 65 if (bmsClient == nullptr) { 66 LOGE("bundleMgrClient is nullptr."); 67 return false; 68 } 69 auto bmsRet = bmsClient->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); 70 if (bmsRet != ERR_OK) { 71 LOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", bmsRet); 72 return false; 73 } 74 return true; 75} 76 77string CommonFunc::GetSelfBundleName() 78{ 79 sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy(); 80 if (!bundleMgrProxy) { 81 LOGE("GetSelfBundleName: bundle mgr proxy is nullptr."); 82 return ""; 83 } 84 85 BundleInfo bundleInfo; 86 auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo); 87 if (ret != ERR_OK) { 88 LOGE("GetSelfBundleName: bundleName get fail."); 89 return ""; 90 } 91 string dirName = "" ; 92 if (GetDirByBundleNameAndAppIndex(bundleInfo.name, bundleInfo.appIndex, dirName)) { 93 return dirName; 94 } 95 return bundleInfo.name; 96} 97 98static void NormalizePath(string &path) 99{ 100 if (path.size() == 0) { 101 return; 102 } 103 104 if (path[0] != BACKFLASH) { 105 path.insert(0, 1, BACKFLASH); 106 } 107} 108 109string CommonFunc::GetUriFromPath(const string &path) 110{ 111 string realPath = path; 112 NormalizePath(realPath); 113 114 { 115 std::lock_guard<std::mutex> lock(g_globalMutex); 116 if (g_bundleName == "") { 117 g_bundleName = GetSelfBundleName(); 118 } 119 } 120 string packageName = (path.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : g_bundleName; 121 realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath); 122 return realPath; 123} 124} // namespace AppFileService 125} // namespace OHOS 126 127