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 "storage_daemon_communication/storage_daemon_communication.h"
17
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20
21 #include "ipc/istorage_daemon.h"
22 #include "ipc/storage_daemon.h"
23 #include "ipc/storage_daemon_proxy.h"
24 #include "storage_service_errno.h"
25 #include "storage_service_log.h"
26
27 namespace OHOS {
28 namespace StorageManager {
StorageDaemonCommunication()29 StorageDaemonCommunication::StorageDaemonCommunication()
30 {
31 LOGI("DEBUG StorageDaemonCommunication constructer");
32 storageDaemon_ = nullptr;
33 }
34
~StorageDaemonCommunication()35 StorageDaemonCommunication::~StorageDaemonCommunication()
36 {
37 LOGI("DEBUG ~StorageDaemonCommunication destructer ~");
38 }
39
Connect()40 int32_t StorageDaemonCommunication::Connect()
41 {
42 LOGI("StorageDaemonCommunication::Connect start");
43 std::lock_guard<std::mutex> lock(mutex_);
44 if (storageDaemon_ == nullptr) {
45 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46 if (sam == nullptr) {
47 LOGE("StorageDaemonCommunication::Connect samgr nullptr");
48 return E_SA_IS_NULLPTR;
49 }
50 auto object = sam->GetSystemAbility(STORAGE_MANAGER_DAEMON_ID);
51 if (object == nullptr) {
52 LOGE("StorageDaemonCommunication::Connect object nullptr");
53 return E_REMOTE_IS_NULLPTR;
54 }
55 storageDaemon_ = iface_cast<OHOS::StorageDaemon::IStorageDaemon>(object);
56 if (storageDaemon_ == nullptr) {
57 LOGE("StorageDaemonCommunication::Connect service nullptr");
58 return E_SERVICE_IS_NULLPTR;
59 }
60 deathRecipient_ = new (std::nothrow) SdDeathRecipient();
61 if (!deathRecipient_) {
62 LOGE("StorageDaemonCommunication::Connect failed to create death recipient");
63 return E_DEATH_RECIPIENT_IS_NULLPTR;
64 }
65
66 storageDaemon_->AsObject()->AddDeathRecipient(deathRecipient_);
67 }
68 LOGI("StorageDaemonCommunication::Connect end");
69 return E_OK;
70 }
71
PrepareAddUser(int32_t userId,uint32_t flags)72 int32_t StorageDaemonCommunication::PrepareAddUser(int32_t userId, uint32_t flags)
73 {
74 LOGI("StorageDaemonCommunication::PrepareAddUser start");
75 int32_t err = Connect();
76 if (err != E_OK) {
77 LOGE("StorageDaemonCommunication::PrepareAddUser connect failed");
78 return err;
79 }
80 return storageDaemon_->PrepareUserDirs(userId, flags);
81 }
82
RemoveUser(int32_t userId,uint32_t flags)83 int32_t StorageDaemonCommunication::RemoveUser(int32_t userId, uint32_t flags)
84 {
85 LOGI("StorageDaemonCommunication::RemoveUser start");
86 int32_t err = Connect();
87 if (err != E_OK) {
88 LOGE("StorageDaemonCommunication::RemoveUser connect failed");
89 return err;
90 }
91 return storageDaemon_->DestroyUserDirs(userId, flags);
92 }
93
PrepareStartUser(int32_t userId)94 int32_t StorageDaemonCommunication::PrepareStartUser(int32_t userId)
95 {
96 LOGI("StorageDaemonCommunication::PrepareStartUser start");
97 int32_t err = Connect();
98 if (err != E_OK) {
99 LOGE("StorageDaemonCommunication::PrepareStartUser connect failed");
100 return err;
101 }
102 return storageDaemon_->StartUser(userId);
103 }
104
StopUser(int32_t userId)105 int32_t StorageDaemonCommunication::StopUser(int32_t userId)
106 {
107 LOGI("StorageDaemonCommunication::StopUser start");
108 int32_t err = Connect();
109 if (err != E_OK) {
110 LOGE("StorageDaemonCommunication::StopUser connect failed");
111 return err;
112 }
113 return storageDaemon_->StopUser(userId);
114 }
115
Mount(std::string volumeId,int32_t flag)116 int32_t StorageDaemonCommunication::Mount(std::string volumeId, int32_t flag)
117 {
118 LOGI("StorageDaemonCommunication::mount start");
119 int32_t err = Connect();
120 if (err != E_OK) {
121 LOGE("StorageDaemonCommunication::mount connect failed");
122 return err;
123 }
124 return storageDaemon_->Mount(volumeId, flag);
125 }
126
Unmount(std::string volumeId)127 int32_t StorageDaemonCommunication::Unmount(std::string volumeId)
128 {
129 LOGI("StorageDaemonCommunication::unmount start");
130 int32_t err = Connect();
131 if (err != E_OK) {
132 LOGE("StorageDaemonCommunication::unmount connect failed");
133 return err;
134 }
135 return storageDaemon_->UMount(volumeId);
136 }
137
Check(std::string volumeId)138 int32_t StorageDaemonCommunication::Check(std::string volumeId)
139 {
140 LOGI("StorageDaemonCommunication::check start");
141 int32_t err = Connect();
142 if (err != E_OK) {
143 LOGE("StorageDaemonCommunication::check connect failed");
144 return err;
145 }
146 return storageDaemon_->Check(volumeId);
147 }
148
Partition(std::string diskId,int32_t type)149 int32_t StorageDaemonCommunication::Partition(std::string diskId, int32_t type)
150 {
151 LOGI("StorageDaemonCommunication::Partition start");
152 int32_t err = Connect();
153 if (err != E_OK) {
154 LOGE("StorageDaemonCommunication::Partition connect failed");
155 return err;
156 }
157 return storageDaemon_->Partition(diskId, type);
158 }
159
Format(std::string volumeId,std::string type)160 int32_t StorageDaemonCommunication::Format(std::string volumeId, std::string type)
161 {
162 LOGI("StorageDaemonCommunication::Format start");
163 int32_t err = Connect();
164 if (err != E_OK) {
165 LOGE("StorageDaemonCommunication::Format connect failed");
166 return err;
167 }
168 return storageDaemon_->Format(volumeId, type);
169 }
170
SetVolumeDescription(std::string volumeId,std::string description)171 int32_t StorageDaemonCommunication::SetVolumeDescription(std::string volumeId, std::string description)
172 {
173 LOGI("StorageDaemonCommunication::SetVolumeDescription start");
174 int32_t err = Connect();
175 if (err != E_OK) {
176 LOGE("StorageDaemonCommunication::SetVolumeDescription connect failed");
177 return err;
178 }
179 return storageDaemon_->SetVolumeDescription(volumeId, description);
180 }
181
GenerateUserKeys(uint32_t userId,uint32_t flags)182 int32_t StorageDaemonCommunication::GenerateUserKeys(uint32_t userId, uint32_t flags)
183 {
184 LOGI("enter");
185 int32_t err = Connect();
186 if (err != E_OK) {
187 LOGE("Connect failed");
188 return err;
189 }
190 return storageDaemon_->GenerateUserKeys(userId, flags);
191 }
192
DeleteUserKeys(uint32_t userId)193 int32_t StorageDaemonCommunication::DeleteUserKeys(uint32_t userId)
194 {
195 LOGI("enter");
196 int32_t err = Connect();
197 if (err != E_OK) {
198 LOGE("Connect failed");
199 return err;
200 }
201 return storageDaemon_->DeleteUserKeys(userId);
202 }
203
UpdateUserAuth(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)204 int32_t StorageDaemonCommunication::UpdateUserAuth(uint32_t userId,
205 const std::vector<uint8_t> &token,
206 const std::vector<uint8_t> &oldSecret,
207 const std::vector<uint8_t> &newSecret)
208 {
209 LOGI("enter");
210 int32_t err = Connect();
211 if (err != E_OK) {
212 LOGE("Connect failed");
213 return err;
214 }
215 return storageDaemon_->UpdateUserAuth(userId, token, oldSecret, newSecret);
216 }
217
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)218 int32_t StorageDaemonCommunication::ActiveUserKey(uint32_t userId,
219 const std::vector<uint8_t> &token,
220 const std::vector<uint8_t> &secret)
221 {
222 LOGI("enter");
223 int32_t err = Connect();
224 if (err != E_OK) {
225 LOGE("Connect failed");
226 return err;
227 }
228 return storageDaemon_->ActiveUserKey(userId, token, secret);
229 }
230
InactiveUserKey(uint32_t userId)231 int32_t StorageDaemonCommunication::InactiveUserKey(uint32_t userId)
232 {
233 LOGI("enter");
234 int32_t err = Connect();
235 if (err != E_OK) {
236 LOGE("Connect failed");
237 return err;
238 }
239 return storageDaemon_->InactiveUserKey(userId);
240 }
241
UpdateKeyContext(uint32_t userId)242 int32_t StorageDaemonCommunication::UpdateKeyContext(uint32_t userId)
243 {
244 LOGI("enter");
245 int32_t err = Connect();
246 if (err != E_OK) {
247 LOGE("Connect failed");
248 return err;
249 }
250 return storageDaemon_->UpdateKeyContext(userId);
251 }
252
ResetSdProxy()253 int32_t StorageDaemonCommunication::ResetSdProxy()
254 {
255 LOGD("enter");
256 std::lock_guard<std::mutex> lock(mutex_);
257 if ((storageDaemon_ != nullptr) && (storageDaemon_->AsObject() != nullptr)) {
258 storageDaemon_->AsObject()->RemoveDeathRecipient(deathRecipient_);
259 }
260 storageDaemon_ = nullptr;
261
262 return E_OK;
263 }
264
OnRemoteDied(const wptr<IRemoteObject> & remote)265 void SdDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
266 {
267 DelayedSingleton<StorageDaemonCommunication>::GetInstance()->ResetSdProxy();
268 }
269
CreateShareFile(std::string uri,uint32_t tokenId,uint32_t flag)270 int32_t StorageDaemonCommunication::CreateShareFile(std::string uri, uint32_t tokenId, uint32_t flag)
271 {
272 LOGI("enter");
273 int32_t err = Connect();
274 if (err != E_OK) {
275 LOGE("Connect failed");
276 return err;
277 }
278 return storageDaemon_->CreateShareFile(uri, tokenId, flag);
279 }
280
DeleteShareFile(uint32_t tokenId,std::vector<std::string> sharePathList)281 int32_t StorageDaemonCommunication::DeleteShareFile(uint32_t tokenId, std::vector<std::string>sharePathList)
282 {
283 LOGI("enter");
284 int32_t err = Connect();
285 if (err != E_OK) {
286 LOGE("Connect failed");
287 return err;
288 }
289 return storageDaemon_->DeleteShareFile(tokenId, sharePathList);
290 }
291 } // namespace StorageManager
292 } // namespace OHOS
293