• 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 "storage_daemon_communication/storage_daemon_communication.h"
17 
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 
21 #include "os_account_manager.h"
22 #ifdef ENABLE_SCREENLOCK_MANAGER
23 #include "screenlock_manager.h"
24 #endif
25 #include "storage_service_errno.h"
26 #include "storage_service_log.h"
27 #include "utils/storage_radar.h"
28 
29 using namespace OHOS::StorageService;
30 namespace OHOS {
31 namespace StorageManager {
StorageDaemonCommunication()32 StorageDaemonCommunication::StorageDaemonCommunication()
33 {
34     LOGI("DEBUG StorageDaemonCommunication constructer");
35     storageDaemon_ = nullptr;
36 }
37 
~StorageDaemonCommunication()38 StorageDaemonCommunication::~StorageDaemonCommunication()
39 {
40     LOGI("DEBUG ~StorageDaemonCommunication destructer ~");
41 }
42 
Connect()43 int32_t StorageDaemonCommunication::Connect()
44 {
45     LOGD("StorageDaemonCommunication::Connect start");
46     std::lock_guard<std::mutex> lock(mutex_);
47     if (storageDaemon_ == nullptr) {
48         auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49         if (sam == nullptr) {
50             LOGE("StorageDaemonCommunication::Connect samgr nullptr");
51             return E_SA_IS_NULLPTR;
52         }
53         auto object = sam->GetSystemAbility(STORAGE_MANAGER_DAEMON_ID);
54         if (object == nullptr) {
55             LOGE("StorageDaemonCommunication::Connect object nullptr");
56             return E_REMOTE_IS_NULLPTR;
57         }
58         storageDaemon_ = iface_cast<OHOS::StorageDaemon::IStorageDaemon>(object);
59         if (storageDaemon_ == nullptr) {
60             LOGE("StorageDaemonCommunication::Connect service nullptr");
61             return E_SERVICE_IS_NULLPTR;
62         }
63         deathRecipient_ = new (std::nothrow) SdDeathRecipient();
64         if (!deathRecipient_) {
65             LOGE("StorageDaemonCommunication::Connect failed to create death recipient");
66             return E_DEATHRECIPIENT_IS_NULLPTR;
67         }
68 
69         storageDaemon_->AsObject()->AddDeathRecipient(deathRecipient_);
70     }
71     LOGD("StorageDaemonCommunication::Connect end");
72     return E_OK;
73 }
74 
PrepareAddUser(int32_t userId,uint32_t flags)75 int32_t StorageDaemonCommunication::PrepareAddUser(int32_t userId, uint32_t flags)
76 {
77     LOGI("StorageDaemonCommunication::PrepareAddUser start");
78     int32_t err = Connect();
79     if (err != E_OK) {
80         LOGE("StorageDaemonCommunication::PrepareAddUser connect failed");
81         std::string extraData = "flags=" + std::to_string(flags);
82         StorageRadar::ReportUserManager("PrepareAddUser::Connect", userId, err, extraData);
83         return err;
84     }
85     if (storageDaemon_ == nullptr) {
86         LOGE("StorageDaemonCommunication::Connect service nullptr");
87         std::string extraData = "flags=" + std::to_string(flags);
88         StorageRadar::ReportUserManager("StorageDaemonCommunication::PrepareAddUser",
89             userId, E_SERVICE_IS_NULLPTR, extraData);
90         return E_SERVICE_IS_NULLPTR;
91     }
92     return storageDaemon_->PrepareUserDirs(userId, flags);
93 }
94 
RemoveUser(int32_t userId,uint32_t flags)95 int32_t StorageDaemonCommunication::RemoveUser(int32_t userId, uint32_t flags)
96 {
97     LOGI("StorageDaemonCommunication::RemoveUser start");
98     int32_t err = Connect();
99     if (err != E_OK) {
100         LOGE("StorageDaemonCommunication::RemoveUser connect failed");
101         std::string extraData = "flags=" + std::to_string(flags);
102         StorageRadar::ReportUserManager("RemoveUser::Connect", userId, err, extraData);
103         return err;
104     }
105     if (storageDaemon_ == nullptr) {
106         LOGE("StorageDaemonCommunication::Connect service nullptr");
107         std::string extraData = "flags=" + std::to_string(flags);
108         StorageRadar::ReportUserManager("StorageDaemonCommunication::RemoveUser",
109             userId, E_SERVICE_IS_NULLPTR, extraData);
110         return E_SERVICE_IS_NULLPTR;
111     }
112     return storageDaemon_->DestroyUserDirs(userId, flags);
113 }
114 
PrepareStartUser(int32_t userId)115 int32_t StorageDaemonCommunication::PrepareStartUser(int32_t userId)
116 {
117     LOGD("StorageDaemonCommunication::PrepareStartUser start");
118     int32_t err = Connect();
119     if (err != E_OK) {
120         LOGE("StorageDaemonCommunication::PrepareStartUser connect failed");
121         StorageRadar::ReportUserManager("StorageDaemonCommunication::PrepareStartUser::Connect",
122             userId, err, "");
123         return err;
124     }
125     if (storageDaemon_ == nullptr) {
126         LOGE("StorageDaemonCommunication::Connect service nullptr");
127         StorageRadar::ReportUserManager("StorageDaemonCommunication::PrepareStartUser",
128             userId, E_SERVICE_IS_NULLPTR, "");
129         return E_SERVICE_IS_NULLPTR;
130     }
131     return storageDaemon_->StartUser(userId);
132 }
133 
StopUser(int32_t userId)134 int32_t StorageDaemonCommunication::StopUser(int32_t userId)
135 {
136     LOGI("StorageDaemonCommunication::StopUser start");
137     int32_t err = Connect();
138     if (err != E_OK) {
139         LOGE("StorageDaemonCommunication::StopUser connect failed");
140         StorageRadar::ReportUserManager("StorageDaemonCommunication::StopUser::Connect", userId, err, "");
141         return err;
142     }
143     if (storageDaemon_ == nullptr) {
144         LOGE("StorageDaemonCommunication::Connect service nullptr");
145         StorageRadar::ReportUserManager("StorageDaemonCommunication::StopUser", userId, E_SERVICE_IS_NULLPTR, "");
146         return E_SERVICE_IS_NULLPTR;
147     }
148     return storageDaemon_->StopUser(userId);
149 }
150 
CompleteAddUser(int32_t userId)151 int32_t StorageDaemonCommunication::CompleteAddUser(int32_t userId)
152 {
153     LOGI("StorageDaemonCommunication::CompleteAddUser start");
154     int32_t err = Connect();
155     if (err != E_OK) {
156         LOGE("StorageDaemonCommunication::CompleteAddUser connect failed");
157         StorageRadar::ReportUserManager("StorageDaemonCommunication::CompleteAddUser::Connect", userId, err, "");
158         return err;
159     }
160     if (storageDaemon_ == nullptr) {
161         LOGE("StorageDaemonCommunication::CompleteAddUser service nullptr");
162         StorageRadar::ReportUserManager("StorageDaemonCommunication::CompleteAddUser",
163             userId, E_SERVICE_IS_NULLPTR, "");
164         return E_SERVICE_IS_NULLPTR;
165     }
166     return storageDaemon_->CompleteAddUser(userId);
167 }
168 
Mount(std::string volumeId,int32_t flag)169 int32_t StorageDaemonCommunication::Mount(std::string volumeId, int32_t flag)
170 {
171     LOGI("StorageDaemonCommunication::mount start");
172     int32_t err = Connect();
173     if (err != E_OK) {
174         LOGE("StorageDaemonCommunication::mount connect failed");
175         return err;
176     }
177     if (storageDaemon_ == nullptr) {
178         LOGE("StorageDaemonCommunication::Connect service nullptr");
179         return E_SERVICE_IS_NULLPTR;
180     }
181     return storageDaemon_->Mount(volumeId, flag);
182 }
183 
Unmount(std::string volumeId)184 int32_t StorageDaemonCommunication::Unmount(std::string volumeId)
185 {
186     LOGI("StorageDaemonCommunication::unmount start");
187     int32_t err = Connect();
188     if (err != E_OK) {
189         LOGE("StorageDaemonCommunication::unmount connect failed");
190         return err;
191     }
192     if (storageDaemon_ == nullptr) {
193         LOGE("StorageDaemonCommunication::Connect service nullptr");
194         return E_SERVICE_IS_NULLPTR;
195     }
196     return storageDaemon_->UMount(volumeId);
197 }
198 
TryToFix(std::string volumeId,int32_t flag)199 int32_t StorageDaemonCommunication::TryToFix(std::string volumeId, int32_t flag)
200 {
201     LOGI("StorageDaemonCommunication::TryToFix start");
202     int32_t err = Connect();
203     if (err != E_OK) {
204         LOGE("StorageDaemonCommunication::TryToFix connect failed");
205         return err;
206     }
207     if (storageDaemon_ == nullptr) {
208         LOGE("StorageDaemonCommunication::Connect service nullptr");
209         return E_SERVICE_IS_NULLPTR;
210     }
211     return storageDaemon_->TryToFix(volumeId, flag);
212 }
213 
Check(std::string volumeId)214 int32_t StorageDaemonCommunication::Check(std::string volumeId)
215 {
216     LOGI("StorageDaemonCommunication::check start");
217     int32_t err = Connect();
218     if (err != E_OK) {
219         LOGE("StorageDaemonCommunication::check connect failed");
220         return err;
221     }
222     if (storageDaemon_ == nullptr) {
223         LOGE("StorageDaemonCommunication::Connect service nullptr");
224         return E_SERVICE_IS_NULLPTR;
225     }
226     return storageDaemon_->Check(volumeId);
227 }
228 
Partition(std::string diskId,int32_t type)229 int32_t StorageDaemonCommunication::Partition(std::string diskId, int32_t type)
230 {
231     LOGI("StorageDaemonCommunication::Partition start");
232     int32_t err = Connect();
233     if (err != E_OK) {
234         LOGE("StorageDaemonCommunication::Partition connect failed");
235         return err;
236     }
237     if (storageDaemon_ == nullptr) {
238         LOGE("StorageDaemonCommunication::Connect service nullptr");
239         return E_SERVICE_IS_NULLPTR;
240     }
241     return storageDaemon_->Partition(diskId, type);
242 }
243 
Format(std::string volumeId,std::string type)244 int32_t StorageDaemonCommunication::Format(std::string volumeId, std::string type)
245 {
246     LOGI("StorageDaemonCommunication::Format start");
247     int32_t err = Connect();
248     if (err != E_OK) {
249         LOGE("StorageDaemonCommunication::Format connect failed");
250         return err;
251     }
252     if (storageDaemon_ == nullptr) {
253         LOGE("StorageDaemonCommunication::Connect service nullptr");
254         return E_SERVICE_IS_NULLPTR;
255     }
256     return storageDaemon_->Format(volumeId, type);
257 }
258 
SetVolumeDescription(std::string volumeId,std::string description)259 int32_t StorageDaemonCommunication::SetVolumeDescription(std::string volumeId, std::string description)
260 {
261     LOGI("StorageDaemonCommunication::SetVolumeDescription start");
262     int32_t err = Connect();
263     if (err != E_OK) {
264         LOGE("StorageDaemonCommunication::SetVolumeDescription connect failed");
265         return err;
266     }
267     if (storageDaemon_ == nullptr) {
268         LOGE("StorageDaemonCommunication::Connect service nullptr");
269         return E_SERVICE_IS_NULLPTR;
270     }
271     return storageDaemon_->SetVolumeDescription(volumeId, description);
272 }
273 
QueryUsbIsInUse(const std::string & diskPath,bool & isInUse)274 int32_t StorageDaemonCommunication::QueryUsbIsInUse(const std::string &diskPath, bool &isInUse)
275 {
276     LOGI("StorageDaemonCommunication::QueryUsbIsInUse start");
277     int32_t err = Connect();
278     if (err != E_OK) {
279         LOGE("StorageDaemonCommunication::QueryUsbIsInUse connect failed");
280         return err;
281     }
282     if (storageDaemon_ == nullptr) {
283         LOGE("StorageDaemonCommunication::Connect service nullptr");
284         return E_SERVICE_IS_NULLPTR;
285     }
286     return storageDaemon_->QueryUsbIsInUse(diskPath, isInUse);
287 }
288 
GenerateUserKeys(uint32_t userId,uint32_t flags)289 int32_t StorageDaemonCommunication::GenerateUserKeys(uint32_t userId, uint32_t flags)
290 {
291     LOGD("enter");
292     int32_t err = Connect();
293     if (err != E_OK) {
294         LOGE("Connect failed");
295         return err;
296     }
297     if (storageDaemon_ == nullptr) {
298         LOGE("StorageDaemonCommunication::Connect service nullptr");
299         return E_SERVICE_IS_NULLPTR;
300     }
301     return storageDaemon_->GenerateUserKeys(userId, flags);
302 }
303 
DeleteUserKeys(uint32_t userId)304 int32_t StorageDaemonCommunication::DeleteUserKeys(uint32_t userId)
305 {
306     LOGD("enter");
307     int32_t err = Connect();
308     if (err != E_OK) {
309         LOGE("Connect failed");
310         return err;
311     }
312     if (storageDaemon_ == nullptr) {
313         LOGE("StorageDaemonCommunication::Connect service nullptr");
314         return E_SERVICE_IS_NULLPTR;
315     }
316     return storageDaemon_->DeleteUserKeys(userId);
317 }
318 
UpdateUserAuth(uint32_t userId,uint64_t secureUid,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)319 int32_t StorageDaemonCommunication::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
320                                                    const std::vector<uint8_t> &token,
321                                                    const std::vector<uint8_t> &oldSecret,
322                                                    const std::vector<uint8_t> &newSecret)
323 {
324     LOGI("enter");
325     int32_t err = Connect();
326     if (err != E_OK) {
327         LOGE("Connect failed");
328         return err;
329     }
330     if (storageDaemon_ == nullptr) {
331         LOGE("StorageDaemonCommunication::Connect service nullptr");
332         return E_SERVICE_IS_NULLPTR;
333     }
334     return storageDaemon_->UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
335 }
336 
UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> & authToken,const std::vector<uint8_t> & newSecret,uint64_t secureUid,uint32_t userId,const std::vector<std::vector<uint8_t>> & plainText)337 int32_t StorageDaemonCommunication::UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> &authToken,
338                                                                  const std::vector<uint8_t> &newSecret,
339                                                                  uint64_t secureUid,
340                                                                  uint32_t userId,
341                                                                  const std::vector<std::vector<uint8_t>> &plainText)
342 {
343     LOGI("enter");
344     int32_t err = Connect();
345     if (err != E_OK) {
346         LOGE("Connect failed");
347         return err;
348     }
349     if (storageDaemon_ == nullptr) {
350         LOGE("StorageDaemonCommunication::Connect service nullptr");
351         return E_SERVICE_IS_NULLPTR;
352     }
353     return storageDaemon_->UpdateUseAuthWithRecoveryKey(authToken, newSecret, secureUid, userId, plainText);
354 }
355 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)356 int32_t StorageDaemonCommunication::ActiveUserKey(uint32_t userId,
357                                                   const std::vector<uint8_t> &token,
358                                                   const std::vector<uint8_t> &secret)
359 {
360     LOGD("enter");
361     int32_t err = Connect();
362     if (err != E_OK) {
363         LOGE("Connect failed");
364         return err;
365     }
366     if (storageDaemon_ == nullptr) {
367         LOGE("StorageDaemonCommunication::Connect service nullptr");
368         return E_SERVICE_IS_NULLPTR;
369     }
370     return storageDaemon_->ActiveUserKey(userId, token, secret);
371 }
372 
InactiveUserKey(uint32_t userId)373 int32_t StorageDaemonCommunication::InactiveUserKey(uint32_t userId)
374 {
375     LOGD("enter");
376     int32_t err = Connect();
377     if (err != E_OK) {
378         LOGE("Connect failed");
379         return err;
380     }
381     if (storageDaemon_ == nullptr) {
382         LOGE("StorageDaemonCommunication::Connect service nullptr");
383         return E_SERVICE_IS_NULLPTR;
384     }
385     return storageDaemon_->InactiveUserKey(userId);
386 }
387 
LockUserScreen(uint32_t userId)388 int32_t StorageDaemonCommunication::LockUserScreen(uint32_t userId)
389 {
390     LOGI("enter");
391     int32_t err = Connect();
392     if (err != E_OK) {
393         LOGE("Connect failed");
394         return err;
395     }
396     if (storageDaemon_ == nullptr) {
397         LOGE("StorageDaemonCommunication::Connect service nullptr");
398         return E_SERVICE_IS_NULLPTR;
399     }
400     return storageDaemon_->LockUserScreen(userId);
401 }
402 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)403 int32_t StorageDaemonCommunication::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
404 {
405     LOGD("enter");
406     int32_t err = Connect();
407     if (err != E_OK) {
408         LOGE("Connect failed");
409         return err;
410     }
411     if (storageDaemon_ == nullptr) {
412         LOGE("StorageDaemonCommunication::Connect service nullptr");
413         return E_SERVICE_IS_NULLPTR;
414     }
415     return storageDaemon_->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
416 }
417 
GetUserNeedActiveStatus(uint32_t userId,bool & needActive)418 int32_t StorageDaemonCommunication::GetUserNeedActiveStatus(uint32_t userId, bool &needActive)
419 {
420     LOGD("enter");
421     int32_t err = Connect();
422     if (err != E_OK) {
423         LOGE("Connect failed");
424         return err;
425     }
426     if (storageDaemon_ == nullptr) {
427         LOGE("StorageDaemonCommunication::Connect service nullptr");
428         return E_SERVICE_IS_NULLPTR;
429     }
430     return storageDaemon_->GetUserNeedActiveStatus(userId, needActive);
431 }
432 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)433 int32_t StorageDaemonCommunication::UnlockUserScreen(uint32_t userId,
434                                                      const std::vector<uint8_t> &token,
435                                                      const std::vector<uint8_t> &secret)
436 {
437     LOGI("enter");
438     int32_t err = Connect();
439     if (err != E_OK) {
440         LOGE("Connect failed");
441         return err;
442     }
443     if (storageDaemon_ == nullptr) {
444         LOGE("StorageDaemonCommunication::Connect service nullptr");
445         return E_SERVICE_IS_NULLPTR;
446     }
447     return storageDaemon_->UnlockUserScreen(userId, token, secret);
448 }
449 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)450 int32_t StorageDaemonCommunication::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
451 {
452     LOGI("enter");
453     int32_t err = Connect();
454     if (err != E_OK) {
455         LOGE("Connect failed");
456         return err;
457     }
458     if (storageDaemon_ == nullptr) {
459         LOGE("StorageDaemonCommunication::Connect service nullptr");
460         return E_SERVICE_IS_NULLPTR;
461     }
462     return storageDaemon_->GetLockScreenStatus(userId, lockScreenStatus);
463 }
464 
UpdateKeyContext(uint32_t userId,bool needRemoveTmpKey)465 int32_t StorageDaemonCommunication::UpdateKeyContext(uint32_t userId, bool needRemoveTmpKey)
466 {
467     LOGI("enter");
468     int32_t err = Connect();
469     if (err != E_OK) {
470         LOGE("Connect failed");
471         return err;
472     }
473     if (storageDaemon_ == nullptr) {
474         LOGE("StorageDaemonCommunication::Connect service nullptr");
475         return E_SERVICE_IS_NULLPTR;
476     }
477     return storageDaemon_->UpdateKeyContext(userId, needRemoveTmpKey);
478 }
479 
ResetSdProxy()480 int32_t StorageDaemonCommunication::ResetSdProxy()
481 {
482     LOGI("enter");
483     std::lock_guard<std::mutex> lock(mutex_);
484     if ((storageDaemon_ != nullptr) && (storageDaemon_->AsObject() != nullptr)) {
485         storageDaemon_->AsObject()->RemoveDeathRecipient(deathRecipient_);
486     }
487     storageDaemon_ = nullptr;
488 
489     return E_OK;
490 }
491 
ForceLockUserScreen()492 void StorageDaemonCommunication::ForceLockUserScreen()
493 {
494     LOGI("StorageDaemonCommunication::ForceLockUserScreen, storage_daemon process maybe has died.");
495 #ifdef ENABLE_SCREENLOCK_MANAGER
496     std::vector<int32_t> ids;
497     int32_t ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
498     if (ret != ERR_OK || ids.empty()) {
499         LOGE("Query active userid failed, ret = %{public}u", ret);
500         StorageRadar::ReportOsAccountResult("ForceLockUserScreen::QueryActiveOsAccountIds", ret, DEFAULT_USERID);
501         return;
502     }
503     int reasonFlag = static_cast<int>(ScreenLock::StrongAuthReasonFlags::ACTIVE_REQUEST);
504     ret = ScreenLock::ScreenLockManager::GetInstance()->RequestStrongAuth(reasonFlag, ids[0]);
505     if (ret != ScreenLock::E_SCREENLOCK_OK) {
506         LOGE("Request strong auth by screen lock manager failed.");
507         StorageRadar::ReportOsAccountResult("ForceLockUserScreen::RequestStrongAuth", ret, ids[0]);
508         return;
509     }
510     ret = ScreenLock::ScreenLockManager::GetInstance()->Lock(ids[0]);
511     if (ret != ScreenLock::E_SCREENLOCK_OK) {
512         LOGE("Lock user screen by screen lock manager failed.");
513         StorageRadar::ReportOsAccountResult("ForceLockUserScreen::Lock", ret, ids[0]);
514         return;
515     }
516     LOGI("Force lock user screen and request strong auth success for userId = %{public}d.", ids[0]);
517 #endif
518 }
519 
OnRemoteDied(const wptr<IRemoteObject> & remote)520 void SdDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
521 {
522     LOGE("StorageDaemonCommunication::OnRemoteDied, storage_daemon process has died.");
523     DelayedSingleton<StorageDaemonCommunication>::GetInstance()->ResetSdProxy();
524     DelayedSingleton<StorageDaemonCommunication>::GetInstance()->ForceLockUserScreen();
525 }
526 
CreateShareFile(const StorageFileRawData & rawData,uint32_t tokenId,uint32_t flag)527 std::vector<int32_t> StorageDaemonCommunication::CreateShareFile(const StorageFileRawData &rawData,
528                                                                  uint32_t tokenId, uint32_t flag)
529 {
530     LOGI("enter");
531     int32_t err = Connect();
532     if (err != E_OK) {
533         LOGE("Connect failed");
534         return std::vector<int32_t>{err};
535     }
536     if (storageDaemon_ == nullptr) {
537         LOGE("StorageDaemonCommunication::Connect service nullptr");
538         return std::vector<int32_t>{err};
539     }
540     std::vector<int32_t> funcResult;
541 
542     storageDaemon_->CreateShareFile(rawData, tokenId, flag, funcResult);
543     LOGI("StorageDaemonCommunication::CreateShareFile end. result is %{public}zu", funcResult.size());
544     return funcResult;
545 }
546 
DeleteShareFile(uint32_t tokenId,const StorageFileRawData & rawData)547 int32_t StorageDaemonCommunication::DeleteShareFile(uint32_t tokenId, const StorageFileRawData &rawData)
548 {
549     LOGI("enter");
550     int32_t err = Connect();
551     if (err != E_OK) {
552         LOGE("Connect failed");
553         return err;
554     }
555     if (storageDaemon_ == nullptr) {
556         LOGE("StorageDaemonCommunication::Connect service nullptr");
557         return E_SERVICE_IS_NULLPTR;
558     }
559     return storageDaemon_->DeleteShareFile(tokenId, rawData);
560 }
561 
SetBundleQuota(const std::string & bundleName,int32_t uid,const std::string & bundleDataDirPath,int32_t limitSizeMb)562 int32_t StorageDaemonCommunication::SetBundleQuota(const std::string &bundleName, int32_t uid,
563     const std::string &bundleDataDirPath, int32_t limitSizeMb)
564 {
565     LOGD("enter");
566     int32_t err = Connect();
567     if (err != E_OK) {
568         LOGE("Connect failed");
569         return err;
570     }
571     if (storageDaemon_ == nullptr) {
572         LOGE("StorageDaemonCommunication::Connect service nullptr");
573         return E_SERVICE_IS_NULLPTR;
574     }
575     return storageDaemon_->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb);
576 }
577 
GetOccupiedSpace(int32_t idType,int32_t id,int64_t & size)578 int32_t StorageDaemonCommunication::GetOccupiedSpace(int32_t idType, int32_t id, int64_t &size)
579 {
580     LOGD("enter");
581     int32_t err = Connect();
582     if (err != E_OK) {
583         LOGE("Connect failed");
584         return err;
585     }
586     if (storageDaemon_ == nullptr) {
587         LOGE("StorageDaemonCommunication::Connect service nullptr");
588         return E_SERVICE_IS_NULLPTR;
589     }
590     return storageDaemon_->GetOccupiedSpace(idType, id, size);
591 }
592 
MountCryptoPathAgain(int32_t userId)593 int32_t StorageDaemonCommunication::MountCryptoPathAgain(int32_t userId)
594 {
595     int32_t err = Connect();
596     if (err != E_OK) {
597         LOGE("Connect failed");
598         StorageRadar::ReportUserManager("StorageDaemonCommunication::MountCryptoPathAgain::Connect", userId, err, "");
599         return err;
600     }
601     if (storageDaemon_ == nullptr) {
602         LOGE("StorageDaemonCommunication::Connect service nullptr");
603         StorageRadar::ReportUserManager("StorageDaemonCommunication::MountCryptoPathAgain",
604             userId, E_SERVICE_IS_NULLPTR, "");
605         return E_SERVICE_IS_NULLPTR;
606     }
607     return storageDaemon_->MountCryptoPathAgain(userId);
608 }
609 
GenerateAppkey(uint32_t userId,uint32_t hashId,std::string & keyId,bool needReSet)610 int32_t StorageDaemonCommunication::GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId, bool needReSet)
611 {
612     int32_t err = Connect();
613     if (err != E_OK) {
614         LOGE("Connect failed");
615         return err;
616     }
617     if (storageDaemon_ == nullptr) {
618         LOGE("StorageDaemonCommunication::Connect service nullptr");
619         return E_SERVICE_IS_NULLPTR;
620     }
621     return storageDaemon_->GenerateAppkey(userId, hashId, keyId, needReSet);
622 }
623 
DeleteAppkey(uint32_t userId,const std::string keyId)624 int32_t StorageDaemonCommunication::DeleteAppkey(uint32_t userId, const std::string keyId)
625 {
626     int32_t err = Connect();
627     if (err != E_OK) {
628         LOGE("Connect failed");
629         return err;
630     }
631     if (storageDaemon_ == nullptr) {
632         LOGE("StorageDaemonCommunication::Connect service nullptr");
633         return E_SERVICE_IS_NULLPTR;
634     }
635     return storageDaemon_->DeleteAppkey(userId, keyId);
636 }
637 
CreateRecoverKey(uint32_t userId,uint32_t userType,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)638 int32_t StorageDaemonCommunication::CreateRecoverKey(uint32_t userId,
639                                                      uint32_t userType,
640                                                      const std::vector<uint8_t> &token,
641                                                      const std::vector<uint8_t> &secret)
642 {
643     LOGI("enter");
644     int32_t err = Connect();
645     if (err != E_OK) {
646         LOGE("Connect failed");
647         return err;
648     }
649     if (storageDaemon_ == nullptr) {
650         LOGE("StorageDaemonCommunication::Connect service nullptr");
651         return E_SERVICE_IS_NULLPTR;
652     }
653     return storageDaemon_->CreateRecoverKey(userId, userType, token, secret);
654 }
655 
SetRecoverKey(const std::vector<uint8_t> & key)656 int32_t StorageDaemonCommunication::SetRecoverKey(const std::vector<uint8_t> &key)
657 {
658     LOGI("enter");
659     int32_t err = Connect();
660     if (err != E_OK) {
661         LOGE("Connect failed");
662         return err;
663     }
664     if (storageDaemon_ == nullptr) {
665         LOGE("StorageDaemonCommunication::Connect service nullptr");
666         return E_SERVICE_IS_NULLPTR;
667     }
668     return storageDaemon_->SetRecoverKey(key);
669 }
670 
ResetSecretWithRecoveryKey(uint32_t userId,uint32_t rkType,const std::vector<uint8_t> & key)671 int32_t StorageDaemonCommunication::ResetSecretWithRecoveryKey(uint32_t userId,
672     uint32_t rkType, const std::vector<uint8_t> &key)
673 {
674     LOGI("enter");
675     int32_t err = Connect();
676     if (err != E_OK) {
677         LOGE("Connect failed");
678         return err;
679     }
680     if (storageDaemon_ == nullptr) {
681         LOGE("StorageDaemonCommunication::Connect service nullptr");
682         return E_SERVICE_IS_NULLPTR;
683     }
684     return storageDaemon_->ResetSecretWithRecoveryKey(userId, rkType, key);
685 }
686 
UpdateMemoryPara(int32_t size,int32_t & oldSize)687 int32_t StorageDaemonCommunication::UpdateMemoryPara(int32_t size, int32_t &oldSize)
688 {
689     LOGI("StorageDaemonCommunication::UpdateMemoryPara");
690     int32_t err = Connect();
691     if (err != E_OK) {
692         LOGE("Connect failed");
693         return err;
694     }
695     if (storageDaemon_ == nullptr) {
696         LOGE("StorageDaemonCommunication::Connect service nullptr");
697         return E_SERVICE_IS_NULLPTR;
698     }
699     return storageDaemon_->UpdateMemoryPara(size, oldSize);
700 }
701 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)702 int32_t StorageDaemonCommunication::MountDfsDocs(int32_t userId, const std::string &relativePath,
703     const std::string &networkId, const std::string &deviceId)
704 {
705     LOGI("StorageDaemonCommunication::MountDfsDocs start.");
706     int32_t err = Connect();
707     if (err != E_OK) {
708         LOGE("Connect failed");
709         return err;
710     }
711     if (storageDaemon_ == nullptr) {
712         LOGE("StorageDaemonCommunication::Connect service nullptr");
713         return E_SERVICE_IS_NULLPTR;
714     }
715     return storageDaemon_->MountDfsDocs(userId, relativePath, networkId, deviceId);
716 }
717 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)718 int32_t StorageDaemonCommunication::UMountDfsDocs(int32_t userId, const std::string &relativePath,
719     const std::string &networkId, const std::string &deviceId)
720 {
721     LOGI("StorageDaemonCommunication::UMountDfsDocs start.");
722     int32_t err = Connect();
723     if (err != E_OK) {
724         LOGE("Connect failed");
725         return err;
726     }
727     if (storageDaemon_ == nullptr) {
728         LOGE("StorageDaemonCommunication::Connect service nullptr");
729         return E_SERVICE_IS_NULLPTR;
730     }
731     return storageDaemon_->UMountDfsDocs(userId, relativePath, networkId, deviceId);
732 }
733 
MountMediaFuse(int32_t userId,int32_t & devFd)734 int32_t StorageDaemonCommunication::MountMediaFuse(int32_t userId, int32_t &devFd)
735 {
736 #ifdef STORAGE_SERVICE_MEDIA_FUSE
737     int32_t err = Connect();
738     if (err != E_OK) {
739         LOGE("Connect failed");
740         return err;
741     }
742     if (storageDaemon_ == nullptr) {
743         LOGE("StorageDaemonCommunication::Connect service nullptr");
744         return E_SERVICE_IS_NULLPTR;
745     }
746     return storageDaemon_->MountMediaFuse(userId, devFd);
747 #else
748     return E_OK;
749 #endif
750 }
751 
UMountMediaFuse(int32_t userId)752 int32_t StorageDaemonCommunication::UMountMediaFuse(int32_t userId)
753 {
754 #ifdef STORAGE_SERVICE_MEDIA_FUSE
755     int32_t err = Connect();
756     if (err != E_OK) {
757         LOGE("Connect failed");
758         return err;
759     }
760     if (storageDaemon_ == nullptr) {
761         LOGE("StorageDaemonCommunication::Connect service nullptr");
762         return E_SERVICE_IS_NULLPTR;
763     }
764     return storageDaemon_->UMountMediaFuse(userId);
765 #else
766     return E_OK;
767 #endif
768 }
769 
MountFileMgrFuse(int32_t userId,const std::string & path,int32_t & fuseFd)770 int32_t StorageDaemonCommunication::MountFileMgrFuse(int32_t userId, const std::string &path, int32_t &fuseFd)
771 {
772     int32_t err = Connect();
773     if (err != E_OK) {
774         LOGE("Connect failed");
775         return err;
776     }
777     if (storageDaemon_ == nullptr) {
778         LOGE("StorageDaemonCommunication::Connect service nullptr");
779         return E_SERVICE_IS_NULLPTR;
780     }
781     return storageDaemon_->MountFileMgrFuse(userId, path, fuseFd);
782 }
783 
UMountFileMgrFuse(int32_t userId,const std::string & path)784 int32_t StorageDaemonCommunication::UMountFileMgrFuse(int32_t userId, const std::string &path)
785 {
786     int32_t err = Connect();
787     if (err != E_OK) {
788         LOGE("Connect failed");
789         return err;
790     }
791     if (storageDaemon_ == nullptr) {
792         LOGE("StorageDaemonCommunication::Connect service nullptr");
793         return E_SERVICE_IS_NULLPTR;
794     }
795     return storageDaemon_->UMountFileMgrFuse(userId, path);
796 }
797 
IsFileOccupied(const std::string & path,const std::vector<std::string> & inputList,std::vector<std::string> & outputList,bool & isOccupy)798 int32_t StorageDaemonCommunication::IsFileOccupied(const std::string &path, const std::vector<std::string> &inputList,
799     std::vector<std::string> &outputList, bool &isOccupy)
800 {
801     int32_t err = Connect();
802     if (err != E_OK) {
803         LOGE("Connect failed");
804         return err;
805     }
806     if (storageDaemon_ == nullptr) {
807         LOGE("StorageDaemonCommunication::Connect service nullptr");
808         return E_SERVICE_IS_NULLPTR;
809     }
810     return storageDaemon_->IsFileOccupied(path, inputList, outputList, isOccupy);
811 }
812 
MountDisShareFile(int32_t userId,const std::map<std::string,std::string> & shareFiles)813 int32_t StorageDaemonCommunication::MountDisShareFile(int32_t userId,
814     const std::map<std::string, std::string> &shareFiles)
815 {
816     int32_t err = Connect();
817     if (err != E_OK) {
818         LOGE("Connect failed");
819         return err;
820     }
821     if (storageDaemon_ == nullptr) {
822         LOGE("StorageDaemonCommunication::Connect service nullptr");
823         return E_SERVICE_IS_NULLPTR;
824     }
825     return storageDaemon_->MountDisShareFile(userId, shareFiles);
826 }
827 
UMountDisShareFile(int32_t userId,const std::string & networkId)828 int32_t StorageDaemonCommunication::UMountDisShareFile(int32_t userId, const std::string &networkId)
829 {
830     int32_t err = Connect();
831     if (err != E_OK) {
832         LOGE("Connect failed");
833         return err;
834     }
835     if (storageDaemon_ == nullptr) {
836         LOGE("StorageDaemonCommunication::Connect service nullptr");
837         return E_SERVICE_IS_NULLPTR;
838     }
839     return storageDaemon_->UMountDisShareFile(userId, networkId);
840 }
841 
InactiveUserPublicDirKey(uint32_t userId)842 int32_t StorageDaemonCommunication::InactiveUserPublicDirKey(uint32_t userId)
843 {
844     int32_t err = Connect();
845     if (err != E_OK) {
846         LOGE("StorageDaemonCommunication::InactiveUserPublicDirKey connect failed");
847         return err;
848     }
849     if (storageDaemon_ == nullptr) {
850         LOGE("StorageDaemonCommunication::Connect service nullptr");
851         return E_SERVICE_IS_NULLPTR;
852     }
853     return storageDaemon_->InactiveUserPublicDirKey(userId);
854 }
855 
MountUsbFuse(const std::string & volumeId,std::string & fsUuid,int & fuseFd)856 int32_t StorageDaemonCommunication::MountUsbFuse(const std::string &volumeId, std::string &fsUuid, int &fuseFd)
857 {
858     int32_t err = Connect();
859     if (err != E_OK) {
860         LOGE("StorageDaemonCommunication::MountUsbFuse connect failed");
861         return err;
862     }
863     if (storageDaemon_ == nullptr) {
864         LOGE("StorageDaemonCommunication::Connect service nullptr");
865         return E_SERVICE_IS_NULLPTR;
866     }
867     return storageDaemon_->MountUsbFuse(volumeId, fsUuid, fuseFd);
868 }
869 
QueryOccupiedSpaceForSa(const std::string & storageStatus)870 int32_t StorageDaemonCommunication::QueryOccupiedSpaceForSa(const std::string &storageStatus)
871 {
872     int32_t err = Connect();
873     if (err != E_OK) {
874         LOGE("Connect failed");
875         return err;
876     }
877     if (storageDaemon_ == nullptr) {
878         LOGE("StorageDaemonCommunication::Connect service nullptr");
879         return E_SERVICE_IS_NULLPTR;
880     }
881     return storageDaemon_->QueryOccupiedSpaceForSa(storageStatus);
882 }
883 
RegisterUeceActivationCallback(const sptr<StorageManager::IUeceActivationCallback> & ueceCallback)884 int32_t StorageDaemonCommunication::RegisterUeceActivationCallback(
885     const sptr<StorageManager::IUeceActivationCallback> &ueceCallback)
886 {
887 #ifdef EL5_FILEKEY_MANAGER
888     int32_t err = Connect();
889     if (err != E_OK) {
890         LOGE("Connect failed");
891         return err;
892     }
893     if (storageDaemon_ == nullptr) {
894         LOGE("StorageDaemonCommunication::Connect service nullptr");
895         return E_SERVICE_IS_NULLPTR;
896     }
897     return storageDaemon_->RegisterUeceActivationCallback(ueceCallback);
898 #else
899     LOGI("EL5_FILEKEY_MANAGER is not supported");
900     return E_OK;
901 #endif
902 }
903 
UnregisterUeceActivationCallback()904 int32_t StorageDaemonCommunication::UnregisterUeceActivationCallback()
905 {
906 #ifdef EL5_FILEKEY_MANAGER
907     int32_t err = Connect();
908     if (err != E_OK) {
909         LOGE("Connect failed");
910         return err;
911     }
912     if (storageDaemon_ == nullptr) {
913         LOGE("StorageDaemonCommunication::Connect service nullptr");
914         return E_SERVICE_IS_NULLPTR;
915     }
916     return storageDaemon_->UnregisterUeceActivationCallback();
917 #else
918     LOGI("EL5_FILEKEY_MANAGER is not supported");
919     return E_OK;
920 #endif
921 }
922 } // namespace StorageManager
923 } // namespace OHOS
924