1 /*
2 * Copyright (c) 2021-2024 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 LOGD("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 if (storageManager_->AsObject() != nullptr) {
60 storageManager_->AsObject()->AddDeathRecipient(deathRecipient_);
61 }
62 }
63 LOGD("StorageManagerConnect::Connect end");
64 return E_OK;
65 }
66
GetBundleStats(string pkgName,BundleStats & BundleStats,int32_t appIndex,uint32_t statFlag)67 int32_t StorageManagerConnect::GetBundleStats(string pkgName, BundleStats &BundleStats,
68 int32_t appIndex, uint32_t statFlag)
69 {
70 int32_t err = Connect();
71 if (err != E_OK) {
72 LOGE("StorageManagerConnect::GetBundleStats:Connect error");
73 return err;
74 }
75 if (storageManager_ == nullptr) {
76 LOGE("StorageManagerConnect::GetBundleStats service == nullptr");
77 return E_SERVICE_IS_NULLPTR;
78 }
79 return storageManager_->GetBundleStats(pkgName, BundleStats, appIndex, statFlag);
80 }
81
GetFreeSizeOfVolume(string volumeUuid,int64_t & freeSize)82 int32_t StorageManagerConnect::GetFreeSizeOfVolume(string volumeUuid, int64_t &freeSize)
83 {
84 int32_t err = Connect();
85 if (err != E_OK) {
86 LOGE("StorageManagerConnect::GetFreeSizeOfVolume:Connect error");
87 return err;
88 }
89 if (storageManager_ == nullptr) {
90 LOGE("StorageManagerConnect::GetFreeSizeOfVolume service == nullptr");
91 return E_SERVICE_IS_NULLPTR;
92 }
93 return storageManager_->GetFreeSizeOfVolume(volumeUuid, freeSize);
94 }
95
GetTotalSizeOfVolume(string volumeUuid,int64_t & totalSize)96 int32_t StorageManagerConnect::GetTotalSizeOfVolume(string volumeUuid, int64_t &totalSize)
97 {
98 int32_t err = Connect();
99 if (err != E_OK) {
100 LOGE("StorageManagerConnect::GetTotalSizeOfVolume:Connect error");
101 return err;
102 }
103 if (storageManager_ == nullptr) {
104 LOGE("StorageManagerConnect::GetTotalSizeOfVolume service == nullptr");
105 return E_SERVICE_IS_NULLPTR;
106 }
107 return storageManager_->GetTotalSizeOfVolume(volumeUuid, totalSize);
108 }
109
Mount(std::string volumeId)110 int32_t StorageManagerConnect::Mount(std::string volumeId)
111 {
112 int32_t err = Connect();
113 if (err != E_OK) {
114 LOGE("StorageManagerConnect::Mount:Connect error");
115 return err;
116 }
117 if (storageManager_ == nullptr) {
118 LOGE("StorageManagerConnect::Mount service == nullptr");
119 return E_SERVICE_IS_NULLPTR;
120 }
121 return storageManager_->Mount(volumeId);
122 }
123
Unmount(std::string volumeId)124 int32_t StorageManagerConnect::Unmount(std::string volumeId)
125 {
126 int32_t err = Connect();
127 if (err != E_OK) {
128 LOGE("StorageManagerConnect::Unmount:Connect error");
129 return err;
130 }
131 if (storageManager_ == nullptr) {
132 LOGE("StorageManagerConnect::Unmount service == nullptr");
133 return E_SERVICE_IS_NULLPTR;
134 }
135 return storageManager_->Unmount(volumeId);
136 }
137
GetAllVolumes(std::vector<VolumeExternal> & vecOfVol)138 int32_t StorageManagerConnect::GetAllVolumes(std::vector<VolumeExternal> &vecOfVol)
139 {
140 int32_t err = Connect();
141 if (err != E_OK) {
142 LOGE("StorageManagerConnect::GetAllVolumes:Connect error");
143 return err;
144 }
145 if (storageManager_ == nullptr) {
146 LOGE("StorageManagerConnect::GetAllVolumes service == nullptr");
147 return E_SERVICE_IS_NULLPTR;
148 }
149 return storageManager_->GetAllVolumes(vecOfVol);
150 }
151
GetSystemSize(int64_t & systemSize)152 int32_t StorageManagerConnect::GetSystemSize(int64_t &systemSize)
153 {
154 int32_t err = Connect();
155 if (err != E_OK) {
156 LOGE("StorageManagerConnect::GetSystemSize:Connect error");
157 return err;
158 }
159 if (storageManager_ == nullptr) {
160 LOGE("StorageManagerConnect::GetSystemSize service == nullptr");
161 return E_SERVICE_IS_NULLPTR;
162 }
163 return storageManager_->GetSystemSize(systemSize);
164 }
165
GetTotalSize(int64_t & totalSize)166 int32_t StorageManagerConnect::GetTotalSize(int64_t &totalSize)
167 {
168 int32_t err = Connect();
169 if (err != E_OK) {
170 LOGE("StorageManagerConnect::GetTotalSize:Connect error");
171 return err;
172 }
173 if (storageManager_ == nullptr) {
174 LOGE("StorageManagerConnect::GetTotalSize service == nullptr");
175 return E_SERVICE_IS_NULLPTR;
176 }
177 return storageManager_->GetTotalSize(totalSize);
178 }
179
GetFreeSize(int64_t & freeSize)180 int32_t StorageManagerConnect::GetFreeSize(int64_t &freeSize)
181 {
182 int32_t err = Connect();
183 if (err != E_OK) {
184 LOGE("StorageManagerConnect::GetFreeSize:Connect error");
185 return err;
186 }
187 if (storageManager_ == nullptr) {
188 LOGE("StorageManagerConnect::GetFreeSize service == nullptr");
189 return E_SERVICE_IS_NULLPTR;
190 }
191 return storageManager_->GetFreeSize(freeSize);
192 }
193
GetUserStorageStats(StorageStats & storageStats)194 int32_t StorageManagerConnect::GetUserStorageStats(StorageStats &storageStats)
195 {
196 int32_t err = Connect();
197 if (err != E_OK) {
198 LOGE("StorageManagerConnect::GetUserStorageStats:Connect error");
199 return err;
200 }
201 if (storageManager_ == nullptr) {
202 LOGE("StorageManagerConnect::GetUserStorageStats service == nullptr");
203 return E_SERVICE_IS_NULLPTR;
204 }
205 return storageManager_->GetUserStorageStats(storageStats);
206 }
207
GetUserStorageStats(int32_t userId,StorageStats & storageStats)208 int32_t StorageManagerConnect::GetUserStorageStats(int32_t userId, StorageStats &storageStats)
209 {
210 int32_t err = Connect();
211 if (err != E_OK) {
212 LOGE("StorageManagerConnect::GetUserStorageStats:Connect error");
213 return err;
214 }
215 if (storageManager_ == nullptr) {
216 LOGE("StorageManagerConnect::GetUserStorageStats service == nullptr");
217 return E_SERVICE_IS_NULLPTR;
218 }
219 return storageManager_->GetUserStorageStats(userId, storageStats);
220 }
221
GetCurrentBundleStats(BundleStats & bundleStats,uint32_t statFlag)222 int32_t StorageManagerConnect::GetCurrentBundleStats(BundleStats &bundleStats, uint32_t statFlag)
223 {
224 BundleStats result;
225 int32_t err = Connect();
226 if (err != E_OK) {
227 LOGE("StorageManagerConnect::GetCurrentBundleStats:Connect error");
228 return err;
229 }
230 if (storageManager_ == nullptr) {
231 LOGE("StorageManagerConnect::GetCurrentBundleStats service == nullptr");
232 return E_SERVICE_IS_NULLPTR;
233 }
234 return storageManager_->GetCurrentBundleStats(bundleStats, statFlag);
235 }
236
GetVolumeByUuid(std::string uuid,VolumeExternal & vol)237 int32_t StorageManagerConnect::GetVolumeByUuid(std::string uuid, VolumeExternal &vol)
238 {
239 int32_t err = Connect();
240 if (err != E_OK) {
241 LOGE("StorageManagerConnect::GetVolumeByUuid:Connect error");
242 return err;
243 }
244 if (storageManager_ == nullptr) {
245 LOGE("StorageManagerConnect::GetVolumeByUuid service == nullptr");
246 return E_SERVICE_IS_NULLPTR;
247 }
248 return storageManager_->GetVolumeByUuid(uuid, vol);
249 }
250
GetVolumeById(std::string volumeId,VolumeExternal & vol)251 int32_t StorageManagerConnect::GetVolumeById(std::string volumeId, VolumeExternal &vol)
252 {
253 int32_t err = Connect();
254 if (err != E_OK) {
255 LOGE("StorageManagerConnect::GetVolumeById:Connect error");
256 return err;
257 }
258 if (storageManager_ == nullptr) {
259 LOGE("StorageManagerConnect::GetVolumeById service == nullptr");
260 return E_SERVICE_IS_NULLPTR;
261 }
262 return storageManager_->GetVolumeById(volumeId, vol);
263 }
264
SetVolumeDescription(std::string uuid,std::string description)265 int32_t StorageManagerConnect::SetVolumeDescription(std::string uuid, std::string description)
266 {
267 int32_t err = Connect();
268 if (err != E_OK) {
269 LOGE("StorageManagerConnect::SetVolumeDescription:Connect error");
270 return err;
271 }
272 if (storageManager_ == nullptr) {
273 LOGE("StorageManagerConnect::SetVolumeDescription service == nullptr");
274 return E_SERVICE_IS_NULLPTR;
275 }
276 return storageManager_->SetVolumeDescription(uuid, description);
277 }
278
Format(std::string volumeId,std::string fsType)279 int32_t StorageManagerConnect::Format(std::string volumeId, std::string fsType)
280 {
281 int32_t err = Connect();
282 if (err != E_OK) {
283 LOGE("StorageManagerConnect::Format:Connect error");
284 return err;
285 }
286 if (storageManager_ == nullptr) {
287 LOGE("StorageManagerConnect::Format service == nullptr");
288 return E_SERVICE_IS_NULLPTR;
289 }
290 return storageManager_->Format(volumeId, fsType);
291 }
292
Partition(std::string diskId,int32_t type)293 int32_t StorageManagerConnect::Partition(std::string diskId, int32_t type)
294 {
295 int32_t err = Connect();
296 if (err != E_OK) {
297 LOGE("StorageManagerConnect::Partition:Connect error");
298 return err;
299 }
300 if (storageManager_ == nullptr) {
301 LOGE("StorageManagerConnect::Partition service == nullptr");
302 return E_SERVICE_IS_NULLPTR;
303 }
304 return storageManager_->Partition(diskId, type);
305 }
306
ResetProxy()307 int32_t StorageManagerConnect::ResetProxy()
308 {
309 LOGD("enter");
310 std::lock_guard<std::mutex> lock(mutex_);
311 if ((storageManager_ != nullptr) && (storageManager_->AsObject() != nullptr)) {
312 storageManager_->AsObject()->RemoveDeathRecipient(deathRecipient_);
313 }
314 storageManager_ = nullptr;
315
316 return E_OK;
317 }
318
OnRemoteDied(const wptr<IRemoteObject> & remote)319 void SmDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
320 {
321 LOGI("SmDeathRecipient::OnRemoteDied reset proxy.");
322 DelayedSingleton<StorageManagerConnect>::GetInstance()->ResetProxy();
323 }
324
IsSystemApp()325 bool IsSystemApp()
326 {
327 uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID();
328 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
329 }
330
Convert2JsErrNum(int32_t errNum)331 int32_t Convert2JsErrNum(int32_t errNum)
332 {
333 static std::unordered_map<int32_t, int32_t> errCodeTable {
334 { E_PERMISSION_DENIED, E_PERMISSION },
335 { E_WRITE_DESCRIPTOR_ERR, E_IPCSS},
336 { E_EXIST, E_PREPARE},
337 { E_WRONG_TYPE, E_WRONG_TYPE},
338 { E_USER_STATE, E_USER_STATE},
339 { E_NON_EXIST, E_NOOBJECT},
340 { E_PREPARE_DIR, E_PREPARE_DIR},
341 { E_DESTROY_DIR, E_DESTROY_DIR},
342 { E_MOUNT, E_MOUNT_ERR},
343 { E_UMOUNT, E_UNMOUNT},
344 { E_SET_POLICY, E_SET_POLICY},
345 { E_USERID_RANGE, E_OUTOFRANGE},
346 { E_VOL_STATE, E_VOLUMESTATE},
347 { E_WAIT, E_VOLUMESTATE},
348 { E_NOT_SUPPORT, E_SUPPORTEDFS},
349 { E_SYS_CALL, E_UNMOUNT},
350 { E_NO_CHILD, E_NO_CHILD},
351 { E_WRITE_PARCEL_ERR, E_IPCSS},
352 { E_WRITE_REPLY_ERR, E_IPCSS},
353 { E_SA_IS_NULLPTR, E_IPCSS},
354 { E_REMOTE_IS_NULLPTR, E_IPCSS},
355 { E_SERVICE_IS_NULLPTR, E_IPCSS},
356 { E_DEATH_RECIPIENT_IS_NULLPTR, E_IPCSS},
357 { E_BUNDLEMGR_ERROR, E_IPCSS},
358 { E_MEDIALIBRARY_ERROR, E_IPCSS},
359 };
360
361 if (errCodeTable.find(errNum) != errCodeTable.end()) {
362 return errCodeTable.at(errNum);
363 } else {
364 return errNum;
365 }
366 }
367
GetUserStorageStatsByType(int32_t userId,StorageStats & storageStats,std::string type)368 int32_t StorageManagerConnect::GetUserStorageStatsByType(int32_t userId, StorageStats &storageStats, std::string type)
369 {
370 int32_t err = Connect();
371 if (err != E_OK) {
372 LOGE("StorageManagerConnect::GetUserStorageStatsByType:Connect error");
373 return err;
374 }
375 if (storageManager_ == nullptr) {
376 LOGE("StorageManagerConnect::GetUserStorageStatsByType service == nullptr");
377 return E_SERVICE_IS_NULLPTR;
378 }
379 return storageManager_->GetUserStorageStatsByType(userId, storageStats, type);
380 }
381 } // StorageManager
382 } // OHOS
383