• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "ipc/storage_manager_stub.h"
17 #include "accesstoken_kit.h"
18 #include "ipc_skeleton.h"
19 #include "storage/bundle_manager_connector.h"
20 #include "storage_manager_ipc_interface_code.h"
21 #include "storage_service_errno.h"
22 #include "storage_service_log.h"
23 #include "utils/storage_radar.h"
24 
25 namespace OHOS {
26 namespace StorageManager {
27 using namespace std;
28 
29 constexpr pid_t ACCOUNT_UID = 3058;
30 constexpr pid_t BACKUP_SA_UID = 1089;
31 constexpr pid_t FOUNDATION_UID = 5523;
32 constexpr pid_t DFS_UID = 1009;
33 const std::string MEDIALIBRARY_BUNDLE_NAME = "com.ohos.medialibrary.medialibrarydata";
34 const std::string SCENEBOARD_BUNDLE_NAME = "com.ohos.sceneboard";
35 const std::string SYSTEMUI_BUNDLE_NAME = "com.ohos.systemui";
36 const std::string PERMISSION_STORAGE_MANAGER_CRYPT = "ohos.permission.STORAGE_MANAGER_CRYPT";
37 const std::string PERMISSION_STORAGE_MANAGER = "ohos.permission.STORAGE_MANAGER";
38 const std::string PERMISSION_MOUNT_MANAGER = "ohos.permission.MOUNT_UNMOUNT_MANAGER";
39 const std::string PERMISSION_FORMAT_MANAGER = "ohos.permission.MOUNT_FORMAT_MANAGER";
40 const std::string PROCESS_NAME_FOUNDATION = "foundation";
41 
CheckClientPermission(const std::string & permissionStr)42 bool CheckClientPermission(const std::string& permissionStr)
43 {
44     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
45     auto uid = IPCSkeleton::GetCallingUid();
46     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenCaller);
47     int res;
48     if (tokenType == Security::AccessToken::TOKEN_NATIVE && uid == ACCOUNT_UID) {
49         res = Security::AccessToken::PermissionState::PERMISSION_GRANTED;
50     } else {
51         res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permissionStr);
52     }
53 
54     if (res == Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
55         LOGD("StorageMangaer permissionCheck pass!");
56         return true;
57     }
58     LOGE("StorageManager permissionCheck error, need %{public}s", permissionStr.c_str());
59     return false;
60 }
61 
CheckClientPermissionForCrypt(const std::string & permissionStr)62 bool CheckClientPermissionForCrypt(const std::string& permissionStr)
63 {
64     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
65     int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permissionStr);
66     if (res == Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
67         LOGD("StorageMangaer permissionCheck pass!");
68         return true;
69     }
70     LOGE("StorageManager permissionCheck error, need %{public}s", permissionStr.c_str());
71     return false;
72 }
73 
CheckClientPermissionForShareFile()74 bool CheckClientPermissionForShareFile()
75 {
76     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
77     Security::AccessToken::NativeTokenInfo nativeInfo;
78     Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenCaller, nativeInfo);
79 
80     auto uid = IPCSkeleton::GetCallingUid();
81     if (nativeInfo.processName != PROCESS_NAME_FOUNDATION || uid != FOUNDATION_UID) {
82         LOGE("CheckClientPermissionForShareFile error, processName is %{public}s, uid is %{public}d",
83             nativeInfo.processName.c_str(), uid);
84         return false;
85     }
86 
87     return true;
88 }
89 
StorageManagerStub()90 StorageManagerStub::StorageManagerStub()
91 {
92     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER)] =
93         &StorageManagerStub::HandlePrepareAddUser;
94     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::REMOVE_USER)] =
95         &StorageManagerStub::HandleRemoveUser;
96     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::PREPARE_START_USER)] =
97         &StorageManagerStub::HandlePrepareStartUser;
98     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::STOP_USER)] =
99         &StorageManagerStub::HandleStopUser;
100     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_TOTAL)] =
101         &StorageManagerStub::HandleGetTotal;
102     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FREE)] =
103         &StorageManagerStub::HandleGetFree;
104     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_SYSTEM_SIZE)] =
105         &StorageManagerStub::HandleGetSystemSize;
106     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_TOTAL_SIZE)] =
107         &StorageManagerStub::HandleGetTotalSize;
108     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FREE_SIZE)] =
109         &StorageManagerStub::HandleGetFreeSize;
110     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_CURR_USER_STATS)] =
111         &StorageManagerStub::HandleGetCurrUserStorageStats;
112     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_STATS)] =
113         &StorageManagerStub::HandleGetUserStorageStats;
114     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_STATS_BY_TYPE)] =
115         &StorageManagerStub::HandleGetUserStorageStatsByType;
116     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_CURR_BUNDLE_STATS)] =
117         &StorageManagerStub::HandleGetCurrentBundleStats;
118     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATUS)] =
119         &StorageManagerStub::HandleGetBundleStatus;
120     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_CREATED)] =
121         &StorageManagerStub::HandleNotifyVolumeCreated;
122     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_MOUNTED)] =
123         &StorageManagerStub::HandleNotifyVolumeMounted;
124     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_STATE_CHANGED)] =
125         &StorageManagerStub::HandleNotifyVolumeStateChanged;
126     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT)] =
127         &StorageManagerStub::HandleMount;
128     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UNMOUNT)] =
129         &StorageManagerStub::HandleUnmount;
130     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_ALL_VOLUMES)] =
131         &StorageManagerStub::HandleGetAllVolumes;
132     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_CREATED)] =
133         &StorageManagerStub::HandleNotifyDiskCreated;
134     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_DESTROYED)] =
135         &StorageManagerStub::HandleNotifyDiskDestroyed;
136     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::PARTITION)] =
137         &StorageManagerStub::HandlePartition;
138     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_ALL_DISKS)] =
139         &StorageManagerStub::HandleGetAllDisks;
140     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_VOL_BY_UUID)] =
141         &StorageManagerStub::HandleGetVolumeByUuid;
142     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_VOL_BY_ID)] =
143         &StorageManagerStub::HandleGetVolumeById;
144     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::SET_VOL_DESC)] =
145         &StorageManagerStub::HandleSetVolDesc;
146     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::FORMAT)] =
147         &StorageManagerStub::HandleFormat;
148     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_DISK_BY_ID)] =
149         &StorageManagerStub::HandleGetDiskById;
150     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_USER_KEYS)] =
151         &StorageManagerStub::HandleGenerateUserKeys;
152     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_USER_KEYS)] =
153         &StorageManagerStub::HandleDeleteUserKeys;
154     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_USER_AUTH)] =
155         &StorageManagerStub::HandleUpdateUserAuth;
156     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_USER_AUTH_RECOVER_KEY)] =
157         &StorageManagerStub::HandleUpdateUseAuthWithRecoveryKey;
158     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::ACTIVE_USER_KEY)] =
159         &StorageManagerStub::HandleActiveUserKey;
160     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::INACTIVE_USER_KEY)] =
161         &StorageManagerStub::HandleInactiveUserKey;
162     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::LOCK_USER_SCREEN)] =
163         &StorageManagerStub::HandleLockUserScreen;
164     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UNLOCK_USER_SCREEN)] =
165         &StorageManagerStub::HandleUnlockUserScreen;
166     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::LOCK_SCREEN_STATUS)] =
167         &StorageManagerStub::HandleGetLockScreenStatus;
168     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_KEY_CONTEXT)] =
169         &StorageManagerStub::HandleUpdateKeyContext;
170     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_SHARE_FILE)] =
171         &StorageManagerStub::HandleCreateShareFile;
172     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_SHARE_FILE)] =
173         &StorageManagerStub::HandleDeleteShareFile;
174     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::SET_BUNDLE_QUOTA)] =
175         &StorageManagerStub::HandleSetBundleQuota;
176     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_MEM_PARA)] =
177         &StorageManagerStub::HandleUpdateMemoryPara;
178     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATS_INCREASE)] =
179         &StorageManagerStub::HandleGetBundleStatsForIncrease;
180     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GENERATE_APP_KEY)] =
181         &StorageManagerStub::HandleGenerateAppkey;
182     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_APP_KEY)] =
183         &StorageManagerStub::HandleDeleteAppkey;
184     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT_DFS_DOCS)] =
185         &StorageManagerStub::HandleMountDfsDocs;
186     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UMOUNT_DFS_DOCS)] =
187         &StorageManagerStub::HandleUMountDfsDocs;
188     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS)] =
189         &StorageManagerStub::HandleGetFileEncryptStatus;
190     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_RECOVER_KEY)] =
191         &StorageManagerStub::HandleCreateRecoverKey;
192     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_NEED_ACTIVE_STATUS)] =
193         &StorageManagerStub::HandleGetUserNeedActiveStatus;
194     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::SET_RECOVER_KEY)] =
195         &StorageManagerStub::HandleSetRecoverKey;
196     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_MTP_MOUNT)] =
197         &StorageManagerStub::HandleNotifyMtpMount;
198     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_MTP_UNMOUNT)] =
199         &StorageManagerStub::HandleNotifyMtpUnmount;
200     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT_MEDIA_FUSE)] =
201         &StorageManagerStub::HandleMountMediaFuse;
202     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UMOUNT_MEDIA_FUSE)] =
203         &StorageManagerStub::HandleUMountMediaFuse;
204     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT_FILE_MGR_FUSE)] =
205             &StorageManagerStub::HandleMountFileMgrFuse;
206     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UMOUNT_FILE_MGR_FUSE)] =
207             &StorageManagerStub::HandleUMountFileMgrFuse;
208 }
209 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)210 int32_t StorageManagerStub::OnRemoteRequest(uint32_t code,
211     MessageParcel &data, MessageParcel &reply, MessageOption &option)
212 {
213     if (data.ReadInterfaceToken() != GetDescriptor()) {
214         return E_PERMISSION_DENIED;
215     }
216     switch (code) {
217         case static_cast<uint32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER):
218             return HandlePrepareAddUser(data, reply);
219         case static_cast<uint32_t>(StorageManagerInterfaceCode::REMOVE_USER):
220             return HandleRemoveUser(data, reply);
221         case static_cast<uint32_t>(StorageManagerInterfaceCode::PREPARE_START_USER):
222             return HandlePrepareStartUser(data, reply);
223         case static_cast<uint32_t>(StorageManagerInterfaceCode::STOP_USER):
224             return HandleStopUser(data, reply);
225         case static_cast<uint32_t>(StorageManagerInterfaceCode::COMPLETE_ADD_USER):
226             return HandleCompleteAddUser(data, reply);
227         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_TOTAL):
228             return HandleGetTotal(data, reply);
229         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FREE):
230             return HandleGetFree(data, reply);
231         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_SYSTEM_SIZE):
232             return HandleGetSystemSize(data, reply);
233         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_TOTAL_SIZE):
234             return HandleGetTotalSize(data, reply);
235         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FREE_SIZE):
236             return HandleGetFreeSize(data, reply);
237         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_CURR_USER_STATS):
238             return HandleGetCurrUserStorageStats(data, reply);
239         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_STATS):
240             return HandleGetUserStorageStats(data, reply);
241         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_STATS_BY_TYPE):
242             return HandleGetUserStorageStatsByType(data, reply);
243         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_CURR_BUNDLE_STATS):
244             return HandleGetCurrentBundleStats(data, reply);
245         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATUS):
246             return HandleGetBundleStatus(data, reply);
247         case static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_CREATED):
248             return HandleNotifyVolumeCreated(data, reply);
249         case static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_MOUNTED):
250             return HandleNotifyVolumeMounted(data, reply);
251         case static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_STATE_CHANGED):
252             return HandleNotifyVolumeStateChanged(data, reply);
253         case static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT):
254             return HandleMount(data, reply);
255         case static_cast<uint32_t>(StorageManagerInterfaceCode::UNMOUNT):
256             return HandleUnmount(data, reply);
257         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_ALL_VOLUMES):
258             return HandleGetAllVolumes(data, reply);
259         case static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_CREATED):
260             return HandleNotifyDiskCreated(data, reply);
261         case static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_DESTROYED):
262             return HandleNotifyDiskDestroyed(data, reply);
263         case static_cast<uint32_t>(StorageManagerInterfaceCode::PARTITION):
264             return HandlePartition(data, reply);
265         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_ALL_DISKS):
266             return HandleGetAllDisks(data, reply);
267         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_VOL_BY_UUID):
268             return HandleGetVolumeByUuid(data, reply);
269         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_VOL_BY_ID):
270             return HandleGetVolumeById(data, reply);
271         case static_cast<uint32_t>(StorageManagerInterfaceCode::SET_VOL_DESC):
272             return HandleSetVolDesc(data, reply);
273         case static_cast<uint32_t>(StorageManagerInterfaceCode::FORMAT):
274             return HandleFormat(data, reply);
275         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_DISK_BY_ID):
276             return HandleGetDiskById(data, reply);
277         case static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_USER_KEYS):
278             return HandleGenerateUserKeys(data, reply);
279         case static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_USER_KEYS):
280             return HandleDeleteUserKeys(data, reply);
281         case static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_USER_AUTH):
282             return HandleUpdateUserAuth(data, reply);
283         case static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_USER_AUTH_RECOVER_KEY):
284             return HandleUpdateUseAuthWithRecoveryKey(data, reply);
285         case static_cast<uint32_t>(StorageManagerInterfaceCode::ACTIVE_USER_KEY):
286             return HandleActiveUserKey(data, reply);
287         case static_cast<uint32_t>(StorageManagerInterfaceCode::INACTIVE_USER_KEY):
288             return HandleInactiveUserKey(data, reply);
289         case static_cast<uint32_t>(StorageManagerInterfaceCode::LOCK_USER_SCREEN):
290             return HandleLockUserScreen(data, reply);
291         case static_cast<uint32_t>(StorageManagerInterfaceCode::UNLOCK_USER_SCREEN):
292             return HandleUnlockUserScreen(data, reply);
293         case static_cast<uint32_t>(StorageManagerInterfaceCode::LOCK_SCREEN_STATUS):
294             return HandleGetLockScreenStatus(data, reply);
295         case static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_KEY_CONTEXT):
296             return HandleUpdateKeyContext(data, reply);
297         case static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_SHARE_FILE):
298             return HandleCreateShareFile(data, reply);
299         case static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_SHARE_FILE):
300             return HandleDeleteShareFile(data, reply);
301         case static_cast<uint32_t>(StorageManagerInterfaceCode::SET_BUNDLE_QUOTA):
302             return HandleSetBundleQuota(data, reply);
303         case static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_MEM_PARA):
304             return HandleUpdateMemoryPara(data, reply);
305         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATS_INCREASE):
306             return HandleGetBundleStatsForIncrease(data, reply);
307         case static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT_DFS_DOCS):
308             return HandleMountDfsDocs(data, reply);
309         case static_cast<uint32_t>(StorageManagerInterfaceCode::UMOUNT_DFS_DOCS):
310             return HandleUMountDfsDocs(data, reply);
311         case static_cast<uint32_t>(StorageManagerInterfaceCode::GENERATE_APP_KEY):
312             return HandleGenerateAppkey(data, reply);
313         case static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_APP_KEY):
314             return HandleDeleteAppkey(data, reply);
315         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS):
316             return HandleGetFileEncryptStatus(data, reply);
317         case static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_NEED_ACTIVE_STATUS):
318             return HandleGetUserNeedActiveStatus(data, reply);
319         case static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_RECOVER_KEY):
320             return HandleCreateRecoverKey(data, reply);
321         case static_cast<uint32_t>(StorageManagerInterfaceCode::SET_RECOVER_KEY):
322             return HandleSetRecoverKey(data, reply);
323         case static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_MTP_MOUNT):
324             return HandleNotifyMtpMount(data, reply);
325         case static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_MTP_UNMOUNT):
326             return HandleNotifyMtpUnmount(data, reply);
327         case static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT_MEDIA_FUSE):
328             return HandleMountMediaFuse(data, reply);
329         case static_cast<uint32_t>(StorageManagerInterfaceCode::UMOUNT_MEDIA_FUSE):
330             return HandleUMountMediaFuse(data, reply);
331         case static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT_FILE_MGR_FUSE):
332             return HandleMountFileMgrFuse(data, reply);
333         case static_cast<uint32_t>(StorageManagerInterfaceCode::UMOUNT_FILE_MGR_FUSE):
334             return HandleUMountFileMgrFuse(data, reply);
335         default:
336             LOGE("Cannot response request %{public}d: unknown tranction", code);
337             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
338     }
339 }
340 
HandlePrepareAddUser(MessageParcel & data,MessageParcel & reply)341 int32_t StorageManagerStub::HandlePrepareAddUser(MessageParcel &data, MessageParcel &reply)
342 {
343     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT) &&
344         IPCSkeleton::GetCallingUid() != ACCOUNT_UID) {
345         return E_PERMISSION_DENIED;
346     }
347     int32_t userId = data.ReadInt32();
348     uint32_t flags = data.ReadUint32();
349     LOGI("StorageManagerStub::HandlePrepareAddUser, userId:%{public}d", userId);
350     int err = PrepareAddUser(userId, flags);
351     if (!reply.WriteUint32(err)) {
352         LOGE("StorageManagerStub::HandlePrepareAddUser call PrepareAddUser failed");
353         return  E_WRITE_REPLY_ERR;
354     }
355     return E_OK;
356 }
357 
HandleRemoveUser(MessageParcel & data,MessageParcel & reply)358 int32_t StorageManagerStub::HandleRemoveUser(MessageParcel &data, MessageParcel &reply)
359 {
360     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT) &&
361         IPCSkeleton::GetCallingUid() != ACCOUNT_UID) {
362         return E_PERMISSION_DENIED;
363     }
364     int32_t userId = data.ReadInt32();
365     uint32_t flags = data.ReadUint32();
366     LOGI("StorageManagerStub::HandleRemoveUser, userId:%{public}d", userId);
367     int err = RemoveUser(userId, flags);
368     if (!reply.WriteUint32(err)) {
369         LOGE("StorageManagerStub::HandleRemoveUser call RemoveUser failed");
370         return E_WRITE_REPLY_ERR;
371     }
372     return E_OK;
373 }
374 
HandlePrepareStartUser(MessageParcel & data,MessageParcel & reply)375 int32_t StorageManagerStub::HandlePrepareStartUser(MessageParcel &data, MessageParcel &reply)
376 {
377     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
378         return E_PERMISSION_DENIED;
379     }
380     int32_t userId = data.ReadInt32();
381     LOGI("StorageManagerStub::HandlePrepareStartUser, userId:%{public}d", userId);
382     int err = PrepareStartUser(userId);
383     if (!reply.WriteUint32(err)) {
384         LOGE("StorageManagerStub::HandlePrepareStartUser call PrepareStartUser failed");
385         return E_WRITE_REPLY_ERR;
386     }
387     return E_OK;
388 }
389 
HandleStopUser(MessageParcel & data,MessageParcel & reply)390 int32_t StorageManagerStub::HandleStopUser(MessageParcel &data, MessageParcel &reply)
391 {
392     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
393         return E_PERMISSION_DENIED;
394     }
395     int32_t userId = data.ReadInt32();
396     LOGI("StorageManagerStub::HandleStopUser, userId:%{public}d", userId);
397     int err = StopUser(userId);
398     if (!reply.WriteUint32(err)) {
399         LOGE("StorageManagerStub::HandleStopUser call StopUser failed");
400         return E_WRITE_REPLY_ERR;
401     }
402     return E_OK;
403 }
404 
HandleCompleteAddUser(MessageParcel & data,MessageParcel & reply)405 int32_t StorageManagerStub::HandleCompleteAddUser(MessageParcel &data, MessageParcel &reply)
406 {
407     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
408         return E_PERMISSION_DENIED;
409     }
410     int32_t userId = data.ReadInt32();
411     LOGI("StorageManagerStub::HandleCompleteAddUser, userId:%{public}d", userId);
412     int err = CompleteAddUser(userId);
413     if (!reply.WriteUint32(err)) {
414         LOGE("StorageManagerStub::HandleCompleteAddUser call CompleteAddUser failed");
415         return E_WRITE_REPLY_ERR;
416     }
417     return E_OK;
418 }
419 
HandleGetTotal(MessageParcel & data,MessageParcel & reply)420 int32_t StorageManagerStub::HandleGetTotal(MessageParcel &data, MessageParcel &reply)
421 {
422     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
423         return E_PERMISSION_DENIED;
424     }
425     LOGE("StorageManagerStub::HandleGetTotal Begin.");
426     std::string volumeId = data.ReadString();
427     int64_t totalSize;
428     int32_t err = GetTotalSizeOfVolume(volumeId, totalSize);
429     if (!reply.WriteInt32(err)) {
430         LOGE("StorageManagerStub::HandleGetTotal call OnUserDGetTotalSizeOfVolume failed");
431         return  E_WRITE_REPLY_ERR;
432     }
433     if (!reply.WriteInt64(totalSize)) {
434         return  E_WRITE_REPLY_ERR;
435     }
436     return E_OK;
437 }
438 
HandleGetFree(MessageParcel & data,MessageParcel & reply)439 int32_t StorageManagerStub::HandleGetFree(MessageParcel &data, MessageParcel &reply)
440 {
441     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
442         return E_PERMISSION_DENIED;
443     }
444     std::string volumeId = data.ReadString();
445     int64_t freeSize;
446     int32_t err = GetFreeSizeOfVolume(volumeId, freeSize);
447     if (!reply.WriteInt32(err)) {
448         LOGE("StorageManagerStub::HandleGetFree call GetFreeSizeOfVolume failed");
449         return  E_WRITE_REPLY_ERR;
450     }
451     if (!reply.WriteInt64(freeSize)) {
452         return  E_WRITE_REPLY_ERR;
453     }
454     return E_OK;
455 }
456 
HandleGetBundleStatus(MessageParcel & data,MessageParcel & reply)457 int32_t StorageManagerStub::HandleGetBundleStatus(MessageParcel &data, MessageParcel &reply)
458 {
459     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
460         return E_PERMISSION_DENIED;
461     }
462     std::string pkgName = data.ReadString();
463     int32_t appIndex = data.ReadInt32();
464     uint32_t statFlag = data.ReadUint32();
465     BundleStats bundleStats;
466     int32_t err = GetBundleStats(pkgName, bundleStats, appIndex, statFlag);
467     if (!reply.WriteInt32(err)) {
468         return  E_WRITE_REPLY_ERR;
469     }
470     if (!bundleStats.Marshalling(reply)) {
471         return  E_WRITE_REPLY_ERR;
472     }
473     return E_OK;
474 }
475 
HandleGetSystemSize(MessageParcel & data,MessageParcel & reply)476 int32_t StorageManagerStub::HandleGetSystemSize(MessageParcel &data, MessageParcel &reply)
477 {
478     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
479         return E_PERMISSION_DENIED;
480     }
481     int64_t systemSize;
482     int32_t err = GetSystemSize(systemSize);
483     if (!reply.WriteInt32(err)) {
484         return  E_WRITE_REPLY_ERR;
485     }
486     if (!reply.WriteInt64(systemSize)) {
487         LOGE("StorageManagerStub::HandleGetFree call GetSystemSize failed");
488         return  E_WRITE_REPLY_ERR;
489     }
490     return E_OK;
491 }
492 
HandleGetTotalSize(MessageParcel & data,MessageParcel & reply)493 int32_t StorageManagerStub::HandleGetTotalSize(MessageParcel &data, MessageParcel &reply)
494 {
495     int64_t totalSize;
496     int32_t err = GetTotalSize(totalSize);
497     if (!reply.WriteInt32(err)) {
498         return  E_WRITE_REPLY_ERR;
499     }
500     if (!reply.WriteInt64(totalSize)) {
501         LOGE("StorageManagerStub::HandleGetFree call GetTotalSize failed");
502         return  E_WRITE_REPLY_ERR;
503     }
504     return E_OK;
505 }
506 
HandleGetFreeSize(MessageParcel & data,MessageParcel & reply)507 int32_t StorageManagerStub::HandleGetFreeSize(MessageParcel &data, MessageParcel &reply)
508 {
509     int64_t freeSize;
510     int32_t err = GetFreeSize(freeSize);
511     if (!reply.WriteInt32(err)) {
512         return  E_WRITE_REPLY_ERR;
513     }
514     if (!reply.WriteInt64(freeSize)) {
515         LOGE("StorageManagerStub::HandleGetFree call GetFreeSize failed");
516         return  E_WRITE_REPLY_ERR;
517     }
518     return E_OK;
519 }
520 
HandleGetCurrUserStorageStats(MessageParcel & data,MessageParcel & reply)521 int32_t StorageManagerStub::HandleGetCurrUserStorageStats(MessageParcel &data, MessageParcel &reply)
522 {
523     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
524         return E_PERMISSION_DENIED;
525     }
526     StorageStats storageStats;
527     int32_t err = GetUserStorageStats(storageStats);
528     if (!reply.WriteInt32(err)) {
529         return  E_WRITE_REPLY_ERR;
530     }
531     if (!storageStats.Marshalling(reply)) {
532         return  E_WRITE_REPLY_ERR;
533     }
534     return E_OK;
535 }
536 
HandleGetUserStorageStats(MessageParcel & data,MessageParcel & reply)537 int32_t StorageManagerStub::HandleGetUserStorageStats(MessageParcel &data, MessageParcel &reply)
538 {
539     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
540         return E_PERMISSION_DENIED;
541     }
542     int32_t userId = data.ReadInt32();
543     StorageStats storageStats;
544     int32_t err = GetUserStorageStats(userId, storageStats);
545     if (!reply.WriteInt32(err)) {
546         return  E_WRITE_REPLY_ERR;
547     }
548     if (!storageStats.Marshalling(reply)) {
549         return  E_WRITE_REPLY_ERR;
550     }
551     return E_OK;
552 }
553 
HandleGetCurrentBundleStats(MessageParcel & data,MessageParcel & reply)554 int32_t StorageManagerStub::HandleGetCurrentBundleStats(MessageParcel &data, MessageParcel &reply)
555 {
556     BundleStats bundleStats;
557     uint32_t statFlag = data.ReadUint32();
558     int32_t err = GetCurrentBundleStats(bundleStats, statFlag);
559     if (!reply.WriteInt32(err)) {
560         return  E_WRITE_REPLY_ERR;
561     }
562     if (!bundleStats.Marshalling(reply)) {
563         return  E_WRITE_REPLY_ERR;
564     }
565     return E_OK;
566 }
567 
HandleGetAllVolumes(MessageParcel & data,MessageParcel & reply)568 int32_t StorageManagerStub::HandleGetAllVolumes(MessageParcel &data, MessageParcel &reply)
569 {
570     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
571         return E_PERMISSION_DENIED;
572     }
573     std::vector<VolumeExternal> ve;
574     int32_t err = GetAllVolumes(ve);
575     if (!reply.WriteInt32(err)) {
576         return  E_WRITE_REPLY_ERR;
577     }
578     uint size = ve.size();
579     if (size == 0) {
580         LOGE("StorageManagerStub::No volume.");
581         if (!reply.WriteUint32(0)) {
582             return  E_WRITE_REPLY_ERR;
583         }
584         return E_OK;
585     }
586     if (!reply.WriteUint32(ve.size())) {
587         return  E_WRITE_REPLY_ERR;
588     }
589     for (uint i = 0; i < size; i++) {
590         if (!ve[i].Marshalling(reply)) {
591             return  E_WRITE_REPLY_ERR;
592         }
593     }
594     return E_OK;
595 }
596 
HandleNotifyVolumeCreated(MessageParcel & data,MessageParcel & reply)597 int32_t StorageManagerStub::HandleNotifyVolumeCreated(MessageParcel &data, MessageParcel &reply)
598 {
599     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
600         return E_PERMISSION_DENIED;
601     }
602     std::unique_ptr<VolumeCore> vc = VolumeCore::Unmarshalling(data);
603     NotifyVolumeCreated(*vc);
604     LOGI("StorageManagerStub::HandleNotifyVolumeCreated");
605     return E_OK;
606 }
607 
HandleNotifyVolumeMounted(MessageParcel & data,MessageParcel & reply)608 int32_t StorageManagerStub::HandleNotifyVolumeMounted(MessageParcel &data, MessageParcel &reply)
609 {
610     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
611         return E_PERMISSION_DENIED;
612     }
613     std::string volumeId = data.ReadString();
614     int32_t fsType = data.ReadInt32();
615     std::string fsUuid = data.ReadString();
616     std::string path = data.ReadString();
617     std::string description = data.ReadString();
618     NotifyVolumeMounted(volumeId, fsType, fsUuid, path, description);
619     LOGI("StorageManagerStub::HandleNotifyVolumeMounted");
620     return E_OK;
621 }
622 
HandleNotifyVolumeStateChanged(MessageParcel & data,MessageParcel & reply)623 int32_t StorageManagerStub::HandleNotifyVolumeStateChanged(MessageParcel &data, MessageParcel &reply)
624 {
625     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
626         return E_PERMISSION_DENIED;
627     }
628     std::string volumeId = data.ReadString();
629     VolumeState state = VolumeState(data.ReadInt32());
630     NotifyVolumeStateChanged(volumeId, state);
631     LOGI("StorageManagerStub::HandleNotifyVolumeStateChanged");
632     return E_OK;
633 }
634 
HandleMount(MessageParcel & data,MessageParcel & reply)635 int32_t StorageManagerStub::HandleMount(MessageParcel &data, MessageParcel &reply)
636 {
637     if (!CheckClientPermission(PERMISSION_MOUNT_MANAGER)) {
638         return E_PERMISSION_DENIED;
639     }
640     std::string volumeId = data.ReadString();
641     int err = Mount(volumeId);
642     if (!reply.WriteUint32(err)) {
643         LOGE("StorageManagerStub::HandleMount call Mount failed");
644         return  E_WRITE_REPLY_ERR;
645     }
646     return E_OK;
647 }
648 
HandleUnmount(MessageParcel & data,MessageParcel & reply)649 int32_t StorageManagerStub::HandleUnmount(MessageParcel &data, MessageParcel &reply)
650 {
651     if (!CheckClientPermission(PERMISSION_MOUNT_MANAGER)) {
652         return E_PERMISSION_DENIED;
653     }
654     std::string volumeId = data.ReadString();
655     int err = Unmount(volumeId);
656     if (!reply.WriteUint32(err)) {
657         LOGE("StorageManagerStub::HandleUnmount call Mount failed");
658         return  E_WRITE_REPLY_ERR;
659     }
660     return E_OK;
661 }
662 
HandleNotifyDiskCreated(MessageParcel & data,MessageParcel & reply)663 int32_t StorageManagerStub::HandleNotifyDiskCreated(MessageParcel &data, MessageParcel &reply)
664 {
665     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
666         return E_PERMISSION_DENIED;
667     }
668     auto disk = Disk::Unmarshalling(data);
669     NotifyDiskCreated(*disk);
670     return E_OK;
671 }
672 
HandleNotifyDiskDestroyed(MessageParcel & data,MessageParcel & reply)673 int32_t StorageManagerStub::HandleNotifyDiskDestroyed(MessageParcel &data, MessageParcel &reply)
674 {
675     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
676         return E_PERMISSION_DENIED;
677     }
678     std::string diskId = data.ReadString();
679     NotifyDiskDestroyed(diskId);
680     return E_OK;
681 }
682 
HandlePartition(MessageParcel & data,MessageParcel & reply)683 int32_t StorageManagerStub::HandlePartition(MessageParcel &data, MessageParcel &reply)
684 {
685     if (!CheckClientPermission(PERMISSION_FORMAT_MANAGER)) {
686         return E_PERMISSION_DENIED;
687     }
688     std::string diskId = data.ReadString();
689     int32_t type = data.ReadInt32();
690     int err = Partition(diskId, type);
691     if (!reply.WriteUint32(err)) {
692         LOGE("StorageManagerStub::HandlePartition call Partition failed");
693         return E_WRITE_REPLY_ERR;
694     }
695     return E_OK;
696 }
697 
HandleGetAllDisks(MessageParcel & data,MessageParcel & reply)698 int32_t StorageManagerStub::HandleGetAllDisks(MessageParcel &data, MessageParcel &reply)
699 {
700     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
701         return E_PERMISSION_DENIED;
702     }
703     std::vector<Disk> disks;
704     int32_t err = GetAllDisks(disks);
705     if (!reply.WriteUint32(err)) {
706         return E_WRITE_REPLY_ERR;
707     }
708     uint size = disks.size();
709     if (size == 0) {
710         LOGE("StorageManagerStub::No Disk.");
711         if (!reply.WriteUint32(0)) {
712             return  E_WRITE_REPLY_ERR;
713         }
714         return E_OK;
715     }
716     if (!reply.WriteUint32(disks.size())) {
717         return  E_WRITE_REPLY_ERR;
718     }
719     for (uint i = 0; i < size; i++) {
720         if (!disks[i].Marshalling(reply)) {
721             return  E_WRITE_REPLY_ERR;
722         }
723     }
724     return E_OK;
725 }
726 
HandleGetVolumeByUuid(MessageParcel & data,MessageParcel & reply)727 int32_t StorageManagerStub::HandleGetVolumeByUuid(MessageParcel &data, MessageParcel &reply)
728 {
729     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
730         return E_PERMISSION_DENIED;
731     }
732     std::string fsUuid = data.ReadString();
733     VolumeExternal vc;
734     int err = GetVolumeByUuid(fsUuid, vc);
735     if (!vc.Marshalling(reply)) {
736         return E_WRITE_REPLY_ERR;
737     }
738     if (!reply.WriteUint32(err)) {
739         LOGE("StorageManagerStub::HandleGetVolumeByUuid call GetVolumeByUuid failed");
740         return E_WRITE_REPLY_ERR;
741     }
742     return E_OK;
743 }
744 
HandleGetVolumeById(MessageParcel & data,MessageParcel & reply)745 int32_t StorageManagerStub::HandleGetVolumeById(MessageParcel &data, MessageParcel &reply)
746 {
747     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
748         return E_PERMISSION_DENIED;
749     }
750     std::string volId = data.ReadString();
751     VolumeExternal vc;
752     int err = GetVolumeById(volId, vc);
753     if (!vc.Marshalling(reply)) {
754         return E_WRITE_REPLY_ERR;
755     }
756     if (!reply.WriteUint32(err)) {
757         LOGE("StorageManagerStub::HandleGetVolumeById call GetVolumeById failed");
758         return E_WRITE_REPLY_ERR;
759     }
760     return E_OK;
761 }
762 
HandleSetVolDesc(MessageParcel & data,MessageParcel & reply)763 int32_t StorageManagerStub::HandleSetVolDesc(MessageParcel &data, MessageParcel &reply)
764 {
765     if (!CheckClientPermission(PERMISSION_MOUNT_MANAGER)) {
766         return E_PERMISSION_DENIED;
767     }
768     std::string fsUuid = data.ReadString();
769     std::string desc = data.ReadString();
770     int err = SetVolumeDescription(fsUuid, desc);
771     if (!reply.WriteUint32(err)) {
772         LOGE("StorageManagerStub::HandleSetVolDesc call SetVolumeDescription failed");
773         return E_WRITE_REPLY_ERR;
774     }
775     return E_OK;
776 }
777 
HandleFormat(MessageParcel & data,MessageParcel & reply)778 int32_t StorageManagerStub::HandleFormat(MessageParcel &data, MessageParcel &reply)
779 {
780     if (!CheckClientPermission(PERMISSION_FORMAT_MANAGER)) {
781         return E_PERMISSION_DENIED;
782     }
783     std::string volId = data.ReadString();
784     std::string fsType = data.ReadString();
785     int err = Format(volId, fsType);
786     if (!reply.WriteUint32(err)) {
787         LOGE("StorageManagerStub::HandleFormat call Format failed");
788         return E_WRITE_REPLY_ERR;
789     }
790     return E_OK;
791 }
792 
HandleGetDiskById(MessageParcel & data,MessageParcel & reply)793 int32_t StorageManagerStub::HandleGetDiskById(MessageParcel &data, MessageParcel &reply)
794 {
795     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
796         return E_PERMISSION_DENIED;
797     }
798     std::string volId = data.ReadString();
799     Disk disk;
800     int err = GetDiskById(volId, disk);
801     if (!disk.Marshalling(reply)) {
802         return E_WRITE_REPLY_ERR;
803     }
804     if (!reply.WriteUint32(err)) {
805         LOGE("StorageManagerStub::HandleGetDiskById call GetDiskById failed");
806         return E_WRITE_REPLY_ERR;
807     }
808     return E_OK;
809 }
810 
HandleGenerateUserKeys(MessageParcel & data,MessageParcel & reply)811 int32_t StorageManagerStub::HandleGenerateUserKeys(MessageParcel &data, MessageParcel &reply)
812 {
813     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
814         return E_PERMISSION_DENIED;
815     }
816     uint32_t userId = data.ReadUint32();
817     uint32_t flags = data.ReadUint32();
818     int32_t err = GenerateUserKeys(userId, flags);
819     if (!reply.WriteInt32(err)) {
820         LOGE("Write reply error code failed");
821         return E_WRITE_REPLY_ERR;
822     }
823 
824     return E_OK;
825 }
826 
HandleDeleteUserKeys(MessageParcel & data,MessageParcel & reply)827 int32_t StorageManagerStub::HandleDeleteUserKeys(MessageParcel &data, MessageParcel &reply)
828 {
829     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
830         return E_PERMISSION_DENIED;
831     }
832     uint32_t userId = data.ReadUint32();
833     int32_t err = DeleteUserKeys(userId);
834     if (!reply.WriteInt32(err)) {
835         LOGE("Write reply error code failed");
836         return E_WRITE_REPLY_ERR;
837     }
838 
839     return E_OK;
840 }
841 
HandleUpdateUserAuth(MessageParcel & data,MessageParcel & reply)842 int32_t StorageManagerStub::HandleUpdateUserAuth(MessageParcel &data, MessageParcel &reply)
843 {
844     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
845         return E_PERMISSION_DENIED;
846     }
847     uint32_t userId = data.ReadUint32();
848     uint64_t secureUid = data.ReadUint64();
849 
850     std::vector<uint8_t> token;
851     std::vector<uint8_t> oldSecret;
852     std::vector<uint8_t> newSecret;
853     data.ReadUInt8Vector(&token);
854     data.ReadUInt8Vector(&oldSecret);
855     data.ReadUInt8Vector(&newSecret);
856 
857     int32_t err = UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
858     if (!reply.WriteInt32(err)) {
859         LOGE("Write reply error code failed");
860         return E_WRITE_REPLY_ERR;
861     }
862     return E_OK;
863 }
864 
HandleUpdateUseAuthWithRecoveryKey(MessageParcel & data,MessageParcel & reply)865 int32_t StorageManagerStub::HandleUpdateUseAuthWithRecoveryKey(MessageParcel &data, MessageParcel &reply)
866 {
867     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
868         return E_PERMISSION_DENIED;
869     }
870     uint32_t userId = data.ReadUint32();
871     uint64_t secureUid = data.ReadUint64();
872 
873     std::vector<uint8_t> token;
874     std::vector<uint8_t> newSecret;
875     data.ReadUInt8Vector(&token);
876     data.ReadUInt8Vector(&newSecret);
877     std::vector<std::vector<uint8_t>> plainText;
878     const int CKEY_NUMS = 6;
879     for (uint32_t i = 0; i < CKEY_NUMS; i++) {
880         std::vector<uint8_t> iv;
881         data.ReadUInt8Vector(&iv);
882         plainText.push_back(iv);
883     }
884 
885     int32_t err = UpdateUseAuthWithRecoveryKey(token, newSecret, secureUid, userId, plainText);
886     if (!reply.WriteInt32(err)) {
887         LOGE("Write reply error code failed");
888         return E_WRITE_REPLY_ERR;
889     }
890     return E_OK;
891 }
892 
HandleActiveUserKey(MessageParcel & data,MessageParcel & reply)893 int32_t StorageManagerStub::HandleActiveUserKey(MessageParcel &data, MessageParcel &reply)
894 {
895     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT) &&
896         IPCSkeleton::GetCallingUid() != ACCOUNT_UID) {
897         return E_PERMISSION_DENIED;
898     }
899     uint32_t userId = data.ReadUint32();
900 
901     std::vector<uint8_t> token;
902     std::vector<uint8_t> secret;
903     data.ReadUInt8Vector(&token);
904     data.ReadUInt8Vector(&secret);
905 
906     int32_t err = ActiveUserKey(userId, token, secret);
907     if (!reply.WriteInt32(err)) {
908         LOGE("Write reply error code failed");
909         return E_WRITE_REPLY_ERR;
910     }
911     return E_OK;
912 }
913 
HandleInactiveUserKey(MessageParcel & data,MessageParcel & reply)914 int32_t StorageManagerStub::HandleInactiveUserKey(MessageParcel &data, MessageParcel &reply)
915 {
916     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
917         return E_PERMISSION_DENIED;
918     }
919     uint32_t userId = data.ReadUint32();
920     int32_t err = InactiveUserKey(userId);
921     if (!reply.WriteInt32(err)) {
922         LOGE("Write reply error code failed");
923         return E_WRITE_REPLY_ERR;
924     }
925     return E_OK;
926 }
927 
HandleLockUserScreen(MessageParcel & data,MessageParcel & reply)928 int32_t StorageManagerStub::HandleLockUserScreen(MessageParcel &data, MessageParcel &reply)
929 {
930     // Only for sceneboard
931     std::string bundleName;
932     int32_t uid = IPCSkeleton::GetCallingUid();
933     uint32_t userId = data.ReadUint32();
934     auto bundleMgr = DelayedSingleton<BundleMgrConnector>::GetInstance()->GetBundleMgrProxy();
935     if (bundleMgr == nullptr) {
936         LOGE("Connect bundle manager sa proxy failed.");
937         return E_SERVICE_IS_NULLPTR;
938     }
939     if (!bundleMgr->GetBundleNameForUid(uid, bundleName)) {
940         LOGE("Invoke bundleMgr interface to get bundle name failed.");
941         StorageService::StorageRadar::ReportBundleMgrResult("StorageManagerStub::HandleLockUserScreen",
942             E_BUNDLEMGR_ERROR, userId, "pkgName="+SCENEBOARD_BUNDLE_NAME);
943         return E_BUNDLEMGR_ERROR;
944     }
945 
946     if (bundleName != SCENEBOARD_BUNDLE_NAME && bundleName != SYSTEMUI_BUNDLE_NAME) {
947         LOGE("permissionCheck error, caller is %{public}s(%{public}d), should be %{public}s",
948             bundleName.c_str(), uid, SCENEBOARD_BUNDLE_NAME.c_str());
949         return E_PERMISSION_DENIED;
950     }
951     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER_CRYPT)) {
952         return E_PERMISSION_DENIED;
953     }
954 
955     int32_t err = LockUserScreen(userId);
956     if (!reply.WriteInt32(err)) {
957         LOGE("Write reply error code failed");
958         return E_WRITE_REPLY_ERR;
959     }
960 
961     return E_OK;
962 }
963 
HandleGetFileEncryptStatus(MessageParcel & data,MessageParcel & reply)964 int32_t StorageManagerStub::HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply)
965 {
966     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
967         return E_PERMISSION_DENIED;
968     }
969     bool isEncrypted = true;
970     uint32_t userId = data.ReadUint32();
971     bool needCheckDirMount = data.ReadBool();
972     int32_t err = GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
973     if (!reply.WriteBool(isEncrypted)) {
974         LOGE("Write reply isEncrypted failed");
975         return E_WRITE_REPLY_ERR;
976     }
977     if (!reply.WriteInt32(err)) {
978         LOGE("Write reply error code failed");
979         return E_WRITE_REPLY_ERR;
980     }
981     return E_OK;
982 }
983 
HandleGetUserNeedActiveStatus(MessageParcel & data,MessageParcel & reply)984 int32_t StorageManagerStub::HandleGetUserNeedActiveStatus(MessageParcel &data, MessageParcel &reply)
985 {
986     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
987         return E_PERMISSION_DENIED;
988     }
989     bool needActive = false;
990     uint32_t userId = data.ReadUint32();
991     int32_t err = GetUserNeedActiveStatus(userId, needActive);
992     if (!reply.WriteBool(needActive)) {
993         LOGE("Write needActive failed");
994         return E_WRITE_REPLY_ERR;
995     }
996     if (!reply.WriteInt32(err)) {
997         LOGE("Write reply error code failed");
998         return E_WRITE_REPLY_ERR;
999     }
1000     return E_OK;
1001 }
1002 
HandleUnlockUserScreen(MessageParcel & data,MessageParcel & reply)1003 int32_t StorageManagerStub::HandleUnlockUserScreen(MessageParcel &data, MessageParcel &reply)
1004 {
1005     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
1006         return E_PERMISSION_DENIED;
1007     }
1008     uint32_t userId = data.ReadUint32();
1009 
1010     std::vector<uint8_t> token;
1011     std::vector<uint8_t> secret;
1012     data.ReadUInt8Vector(&token);
1013     data.ReadUInt8Vector(&secret);
1014 
1015     int32_t err = UnlockUserScreen(userId, token, secret);
1016     if (!reply.WriteInt32(err)) {
1017         LOGE("Write reply error code failed");
1018         return E_WRITE_REPLY_ERR;
1019     }
1020     return E_OK;
1021 }
1022 
HandleGetLockScreenStatus(MessageParcel & data,MessageParcel & reply)1023 int32_t StorageManagerStub::HandleGetLockScreenStatus(MessageParcel &data, MessageParcel &reply)
1024 {
1025     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER)) {
1026         return E_PERMISSION_DENIED;
1027     }
1028     uint32_t userId = data.ReadUint32();
1029     bool lockScreenStatus = false;
1030     int32_t err = GetLockScreenStatus(userId, lockScreenStatus);
1031     if (!reply.WriteBool(lockScreenStatus)) {
1032         LOGE("Write reply lockScreenStatus failed");
1033         return E_WRITE_REPLY_ERR;
1034     }
1035     if (!reply.WriteInt32(err)) {
1036         LOGE("Write reply error code failed");
1037         return E_WRITE_REPLY_ERR;
1038     }
1039     return E_OK;
1040 }
1041 
HandleGenerateAppkey(MessageParcel & data,MessageParcel & reply)1042 int32_t StorageManagerStub::HandleGenerateAppkey(MessageParcel &data, MessageParcel &reply)
1043 {
1044     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
1045         return E_PERMISSION_DENIED;
1046     }
1047     uint32_t hashId = data.ReadUint32();
1048     uint32_t userId = data.ReadUint32();
1049     std::string keyId;
1050     int32_t err = GenerateAppkey(hashId, userId, keyId);
1051     if (!reply.WriteString(keyId)) {
1052         LOGE("Write reply lockScreenStatus failed");
1053         return E_WRITE_REPLY_ERR;
1054     }
1055     if (!reply.WriteInt32(err)) {
1056         LOGE("Write reply error code failed");
1057         return E_WRITE_REPLY_ERR;
1058     }
1059     return E_OK;
1060 }
1061 
HandleDeleteAppkey(MessageParcel & data,MessageParcel & reply)1062 int32_t StorageManagerStub::HandleDeleteAppkey(MessageParcel &data, MessageParcel &reply)
1063 {
1064     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
1065         return E_PERMISSION_DENIED;
1066     }
1067     std::string keyId = data.ReadString();
1068     int32_t err = DeleteAppkey(keyId);
1069     if (!reply.WriteInt32(err)) {
1070         LOGE("Write reply error code failed");
1071         return E_WRITE_REPLY_ERR;
1072     }
1073     return E_OK;
1074 }
1075 
HandleCreateRecoverKey(MessageParcel & data,MessageParcel & reply)1076 int32_t StorageManagerStub::HandleCreateRecoverKey(MessageParcel &data, MessageParcel &reply)
1077 {
1078     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
1079         return E_PERMISSION_DENIED;
1080     }
1081     uint32_t userId = data.ReadUint32();
1082     uint32_t userType = data.ReadUint32();
1083     std::vector<uint8_t> token;
1084     std::vector<uint8_t> secret;
1085     data.ReadUInt8Vector(&token);
1086     data.ReadUInt8Vector(&secret);
1087 
1088     int32_t err = CreateRecoverKey(userId, userType, token, secret);
1089     if (!reply.WriteInt32(err)) {
1090         LOGE("Write reply error code failed");
1091         return E_WRITE_REPLY_ERR;
1092     }
1093     return E_OK;
1094 }
1095 
HandleSetRecoverKey(MessageParcel & data,MessageParcel & reply)1096 int32_t StorageManagerStub::HandleSetRecoverKey(MessageParcel &data, MessageParcel &reply)
1097 {
1098     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
1099         return E_PERMISSION_DENIED;
1100     }
1101     std::vector<uint8_t> key;
1102     data.ReadUInt8Vector(&key);
1103 
1104     int32_t err = SetRecoverKey(key);
1105     if (!reply.WriteInt32(err)) {
1106         LOGE("Write reply error code failed");
1107         return E_WRITE_REPLY_ERR;
1108     }
1109     return E_OK;
1110 }
1111 
HandleUpdateKeyContext(MessageParcel & data,MessageParcel & reply)1112 int32_t StorageManagerStub::HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply)
1113 {
1114     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
1115         return E_PERMISSION_DENIED;
1116     }
1117     uint32_t userId = data.ReadUint32();
1118     bool needRemoveTmpKey = data.ReadBool();
1119     int32_t err = UpdateKeyContext(userId, needRemoveTmpKey);
1120     if (!reply.WriteInt32(err)) {
1121         LOGE("Write reply error code failed");
1122         return E_WRITE_REPLY_ERR;
1123     }
1124     return E_OK;
1125 }
1126 
HandleCreateShareFile(MessageParcel & data,MessageParcel & reply)1127 int32_t StorageManagerStub::HandleCreateShareFile(MessageParcel &data, MessageParcel &reply)
1128 {
1129     if (!CheckClientPermissionForShareFile()) {
1130         return E_PERMISSION_DENIED;
1131     }
1132 
1133     std::vector<std::string> uriList;
1134     if (!data.ReadStringVector(&uriList)) {
1135         return E_WRITE_REPLY_ERR;
1136     }
1137     uint32_t tokenId = data.ReadUint32();
1138     uint32_t flag = data.ReadUint32();
1139     std::vector<int32_t> retList = CreateShareFile(uriList, tokenId, flag);
1140     if (!reply.WriteInt32Vector(retList)) {
1141         return E_WRITE_REPLY_ERR;
1142     }
1143     return E_OK;
1144 }
1145 
HandleDeleteShareFile(MessageParcel & data,MessageParcel & reply)1146 int32_t StorageManagerStub::HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply)
1147 {
1148     if (!CheckClientPermissionForShareFile()) {
1149         return E_PERMISSION_DENIED;
1150     }
1151 
1152     uint32_t tokenId = data.ReadUint32();
1153     std::vector<std::string> uriList;
1154     if (!data.ReadStringVector(&uriList)) {
1155         return E_WRITE_REPLY_ERR;
1156     }
1157 
1158     int err = DeleteShareFile(tokenId, uriList);
1159     if (!reply.WriteInt32(err)) {
1160         return E_WRITE_REPLY_ERR;
1161     }
1162     return E_OK;
1163 }
1164 
HandleSetBundleQuota(MessageParcel & data,MessageParcel & reply)1165 int32_t StorageManagerStub::HandleSetBundleQuota(MessageParcel &data, MessageParcel &reply)
1166 {
1167     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
1168         return E_PERMISSION_DENIED;
1169     }
1170 
1171     std::string bundleName = data.ReadString();
1172     int32_t uid = data.ReadInt32();
1173     std::string bundleDataDirPath = data.ReadString();
1174     int32_t limitSizeMb = data.ReadInt32();
1175     int err = SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb);
1176     if (!reply.WriteInt32(err)) {
1177         return E_WRITE_REPLY_ERR;
1178     }
1179     return E_OK;
1180 }
1181 
HandleGetBundleStatsForIncrease(MessageParcel & data,MessageParcel & reply)1182 int32_t StorageManagerStub::HandleGetBundleStatsForIncrease(MessageParcel &data, MessageParcel &reply)
1183 {
1184     if (IPCSkeleton::GetCallingUid() != BACKUP_SA_UID) {
1185         LOGE("StorageManager permissionCheck error, calling uid is invalid, need backup_sa uid.");
1186         return E_PERMISSION_DENIED;
1187     }
1188 
1189     uint32_t userId = data.ReadUint32();
1190     std::vector<std::string> bundleNames;
1191     if (!data.ReadStringVector(&bundleNames)) {
1192         return E_WRITE_REPLY_ERR;
1193     }
1194     std::vector<int64_t> incrementalBackTimes;
1195     if (!data.ReadInt64Vector(&incrementalBackTimes)) {
1196         return E_WRITE_REPLY_ERR;
1197     }
1198 
1199     std::vector<int64_t> pkgFileSizes;
1200     std::vector<int64_t> incPkgFileSizes;
1201     int32_t err = GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes);
1202     if (!reply.WriteUint32(err)) {
1203         return E_WRITE_REPLY_ERR;
1204     }
1205     if (!reply.WriteInt64Vector(pkgFileSizes)) {
1206         LOGE("StorageManagerStub::HandleGetBundleStatsForIncrease call GetBundleStatsForIncrease failed");
1207         return  E_WRITE_REPLY_ERR;
1208     }
1209     if (!reply.WriteInt64Vector(incPkgFileSizes)) {
1210         LOGE("StorageManagerStub::HandleGetBundleStatsForIncrease call GetBundleStatsForIncrease failed");
1211         return  E_WRITE_REPLY_ERR;
1212     }
1213     return E_OK;
1214 }
1215 
1216 
HandleGetUserStorageStatsByType(MessageParcel & data,MessageParcel & reply)1217 int32_t StorageManagerStub::HandleGetUserStorageStatsByType(MessageParcel &data, MessageParcel &reply)
1218 {
1219     if (IPCSkeleton::GetCallingUid() != BACKUP_SA_UID) {
1220         LOGE("StorageManager permissionCheck error, calling uid is invalid, need backup_sa uid.");
1221         return E_PERMISSION_DENIED;
1222     }
1223 
1224     int32_t userId = data.ReadInt32();
1225     std::string type = data.ReadString();
1226     StorageStats storageStats;
1227     int32_t err = GetUserStorageStatsByType(userId, storageStats, type);
1228     if (!reply.WriteInt32(err)) {
1229         return E_WRITE_REPLY_ERR;
1230     }
1231     if (!storageStats.Marshalling(reply)) {
1232         return E_WRITE_REPLY_ERR;
1233     }
1234     return E_OK;
1235 }
1236 
HandleUpdateMemoryPara(MessageParcel & data,MessageParcel & reply)1237 int32_t StorageManagerStub::HandleUpdateMemoryPara(MessageParcel &data, MessageParcel &reply)
1238 {
1239     if (IPCSkeleton::GetCallingUid() != BACKUP_SA_UID) {
1240         LOGE("StorageManager permissionCheck error, calling uid is invalid, need backup_sa uid.");
1241         return E_PERMISSION_DENIED;
1242     }
1243 
1244     int32_t size = data.ReadInt32();
1245     int32_t oldSize = 0;
1246     int err = UpdateMemoryPara(size, oldSize);
1247     if (!reply.WriteInt32(err)) {
1248         return E_WRITE_REPLY_ERR;
1249     }
1250     if (!reply.WriteInt32(oldSize)) {
1251         LOGE("StorageManagerStub::HandleUpdateMemoryPara call UpdateMemoryPara failed");
1252         return E_WRITE_REPLY_ERR;
1253     }
1254     return E_OK;
1255 }
1256 
HandleMountDfsDocs(MessageParcel & data,MessageParcel & reply)1257 int32_t StorageManagerStub::HandleMountDfsDocs(MessageParcel &data, MessageParcel &reply)
1258 {
1259     // Only for dfs create device dir and bind mount from DFS Docs.
1260     if (IPCSkeleton::GetCallingUid() != DFS_UID) {
1261         LOGE("HandleMountDfsDocs permissionCheck error, calling uid now is %{public}d, should be DFS_UID: %{public}d",
1262             IPCSkeleton::GetCallingUid(), DFS_UID);
1263         return E_PERMISSION_DENIED;
1264     }
1265 
1266     int32_t userId = data.ReadInt32();
1267     std::string relativePath = data.ReadString();
1268     std::string networkId = data.ReadString();
1269     std::string deviceId = data.ReadString();
1270 
1271     int32_t err = MountDfsDocs(userId, relativePath, networkId, deviceId);
1272     if (!reply.WriteInt32(err)) {
1273         return E_WRITE_REPLY_ERR;
1274     }
1275     return E_OK;
1276 }
1277 
HandleUMountDfsDocs(MessageParcel & data,MessageParcel & reply)1278 int32_t StorageManagerStub::HandleUMountDfsDocs(MessageParcel &data, MessageParcel &reply)
1279 {
1280     // Only for dfs create device dir and bind mount from DFS Docs.
1281     if (IPCSkeleton::GetCallingUid() != DFS_UID) {
1282         LOGE("HandleUMountDfsDocs permissionCheck error, calling uid now is %{public}d, should be DFS_UID: %{public}d",
1283              IPCSkeleton::GetCallingUid(), DFS_UID);
1284         return E_PERMISSION_DENIED;
1285     }
1286 
1287     int32_t userId = data.ReadInt32();
1288     std::string relativePath = data.ReadString();
1289     std::string networkId = data.ReadString();
1290     std::string deviceId = data.ReadString();
1291     int32_t err = UMountDfsDocs(userId, relativePath, networkId, deviceId);
1292     if (!reply.WriteInt32(err)) {
1293         return E_WRITE_REPLY_ERR;
1294     }
1295     return E_OK;
1296 }
1297 
HandleNotifyMtpMount(MessageParcel & data,MessageParcel & reply)1298 int32_t StorageManagerStub::HandleNotifyMtpMount(MessageParcel &data, MessageParcel &reply)
1299 {
1300     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
1301         return E_PERMISSION_DENIED;
1302     }
1303 
1304     std::string id = data.ReadString();
1305     std::string path = data.ReadString();
1306     std::string desc = data.ReadString();
1307     std::string uuid = data.ReadString();
1308     int32_t err = NotifyMtpMounted(id, path, desc, uuid);
1309     if (!reply.WriteInt32(err)) {
1310         LOGE("Write reply error code failed");
1311         return E_WRITE_REPLY_ERR;
1312     }
1313 
1314     LOGI("StorageManagerStub::HandleNotifyMtpMount");
1315     return E_OK;
1316 }
1317 
HandleNotifyMtpUnmount(MessageParcel & data,MessageParcel & reply)1318 int32_t StorageManagerStub::HandleNotifyMtpUnmount(MessageParcel &data, MessageParcel &reply)
1319 {
1320     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
1321         return E_PERMISSION_DENIED;
1322     }
1323 
1324     std::string id = data.ReadString();
1325     std::string path = data.ReadString();
1326     bool isBadRemove = data.ReadBool();
1327     int32_t err = NotifyMtpUnmounted(id, path, isBadRemove);
1328     if (!reply.WriteInt32(err)) {
1329         LOGE("Write reply error code failed");
1330         return E_WRITE_REPLY_ERR;
1331     }
1332 
1333     LOGI("StorageManagerStub::HandleNotifyMtpUnmount");
1334     return E_OK;
1335 }
1336 
HandleMountMediaFuse(MessageParcel & data,MessageParcel & reply)1337 int32_t StorageManagerStub::HandleMountMediaFuse(MessageParcel &data, MessageParcel &reply)
1338 {
1339 #ifdef STORAGE_SERVICE_MEDIA_FUSE
1340     LOGI("StorageManagerStub::HandleMountMediaFuse start.");
1341 
1342     // Only for medialibrary to mount fuse.
1343     std::string bundleName;
1344     int32_t uid = IPCSkeleton::GetCallingUid();
1345     auto bundleMgr = DelayedSingleton<BundleMgrConnector>::GetInstance()->GetBundleMgrProxy();
1346     if (bundleMgr == nullptr) {
1347         LOGE("Connect bundle manager sa proxy failed.");
1348         return E_SERVICE_IS_NULLPTR;
1349     }
1350     if (!bundleMgr->GetBundleNameForUid(uid, bundleName)) {
1351         LOGE("Invoke bundleMgr interface to get bundle name failed.");
1352         return E_BUNDLEMGR_ERROR;
1353     }
1354     if (bundleName != MEDIALIBRARY_BUNDLE_NAME) {
1355         LOGE("permissionCheck error, caller is %{public}s(%{public}d), should be %{public}s",
1356             bundleName.c_str(), uid, MEDIALIBRARY_BUNDLE_NAME.c_str());
1357         return E_PERMISSION_DENIED;
1358     }
1359 
1360     int32_t userId = data.ReadInt32();
1361     int32_t fd = -1;
1362     int32_t ret = MountMediaFuse(userId, fd);
1363     if (!reply.WriteInt32(ret)) {
1364         LOGE("Write reply error code failed");
1365         if (ret == E_OK) {
1366             close(fd);
1367         }
1368         return E_WRITE_REPLY_ERR;
1369     }
1370     if (ret == E_OK) {
1371         if (!reply.WriteFileDescriptor(fd)) {
1372             LOGE("Write reply fd failed");
1373             close(fd);
1374             return E_WRITE_REPLY_ERR;
1375         }
1376         close(fd);
1377     }
1378 #endif
1379     return E_OK;
1380 }
1381 
HandleUMountMediaFuse(MessageParcel & data,MessageParcel & reply)1382 int32_t StorageManagerStub::HandleUMountMediaFuse(MessageParcel &data, MessageParcel &reply)
1383 {
1384 #ifdef STORAGE_SERVICE_MEDIA_FUSE
1385     LOGI("StorageManagerStub::HandleUMountMediaFuse start.");
1386 
1387     // Only for medialibrary to mount fuse.
1388     std::string bundleName;
1389     int32_t uid = IPCSkeleton::GetCallingUid();
1390     auto bundleMgr = DelayedSingleton<BundleMgrConnector>::GetInstance()->GetBundleMgrProxy();
1391     if (bundleMgr == nullptr) {
1392         LOGE("Connect bundle manager sa proxy failed.");
1393         return E_SERVICE_IS_NULLPTR;
1394     }
1395     if (!bundleMgr->GetBundleNameForUid(uid, bundleName)) {
1396         LOGE("Invoke bundleMgr interface to get bundle name failed.");
1397         return E_BUNDLEMGR_ERROR;
1398     }
1399     if (bundleName != MEDIALIBRARY_BUNDLE_NAME) {
1400         LOGE("permissionCheck error, caller is %{public}s(%{public}d), should be %{public}s",
1401             bundleName.c_str(), uid, MEDIALIBRARY_BUNDLE_NAME.c_str());
1402         return E_PERMISSION_DENIED;
1403     }
1404 
1405     int32_t userId = data.ReadInt32();
1406     int32_t ret = UMountMediaFuse(userId);
1407     if (!reply.WriteInt32(ret)) {
1408         return E_WRITE_REPLY_ERR;
1409     }
1410 #endif
1411     return E_OK;
1412 }
1413 
HandleMountFileMgrFuse(MessageParcel & data,MessageParcel & reply)1414 int32_t StorageManagerStub::HandleMountFileMgrFuse(MessageParcel &data, MessageParcel &reply)
1415 {
1416     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
1417         return E_PERMISSION_DENIED;
1418     }
1419     LOGI("StorageManagerStub::HandleMountFileMgrFuse start.");
1420     int32_t userId = data.ReadInt32();
1421     std::string path = data.ReadString();
1422     int32_t fuseFd = -1;
1423     int32_t ret = MountFileMgrFuse(userId, path, fuseFd);
1424     if (!reply.WriteInt32(ret)) {
1425         LOGE("Write reply error code failed");
1426         if (ret == E_OK) {
1427             close(fuseFd);
1428         }
1429         return E_WRITE_REPLY_ERR;
1430     }
1431     if (ret == E_OK) {
1432         if (!reply.WriteFileDescriptor(fuseFd)) {
1433             LOGE("Write reply fuseFd failed");
1434             close(fuseFd);
1435             return E_WRITE_REPLY_ERR;
1436         }
1437         close(fuseFd);
1438     }
1439     return E_OK;
1440 }
1441 
HandleUMountFileMgrFuse(MessageParcel & data,MessageParcel & reply)1442 int32_t StorageManagerStub::HandleUMountFileMgrFuse(MessageParcel &data, MessageParcel &reply)
1443 {
1444     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
1445         return E_PERMISSION_DENIED;
1446     }
1447     LOGI("StorageManagerStub::HandleUMountFileMgrFuse start.");
1448     int32_t userId = data.ReadInt32();
1449     std::string path = data.ReadString();
1450     int32_t ret = UMountFileMgrFuse(userId, path);
1451     if (!reply.WriteInt32(ret)) {
1452         return E_WRITE_REPLY_ERR;
1453     }
1454     return E_OK;
1455 }
1456 } // StorageManager
1457 } // OHOS
1458