• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ipc/storage_daemon_stub.h"
17 
18 #include <cinttypes>
19 #include "ipc/storage_daemon_ipc_interface_code.h"
20 #include "storage_service_errno.h"
21 #include "storage_service_log.h"
22 #include "securec.h"
23 #include "utils/storage_xcollie.h"
24 #include "utils/storage_radar.h"
25 
26 namespace OHOS {
27 namespace StorageDaemon {
28 using namespace std;
29 constexpr unsigned int LOCAL_TIME_OUT_SECONDS = 10;
30 constexpr unsigned int INACTIVE_USER_KEY_OUT_SECONDS = 25;
31 constexpr unsigned int UPDATE_RADAR_STATISTIC_INTERVAL_SECONDS = 300;
32 constexpr unsigned int RADAR_REPORT_STATISTIC_INTERVAL_MINUTES = 1440;
33 constexpr unsigned int USER0ID = 0;
34 constexpr unsigned int USER100ID = 100;
35 constexpr unsigned int RADAR_STATISTIC_THREAD_WAIT_SECONDS = 60;
36 constexpr size_t MAX_IPC_RAW_DATA_SIZE = 128 * 1024 * 1024;
37 
GetData(void * & buffer,size_t size,const void * data)38 static bool GetData(void *&buffer, size_t size, const void *data)
39 {
40     if (data == nullptr) {
41         LOGE("null data");
42         return false;
43     }
44     if (size == 0 || size > MAX_IPC_RAW_DATA_SIZE) {
45         LOGE("size invalid: %{public}zu", size);
46         return false;
47     }
48     buffer = malloc(size);
49     if (buffer == nullptr) {
50         LOGE("malloc buffer failed");
51         return false;
52     }
53     if (memcpy_s(buffer, size, data, size) != E_OK) {
54         free(buffer);
55         LOGE("memcpy failed");
56         return false;
57     }
58     return true;
59 }
60 
ReadBatchUriByRawData(MessageParcel & data,std::vector<std::string> & uriVec)61 static bool ReadBatchUriByRawData(MessageParcel &data, std::vector<std::string> &uriVec)
62 {
63     size_t dataSize = static_cast<size_t>(data.ReadInt32());
64     if (dataSize == 0) {
65         LOGE("parcel no data");
66         return false;
67     }
68 
69     void *buffer = nullptr;
70     if (!GetData(buffer, dataSize, data.ReadRawData(dataSize))) {
71         LOGE("read raw data failed: %{public}zu", dataSize);
72         return false;
73     }
74 
75     MessageParcel tempParcel;
76     if (!tempParcel.ParseFrom(reinterpret_cast<uintptr_t>(buffer), dataSize)) {
77         LOGE("failed to parseFrom");
78         free(buffer);
79         return false;
80     }
81     tempParcel.ReadStringVector(&uriVec);
82     return true;
83 }
84 
ReadBatchUris(MessageParcel & data,std::vector<std::string> & uriVec)85 int32_t ReadBatchUris(MessageParcel &data, std::vector<std::string> &uriVec)
86 {
87     uint32_t size = data.ReadUint32();
88     if (size == 0) {
89         LOGE("out of range: %{public}u", size);
90         return E_PARAMS_INVALID;
91     }
92     if (!ReadBatchUriByRawData(data, uriVec)) {
93         LOGE("read uris failed");
94         return E_WRITE_REPLY_ERR;
95     }
96     return E_OK;
97 }
98 
GetUserStatistics(const uint32_t userId)99 std::map<uint32_t, RadarStatisticInfo>::iterator StorageDaemonStub::GetUserStatistics(const uint32_t userId)
100 {
101     auto it = opStatistics_.find(userId);
102     if (it != opStatistics_.end()) {
103         return it;
104     }
105     RadarStatisticInfo radarInfo = { 0 };
106     return opStatistics_.insert(make_pair(userId, radarInfo)).first;
107 }
108 
GetTempStatistics(std::map<uint32_t,RadarStatisticInfo> & statistics)109 void StorageDaemonStub::GetTempStatistics(std::map<uint32_t, RadarStatisticInfo> &statistics)
110 {
111     std::lock_guard<std::mutex> lock(mutex_);
112     statistics.insert(opStatistics_.begin(), opStatistics_.end());
113     opStatistics_.clear();
114 }
115 
StorageRadarThd(void)116 void StorageDaemonStub::StorageRadarThd(void)
117 {
118     // report radar statistics when restart
119     std::unique_lock<std::mutex> lock(onRadarReportLock_);
120     if (execRadarReportCon_.wait_for(lock, std::chrono::seconds(RADAR_STATISTIC_THREAD_WAIT_SECONDS),
121         [this] { return this->stopRadarReport_.load(); })) {
122         LOGI("Storage statistic radar exit.");
123         return;
124     }
125     lock.unlock();
126     LOGI("Storage statistic thread start.");
127     StorageStatisticRadar::GetInstance().CreateStatisticFile();
128     std::map<uint32_t, RadarStatisticInfo> opStatisticsTemp;
129     StorageStatisticRadar::GetInstance().ReadStatisticFile(opStatisticsTemp);
130     if (!opStatisticsTemp.empty()) {
131         for (auto ele : opStatisticsTemp) {
132             StorageService::StorageRadar::ReportStatistics(ele.first, ele.second);
133         }
134         StorageStatisticRadar::GetInstance().CleanStatisticFile();
135     }
136     lastRadarReportTime_ = std::chrono::system_clock::now();
137     while (!stopRadarReport_.load()) {
138         std::unique_lock<std::mutex> lock(onRadarReportLock_);
139         if (execRadarReportCon_.wait_for(lock, std::chrono::seconds(UPDATE_RADAR_STATISTIC_INTERVAL_SECONDS),
140             [this] { return this->stopRadarReport_.load(); })) {
141             LOGI("Storage statistic radar exit.");
142             return;
143         }
144         std::chrono::time_point<std::chrono::system_clock> nowTime = std::chrono::system_clock::now();
145         int64_t intervalMinutes =
146             std::chrono::duration_cast<std::chrono::minutes>(nowTime - lastRadarReportTime_).count();
147         if ((intervalMinutes > RADAR_REPORT_STATISTIC_INTERVAL_MINUTES) && !opStatistics_.empty()) {
148             LOGI("Storage statistic report, intervalMinutes:%{public}" PRId64, intervalMinutes);
149             opStatisticsTemp.clear();
150             GetTempStatistics(opStatisticsTemp);
151             for (auto ele : opStatisticsTemp) {
152                 StorageService::StorageRadar::ReportStatistics(ele.first, ele.second);
153             }
154             lastRadarReportTime_ = std::chrono::system_clock::now();
155             StorageStatisticRadar::GetInstance().CleanStatisticFile();
156             continue;
157         }
158         if (!isNeedUpdateRadarFile_) {
159             LOGI("Storage statistic not need update.");
160             continue;
161         }
162         LOGI("Storage statistic update, intervalMinutes:%{public}" PRId64, intervalMinutes);
163         isNeedUpdateRadarFile_ = false;
164         StorageStatisticRadar::GetInstance().UpdateStatisticFile(opStatistics_);
165     }
166 }
167 
StorageDaemonStub()168 StorageDaemonStub::StorageDaemonStub()
169 {
170     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::SHUTDOWN)] =
171         &StorageDaemonStub::HandleShutdown;
172     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::CHECK)] =
173         &StorageDaemonStub::HandleCheck;
174     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT)] =
175         &StorageDaemonStub::HandleMount;
176     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT)] =
177         &StorageDaemonStub::HandleUMount;
178     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::PARTITION)] =
179         &StorageDaemonStub::HandlePartition;
180     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::FORMAT)] =
181         &StorageDaemonStub::HandleFormat;
182     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC)] =
183         &StorageDaemonStub::HandleSetVolDesc;
184     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS)] =
185         &StorageDaemonStub::HandlePrepareUserDirs;
186     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS)] =
187         &StorageDaemonStub::HandleDestroyUserDirs;
188     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::START_USER)] =
189         &StorageDaemonStub::HandleStartUser;
190     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::STOP_USER)] =
191         &StorageDaemonStub::HandleStopUser;
192     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY)] =
193         &StorageDaemonStub::HandleInitGlobalKey;
194     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS)] =
195         &StorageDaemonStub::HandleInitGlobalUserKeys;
196     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS)] =
197         &StorageDaemonStub::HandleGenerateUserKeys;
198     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS)] =
199         &StorageDaemonStub::HandleDeleteUserKeys;
200     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH)] =
201         &StorageDaemonStub::HandleUpdateUserAuth;
202     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH_RECOVER_KEY)] =
203         &StorageDaemonStub::HandleUpdateUseAuthWithRecoveryKey;
204     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY)] =
205         &StorageDaemonStub::HandleActiveUserKey;
206     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY)] =
207         &StorageDaemonStub::HandleInactiveUserKey;
208     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_USER_SCREEN)] =
209         &StorageDaemonStub::HandleLockUserScreen;
210     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UNLOCK_USER_SCREEN)] =
211         &StorageDaemonStub::HandleUnlockUserScreen;
212     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_SCREEN_STATUS)] =
213         &StorageDaemonStub::HandleGetLockScreenStatus;
214     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT)] =
215         &StorageDaemonStub::HandleUpdateKeyContext;
216     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_CRYPTO_PATH_AGAIN)] =
217         &StorageDaemonStub::HandleMountCryptoPathAgain;
218     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE)] =
219         &StorageDaemonStub::HandleCreateShareFile;
220     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE)] =
221         &StorageDaemonStub::HandleDeleteShareFile;
222     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA)] =
223         &StorageDaemonStub::HandleSetBundleQuota;
224     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_SPACE)] =
225         &StorageDaemonStub::HandleGetOccupiedSpace;
226     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_MEM_PARA)] =
227         &StorageDaemonStub::HandleUpdateMemoryPara;
228     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_BUNDLE_STATS_INCREASE)] =
229         &StorageDaemonStub::HandleGetBundleStatsForIncrease;
230     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GENERATE_APP_KEY)] =
231         &StorageDaemonStub::HandleGenerateAppkey;
232     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_APP_KEY)] =
233         &StorageDaemonStub::HandleDeleteAppkey;
234     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_DFS_DOCS)] =
235         &StorageDaemonStub::HandleMountDfsDocs;
236     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS)] =
237         &StorageDaemonStub::HandleUMountDfsDocs;
238     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS)] =
239         &StorageDaemonStub::HandleGetFileEncryptStatus;
240     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_RECOVER_KEY)] =
241         &StorageDaemonStub::HandleCreateRecoverKey;
242     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_RECOVER_KEY)] =
243         &StorageDaemonStub::HandleSetRecoverKey;
244     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_MEDIA_FUSE)] =
245         &StorageDaemonStub::HandleMountMediaFuse;
246     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_MEDIA_FUSE)] =
247         &StorageDaemonStub::HandleUMountMediaFuse;
248     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_USER_NEED_ACTIVE_STATUS)] =
249         &StorageDaemonStub::HandleGetUserNeedActiveStatus;
250     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_FILE_MGR_FUSE)] =
251             &StorageDaemonStub::HandleMountFileMgrFuse;
252     opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_FILE_MGR_FUSE)] =
253             &StorageDaemonStub::HandleUMountFileMgrFuse;
254     callRadarStatisticReportThread_ = std::thread([this]() { StorageRadarThd(); });
255 }
256 
~StorageDaemonStub()257 StorageDaemonStub::~StorageDaemonStub()
258 {
259     std::unique_lock<std::mutex> lock(onRadarReportLock_);
260     stopRadarReport_ = true;
261     execRadarReportCon_.notify_one();
262     lock.unlock();
263     if (callRadarStatisticReportThread_.joinable()) {
264         callRadarStatisticReportThread_.join();
265     }
266 }
267 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)268 int32_t StorageDaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
269                                            MessageParcel &reply, MessageOption &option)
270 {
271     if (data.ReadInterfaceToken() != GetDescriptor()) {
272         return E_PERMISSION_DENIED;
273     }
274     switch (code) {
275         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SHUTDOWN):
276         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CHECK):
277         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT):
278         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT):
279         case static_cast<uint32_t>(StorageDaemonInterfaceCode::PARTITION):
280         case static_cast<uint32_t>(StorageDaemonInterfaceCode::FORMAT):
281         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC):
282             return OnRemoteRequestForBase(code, data, reply);
283         case static_cast<uint32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS):
284         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS):
285         case static_cast<uint32_t>(StorageDaemonInterfaceCode::START_USER):
286         case static_cast<uint32_t>(StorageDaemonInterfaceCode::STOP_USER):
287         case static_cast<uint32_t>(StorageDaemonInterfaceCode::COMPLETE_ADD_USER):
288         case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY):
289         case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS):
290         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS):
291         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS):
292         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH):
293         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH_RECOVER_KEY):
294         case static_cast<uint32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY):
295         case static_cast<uint32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY):
296         case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_USER_SCREEN):
297         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UNLOCK_USER_SCREEN):
298         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_RECOVER_KEY):
299         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_RECOVER_KEY):
300             return OnRemoteRequestForUser(code, data, reply);
301         case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_SCREEN_STATUS):
302         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT):
303         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_CRYPTO_PATH_AGAIN):
304         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE):
305         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE):
306         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA):
307         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_SPACE):
308         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_MEM_PARA):
309         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_BUNDLE_STATS_INCREASE):
310         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_DFS_DOCS):
311         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS):
312         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GENERATE_APP_KEY):
313         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_APP_KEY):
314         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS):
315         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_MEDIA_FUSE):
316         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_MEDIA_FUSE):
317         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_USER_NEED_ACTIVE_STATUS):
318         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_FILE_MGR_FUSE):
319         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_FILE_MGR_FUSE):
320             return OnRemoteRequestForApp(code, data, reply);
321         default:
322             LOGE("Cannot response request %{public}d: unknown tranction", code);
323             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
324     }
325 }
326 
OnRemoteRequestForBase(uint32_t code,MessageParcel & data,MessageParcel & reply)327 int32_t StorageDaemonStub::OnRemoteRequestForBase(uint32_t code, MessageParcel &data, MessageParcel &reply)
328 {
329     switch (code) {
330         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SHUTDOWN):
331             return HandleShutdown(data, reply);
332         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CHECK):
333             return HandleCheck(data, reply);
334         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT):
335             return HandleMount(data, reply);
336         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT):
337             return HandleUMount(data, reply);
338         case static_cast<uint32_t>(StorageDaemonInterfaceCode::PARTITION):
339             return HandlePartition(data, reply);
340         case static_cast<uint32_t>(StorageDaemonInterfaceCode::FORMAT):
341             return HandleFormat(data, reply);
342         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC):
343             return HandleSetVolDesc(data, reply);
344         default:
345             LOGE("Cannot response request %{public}d: unknown tranction", code);
346             return E_SYS_KERNEL_ERR;
347     }
348 }
OnRemoteRequestForUser(uint32_t code,MessageParcel & data,MessageParcel & reply)349 int32_t StorageDaemonStub::OnRemoteRequestForUser(uint32_t code, MessageParcel &data, MessageParcel &reply)
350 {
351     switch (code) {
352         case static_cast<uint32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS):
353             return HandlePrepareUserDirs(data, reply);
354         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS):
355             return HandleDestroyUserDirs(data, reply);
356         case static_cast<uint32_t>(StorageDaemonInterfaceCode::START_USER):
357             return HandleStartUser(data, reply);
358         case static_cast<uint32_t>(StorageDaemonInterfaceCode::STOP_USER):
359             return HandleStopUser(data, reply);
360         case static_cast<uint32_t>(StorageDaemonInterfaceCode::COMPLETE_ADD_USER):
361             return HandleCompleteAddUser(data, reply);
362         case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY):
363             return HandleInitGlobalKey(data, reply);
364         case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS):
365             return HandleInitGlobalUserKeys(data, reply);
366         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS):
367             return HandleGenerateUserKeys(data, reply);
368         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS):
369             return HandleDeleteUserKeys(data, reply);
370         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH):
371             return HandleUpdateUserAuth(data, reply);
372         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH_RECOVER_KEY):
373             return HandleUpdateUseAuthWithRecoveryKey(data, reply);
374         case static_cast<uint32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY):
375             return HandleActiveUserKey(data, reply);
376         case static_cast<uint32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY):
377             return HandleInactiveUserKey(data, reply);
378         case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_USER_SCREEN):
379             return HandleLockUserScreen(data, reply);
380         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UNLOCK_USER_SCREEN):
381             return HandleUnlockUserScreen(data, reply);
382         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_RECOVER_KEY):
383             return HandleCreateRecoverKey(data, reply);
384         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_RECOVER_KEY):
385             return HandleSetRecoverKey(data, reply);
386         default:
387             LOGE("Cannot response request %{public}d: unknown tranction", code);
388             return E_SYS_KERNEL_ERR;
389     }
390 }
OnRemoteRequestForApp(uint32_t code,MessageParcel & data,MessageParcel & reply)391 int32_t StorageDaemonStub::OnRemoteRequestForApp(uint32_t code, MessageParcel &data, MessageParcel &reply)
392 {
393     switch (code) {
394         case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_SCREEN_STATUS):
395             return HandleGetLockScreenStatus(data, reply);
396         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT):
397             return HandleUpdateKeyContext(data, reply);
398         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_CRYPTO_PATH_AGAIN):
399             return HandleMountCryptoPathAgain(data, reply);
400         case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE):
401             return HandleCreateShareFile(data, reply);
402         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE):
403             return HandleDeleteShareFile(data, reply);
404         case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA):
405             return HandleSetBundleQuota(data, reply);
406         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_SPACE):
407             return HandleGetOccupiedSpace(data, reply);
408         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_MEM_PARA):
409             return HandleUpdateMemoryPara(data, reply);
410         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_BUNDLE_STATS_INCREASE):
411             return HandleGetBundleStatsForIncrease(data, reply);
412         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_DFS_DOCS):
413             return HandleMountDfsDocs(data, reply);
414         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS):
415             return HandleUMountDfsDocs(data, reply);
416         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GENERATE_APP_KEY):
417             return HandleGenerateAppkey(data, reply);
418         case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_APP_KEY):
419             return HandleDeleteAppkey(data, reply);
420         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS):
421             return HandleGetFileEncryptStatus(data, reply);
422         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_MEDIA_FUSE):
423             return HandleMountMediaFuse(data, reply);
424         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_MEDIA_FUSE):
425             return HandleUMountMediaFuse(data, reply);
426         case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_USER_NEED_ACTIVE_STATUS):
427             return HandleGetUserNeedActiveStatus(data, reply);
428         case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_FILE_MGR_FUSE):
429             return HandleMountFileMgrFuse(data, reply);
430         case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_FILE_MGR_FUSE):
431             return HandleUMountFileMgrFuse(data, reply);
432         default:
433             LOGE("Cannot response request %{public}d: unknown tranction", code);
434             return E_SYS_KERNEL_ERR;
435     }
436 }
437 
HandleShutdown(MessageParcel & data,MessageParcel & reply)438 int32_t StorageDaemonStub::HandleShutdown(MessageParcel &data, MessageParcel &reply)
439 {
440     Shutdown();
441     return E_OK;
442 }
443 
HandleMount(MessageParcel & data,MessageParcel & reply)444 int32_t StorageDaemonStub::HandleMount(MessageParcel &data, MessageParcel &reply)
445 {
446     std::string volId = data.ReadString();
447     uint32_t flags = data.ReadUint32();
448 
449     int err = Mount(volId, flags);
450     if (!reply.WriteInt32(err)) {
451         return  E_WRITE_REPLY_ERR;
452     }
453 
454     return E_OK;
455 }
456 
HandleUMount(MessageParcel & data,MessageParcel & reply)457 int32_t StorageDaemonStub::HandleUMount(MessageParcel &data, MessageParcel &reply)
458 {
459     std::string volId = data.ReadString();
460 
461     int err = UMount(volId);
462     if (!reply.WriteInt32(err)) {
463         return  E_WRITE_REPLY_ERR;
464     }
465 
466     return E_OK;
467 }
468 
HandleCheck(MessageParcel & data,MessageParcel & reply)469 int32_t StorageDaemonStub::HandleCheck(MessageParcel &data, MessageParcel &reply)
470 {
471     std::string volId = data.ReadString();
472 
473     int err = Check(volId);
474     if (!reply.WriteInt32(err)) {
475         return  E_WRITE_REPLY_ERR;
476     }
477 
478     return E_OK;
479 }
480 
HandleFormat(MessageParcel & data,MessageParcel & reply)481 int32_t StorageDaemonStub::HandleFormat(MessageParcel &data, MessageParcel &reply)
482 {
483     std::string volId = data.ReadString();
484     std::string fsType = data.ReadString();
485 
486     int err = Format(volId, fsType);
487     if (!reply.WriteInt32(err)) {
488         return  E_WRITE_REPLY_ERR;
489     }
490 
491     return E_OK;
492 }
493 
HandlePartition(MessageParcel & data,MessageParcel & reply)494 int32_t StorageDaemonStub::HandlePartition(MessageParcel &data, MessageParcel &reply)
495 {
496     std::string volId = data.ReadString();
497     int32_t type = data.ReadInt32();
498 
499     int err = Partition(volId, type);
500     if (!reply.WriteInt32(err)) {
501         return  E_WRITE_REPLY_ERR;
502     }
503 
504     return E_OK;
505 }
506 
HandleSetVolDesc(MessageParcel & data,MessageParcel & reply)507 int32_t StorageDaemonStub::HandleSetVolDesc(MessageParcel &data, MessageParcel &reply)
508 {
509     std::string volId = data.ReadString();
510     std::string description = data.ReadString();
511 
512     int err = SetVolumeDescription(volId, description);
513     if (!reply.WriteInt32(err)) {
514         return E_WRITE_REPLY_ERR;
515     }
516 
517     return E_OK;
518 }
519 
HandlePrepareUserDirs(MessageParcel & data,MessageParcel & reply)520 int32_t StorageDaemonStub::HandlePrepareUserDirs(MessageParcel &data, MessageParcel &reply)
521 {
522     std::lock_guard<std::mutex> lock(mutex_);
523     int32_t userId = data.ReadInt32();
524     uint32_t flags = data.ReadUint32();
525     auto it = GetUserStatistics(userId);
526     isNeedUpdateRadarFile_ = true;
527     int err = PrepareUserDirs(userId, flags);
528     if (err != E_OK) {
529         it->second.userAddFailCount++;
530     } else {
531         it->second.userAddSuccCount++;
532     }
533     if (!reply.WriteInt32(err)) {
534         return E_WRITE_REPLY_ERR;
535     }
536     return E_OK;
537 }
538 
HandleDestroyUserDirs(MessageParcel & data,MessageParcel & reply)539 int32_t StorageDaemonStub::HandleDestroyUserDirs(MessageParcel &data, MessageParcel &reply)
540 {
541     std::lock_guard<std::mutex> lock(mutex_);
542     int32_t userId = data.ReadInt32();
543     uint32_t flags = data.ReadUint32();
544     auto it = GetUserStatistics(userId);
545     isNeedUpdateRadarFile_ = true;
546     int err = DestroyUserDirs(userId, flags);
547     if (err != E_OK) {
548         it->second.userRemoveFailCount++;
549     } else {
550         it->second.userRemoveSuccCount++;
551     }
552     if (!reply.WriteInt32(err)) {
553         return E_WRITE_REPLY_ERR;
554     }
555     return E_OK;
556 }
557 
HandleStartUser(MessageParcel & data,MessageParcel & reply)558 int32_t StorageDaemonStub::HandleStartUser(MessageParcel &data, MessageParcel &reply)
559 {
560     int32_t userId = data.ReadInt32();
561     auto it = GetUserStatistics(userId);
562     isNeedUpdateRadarFile_ = true;
563     int32_t err = StartUser(userId);
564     if (err != E_OK) {
565         it->second.userStartFailCount++;
566     } else {
567         it->second.userStartSuccCount++;
568     }
569     if (!reply.WriteInt32(err)) {
570         return E_WRITE_REPLY_ERR;
571     }
572     return E_OK;
573 }
574 
HandleStopUser(MessageParcel & data,MessageParcel & reply)575 int32_t StorageDaemonStub::HandleStopUser(MessageParcel &data, MessageParcel &reply)
576 {
577     int32_t userId = data.ReadInt32();
578     auto it = GetUserStatistics(userId);
579     isNeedUpdateRadarFile_ = true;
580     int32_t err = StopUser(userId);
581     if (err != E_OK) {
582         it->second.userStopFailCount++;
583     } else {
584         it->second.userStopSuccCount++;
585     }
586     if (!reply.WriteInt32(err)) {
587         return E_WRITE_REPLY_ERR;
588     }
589     return E_OK;
590 }
591 
HandleCompleteAddUser(MessageParcel & data,MessageParcel & reply)592 int32_t StorageDaemonStub::HandleCompleteAddUser(MessageParcel &data, MessageParcel &reply)
593 {
594     int32_t userId = data.ReadInt32();
595 
596     int32_t err = CompleteAddUser(userId);
597     if (!reply.WriteInt32(err)) {
598         return E_WRITE_REPLY_ERR;
599     }
600 
601     return E_OK;
602 }
603 
HandleInitGlobalKey(MessageParcel & data,MessageParcel & reply)604 int32_t StorageDaemonStub::HandleInitGlobalKey(MessageParcel &data, MessageParcel &reply)
605 {
606     std::lock_guard<std::mutex> lock(mutex_);
607     auto it = GetUserStatistics(USER0ID);
608     isNeedUpdateRadarFile_ = true;
609     int err = InitGlobalKey();
610     if (err != E_OK) {
611         it->second.keyLoadFailCount++;
612     } else {
613         it->second.keyLoadSuccCount++;
614     }
615     if (!reply.WriteInt32(err)) {
616         return E_WRITE_REPLY_ERR;
617     }
618     return E_OK;
619 }
620 
HandleInitGlobalUserKeys(MessageParcel & data,MessageParcel & reply)621 int32_t StorageDaemonStub::HandleInitGlobalUserKeys(MessageParcel &data, MessageParcel &reply)
622 {
623     LOGI("Stub_InitGlobalUserKeys start.");
624     std::lock_guard<std::mutex> lock(mutex_);
625     auto it = GetUserStatistics(USER100ID);
626     isNeedUpdateRadarFile_ = true;
627     int err = InitGlobalUserKeys();
628     if (err != E_OK) {
629         it->second.keyLoadFailCount++;
630     } else {
631         it->second.keyLoadSuccCount++;
632     }
633     if (!reply.WriteInt32(err)) {
634         return E_WRITE_REPLY_ERR;
635     }
636     return E_OK;
637 }
638 
HandleGenerateUserKeys(MessageParcel & data,MessageParcel & reply)639 int32_t StorageDaemonStub::HandleGenerateUserKeys(MessageParcel &data, MessageParcel &reply)
640 {
641     uint32_t userId = data.ReadUint32();
642     uint32_t flags = data.ReadUint32();
643     int timerId = StorageXCollie::SetTimer("storage:GenerateUserKeys", LOCAL_TIME_OUT_SECONDS);
644     int err = GenerateUserKeys(userId, flags);
645     StorageXCollie::CancelTimer(timerId);
646     if (!reply.WriteInt32(err)) {
647         return E_WRITE_REPLY_ERR;
648     }
649 
650     return E_OK;
651 }
652 
HandleDeleteUserKeys(MessageParcel & data,MessageParcel & reply)653 int32_t StorageDaemonStub::HandleDeleteUserKeys(MessageParcel &data, MessageParcel &reply)
654 {
655     uint32_t userId = data.ReadUint32();
656     int timerId = StorageXCollie::SetTimer("storage:DeleteUserKeys", LOCAL_TIME_OUT_SECONDS);
657     int err = DeleteUserKeys(userId);
658     StorageXCollie::CancelTimer(timerId);
659     if (!reply.WriteInt32(err)) {
660         return E_WRITE_REPLY_ERR;
661     }
662 
663     return E_OK;
664 }
665 
HandleUpdateUserAuth(MessageParcel & data,MessageParcel & reply)666 int32_t StorageDaemonStub::HandleUpdateUserAuth(MessageParcel &data, MessageParcel &reply)
667 {
668     uint32_t userId = data.ReadUint32();
669     uint64_t secureUid = data.ReadUint64();
670 
671     std::vector<uint8_t> token;
672     std::vector<uint8_t> oldSecret;
673     std::vector<uint8_t> newSecret;
674     data.ReadUInt8Vector(&token);
675     data.ReadUInt8Vector(&oldSecret);
676     data.ReadUInt8Vector(&newSecret);
677 
678     int timerId = StorageXCollie::SetTimer("storage:UpdateUserAuth", LOCAL_TIME_OUT_SECONDS);
679     std::lock_guard<std::mutex> lock(mutex_);
680     int err = UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
681     StorageXCollie::CancelTimer(timerId);
682     if (!reply.WriteInt32(err)) {
683         return E_WRITE_REPLY_ERR;
684     }
685 
686     return E_OK;
687 }
688 
HandleUpdateUseAuthWithRecoveryKey(MessageParcel & data,MessageParcel & reply)689 int32_t StorageDaemonStub::HandleUpdateUseAuthWithRecoveryKey(MessageParcel &data, MessageParcel &reply)
690 {
691     uint32_t userId = data.ReadUint32();
692     uint64_t secureUid = data.ReadUint64();
693 
694     std::vector<uint8_t> token;
695     std::vector<uint8_t> newSecret;
696     data.ReadUInt8Vector(&token);
697     data.ReadUInt8Vector(&newSecret);
698     std::vector<std::vector<uint8_t>> plainText;
699     const int CKEY_NUMS = 6;
700     for (uint32_t i = 0; i < CKEY_NUMS; i++) {
701         std::vector<uint8_t> iv;
702         data.ReadUInt8Vector(&iv);
703         plainText.push_back(iv);
704     }
705 
706     int err = UpdateUseAuthWithRecoveryKey(token, newSecret, secureUid, userId, plainText);
707     if (!reply.WriteInt32(err)) {
708         return E_WRITE_REPLY_ERR;
709     }
710 
711     return E_OK;
712 }
713 
HandleActiveUserKey(MessageParcel & data,MessageParcel & reply)714 int32_t StorageDaemonStub::HandleActiveUserKey(MessageParcel &data, MessageParcel &reply)
715 {
716     uint32_t userId = data.ReadUint32();
717 
718     std::vector<uint8_t> token;
719     std::vector<uint8_t> secret;
720     data.ReadUInt8Vector(&token);
721     data.ReadUInt8Vector(&secret);
722 
723     int timerId = StorageXCollie::SetTimer("storage:ActiveUserKey", LOCAL_TIME_OUT_SECONDS);
724     std::lock_guard<std::mutex> lock(mutex_);
725     auto it = GetUserStatistics(userId);
726     isNeedUpdateRadarFile_ = true;
727     int err = ActiveUserKey(userId, token, secret);
728     StorageXCollie::CancelTimer(timerId);
729     if ((err == E_OK) || ((err == E_ACTIVE_EL2_FAILED) && token.empty() && secret.empty())) {
730         it->second.keyLoadSuccCount++;
731     } else {
732         it->second.keyLoadFailCount++;
733     }
734     if (!reply.WriteInt32(err)) {
735         return E_WRITE_REPLY_ERR;
736     }
737     return E_OK;
738 }
739 
HandleInactiveUserKey(MessageParcel & data,MessageParcel & reply)740 int32_t StorageDaemonStub::HandleInactiveUserKey(MessageParcel &data, MessageParcel &reply)
741 {
742     uint32_t userId = data.ReadUint32();
743 
744     int timerId = StorageXCollie::SetTimer("storage:InactiveUserKey", INACTIVE_USER_KEY_OUT_SECONDS);
745     std::lock_guard<std::mutex> lock(mutex_);
746     auto it = GetUserStatistics(userId);
747     isNeedUpdateRadarFile_ = true;
748     int err = InactiveUserKey(userId);
749     StorageXCollie::CancelTimer(timerId);
750     if (err != E_OK) {
751         it->second.keyUnloadFailCount++;
752     } else {
753         it->second.keyUnloadSuccCount++;
754     }
755     if (!reply.WriteInt32(err)) {
756         return E_WRITE_REPLY_ERR;
757     }
758     return E_OK;
759 }
760 
HandleLockUserScreen(MessageParcel & data,MessageParcel & reply)761 int32_t StorageDaemonStub::HandleLockUserScreen(MessageParcel &data, MessageParcel &reply)
762 {
763     uint32_t userId = data.ReadUint32();
764 
765     int timerId = StorageXCollie::SetTimer("storage:LockUserScreen", LOCAL_TIME_OUT_SECONDS);
766     std::lock_guard<std::mutex> lock(mutex_);
767     auto it = GetUserStatistics(userId);
768     isNeedUpdateRadarFile_ = true;
769     int err = LockUserScreen(userId);
770     StorageXCollie::CancelTimer(timerId);
771     if (err != E_OK) {
772         it->second.keyUnloadFailCount++;
773     } else {
774         it->second.keyUnloadSuccCount++;
775     }
776     if (!reply.WriteInt32(err)) {
777         return E_WRITE_REPLY_ERR;
778     }
779     return E_OK;
780 }
781 
HandleUnlockUserScreen(MessageParcel & data,MessageParcel & reply)782 int32_t StorageDaemonStub::HandleUnlockUserScreen(MessageParcel &data, MessageParcel &reply)
783 {
784     uint32_t userId = data.ReadUint32();
785 
786     std::vector<uint8_t> token;
787     std::vector<uint8_t> secret;
788     data.ReadUInt8Vector(&token);
789     data.ReadUInt8Vector(&secret);
790 
791     int timerId = StorageXCollie::SetTimer("storage:UnlockUserScreen", LOCAL_TIME_OUT_SECONDS);
792     std::lock_guard<std::mutex> lock(mutex_);
793     auto it = GetUserStatistics(userId);
794     isNeedUpdateRadarFile_ = true;
795     int err = UnlockUserScreen(userId, token, secret);
796     StorageXCollie::CancelTimer(timerId);
797     if (err != E_OK) {
798         it->second.keyLoadFailCount++;
799     } else {
800         it->second.keyLoadSuccCount++;
801     }
802     if (!reply.WriteInt32(err)) {
803         return E_WRITE_REPLY_ERR;
804     }
805     return E_OK;
806 }
807 
HandleGetLockScreenStatus(MessageParcel & data,MessageParcel & reply)808 int32_t StorageDaemonStub::HandleGetLockScreenStatus(MessageParcel &data, MessageParcel &reply)
809 {
810     uint32_t userId = data.ReadUint32();
811     bool lockScreenStatus = false;
812     int timerId = StorageXCollie::SetTimer("storage:GetLockScreenStatus", LOCAL_TIME_OUT_SECONDS);
813     std::lock_guard<std::mutex> lock(mutex_);
814     int err = GetLockScreenStatus(userId, lockScreenStatus);
815     StorageXCollie::CancelTimer(timerId);
816     if (!reply.WriteBool(lockScreenStatus)) {
817         return E_WRITE_REPLY_ERR;
818     }
819     if (!reply.WriteInt32(err)) {
820         return E_WRITE_REPLY_ERR;
821     }
822 
823     return E_OK;
824 }
825 
HandleGenerateAppkey(MessageParcel & data,MessageParcel & reply)826 int32_t StorageDaemonStub::HandleGenerateAppkey(MessageParcel &data, MessageParcel &reply)
827 {
828     uint32_t userId = data.ReadUint32();
829     uint32_t hashId = data.ReadUint32();
830     std::string keyId;
831     int timerId = StorageXCollie::SetTimer("storage:GenerateAppkey", LOCAL_TIME_OUT_SECONDS);
832     int err = GenerateAppkey(userId, hashId, keyId);
833     StorageXCollie::CancelTimer(timerId);
834     if (!reply.WriteString(keyId)) {
835         return E_WRITE_REPLY_ERR;
836     }
837     if (!reply.WriteInt32(err)) {
838         return E_WRITE_REPLY_ERR;
839     }
840 
841     return E_OK;
842 }
843 
HandleDeleteAppkey(MessageParcel & data,MessageParcel & reply)844 int32_t StorageDaemonStub::HandleDeleteAppkey(MessageParcel &data, MessageParcel &reply)
845 {
846     uint32_t userId = data.ReadUint32();
847     std::string keyId = data.ReadString();
848     int timerId = StorageXCollie::SetTimer("storage:DeleteAppkey", LOCAL_TIME_OUT_SECONDS);
849     std::lock_guard<std::mutex> lock(mutex_);
850     int err = DeleteAppkey(userId, keyId);
851     StorageXCollie::CancelTimer(timerId);
852     if (!reply.WriteInt32(err)) {
853         return E_WRITE_REPLY_ERR;
854     }
855 
856     return E_OK;
857 }
858 
HandleCreateRecoverKey(MessageParcel & data,MessageParcel & reply)859 int32_t StorageDaemonStub::HandleCreateRecoverKey(MessageParcel &data, MessageParcel &reply)
860 {
861     uint32_t userId = data.ReadUint32();
862     uint32_t userType = data.ReadUint32();
863 
864     std::vector<uint8_t> token;
865     std::vector<uint8_t> secret;
866     data.ReadUInt8Vector(&token);
867     data.ReadUInt8Vector(&secret);
868     int err = CreateRecoverKey(userId, userType, token, secret);
869     if (!reply.WriteInt32(err)) {
870         return E_WRITE_REPLY_ERR;
871     }
872 
873     return E_OK;
874 }
875 
HandleSetRecoverKey(MessageParcel & data,MessageParcel & reply)876 int32_t StorageDaemonStub::HandleSetRecoverKey(MessageParcel &data, MessageParcel &reply)
877 {
878     std::vector<uint8_t> key;
879     data.ReadUInt8Vector(&key);
880     int err = SetRecoverKey(key);
881     if (!reply.WriteInt32(err)) {
882         return E_WRITE_REPLY_ERR;
883     }
884 
885     return E_OK;
886 }
887 
HandleUpdateKeyContext(MessageParcel & data,MessageParcel & reply)888 int32_t StorageDaemonStub::HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply)
889 {
890     uint32_t userId = data.ReadUint32();
891     bool needRemoveTmpKey = data.ReadBool();
892 
893     int timerId = StorageXCollie::SetTimer("storage:UpdateKeyContext", LOCAL_TIME_OUT_SECONDS);
894     std::lock_guard<std::mutex> lock(mutex_);
895     int err = UpdateKeyContext(userId, needRemoveTmpKey);
896     StorageXCollie::CancelTimer(timerId);
897     if (!reply.WriteInt32(err)) {
898         return E_WRITE_REPLY_ERR;
899     }
900 
901     return E_OK;
902 }
903 
HandleMountCryptoPathAgain(MessageParcel & data,MessageParcel & reply)904 int32_t StorageDaemonStub::HandleMountCryptoPathAgain(MessageParcel &data, MessageParcel &reply)
905 {
906     uint32_t userId = data.ReadUint32();
907     int32_t err = MountCryptoPathAgain(userId);
908     if (!reply.WriteInt32(err)) {
909         return E_WRITE_REPLY_ERR;
910     }
911     return E_OK;
912 }
913 
HandleCreateShareFile(MessageParcel & data,MessageParcel & reply)914 int32_t StorageDaemonStub::HandleCreateShareFile(MessageParcel &data, MessageParcel &reply)
915 {
916     std::vector<std::string> uriList;
917     auto ret = ReadBatchUris(data, uriList);
918     if (ret != E_OK) {
919         return ret;
920     }
921     uint32_t tokenId = data.ReadUint32();
922     uint32_t flag = data.ReadUint32();
923     std::vector<int32_t> retList = CreateShareFile(uriList, tokenId, flag);
924     if (!reply.WriteInt32Vector(retList)) {
925         return E_WRITE_REPLY_ERR;
926     }
927     return E_OK;
928 }
929 
HandleDeleteShareFile(MessageParcel & data,MessageParcel & reply)930 int32_t StorageDaemonStub::HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply)
931 {
932     uint32_t tokenId = data.ReadUint32();
933     std::vector<std::string> uriList;
934     auto ret = ReadBatchUris(data, uriList);
935     if (ret != E_OK) {
936         return ret;
937     }
938     int err = DeleteShareFile(tokenId, uriList);
939     if (!reply.WriteInt32(err)) {
940         return E_WRITE_REPLY_ERR;
941     }
942     return E_OK;
943 }
944 
HandleSetBundleQuota(MessageParcel & data,MessageParcel & reply)945 int32_t StorageDaemonStub::HandleSetBundleQuota(MessageParcel &data, MessageParcel &reply)
946 {
947     std::string bundleName = data.ReadString();
948     int32_t uid = data.ReadInt32();
949     std::string bundleDataDirPath = data.ReadString();
950     int32_t limitSizeMb = data.ReadInt32();
951     int err = SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb);
952     if (!reply.WriteInt32(err)) {
953         return E_WRITE_REPLY_ERR;
954     }
955     return E_OK;
956 }
957 
HandleGetOccupiedSpace(MessageParcel & data,MessageParcel & reply)958 int32_t StorageDaemonStub::HandleGetOccupiedSpace(MessageParcel &data, MessageParcel &reply)
959 {
960     int32_t idType = data.ReadInt32();
961     int32_t id = data.ReadInt32();
962     int64_t size = 0;
963     int err = GetOccupiedSpace(idType, id, size);
964     if (!reply.WriteInt32(err)) {
965         return E_WRITE_REPLY_ERR;
966     }
967     if (!reply.WriteInt64(size)) {
968         LOGE("StorageManagerStub::HandleGetFree call GetTotalSize failed");
969         return  E_WRITE_REPLY_ERR;
970     }
971     return E_OK;
972 }
HandleUpdateMemoryPara(MessageParcel & data,MessageParcel & reply)973 int32_t StorageDaemonStub::HandleUpdateMemoryPara(MessageParcel &data, MessageParcel &reply)
974 {
975     int32_t size = data.ReadInt32();
976     int32_t oldSize = 0;
977     int err = UpdateMemoryPara(size, oldSize);
978     if (!reply.WriteInt32(err)) {
979         return E_WRITE_REPLY_ERR;
980     }
981     if (!reply.WriteInt32(oldSize)) {
982     LOGE("StorageManagerStub::HandleUpdateMemoryPara call Write oldSize failed");
983         return E_WRITE_REPLY_ERR;
984     }
985     return E_OK;
986 }
987 
HandleGetBundleStatsForIncrease(MessageParcel & data,MessageParcel & reply)988 int32_t StorageDaemonStub::HandleGetBundleStatsForIncrease(MessageParcel &data, MessageParcel &reply)
989 {
990     uint32_t userId = data.ReadUint32();
991     std::vector<std::string> bundleNames;
992     if (!data.ReadStringVector(&bundleNames)) {
993         return E_WRITE_REPLY_ERR;
994     }
995     std::vector<int64_t> incrementalBackTimes;
996     if (!data.ReadInt64Vector(&incrementalBackTimes)) {
997         return E_WRITE_REPLY_ERR;
998     }
999 
1000     std::vector<int64_t> pkgFileSizes;
1001     std::vector<int64_t> incPkgFileSizes;
1002     int32_t err = GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes);
1003     if (!reply.WriteInt32(err)) {
1004         return E_WRITE_REPLY_ERR;
1005     }
1006     if (!reply.WriteInt64Vector(pkgFileSizes)) {
1007         LOGE("StorageDaemonStub::HandleGetBundleStatsForIncrease call GetBundleStatsForIncrease failed");
1008         return  E_WRITE_REPLY_ERR;
1009     }
1010     if (!reply.WriteInt64Vector(incPkgFileSizes)) {
1011         LOGE("StorageDaemonStub::HandleGetBundleStatsForIncrease call GetBundleStatsForIncrease failed");
1012         return  E_WRITE_REPLY_ERR;
1013     }
1014     return E_OK;
1015 }
1016 
HandleMountDfsDocs(MessageParcel & data,MessageParcel & reply)1017 int32_t StorageDaemonStub::HandleMountDfsDocs(MessageParcel &data, MessageParcel &reply)
1018 {
1019     LOGI("StorageDaemonStub::HandleMountDfsDocs start.");
1020     int32_t userId = data.ReadInt32();
1021     std::string relativePath = data.ReadString();
1022     std::string networkId = data.ReadString();
1023     std::string deviceId = data.ReadString();
1024 
1025     int32_t err = MountDfsDocs(userId, relativePath, networkId, deviceId);
1026     if (!reply.WriteInt32(err)) {
1027         return E_WRITE_REPLY_ERR;
1028     }
1029     return E_OK;
1030 }
1031 
HandleUMountDfsDocs(MessageParcel & data,MessageParcel & reply)1032 int32_t StorageDaemonStub::HandleUMountDfsDocs(MessageParcel &data, MessageParcel &reply)
1033 {
1034     LOGI("StorageDaemonStub::HandleUMountDfsDocs start.");
1035     int32_t userId = data.ReadInt32();
1036     std::string relativePath = data.ReadString();
1037     std::string networkId = data.ReadString();
1038     std::string deviceId = data.ReadString();
1039 
1040     int32_t err = UMountDfsDocs(userId, relativePath, networkId, deviceId);
1041     if (!reply.WriteInt32(err)) {
1042         return E_WRITE_REPLY_ERR;
1043     }
1044     return E_OK;
1045 }
1046 
HandleGetFileEncryptStatus(MessageParcel & data,MessageParcel & reply)1047 int32_t StorageDaemonStub::HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply)
1048 {
1049     uint32_t userId = data.ReadUint32();
1050     bool needCheckDirMount = data.ReadBool();
1051     bool isEncrypted = true;
1052     int timerId = StorageXCollie::SetTimer("storage:GetFileEncryptStatus", LOCAL_TIME_OUT_SECONDS);
1053     std::lock_guard<std::mutex> lock(mutex_);
1054     int32_t err = GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
1055     StorageXCollie::CancelTimer(timerId);
1056     if (!reply.WriteBool(isEncrypted)) {
1057         return E_WRITE_REPLY_ERR;
1058     }
1059     if (!reply.WriteInt32(err)) {
1060         return E_WRITE_REPLY_ERR;
1061     }
1062     return E_OK;
1063 }
1064 
HandleMountMediaFuse(MessageParcel & data,MessageParcel & reply)1065 int32_t StorageDaemonStub::HandleMountMediaFuse(MessageParcel &data, MessageParcel &reply)
1066 {
1067 #ifdef STORAGE_SERVICE_MEDIA_FUSE
1068     LOGI("StorageDaemonStub::HandleMountMediaFuse start.");
1069 
1070     int32_t userId = data.ReadInt32();
1071     int32_t fd = -1;
1072     int32_t ret = MountMediaFuse(userId, fd);
1073     if (!reply.WriteInt32(ret)) {
1074         LOGE("Write reply error code failed");
1075         if (ret == E_OK) {
1076             close(fd);
1077         }
1078         return E_WRITE_REPLY_ERR;
1079     }
1080     if (ret == E_OK && fd > 0) {
1081         if (!reply.WriteFileDescriptor(fd)) {
1082             LOGE("Write reply fd failed");
1083             close(fd);
1084             return E_WRITE_REPLY_ERR;
1085         }
1086         close(fd);
1087     }
1088 #endif
1089     return E_OK;
1090 }
1091 
HandleUMountMediaFuse(MessageParcel & data,MessageParcel & reply)1092 int32_t StorageDaemonStub::HandleUMountMediaFuse(MessageParcel &data, MessageParcel &reply)
1093 {
1094 #ifdef STORAGE_SERVICE_MEDIA_FUSE
1095     LOGI("StorageDaemonStub::HandleUMountMediaFuse start.");
1096 
1097     int32_t userId = data.ReadInt32();
1098     int32_t ret = UMountMediaFuse(userId);
1099     if (!reply.WriteInt32(ret)) {
1100         return E_WRITE_REPLY_ERR;
1101     }
1102 #endif
1103     return E_OK;
1104 }
1105 
HandleGetUserNeedActiveStatus(MessageParcel & data,MessageParcel & reply)1106 int32_t StorageDaemonStub::HandleGetUserNeedActiveStatus(MessageParcel &data, MessageParcel &reply)
1107 {
1108     uint32_t userId = data.ReadUint32();
1109     bool needActive = false;
1110     int timerId = StorageXCollie::SetTimer("storage:GetUserNeedActiveStatus", LOCAL_TIME_OUT_SECONDS);
1111     std::lock_guard<std::mutex> lock(mutex_);
1112     int32_t err = GetUserNeedActiveStatus(userId, needActive);
1113     StorageXCollie::CancelTimer(timerId);
1114     if (!reply.WriteBool(needActive)) {
1115         return E_WRITE_REPLY_ERR;
1116     }
1117     if (!reply.WriteInt32(err)) {
1118         return E_WRITE_REPLY_ERR;
1119     }
1120     return E_OK;
1121 }
1122 
HandleMountFileMgrFuse(MessageParcel & data,MessageParcel & reply)1123 int32_t StorageDaemonStub::HandleMountFileMgrFuse(MessageParcel &data, MessageParcel &reply)
1124 {
1125     LOGI("StorageDaemonStub::HandleMountFileMgrFuse start.");
1126     int32_t userId = data.ReadInt32();
1127     std::string path = data.ReadString();
1128     int32_t fuseFd = -1;
1129     int32_t ret = MountFileMgrFuse(userId, path, fuseFd);
1130     if (!reply.WriteInt32(ret)) {
1131         LOGE("Write reply error code failed");
1132         if (ret == E_OK) {
1133             close(fuseFd);
1134         }
1135         return E_WRITE_REPLY_ERR;
1136     }
1137     if (ret == E_OK && fuseFd > 0) {
1138         if (!reply.WriteFileDescriptor(fuseFd)) {
1139             LOGE("Write reply fuseFd failed");
1140             close(fuseFd);
1141             return E_WRITE_REPLY_ERR;
1142         }
1143         close(fuseFd);
1144     }
1145     return E_OK;
1146 }
1147 
HandleUMountFileMgrFuse(MessageParcel & data,MessageParcel & reply)1148 int32_t StorageDaemonStub::HandleUMountFileMgrFuse(MessageParcel &data, MessageParcel &reply)
1149 {
1150     LOGI("StorageDaemonStub::HandleUMountFileMgrFuse start.");
1151     int32_t userId = data.ReadInt32();
1152     std::string path = data.ReadString();
1153     int32_t ret = UMountFileMgrFuse(userId, path);
1154     if (!reply.WriteInt32(ret)) {
1155         return E_WRITE_REPLY_ERR;
1156     }
1157     return E_OK;
1158 }
1159 } // StorageDaemon
1160 } // OHOS
1161