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 BACKSLASH = '/';
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<AppExecFwk::IBundleMgr> 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<AppExecFwk::IBundleMgr>(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<AppExecFwk::IBundleMgr> 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] != BACKSLASH) {
107 path.insert(0, 1, BACKSLASH);
108 }
109 }
110
GetUriFromPath(const string & path)111 string CommonFunc::GetUriFromPath(const string &path)
112 {
113 if (!SandboxHelper::IsValidPath(path)) {
114 LOGE("path is ValidPath, The path contains '../' characters");
115 return "";
116 }
117 if (path.find(FILE_SCHEME_PREFIX) == 0) {
118 return path;
119 }
120 string realPath = path;
121 NormalizePath(realPath);
122 if (realPath.find(MEDIA_FUSE_PATH_HEAD) == 0) {
123 realPath = SandboxHelper::Encode(realPath);
124 return realPath.replace(realPath.find(MEDIA_FUSE_PATH_HEAD), MEDIA_FUSE_PATH_HEAD.length(), MEDIA_AUTHORITY);
125 }
126 {
127 std::lock_guard<std::mutex> lock(g_globalMutex);
128 if (g_bundleName == "") {
129 g_bundleName = GetSelfBundleName();
130 }
131 }
132 string packageName = (realPath.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : g_bundleName;
133 realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath);
134 return realPath;
135 }
136
EndsWith(const std::string & str,const std::string & suffix)137 bool CommonFunc::EndsWith(const std::string &str, const std::string &suffix)
138 {
139 if (suffix.length() > str.length()) {
140 return false;
141 }
142 return (str.rfind(suffix) == (str.length() - suffix.length()));
143 }
144 } // namespace AppFileService
145 } // namespace OHOS
146