1 /*
2 * Copyright (c) 2021 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 "storage_manager_connect.h"
17
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20
21 #include "storage_manager_proxy.h"
22 #include "ipc_skeleton.h"
23 #include "storage_service_errno.h"
24 #include "storage_service_log.h"
25 #include "tokenid_kit.h"
26
27 using namespace std;
28
29 namespace OHOS {
30 namespace StorageManager {
StorageManagerConnect()31 StorageManagerConnect::StorageManagerConnect() {}
~StorageManagerConnect()32 StorageManagerConnect::~StorageManagerConnect() {}
33
Connect()34 int32_t StorageManagerConnect::Connect()
35 {
36 LOGI("StorageManagerConnect::Connect start");
37 std::lock_guard<std::mutex> lock(mutex_);
38 if (storageManager_ == nullptr) {
39 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 if (sam == nullptr) {
41 LOGE("StorageManagerConnect::Connect samgr == nullptr");
42 return E_SA_IS_NULLPTR;
43 }
44 auto object = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
45 if (object == nullptr) {
46 LOGE("StorageManagerConnect::Connect object == nullptr");
47 return E_REMOTE_IS_NULLPTR;
48 }
49 storageManager_ = iface_cast<StorageManager::IStorageManager>(object);
50 if (storageManager_ == nullptr) {
51 LOGE("StorageManagerConnect::Connect service == nullptr");
52 return E_SERVICE_IS_NULLPTR;
53 }
54 deathRecipient_ = new (std::nothrow) SmDeathRecipient();
55 if (!deathRecipient_) {
56 LOGE("StorageManagerConnect::Connect failed to create death recipient");
57 return E_DEATH_RECIPIENT_IS_NULLPTR;
58 }
59
60 storageManager_->AsObject()->AddDeathRecipient(deathRecipient_);
61 }
62 LOGI("StorageManagerConnect::Connect end");
63 return E_OK;
64 }
65
GetBundleStats(string pkgName,BundleStats & BundleStats)66 int32_t StorageManagerConnect::GetBundleStats(string pkgName, BundleStats &BundleStats)
67 {
68 int32_t err = Connect();
69 if (err != E_OK) {
70 LOGE("StorageManagerConnect::GetBundleStats:Connect error");
71 return err;
72 }
73 return storageManager_->GetBundleStats(pkgName, BundleStats);
74 }
75
GetFreeSizeOfVolume(string volumeUuid,int64_t & freeSize)76 int32_t StorageManagerConnect::GetFreeSizeOfVolume(string volumeUuid, int64_t &freeSize)
77 {
78 int32_t err = Connect();
79 if (err != E_OK) {
80 LOGE("StorageManagerConnect::GetFreeSizeOfVolume:Connect error");
81 return err;
82 }
83 return storageManager_->GetFreeSizeOfVolume(volumeUuid, freeSize);
84 }
85
GetTotalSizeOfVolume(string volumeUuid,int64_t & totalSize)86 int32_t StorageManagerConnect::GetTotalSizeOfVolume(string volumeUuid, int64_t &totalSize)
87 {
88 int32_t err = Connect();
89 if (err != E_OK) {
90 LOGE("StorageManagerConnect::GetTotalSizeOfVolume:Connect error");
91 return err;
92 }
93 return storageManager_->GetTotalSizeOfVolume(volumeUuid, totalSize);
94 }
95
Mount(std::string volumeId)96 int32_t StorageManagerConnect::Mount(std::string volumeId)
97 {
98 int32_t err = Connect();
99 if (err != E_OK) {
100 LOGE("StorageManagerConnect::Mount:Connect error");
101 return err;
102 }
103 return storageManager_->Mount(volumeId);
104 }
105
Unmount(std::string volumeId)106 int32_t StorageManagerConnect::Unmount(std::string volumeId)
107 {
108 int32_t err = Connect();
109 if (err != E_OK) {
110 LOGE("StorageManagerConnect::Unmount:Connect error");
111 return err;
112 }
113 return storageManager_->Unmount(volumeId);
114 }
115
GetAllVolumes(std::vector<VolumeExternal> & vecOfVol)116 int32_t StorageManagerConnect::GetAllVolumes(std::vector<VolumeExternal> &vecOfVol)
117 {
118 int32_t err = Connect();
119 if (err != E_OK) {
120 LOGE("StorageManagerConnect::GetAllVolumes:Connect error");
121 return err;
122 }
123 return storageManager_->GetAllVolumes(vecOfVol);
124 }
125
GetSystemSize(int64_t & systemSize)126 int32_t StorageManagerConnect::GetSystemSize(int64_t &systemSize)
127 {
128 int32_t err = Connect();
129 if (err != E_OK) {
130 LOGE("StorageManagerConnect::GetSystemSize:Connect error");
131 return err;
132 }
133 return storageManager_->GetSystemSize(systemSize);
134 }
135
GetTotalSize(int64_t & totalSize)136 int32_t StorageManagerConnect::GetTotalSize(int64_t &totalSize)
137 {
138 int32_t err = Connect();
139 if (err != E_OK) {
140 LOGE("StorageManagerConnect::GetTotalSize:Connect error");
141 return err;
142 }
143 return storageManager_->GetTotalSize(totalSize);
144 }
145
GetFreeSize(int64_t & freeSize)146 int32_t StorageManagerConnect::GetFreeSize(int64_t &freeSize)
147 {
148 int32_t err = Connect();
149 if (err != E_OK) {
150 LOGE("StorageManagerConnect::GetFreeSize:Connect error");
151 return err;
152 }
153 return storageManager_->GetFreeSize(freeSize);
154 }
155
GetUserStorageStats(StorageStats & storageStats)156 int32_t StorageManagerConnect::GetUserStorageStats(StorageStats &storageStats)
157 {
158 int32_t err = Connect();
159 if (err != E_OK) {
160 LOGE("StorageManagerConnect::GetUserStorageStats:Connect error");
161 return err;
162 }
163 return storageManager_->GetUserStorageStats(storageStats);
164 }
165
GetUserStorageStats(int32_t userId,StorageStats & storageStats)166 int32_t StorageManagerConnect::GetUserStorageStats(int32_t userId, StorageStats &storageStats)
167 {
168 int32_t err = Connect();
169 if (err != E_OK) {
170 LOGE("StorageManagerConnect::GetUserStorageStats:Connect error");
171 return err;
172 }
173 return storageManager_->GetUserStorageStats(userId, storageStats);
174 }
175
GetCurrentBundleStats(BundleStats & bundleStats)176 int32_t StorageManagerConnect::GetCurrentBundleStats(BundleStats &bundleStats)
177 {
178 BundleStats result;
179 int32_t err = Connect();
180 if (err != E_OK) {
181 LOGE("StorageManagerConnect::GetCurrentBundleStats:Connect error");
182 return err;
183 }
184 return storageManager_->GetCurrentBundleStats(bundleStats);
185 }
186
GetVolumeByUuid(std::string uuid,VolumeExternal & vol)187 int32_t StorageManagerConnect::GetVolumeByUuid(std::string uuid, VolumeExternal &vol)
188 {
189 int32_t err = Connect();
190 if (err != E_OK) {
191 LOGE("StorageManagerConnect::GetVolumeByUuid:Connect error");
192 return err;
193 }
194 return storageManager_->GetVolumeByUuid(uuid, vol);
195 }
196
GetVolumeById(std::string volumeId,VolumeExternal & vol)197 int32_t StorageManagerConnect::GetVolumeById(std::string volumeId, VolumeExternal &vol)
198 {
199 int32_t err = Connect();
200 if (err != E_OK) {
201 LOGE("StorageManagerConnect::GetVolumeById:Connect error");
202 return err;
203 }
204 return storageManager_->GetVolumeById(volumeId, vol);
205 }
206
SetVolumeDescription(std::string uuid,std::string description)207 int32_t StorageManagerConnect::SetVolumeDescription(std::string uuid, std::string description)
208 {
209 int32_t err = Connect();
210 if (err != E_OK) {
211 LOGE("StorageManagerConnect::SetVolumeDescription:Connect error");
212 return err;
213 }
214 return storageManager_->SetVolumeDescription(uuid, description);
215 }
216
Format(std::string volumeId,std::string fsType)217 int32_t StorageManagerConnect::Format(std::string volumeId, std::string fsType)
218 {
219 int32_t err = Connect();
220 if (err != E_OK) {
221 LOGE("StorageManagerConnect::Format:Connect error");
222 return err;
223 }
224 return storageManager_->Format(volumeId, fsType);
225 }
226
Partition(std::string diskId,int32_t type)227 int32_t StorageManagerConnect::Partition(std::string diskId, int32_t type)
228 {
229 int32_t err = Connect();
230 if (err != E_OK) {
231 LOGE("StorageManagerConnect::Partition:Connect error");
232 return err;
233 }
234 return storageManager_->Partition(diskId, type);
235 }
236
ResetProxy()237 int32_t StorageManagerConnect::ResetProxy()
238 {
239 LOGD("enter");
240 std::lock_guard<std::mutex> lock(mutex_);
241 if ((storageManager_ != nullptr) && (storageManager_->AsObject() != nullptr)) {
242 storageManager_->AsObject()->RemoveDeathRecipient(deathRecipient_);
243 }
244 storageManager_ = nullptr;
245
246 return E_OK;
247 }
248
OnRemoteDied(const wptr<IRemoteObject> & remote)249 void SmDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
250 {
251 DelayedSingleton<StorageManagerConnect>::GetInstance()->ResetProxy();
252 }
253
IsSystemApp()254 bool IsSystemApp()
255 {
256 uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID();
257 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
258 }
259
Convert2JsErrNum(int32_t errNum)260 int32_t Convert2JsErrNum(int32_t errNum)
261 {
262 if (errCodeTable.find(errNum) != errCodeTable.end()) {
263 return errCodeTable.at(errNum);
264 } else {
265 return errNum;
266 }
267 }
268 } // StorageManager
269 } // OHOS
270