• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "storage_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 
Check(std::string volumeId)199 int32_t StorageDaemonCommunication::Check(std::string volumeId)
200 {
201     LOGI("StorageDaemonCommunication::check start");
202     int32_t err = Connect();
203     if (err != E_OK) {
204         LOGE("StorageDaemonCommunication::check 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_->Check(volumeId);
212 }
213 
Partition(std::string diskId,int32_t type)214 int32_t StorageDaemonCommunication::Partition(std::string diskId, int32_t type)
215 {
216     LOGI("StorageDaemonCommunication::Partition start");
217     int32_t err = Connect();
218     if (err != E_OK) {
219         LOGE("StorageDaemonCommunication::Partition 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_->Partition(diskId, type);
227 }
228 
Format(std::string volumeId,std::string type)229 int32_t StorageDaemonCommunication::Format(std::string volumeId, std::string type)
230 {
231     LOGI("StorageDaemonCommunication::Format start");
232     int32_t err = Connect();
233     if (err != E_OK) {
234         LOGE("StorageDaemonCommunication::Format 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_->Format(volumeId, type);
242 }
243 
SetVolumeDescription(std::string volumeId,std::string description)244 int32_t StorageDaemonCommunication::SetVolumeDescription(std::string volumeId, std::string description)
245 {
246     LOGI("StorageDaemonCommunication::SetVolumeDescription start");
247     int32_t err = Connect();
248     if (err != E_OK) {
249         LOGE("StorageDaemonCommunication::SetVolumeDescription 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_->SetVolumeDescription(volumeId, description);
257 }
258 
GenerateUserKeys(uint32_t userId,uint32_t flags)259 int32_t StorageDaemonCommunication::GenerateUserKeys(uint32_t userId, uint32_t flags)
260 {
261     LOGD("enter");
262     int32_t err = Connect();
263     if (err != E_OK) {
264         LOGE("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_->GenerateUserKeys(userId, flags);
272 }
273 
DeleteUserKeys(uint32_t userId)274 int32_t StorageDaemonCommunication::DeleteUserKeys(uint32_t userId)
275 {
276     LOGD("enter");
277     int32_t err = Connect();
278     if (err != E_OK) {
279         LOGE("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_->DeleteUserKeys(userId);
287 }
288 
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)289 int32_t StorageDaemonCommunication::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
290                                                    const std::vector<uint8_t> &token,
291                                                    const std::vector<uint8_t> &oldSecret,
292                                                    const std::vector<uint8_t> &newSecret)
293 {
294     LOGI("enter");
295     int32_t err = Connect();
296     if (err != E_OK) {
297         LOGE("Connect failed");
298         return err;
299     }
300     if (storageDaemon_ == nullptr) {
301         LOGE("StorageDaemonCommunication::Connect service nullptr");
302         return E_SERVICE_IS_NULLPTR;
303     }
304     return storageDaemon_->UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
305 }
306 
UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> & authToken,const std::vector<uint8_t> & newSecret,uint64_t secureUid,uint32_t userId,std::vector<std::vector<uint8_t>> & plainText)307 int32_t StorageDaemonCommunication::UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> &authToken,
308                                                                  const std::vector<uint8_t> &newSecret,
309                                                                  uint64_t secureUid,
310                                                                  uint32_t userId,
311                                                                  std::vector<std::vector<uint8_t>> &plainText)
312 {
313     LOGI("enter");
314     int32_t err = Connect();
315     if (err != E_OK) {
316         LOGE("Connect failed");
317         return err;
318     }
319     if (storageDaemon_ == nullptr) {
320         LOGE("StorageDaemonCommunication::Connect service nullptr");
321         return E_SERVICE_IS_NULLPTR;
322     }
323     return storageDaemon_->UpdateUseAuthWithRecoveryKey(authToken, newSecret, secureUid, userId, plainText);
324 }
325 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)326 int32_t StorageDaemonCommunication::ActiveUserKey(uint32_t userId,
327                                                   const std::vector<uint8_t> &token,
328                                                   const std::vector<uint8_t> &secret)
329 {
330     LOGD("enter");
331     int32_t err = Connect();
332     if (err != E_OK) {
333         LOGE("Connect failed");
334         return err;
335     }
336     if (storageDaemon_ == nullptr) {
337         LOGE("StorageDaemonCommunication::Connect service nullptr");
338         return E_SERVICE_IS_NULLPTR;
339     }
340     return storageDaemon_->ActiveUserKey(userId, token, secret);
341 }
342 
InactiveUserKey(uint32_t userId)343 int32_t StorageDaemonCommunication::InactiveUserKey(uint32_t userId)
344 {
345     LOGD("enter");
346     int32_t err = Connect();
347     if (err != E_OK) {
348         LOGE("Connect failed");
349         return err;
350     }
351     if (storageDaemon_ == nullptr) {
352         LOGE("StorageDaemonCommunication::Connect service nullptr");
353         return E_SERVICE_IS_NULLPTR;
354     }
355     return storageDaemon_->InactiveUserKey(userId);
356 }
357 
LockUserScreen(uint32_t userId)358 int32_t StorageDaemonCommunication::LockUserScreen(uint32_t userId)
359 {
360     LOGI("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_->LockUserScreen(userId);
371 }
372 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)373 int32_t StorageDaemonCommunication::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
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_->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
386 }
387 
GetUserNeedActiveStatus(uint32_t userId,bool & needActive)388 int32_t StorageDaemonCommunication::GetUserNeedActiveStatus(uint32_t userId, bool &needActive)
389 {
390     LOGD("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_->GetUserNeedActiveStatus(userId, needActive);
401 }
402 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)403 int32_t StorageDaemonCommunication::UnlockUserScreen(uint32_t userId,
404                                                      const std::vector<uint8_t> &token,
405                                                      const std::vector<uint8_t> &secret)
406 {
407     LOGI("enter");
408     int32_t err = Connect();
409     if (err != E_OK) {
410         LOGE("Connect failed");
411         return err;
412     }
413     if (storageDaemon_ == nullptr) {
414         LOGE("StorageDaemonCommunication::Connect service nullptr");
415         return E_SERVICE_IS_NULLPTR;
416     }
417     return storageDaemon_->UnlockUserScreen(userId, token, secret);
418 }
419 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)420 int32_t StorageDaemonCommunication::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
421 {
422     LOGI("enter");
423     int32_t err = Connect();
424     if (err != E_OK) {
425         LOGE("Connect failed");
426         return err;
427     }
428     if (storageDaemon_ == nullptr) {
429         LOGE("StorageDaemonCommunication::Connect service nullptr");
430         return E_SERVICE_IS_NULLPTR;
431     }
432     return storageDaemon_->GetLockScreenStatus(userId, lockScreenStatus);
433 }
434 
UpdateKeyContext(uint32_t userId,bool needRemoveTmpKey)435 int32_t StorageDaemonCommunication::UpdateKeyContext(uint32_t userId, bool needRemoveTmpKey)
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_->UpdateKeyContext(userId, needRemoveTmpKey);
448 }
449 
ResetSdProxy()450 int32_t StorageDaemonCommunication::ResetSdProxy()
451 {
452     LOGI("enter");
453     std::lock_guard<std::mutex> lock(mutex_);
454     if ((storageDaemon_ != nullptr) && (storageDaemon_->AsObject() != nullptr)) {
455         storageDaemon_->AsObject()->RemoveDeathRecipient(deathRecipient_);
456     }
457     storageDaemon_ = nullptr;
458 
459     return E_OK;
460 }
461 
ForceLockUserScreen()462 void StorageDaemonCommunication::ForceLockUserScreen()
463 {
464     LOGI("StorageDaemonCommunication::ForceLockUserScreen, storage_daemon process maybe has died.");
465 #ifdef ENABLE_SCREENLOCK_MANAGER
466     std::vector<int32_t> ids;
467     int32_t ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
468     if (ret != ERR_OK || ids.empty()) {
469         LOGE("Query active userid failed, ret = %{public}u", ret);
470         StorageRadar::ReportOsAccountResult("ForceLockUserScreen::QueryActiveOsAccountIds", ret, DEFAULT_USERID);
471         return;
472     }
473     int reasonFlag = static_cast<int>(ScreenLock::StrongAuthReasonFlags::ACTIVE_REQUEST);
474     ret = ScreenLock::ScreenLockManager::GetInstance()->RequestStrongAuth(reasonFlag, ids[0]);
475     if (ret != ScreenLock::E_SCREENLOCK_OK) {
476         LOGE("Request strong auth by screen lock manager failed.");
477         StorageRadar::ReportOsAccountResult("ForceLockUserScreen::RequestStrongAuth", ret, ids[0]);
478         return;
479     }
480     ret = ScreenLock::ScreenLockManager::GetInstance()->Lock(ids[0]);
481     if (ret != ScreenLock::E_SCREENLOCK_OK) {
482         LOGE("Lock user screen by screen lock manager failed.");
483         StorageRadar::ReportOsAccountResult("ForceLockUserScreen::Lock", ret, ids[0]);
484         return;
485     }
486     LOGI("Force lock user screen and request strong auth success for userId = %{public}d.", ids[0]);
487 #endif
488 }
489 
OnRemoteDied(const wptr<IRemoteObject> & remote)490 void SdDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
491 {
492     LOGE("StorageDaemonCommunication::OnRemoteDied, storage_daemon process has died.");
493     DelayedSingleton<StorageDaemonCommunication>::GetInstance()->ResetSdProxy();
494     DelayedSingleton<StorageDaemonCommunication>::GetInstance()->ForceLockUserScreen();
495 }
496 
CreateShareFile(const std::vector<std::string> & uriList,uint32_t tokenId,uint32_t flag)497 std::vector<int32_t> StorageDaemonCommunication::CreateShareFile(const std::vector<std::string> &uriList,
498                                                                  uint32_t tokenId, uint32_t flag)
499 {
500     LOGI("enter");
501     int32_t err = Connect();
502     if (err != E_OK) {
503         LOGE("Connect failed");
504         return std::vector<int32_t>{err};
505     }
506     if (storageDaemon_ == nullptr) {
507         LOGE("StorageDaemonCommunication::Connect service nullptr");
508         return std::vector<int32_t>{err};
509     }
510     return storageDaemon_->CreateShareFile(uriList, tokenId, flag);
511 }
512 
DeleteShareFile(uint32_t tokenId,const std::vector<std::string> & uriList)513 int32_t StorageDaemonCommunication::DeleteShareFile(uint32_t tokenId, const std::vector<std::string> &uriList)
514 {
515     LOGI("enter");
516     int32_t err = Connect();
517     if (err != E_OK) {
518         LOGE("Connect failed");
519         return err;
520     }
521     if (storageDaemon_ == nullptr) {
522         LOGE("StorageDaemonCommunication::Connect service nullptr");
523         return E_SERVICE_IS_NULLPTR;
524     }
525     return storageDaemon_->DeleteShareFile(tokenId, uriList);
526 }
527 
SetBundleQuota(const std::string & bundleName,int32_t uid,const std::string & bundleDataDirPath,int32_t limitSizeMb)528 int32_t StorageDaemonCommunication::SetBundleQuota(const std::string &bundleName, int32_t uid,
529     const std::string &bundleDataDirPath, int32_t limitSizeMb)
530 {
531     LOGD("enter");
532     int32_t err = Connect();
533     if (err != E_OK) {
534         LOGE("Connect failed");
535         return err;
536     }
537     if (storageDaemon_ == nullptr) {
538         LOGE("StorageDaemonCommunication::Connect service nullptr");
539         return E_SERVICE_IS_NULLPTR;
540     }
541     return storageDaemon_->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb);
542 }
543 
GetOccupiedSpace(int32_t idType,int32_t id,int64_t & size)544 int32_t StorageDaemonCommunication::GetOccupiedSpace(int32_t idType, int32_t id, int64_t &size)
545 {
546     LOGD("enter");
547     int32_t err = Connect();
548     if (err != E_OK) {
549         LOGE("Connect failed");
550         return err;
551     }
552     if (storageDaemon_ == nullptr) {
553         LOGE("StorageDaemonCommunication::Connect service nullptr");
554         return E_SERVICE_IS_NULLPTR;
555     }
556     return storageDaemon_->GetOccupiedSpace(idType, id, size);
557 }
558 
MountCryptoPathAgain(int32_t userId)559 int32_t StorageDaemonCommunication::MountCryptoPathAgain(int32_t userId)
560 {
561     int32_t err = Connect();
562     if (err != E_OK) {
563         LOGE("Connect failed");
564         StorageRadar::ReportUserManager("StorageDaemonCommunication::MountCryptoPathAgain::Connect", userId, err, "");
565         return err;
566     }
567     if (storageDaemon_ == nullptr) {
568         LOGE("StorageDaemonCommunication::Connect service nullptr");
569         StorageRadar::ReportUserManager("StorageDaemonCommunication::MountCryptoPathAgain",
570             userId, E_SERVICE_IS_NULLPTR, "");
571         return E_SERVICE_IS_NULLPTR;
572     }
573     return storageDaemon_->MountCryptoPathAgain(userId);
574 }
575 
GenerateAppkey(uint32_t userId,uint32_t hashId,std::string & keyId)576 int32_t StorageDaemonCommunication::GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId)
577 {
578     int32_t err = Connect();
579     if (err != E_OK) {
580         LOGE("Connect failed");
581         return err;
582     }
583     if (storageDaemon_ == nullptr) {
584         LOGE("StorageDaemonCommunication::Connect service nullptr");
585         return E_SERVICE_IS_NULLPTR;
586     }
587     return storageDaemon_->GenerateAppkey(userId, hashId, keyId);
588 }
589 
DeleteAppkey(uint32_t userId,const std::string keyId)590 int32_t StorageDaemonCommunication::DeleteAppkey(uint32_t userId, const std::string keyId)
591 {
592     int32_t err = Connect();
593     if (err != E_OK) {
594         LOGE("Connect failed");
595         return err;
596     }
597     if (storageDaemon_ == nullptr) {
598         LOGE("StorageDaemonCommunication::Connect service nullptr");
599         return E_SERVICE_IS_NULLPTR;
600     }
601     return storageDaemon_->DeleteAppkey(userId, keyId);
602 }
603 
CreateRecoverKey(uint32_t userId,uint32_t userType,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)604 int32_t StorageDaemonCommunication::CreateRecoverKey(uint32_t userId,
605                                                      uint32_t userType,
606                                                      const std::vector<uint8_t> &token,
607                                                      const std::vector<uint8_t> &secret)
608 {
609     LOGI("enter");
610     int32_t err = Connect();
611     if (err != E_OK) {
612         LOGE("Connect failed");
613         return err;
614     }
615     if (storageDaemon_ == nullptr) {
616         LOGE("StorageDaemonCommunication::Connect service nullptr");
617         return E_SERVICE_IS_NULLPTR;
618     }
619     return storageDaemon_->CreateRecoverKey(userId, userType, token, secret);
620 }
621 
SetRecoverKey(const std::vector<uint8_t> & key)622 int32_t StorageDaemonCommunication::SetRecoverKey(const std::vector<uint8_t> &key)
623 {
624     LOGI("enter");
625     int32_t err = Connect();
626     if (err != E_OK) {
627         LOGE("Connect failed");
628         return err;
629     }
630     if (storageDaemon_ == nullptr) {
631         LOGE("StorageDaemonCommunication::Connect service nullptr");
632         return E_SERVICE_IS_NULLPTR;
633     }
634     return storageDaemon_->SetRecoverKey(key);
635 }
636 
GetBundleStatsForIncrease(uint32_t userId,const std::vector<std::string> & bundleNames,const std::vector<int64_t> & incrementalBackTimes,std::vector<int64_t> & pkgFileSizes,std::vector<int64_t> & incPkgFileSizes)637 int32_t StorageDaemonCommunication::GetBundleStatsForIncrease(uint32_t userId,
638     const std::vector<std::string> &bundleNames, const std::vector<int64_t> &incrementalBackTimes,
639     std::vector<int64_t> &pkgFileSizes, std::vector<int64_t> &incPkgFileSizes)
640 {
641     int32_t err = Connect();
642     if (err != E_OK) {
643         LOGE("Connect failed");
644         return err;
645     }
646     if (storageDaemon_ == nullptr) {
647         LOGE("StorageDaemonCommunication::Connect service nullptr");
648         return E_SERVICE_IS_NULLPTR;
649     }
650     return storageDaemon_->GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes,
651         incPkgFileSizes);
652 }
653 
UpdateMemoryPara(int32_t size,int32_t & oldSize)654 int32_t StorageDaemonCommunication::UpdateMemoryPara(int32_t size, int32_t &oldSize)
655 {
656     LOGI("StorageDaemonCommunication::UpdateMemoryPara");
657     int32_t err = Connect();
658     if (err != E_OK) {
659         LOGE("Connect failed");
660         return err;
661     }
662     if (storageDaemon_ == nullptr) {
663         LOGE("StorageDaemonCommunication::Connect service nullptr");
664         return E_SERVICE_IS_NULLPTR;
665     }
666     return storageDaemon_->UpdateMemoryPara(size, oldSize);
667 }
668 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)669 int32_t StorageDaemonCommunication::MountDfsDocs(int32_t userId, const std::string &relativePath,
670     const std::string &networkId, const std::string &deviceId)
671 {
672     LOGI("StorageDaemonCommunication::MountDfsDocs start.");
673     int32_t err = Connect();
674     if (err != E_OK) {
675         LOGE("Connect failed");
676         return err;
677     }
678     if (storageDaemon_ == nullptr) {
679         LOGE("StorageDaemonCommunication::Connect service nullptr");
680         return E_SERVICE_IS_NULLPTR;
681     }
682     return storageDaemon_->MountDfsDocs(userId, relativePath, networkId, deviceId);
683 }
684 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)685 int32_t StorageDaemonCommunication::UMountDfsDocs(int32_t userId, const std::string &relativePath,
686     const std::string &networkId, const std::string &deviceId)
687 {
688     LOGI("StorageDaemonCommunication::UMountDfsDocs start.");
689     int32_t err = Connect();
690     if (err != E_OK) {
691         LOGE("Connect failed");
692         return err;
693     }
694     if (storageDaemon_ == nullptr) {
695         LOGE("StorageDaemonCommunication::Connect service nullptr");
696         return E_SERVICE_IS_NULLPTR;
697     }
698     return storageDaemon_->UMountDfsDocs(userId, relativePath, networkId, deviceId);
699 }
700 
MountMediaFuse(int32_t userId,int32_t & devFd)701 int32_t StorageDaemonCommunication::MountMediaFuse(int32_t userId, int32_t &devFd)
702 {
703 #ifdef STORAGE_SERVICE_MEDIA_FUSE
704     int32_t err = Connect();
705     if (err != E_OK) {
706         LOGE("Connect failed");
707         return err;
708     }
709     if (storageDaemon_ == nullptr) {
710         LOGE("StorageDaemonCommunication::Connect service nullptr");
711         return E_SERVICE_IS_NULLPTR;
712     }
713     return storageDaemon_->MountMediaFuse(userId, devFd);
714 #else
715     return E_OK;
716 #endif
717 }
718 
UMountMediaFuse(int32_t userId)719 int32_t StorageDaemonCommunication::UMountMediaFuse(int32_t userId)
720 {
721 #ifdef STORAGE_SERVICE_MEDIA_FUSE
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_->UMountMediaFuse(userId);
732 #else
733     return E_OK;
734 #endif
735 }
736 
MountFileMgrFuse(int32_t userId,const std::string & path,int32_t & fuseFd)737 int32_t StorageDaemonCommunication::MountFileMgrFuse(int32_t userId, const std::string &path, int32_t &fuseFd)
738 {
739     int32_t err = Connect();
740     if (err != E_OK) {
741         LOGE("Connect failed");
742         return err;
743     }
744     if (storageDaemon_ == nullptr) {
745         LOGE("StorageDaemonCommunication::Connect service nullptr");
746         return E_SERVICE_IS_NULLPTR;
747     }
748     return storageDaemon_->MountFileMgrFuse(userId, path, fuseFd);
749 }
750 
UMountFileMgrFuse(int32_t userId,const std::string & path)751 int32_t StorageDaemonCommunication::UMountFileMgrFuse(int32_t userId, const std::string &path)
752 {
753     int32_t err = Connect();
754     if (err != E_OK) {
755         LOGE("Connect failed");
756         return err;
757     }
758     if (storageDaemon_ == nullptr) {
759         LOGE("StorageDaemonCommunication::Connect service nullptr");
760         return E_SERVICE_IS_NULLPTR;
761     }
762     return storageDaemon_->UMountFileMgrFuse(userId, path);
763 }
764 } // namespace StorageManager
765 } // namespace OHOS
766