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