• 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 #include "storage_daemon_communication/storage_daemon_communication.h"
16 #include <system_ability_definition.h>
17 #include <iservice_registry.h>
18 #include "storage_service_log.h"
19 #include "storage_service_errno.h"
20 #include "ipc/istorage_daemon.h"
21 #include "ipc/storage_daemon.h"
22 #include "ipc/storage_daemon_proxy.h"
23 
24 namespace OHOS {
25 namespace StorageManager {
StorageDaemonCommunication()26 StorageDaemonCommunication::StorageDaemonCommunication()
27 {
28     LOGI("DEBUG StorageDaemonCommunication constructer");
29     storageDaemon_ = nullptr;
30 }
31 
~StorageDaemonCommunication()32 StorageDaemonCommunication::~StorageDaemonCommunication()
33 {
34     LOGI("DEBUG ~StorageDaemonCommunication destructer ~");
35 }
36 
Connect()37 int32_t StorageDaemonCommunication::Connect()
38 {
39     int32_t err = 0;
40     LOGI("StorageDaemonCommunication::Connect start");
41     if (storageDaemon_ == nullptr) {
42         auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43         if (sam == nullptr) {
44             LOGE("StorageDaemonCommunication::Connect samgr nullptr");
45             return E_IPC_ERROR;
46         }
47         auto object = sam->GetSystemAbility(STORAGE_MANAGER_DAEMON_ID);
48         if (object == nullptr) {
49             LOGE("StorageDaemonCommunication::Connect object nullptr");
50             return E_IPC_ERROR;
51         }
52         storageDaemon_ = iface_cast<OHOS::StorageDaemon::IStorageDaemon>(object);
53         if (storageDaemon_ == nullptr) {
54             LOGE("StorageDaemonCommunication::Connect service nullptr");
55             return E_IPC_ERROR;
56         }
57     }
58     LOGI("StorageDaemonCommunication::Connect end");
59     return err;
60 }
61 
PrepareAddUser(int32_t userId,uint32_t flags)62 int32_t StorageDaemonCommunication::PrepareAddUser(int32_t userId, uint32_t flags)
63 {
64     LOGI("StorageDaemonCommunication::PrepareAddUser start");
65 
66     if (Connect() != E_OK) {
67         LOGE("StorageDaemonCommunication::PrepareAddUser connect failed");
68         return E_IPC_ERROR;
69     }
70     return storageDaemon_->PrepareUserDirs(userId, flags);
71 }
72 
RemoveUser(int32_t userId,uint32_t flags)73 int32_t StorageDaemonCommunication::RemoveUser(int32_t userId, uint32_t flags)
74 {
75     LOGI("StorageDaemonCommunication::RemoveUser start");
76     if (Connect() != E_OK) {
77         LOGE("StorageDaemonCommunication::RemoveUser connect failed");
78         return E_IPC_ERROR;
79     }
80     return storageDaemon_->DestroyUserDirs(userId, flags);
81 }
82 
PrepareStartUser(int32_t userId)83 int32_t StorageDaemonCommunication::PrepareStartUser(int32_t userId)
84 {
85     LOGI("StorageDaemonCommunication::PrepareStartUser start");
86     if (Connect() != E_OK) {
87         LOGE("StorageDaemonCommunication::PrepareStartUser connect failed");
88         return E_IPC_ERROR;
89     }
90     return storageDaemon_->StartUser(userId);
91 }
92 
StopUser(int32_t userId)93 int32_t StorageDaemonCommunication::StopUser(int32_t userId)
94 {
95     LOGI("StorageDaemonCommunication::StopUser start");
96     if (Connect() != E_OK) {
97         LOGE("StorageDaemonCommunication::StopUser connect failed");
98         return E_IPC_ERROR;
99     }
100     return storageDaemon_->StopUser(userId);
101 }
102 
Mount(std::string volumeId,int32_t flag)103 int32_t StorageDaemonCommunication::Mount(std::string volumeId, int32_t flag)
104 {
105     LOGI("StorageDaemonCommunication::mount start");
106     if (Connect() != E_OK) {
107         LOGE("StorageDaemonCommunication::mount connect failed");
108         return E_IPC_ERROR;
109     }
110     return storageDaemon_->Mount(volumeId, flag);
111 }
112 
Unmount(std::string volumeId)113 int32_t StorageDaemonCommunication::Unmount(std::string volumeId)
114 {
115     LOGI("StorageDaemonCommunication::unmount start");
116     if (Connect() != E_OK) {
117         LOGE("StorageDaemonCommunication::unmount connect failed");
118         return E_IPC_ERROR;
119     }
120     return storageDaemon_->UMount(volumeId);
121 }
122 
Check(std::string volumeId)123 int32_t StorageDaemonCommunication::Check(std::string volumeId)
124 {
125     LOGI("StorageDaemonCommunication::check start");
126     if (Connect() != E_OK) {
127         LOGE("StorageDaemonCommunication::check connect failed");
128         return E_IPC_ERROR;
129     }
130     return storageDaemon_->Check(volumeId);
131 }
132 
Partition(std::string diskId,int32_t type)133 int32_t StorageDaemonCommunication::Partition(std::string diskId, int32_t type)
134 {
135     LOGI("StorageDaemonCommunication::Partition start");
136     if (Connect() != E_OK) {
137         LOGE("StorageDaemonCommunication::Partition connect failed");
138         return E_IPC_ERROR;
139     }
140     return storageDaemon_->Partition(diskId, type);
141 }
142 
GenerateUserKeys(uint32_t userId,uint32_t flags)143 int32_t StorageDaemonCommunication::GenerateUserKeys(uint32_t userId, uint32_t flags)
144 {
145     LOGI("enter");
146     if (Connect() != E_OK) {
147         LOGE("Connect failed");
148         return E_IPC_ERROR;
149     }
150     return storageDaemon_->GenerateUserKeys(userId, flags);
151 }
152 
DeleteUserKeys(uint32_t userId)153 int32_t StorageDaemonCommunication::DeleteUserKeys(uint32_t userId)
154 {
155     LOGI("enter");
156     if (Connect() != E_OK) {
157         LOGE("Connect failed");
158         return E_IPC_ERROR;
159     }
160     return storageDaemon_->DeleteUserKeys(userId);
161 }
162 
UpdateUserAuth(uint32_t userId,std::string auth,std::string compSecret)163 int32_t StorageDaemonCommunication::UpdateUserAuth(uint32_t userId, std::string auth, std::string compSecret)
164 {
165     LOGI("enter");
166     if (Connect() != E_OK) {
167         LOGE("Connect failed");
168         return E_IPC_ERROR;
169     }
170     return storageDaemon_->UpdateUserAuth(userId, auth, compSecret);
171 }
172 
ActiveUserKey(uint32_t userId,std::string auth,std::string compSecret)173 int32_t StorageDaemonCommunication::ActiveUserKey(uint32_t userId, std::string auth, std::string compSecret)
174 {
175     LOGI("enter");
176     if (Connect() != E_OK) {
177         LOGE("Connect failed");
178         return E_IPC_ERROR;
179     }
180     return storageDaemon_->ActiveUserKey(userId, auth, compSecret);
181 }
182 
InactiveUserKey(uint32_t userId)183 int32_t StorageDaemonCommunication::InactiveUserKey(uint32_t userId)
184 {
185     LOGI("enter");
186     if (Connect() != E_OK) {
187         LOGE("Connect failed");
188         return E_IPC_ERROR;
189     }
190     return storageDaemon_->InactiveUserKey(userId);
191 }
192 
UpdateKeyContext(uint32_t userId)193 int32_t StorageDaemonCommunication::UpdateKeyContext(uint32_t userId)
194 {
195     LOGI("enter");
196     if (Connect() != E_OK) {
197         LOGE("Connect failed");
198         return E_IPC_ERROR;
199     }
200     return storageDaemon_->UpdateKeyContext(userId);
201 }
202 } // StorageManager
203 } // OHOS
204