• 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_proxy.h"
17 #include "storage_service_errno.h"
18 #include "storage_service_log.h"
19 
20 namespace OHOS {
21 namespace StorageDaemon {
StorageDaemonProxy(const sptr<IRemoteObject> & impl)22 StorageDaemonProxy::StorageDaemonProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IStorageDaemon>(impl)
23 {}
24 
Shutdown()25 int32_t StorageDaemonProxy::Shutdown()
26 {
27     MessageParcel data;
28     MessageParcel reply;
29     MessageOption option(MessageOption::TF_SYNC);
30 
31     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
32         return E_WRITE_DESCRIPTOR_ERR;
33     }
34 
35     return SendRequest(SHUTDOWN, data, reply, option);
36 }
37 
Mount(std::string volId,uint32_t flags)38 int32_t StorageDaemonProxy::Mount(std::string volId, uint32_t flags)
39 {
40     MessageParcel data;
41     MessageParcel reply;
42     MessageOption option(MessageOption::TF_SYNC);
43     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
44         return E_WRITE_DESCRIPTOR_ERR;
45     }
46 
47     if (!data.WriteString(volId)) {
48         return E_WRITE_PARCEL_ERR;
49     }
50 
51     if (!data.WriteUint32(flags)) {
52         return E_WRITE_PARCEL_ERR;
53     }
54 
55     int err = SendRequest(MOUNT, data, reply, option);
56     if (err != E_OK) {
57         return err;
58     }
59 
60     return reply.ReadInt32();
61 }
62 
UMount(std::string volId)63 int32_t StorageDaemonProxy::UMount(std::string volId)
64 {
65     MessageParcel data;
66     MessageParcel reply;
67     MessageOption option(MessageOption::TF_SYNC);
68     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
69         return E_WRITE_DESCRIPTOR_ERR;
70     }
71 
72     if (!data.WriteString(volId)) {
73         return E_WRITE_PARCEL_ERR;
74     }
75 
76     int err = SendRequest(UMOUNT, data, reply, option);
77     if (err != E_OK) {
78         return err;
79     }
80 
81     return reply.ReadInt32();
82 }
83 
Check(std::string volId)84 int32_t StorageDaemonProxy::Check(std::string volId)
85 {
86     MessageParcel data;
87     MessageParcel reply;
88     MessageOption option(MessageOption::TF_SYNC);
89     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
90         return E_WRITE_DESCRIPTOR_ERR;
91     }
92 
93     if (!data.WriteString(volId)) {
94         return E_WRITE_PARCEL_ERR;
95     }
96 
97     int err = SendRequest(CHECK, data, reply, option);
98     if (err != E_OK) {
99         return err;
100     }
101 
102     return reply.ReadInt32();
103 }
104 
Format(std::string volId,std::string fsType)105 int32_t StorageDaemonProxy::Format(std::string volId, std::string fsType)
106 {
107     MessageParcel data;
108     MessageParcel reply;
109     MessageOption option(MessageOption::TF_SYNC);
110     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
111         return E_WRITE_DESCRIPTOR_ERR;
112     }
113 
114     if (!data.WriteString(volId)) {
115         return E_WRITE_PARCEL_ERR;
116     }
117 
118     if (!data.WriteString(fsType)) {
119         return E_WRITE_PARCEL_ERR;
120     }
121 
122     int err = SendRequest(FORMAT, data, reply, option);
123     if (err != E_OK) {
124         return err;
125     }
126 
127     return reply.ReadInt32();
128 }
129 
Partition(std::string diskId,int32_t type)130 int32_t StorageDaemonProxy::Partition(std::string diskId, int32_t type)
131 {
132     MessageParcel data;
133     MessageParcel reply;
134     MessageOption option(MessageOption::TF_SYNC);
135     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
136         return E_WRITE_DESCRIPTOR_ERR;
137     }
138 
139     if (!data.WriteString(diskId)) {
140         return E_WRITE_PARCEL_ERR;
141     }
142 
143     if (!data.WriteInt32(type)) {
144         return E_WRITE_PARCEL_ERR;
145     }
146 
147     int err = SendRequest(PARTITION, data, reply, option);
148     if (err != E_OK) {
149         return err;
150     }
151 
152     return reply.ReadInt32();
153 }
154 
SetVolumeDescription(std::string volId,std::string description)155 int32_t StorageDaemonProxy::SetVolumeDescription(std::string volId, std::string description)
156 {
157     MessageParcel data;
158     MessageParcel reply;
159     MessageOption option(MessageOption::TF_SYNC);
160     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
161         return E_WRITE_DESCRIPTOR_ERR;
162     }
163 
164     if (!data.WriteString(volId)) {
165         return E_WRITE_PARCEL_ERR;
166     }
167 
168     if (!data.WriteString(description)) {
169         return E_WRITE_PARCEL_ERR;
170     }
171 
172     int err = SendRequest(SET_VOL_DESC, data, reply, option);
173     if (err != E_OK) {
174         return err;
175     }
176 
177     return reply.ReadInt32();
178 }
179 
PrepareUserDirs(int32_t userId,uint32_t flags)180 int32_t StorageDaemonProxy::PrepareUserDirs(int32_t userId, uint32_t flags)
181 {
182     MessageParcel data;
183     MessageParcel reply;
184     MessageOption option(MessageOption::TF_SYNC);
185 
186     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
187         return E_WRITE_DESCRIPTOR_ERR;
188     }
189 
190     if (!data.WriteInt32(userId)) {
191         return E_WRITE_PARCEL_ERR;
192     }
193 
194     if (!data.WriteUint32(flags)) {
195         return E_WRITE_PARCEL_ERR;
196     }
197 
198     int err = SendRequest(PREPARE_USER_DIRS, data, reply, option);
199     if (err != E_OK) {
200         return err;
201     }
202 
203     return reply.ReadInt32();
204 }
205 
DestroyUserDirs(int32_t userId,uint32_t flags)206 int32_t StorageDaemonProxy::DestroyUserDirs(int32_t userId, uint32_t flags)
207 {
208     MessageParcel data;
209     MessageParcel reply;
210     MessageOption option(MessageOption::TF_SYNC);
211 
212     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
213         return E_WRITE_DESCRIPTOR_ERR;
214     }
215 
216     if (!data.WriteInt32(userId)) {
217         return E_WRITE_PARCEL_ERR;
218     }
219 
220     if (!data.WriteUint32(flags)) {
221         return E_WRITE_PARCEL_ERR;
222     }
223 
224     int err = SendRequest(DESTROY_USER_DIRS, data, reply, option);
225     if (err != E_OK) {
226         return err;
227     }
228 
229     return reply.ReadInt32();
230 }
231 
StartUser(int32_t userId)232 int32_t StorageDaemonProxy::StartUser(int32_t userId)
233 {
234     MessageParcel data;
235     MessageParcel reply;
236     MessageOption option(MessageOption::TF_SYNC);
237 
238     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
239         return E_WRITE_DESCRIPTOR_ERR;
240     }
241 
242     if (!data.WriteInt32(userId)) {
243         return E_WRITE_PARCEL_ERR;
244     }
245 
246     int err = SendRequest(START_USER, data, reply, option);
247     if (err != E_OK) {
248         return err;
249     }
250 
251     return reply.ReadInt32();
252 }
253 
StopUser(int32_t userId)254 int32_t StorageDaemonProxy::StopUser(int32_t userId)
255 {
256     MessageParcel data;
257     MessageParcel reply;
258     MessageOption option(MessageOption::TF_SYNC);
259 
260     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
261         return E_WRITE_DESCRIPTOR_ERR;
262     }
263 
264     if (!data.WriteInt32(userId)) {
265         return E_WRITE_PARCEL_ERR;
266     }
267 
268     int err = SendRequest(STOP_USER, data, reply, option);
269     if (err != E_OK) {
270         return err;
271     }
272 
273     return reply.ReadUint32();
274 }
275 
InitGlobalKey(void)276 int32_t StorageDaemonProxy::InitGlobalKey(void)
277 {
278     MessageParcel data;
279     MessageParcel reply;
280     MessageOption option(MessageOption::TF_SYNC);
281     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
282         return E_WRITE_DESCRIPTOR_ERR;
283     }
284 
285     int err = SendRequest(INIT_GLOBAL_KEY, data, reply, option);
286     if (err != E_OK) {
287         return err;
288     }
289 
290     return reply.ReadUint32();
291 }
292 
InitGlobalUserKeys(void)293 int32_t StorageDaemonProxy::InitGlobalUserKeys(void)
294 {
295     MessageParcel data;
296     MessageParcel reply;
297     MessageOption option(MessageOption::TF_SYNC);
298     LOGI("start");
299     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
300         return E_WRITE_DESCRIPTOR_ERR;
301     }
302 
303     int err = SendRequest(INIT_GLOBAL_USER_KEYS, data, reply, option);
304     if (err != E_OK) {
305         return err;
306     }
307 
308     return reply.ReadUint32();
309 }
310 
GenerateUserKeys(uint32_t userId,uint32_t flags)311 int32_t StorageDaemonProxy::GenerateUserKeys(uint32_t userId, uint32_t flags)
312 {
313     MessageParcel data;
314     MessageParcel reply;
315     MessageOption option(MessageOption::TF_SYNC);
316 
317     LOGI("start");
318     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
319         return E_WRITE_DESCRIPTOR_ERR;
320     }
321 
322     if (!data.WriteUint32(userId)) {
323         return E_WRITE_PARCEL_ERR;
324     }
325     if (!data.WriteUint32(flags)) {
326         return E_WRITE_PARCEL_ERR;
327     }
328 
329     int err = SendRequest(CREATE_USER_KEYS, data, reply, option);
330     if (err != E_OK) {
331         return err;
332     }
333 
334     return reply.ReadUint32();
335 }
336 
DeleteUserKeys(uint32_t userId)337 int32_t StorageDaemonProxy::DeleteUserKeys(uint32_t userId)
338 {
339     MessageParcel data;
340     MessageParcel reply;
341     MessageOption option(MessageOption::TF_SYNC);
342 
343     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
344         return E_WRITE_DESCRIPTOR_ERR;
345     }
346 
347     if (!data.WriteUint32(userId)) {
348         return E_WRITE_PARCEL_ERR;
349     }
350     int err = SendRequest(DELETE_USER_KEYS, data, reply, option);
351     if (err != E_OK) {
352         return err;
353     }
354 
355     return reply.ReadInt32();
356 }
357 
UpdateUserAuth(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)358 int32_t StorageDaemonProxy::UpdateUserAuth(uint32_t userId,
359                                            const std::vector<uint8_t> &token,
360                                            const std::vector<uint8_t> &oldSecret,
361                                            const std::vector<uint8_t> &newSecret)
362 {
363     MessageParcel data;
364     MessageParcel reply;
365     MessageOption option(MessageOption::TF_SYNC);
366 
367     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
368         return E_WRITE_DESCRIPTOR_ERR;
369     }
370 
371     if (!data.WriteUint32(userId)) {
372         return E_WRITE_PARCEL_ERR;
373     }
374     if (!data.WriteUInt8Vector(token)) {
375         return E_WRITE_PARCEL_ERR;
376     }
377     if (!data.WriteUInt8Vector(oldSecret)) {
378         return E_WRITE_PARCEL_ERR;
379     }
380     if (!data.WriteUInt8Vector(newSecret)) {
381         return E_WRITE_PARCEL_ERR;
382     }
383 
384     int err = SendRequest(UPDATE_USER_AUTH, data, reply, option);
385     if (err != E_OK) {
386         return err;
387     }
388 
389     return reply.ReadInt32();
390 }
391 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)392 int32_t StorageDaemonProxy::ActiveUserKey(uint32_t userId,
393                                           const std::vector<uint8_t> &token,
394                                           const std::vector<uint8_t> &secret)
395 {
396     MessageParcel data;
397     MessageParcel reply;
398     MessageOption option(MessageOption::TF_SYNC);
399 
400     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
401         return E_WRITE_DESCRIPTOR_ERR;
402     }
403 
404     if (!data.WriteUint32(userId)) {
405         return E_WRITE_PARCEL_ERR;
406     }
407     if (!data.WriteUInt8Vector(token)) {
408         return E_WRITE_PARCEL_ERR;
409     }
410     if (!data.WriteUInt8Vector(secret)) {
411         return E_WRITE_PARCEL_ERR;
412     }
413 
414     int err = SendRequest(ACTIVE_USER_KEY, data, reply, option);
415     if (err != E_OK) {
416         return err;
417     }
418 
419     return reply.ReadInt32();
420 }
421 
InactiveUserKey(uint32_t userId)422 int32_t StorageDaemonProxy::InactiveUserKey(uint32_t userId)
423 {
424     MessageParcel data;
425     MessageParcel reply;
426     MessageOption option(MessageOption::TF_SYNC);
427 
428     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
429         return E_WRITE_DESCRIPTOR_ERR;
430     }
431 
432     if (!data.WriteUint32(userId)) {
433         return E_WRITE_PARCEL_ERR;
434     }
435     int err = SendRequest(INACTIVE_USER_KEY, data, reply, option);
436     if (err != E_OK) {
437         return err;
438     }
439 
440     return reply.ReadInt32();
441 }
442 
UpdateKeyContext(uint32_t userId)443 int32_t StorageDaemonProxy::UpdateKeyContext(uint32_t userId)
444 {
445     MessageParcel data;
446     MessageParcel reply;
447     MessageOption option(MessageOption::TF_SYNC);
448 
449     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
450         return E_WRITE_DESCRIPTOR_ERR;
451     }
452 
453     if (!data.WriteUint32(userId)) {
454         return E_WRITE_PARCEL_ERR;
455     }
456     int err = SendRequest(UPDATE_KEY_CONTEXT, data, reply, option);
457     if (err != E_OK) {
458         return err;
459     }
460 
461     return reply.ReadInt32();
462 }
463 
CreateShareFile(std::string uri,uint32_t tokenId,uint32_t flag)464 int32_t StorageDaemonProxy::CreateShareFile(std::string uri, uint32_t tokenId, uint32_t flag)
465 {
466     MessageParcel data;
467     MessageParcel reply;
468     MessageOption option(MessageOption::TF_SYNC);
469 
470     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
471         return E_WRITE_DESCRIPTOR_ERR;
472     }
473 
474     if (!data.WriteString(uri)) {
475         return E_WRITE_PARCEL_ERR;
476     }
477 
478     if (!data.WriteUint32(tokenId)) {
479         return E_WRITE_PARCEL_ERR;
480     }
481 
482     if (!data.WriteUint32(flag)) {
483         return E_WRITE_PARCEL_ERR;
484     }
485 
486     int err = SendRequest(CREATE_SHARE_FILE, data, reply, option);
487     if (err != E_OK) {
488         return err;
489     }
490 
491     return reply.ReadInt32();
492 }
493 
DeleteShareFile(uint32_t tokenId,std::vector<std::string> sharePathList)494 int32_t StorageDaemonProxy::DeleteShareFile(uint32_t tokenId, std::vector<std::string>sharePathList)
495 {
496     MessageParcel data;
497     MessageParcel reply;
498     MessageOption option(MessageOption::TF_ASYNC);
499 
500     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
501         return E_WRITE_DESCRIPTOR_ERR;
502     }
503 
504     if (!data.WriteUint32(tokenId)) {
505         return E_WRITE_PARCEL_ERR;
506     }
507 
508     if (!data.WriteStringVector(sharePathList)) {
509         return E_WRITE_PARCEL_ERR;
510     }
511 
512     int err = SendRequest(DELETE_SHARE_FILE, data, reply, option);
513     if (err != E_OK) {
514         return err;
515     }
516 
517     return reply.ReadInt32();
518 }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)519 int32_t StorageDaemonProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
520 {
521     sptr<IRemoteObject> remote = Remote();
522     if (remote == nullptr) {
523         LOGE("remote is nullptr, code = %{public}d", code);
524         return E_REMOTE_IS_NULLPTR;
525     }
526 
527     int32_t result = remote->SendRequest(code, data, reply, option);
528     if (result != E_OK) {
529         LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
530         return result;
531     }
532 
533     return E_OK;
534 }
535 } // StorageDaemon
536 } // OHOS
537