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