• 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 "ipc/storage_daemon_ipc_interface_code.h"
18 #include "storage_service_errno.h"
19 #include "storage_service_log.h"
20 
21 namespace OHOS {
22 namespace StorageDaemon {
23 constexpr size_t MAX_IPC_RAW_DATA_SIZE = 128 * 1024 * 1024;
StorageDaemonProxy(const sptr<IRemoteObject> & impl)24 StorageDaemonProxy::StorageDaemonProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IStorageDaemon>(impl)
25 {}
26 
WriteUriByRawData(MessageParcel & data,const std::vector<std::string> & uriVec)27 static bool WriteUriByRawData(MessageParcel &data, const std::vector<std::string> &uriVec)
28 {
29     MessageParcel tempParcel;
30     tempParcel.SetMaxCapacity(MAX_IPC_RAW_DATA_SIZE);
31     if (!tempParcel.WriteStringVector(uriVec)) {
32         LOGE("Write uris failed");
33         return false;
34     }
35     size_t dataSize = tempParcel.GetDataSize();
36     if (!data.WriteInt32(static_cast<int32_t>(dataSize))) {
37         LOGE("Write data size failed");
38         return false;
39     }
40     if (!data.WriteRawData(reinterpret_cast<uint8_t *>(tempParcel.GetData()), dataSize)) {
41         LOGE("Write raw data failed");
42         return false;
43     }
44     return true;
45 }
46 
WriteBatchUris(MessageParcel & data,const std::vector<std::string> & uriVec)47 bool WriteBatchUris(MessageParcel &data, const std::vector<std::string> &uriVec)
48 {
49     if (!data.WriteUint32(uriVec.size())) {
50         LOGE("Write uri size failed");
51         return false;
52     }
53     if (!WriteUriByRawData(data, uriVec)) {
54         LOGE("Write uri by raw data failed");
55         return false;
56     }
57     return true;
58 }
59 
Shutdown()60 int32_t StorageDaemonProxy::Shutdown()
61 {
62     MessageParcel data;
63     MessageParcel reply;
64     MessageOption option(MessageOption::TF_SYNC);
65 
66     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
67         return E_WRITE_DESCRIPTOR_ERR;
68     }
69 
70     return SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::SHUTDOWN), data, reply, option);
71 }
72 
Mount(const std::string & volId,uint32_t flags)73 int32_t StorageDaemonProxy::Mount(const std::string &volId, uint32_t flags)
74 {
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option(MessageOption::TF_SYNC);
78     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
79         return E_WRITE_DESCRIPTOR_ERR;
80     }
81 
82     if (!data.WriteString(volId)) {
83         return E_WRITE_PARCEL_ERR;
84     }
85 
86     if (!data.WriteUint32(flags)) {
87         return E_WRITE_PARCEL_ERR;
88     }
89 
90     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::MOUNT), data, reply, option);
91     if (err != E_OK) {
92         return err;
93     }
94 
95     return reply.ReadInt32();
96 }
97 
UMount(const std::string & volId)98 int32_t StorageDaemonProxy::UMount(const std::string &volId)
99 {
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option(MessageOption::TF_SYNC);
103     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
104         return E_WRITE_DESCRIPTOR_ERR;
105     }
106 
107     if (!data.WriteString(volId)) {
108         return E_WRITE_PARCEL_ERR;
109     }
110 
111     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::UMOUNT), data, reply, option);
112     if (err != E_OK) {
113         return err;
114     }
115 
116     return reply.ReadInt32();
117 }
118 
Check(const std::string & volId)119 int32_t StorageDaemonProxy::Check(const std::string &volId)
120 {
121     MessageParcel data;
122     MessageParcel reply;
123     MessageOption option(MessageOption::TF_SYNC);
124     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
125         return E_WRITE_DESCRIPTOR_ERR;
126     }
127 
128     if (!data.WriteString(volId)) {
129         return E_WRITE_PARCEL_ERR;
130     }
131 
132     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::CHECK), data, reply, option);
133     if (err != E_OK) {
134         return err;
135     }
136 
137     return reply.ReadInt32();
138 }
139 
Format(const std::string & volId,const std::string & fsType)140 int32_t StorageDaemonProxy::Format(const std::string &volId, const std::string &fsType)
141 {
142     MessageParcel data;
143     MessageParcel reply;
144     MessageOption option(MessageOption::TF_SYNC);
145     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
146         return E_WRITE_DESCRIPTOR_ERR;
147     }
148 
149     if (!data.WriteString(volId)) {
150         return E_WRITE_PARCEL_ERR;
151     }
152 
153     if (!data.WriteString(fsType)) {
154         return E_WRITE_PARCEL_ERR;
155     }
156 
157     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::FORMAT), data, reply, option);
158     if (err != E_OK) {
159         return err;
160     }
161 
162     return reply.ReadInt32();
163 }
164 
Partition(const std::string & diskId,int32_t type)165 int32_t StorageDaemonProxy::Partition(const std::string &diskId, int32_t type)
166 {
167     MessageParcel data;
168     MessageParcel reply;
169     MessageOption option(MessageOption::TF_SYNC);
170     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
171         return E_WRITE_DESCRIPTOR_ERR;
172     }
173 
174     if (!data.WriteString(diskId)) {
175         return E_WRITE_PARCEL_ERR;
176     }
177 
178     if (!data.WriteInt32(type)) {
179         return E_WRITE_PARCEL_ERR;
180     }
181 
182     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::PARTITION), data, reply, option);
183     if (err != E_OK) {
184         return err;
185     }
186 
187     return reply.ReadInt32();
188 }
189 
SetVolumeDescription(const std::string & volId,const std::string & description)190 int32_t StorageDaemonProxy::SetVolumeDescription(const std::string &volId, const std::string &description)
191 {
192     MessageParcel data;
193     MessageParcel reply;
194     MessageOption option(MessageOption::TF_SYNC);
195     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
196         return E_WRITE_DESCRIPTOR_ERR;
197     }
198 
199     if (!data.WriteString(volId)) {
200         return E_WRITE_PARCEL_ERR;
201     }
202 
203     if (!data.WriteString(description)) {
204         return E_WRITE_PARCEL_ERR;
205     }
206 
207     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC), data, reply, option);
208     if (err != E_OK) {
209         return err;
210     }
211 
212     return reply.ReadInt32();
213 }
214 
PrepareUserDirs(int32_t userId,uint32_t flags)215 int32_t StorageDaemonProxy::PrepareUserDirs(int32_t userId, uint32_t flags)
216 {
217     MessageParcel data;
218     MessageParcel reply;
219     MessageOption option(MessageOption::TF_SYNC);
220 
221     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
222         return E_WRITE_DESCRIPTOR_ERR;
223     }
224 
225     if (!data.WriteInt32(userId)) {
226         return E_WRITE_PARCEL_ERR;
227     }
228 
229     if (!data.WriteUint32(flags)) {
230         return E_WRITE_PARCEL_ERR;
231     }
232 
233     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS), data, reply, option);
234     if (err != E_OK) {
235         return err;
236     }
237 
238     return reply.ReadInt32();
239 }
240 
DestroyUserDirs(int32_t userId,uint32_t flags)241 int32_t StorageDaemonProxy::DestroyUserDirs(int32_t userId, uint32_t flags)
242 {
243     MessageParcel data;
244     MessageParcel reply;
245     MessageOption option(MessageOption::TF_SYNC);
246 
247     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
248         return E_WRITE_DESCRIPTOR_ERR;
249     }
250 
251     if (!data.WriteInt32(userId)) {
252         return E_WRITE_PARCEL_ERR;
253     }
254 
255     if (!data.WriteUint32(flags)) {
256         return E_WRITE_PARCEL_ERR;
257     }
258 
259     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS), data, reply, option);
260     if (err != E_OK) {
261         return err;
262     }
263 
264     return reply.ReadInt32();
265 }
266 
StartUser(int32_t userId)267 int32_t StorageDaemonProxy::StartUser(int32_t userId)
268 {
269     MessageParcel data;
270     MessageParcel reply;
271     MessageOption option(MessageOption::TF_SYNC);
272 
273     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
274         return E_WRITE_DESCRIPTOR_ERR;
275     }
276 
277     if (!data.WriteInt32(userId)) {
278         return E_WRITE_PARCEL_ERR;
279     }
280 
281     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::START_USER), data, reply, option);
282     if (err != E_OK) {
283         return err;
284     }
285 
286     return reply.ReadInt32();
287 }
288 
StopUser(int32_t userId)289 int32_t StorageDaemonProxy::StopUser(int32_t userId)
290 {
291     MessageParcel data;
292     MessageParcel reply;
293     MessageOption option(MessageOption::TF_SYNC);
294 
295     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
296         return E_WRITE_DESCRIPTOR_ERR;
297     }
298 
299     if (!data.WriteInt32(userId)) {
300         return E_WRITE_PARCEL_ERR;
301     }
302 
303     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::STOP_USER), data, reply, option);
304     if (err != E_OK) {
305         return err;
306     }
307 
308     return reply.ReadUint32();
309 }
310 
CompleteAddUser(int32_t userId)311 int32_t StorageDaemonProxy::CompleteAddUser(int32_t userId)
312 {
313     MessageParcel data;
314     MessageParcel reply;
315     MessageOption option(MessageOption::TF_SYNC);
316 
317     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
318         return E_WRITE_DESCRIPTOR_ERR;
319     }
320 
321     if (!data.WriteInt32(userId)) {
322         return E_WRITE_PARCEL_ERR;
323     }
324 
325     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::COMPLETE_ADD_USER), data, reply, option);
326     if (err != E_OK) {
327         return err;
328     }
329 
330     return reply.ReadUint32();
331 }
332 
InitGlobalKey(void)333 int32_t StorageDaemonProxy::InitGlobalKey(void)
334 {
335     MessageParcel data;
336     MessageParcel reply;
337     MessageOption option(MessageOption::TF_SYNC);
338     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
339         return E_WRITE_DESCRIPTOR_ERR;
340     }
341 
342     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY), data, reply, option);
343     if (err != E_OK) {
344         return err;
345     }
346 
347     return reply.ReadUint32();
348 }
349 
InitGlobalUserKeys(void)350 int32_t StorageDaemonProxy::InitGlobalUserKeys(void)
351 {
352     MessageParcel data;
353     MessageParcel reply;
354     MessageOption option(MessageOption::TF_SYNC);
355     LOGI("start");
356     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
357         return E_WRITE_DESCRIPTOR_ERR;
358     }
359 
360     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS), data, reply,
361         option);
362     if (err != E_OK) {
363         return err;
364     }
365 
366     return reply.ReadUint32();
367 }
368 
GenerateUserKeys(uint32_t userId,uint32_t flags)369 int32_t StorageDaemonProxy::GenerateUserKeys(uint32_t userId, uint32_t flags)
370 {
371     MessageParcel data;
372     MessageParcel reply;
373     MessageOption option(MessageOption::TF_SYNC);
374 
375     LOGI("start");
376     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
377         return E_WRITE_DESCRIPTOR_ERR;
378     }
379 
380     if (!data.WriteUint32(userId)) {
381         return E_WRITE_PARCEL_ERR;
382     }
383     if (!data.WriteUint32(flags)) {
384         return E_WRITE_PARCEL_ERR;
385     }
386 
387     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS), data, reply, option);
388     if (err != E_OK) {
389         return err;
390     }
391 
392     return reply.ReadUint32();
393 }
394 
DeleteUserKeys(uint32_t userId)395 int32_t StorageDaemonProxy::DeleteUserKeys(uint32_t userId)
396 {
397     MessageParcel data;
398     MessageParcel reply;
399     MessageOption option(MessageOption::TF_SYNC);
400 
401     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
402         return E_WRITE_DESCRIPTOR_ERR;
403     }
404 
405     if (!data.WriteUint32(userId)) {
406         return E_WRITE_PARCEL_ERR;
407     }
408     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS), data, reply, option);
409     if (err != E_OK) {
410         return err;
411     }
412 
413     return reply.ReadInt32();
414 }
415 
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)416 int32_t StorageDaemonProxy::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
417                                            const std::vector<uint8_t> &token,
418                                            const std::vector<uint8_t> &oldSecret,
419                                            const std::vector<uint8_t> &newSecret)
420 {
421     MessageParcel data;
422     MessageParcel reply;
423     MessageOption option(MessageOption::TF_SYNC);
424 
425     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
426         return E_WRITE_DESCRIPTOR_ERR;
427     }
428 
429     if (!data.WriteUint32(userId)) {
430         return E_WRITE_PARCEL_ERR;
431     }
432     if (!data.WriteUint64(secureUid)) {
433         return E_WRITE_PARCEL_ERR;
434     }
435     if (!data.WriteUInt8Vector(token)) {
436         return E_WRITE_PARCEL_ERR;
437     }
438     if (!data.WriteUInt8Vector(oldSecret)) {
439         return E_WRITE_PARCEL_ERR;
440     }
441     if (!data.WriteUInt8Vector(newSecret)) {
442         return E_WRITE_PARCEL_ERR;
443     }
444 
445     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH), data, reply, option);
446     if (err != E_OK) {
447         return err;
448     }
449 
450     return reply.ReadInt32();
451 }
452 
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)453 int32_t StorageDaemonProxy::UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> &authToken,
454                                                          const std::vector<uint8_t> &newSecret,
455                                                          uint64_t secureUid,
456                                                          uint32_t userId,
457                                                          std::vector<std::vector<uint8_t>> &plainText)
458 {
459     MessageParcel data;
460     MessageParcel reply;
461     MessageOption option(MessageOption::TF_SYNC);
462 
463     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
464         return E_WRITE_DESCRIPTOR_ERR;
465     }
466 
467     if (!data.WriteUint32(userId)) {
468         return E_WRITE_PARCEL_ERR;
469     }
470     if (!data.WriteUint64(secureUid)) {
471         return E_WRITE_PARCEL_ERR;
472     }
473     if (!data.WriteUInt8Vector(authToken)) {
474         return E_WRITE_PARCEL_ERR;
475     }
476     if (!data.WriteUInt8Vector(newSecret)) {
477         return E_WRITE_PARCEL_ERR;
478     }
479     for (uint32_t i = 0; i < plainText.size(); i++) {
480         if (!data.WriteUInt8Vector(plainText[i])) {
481             return E_WRITE_PARCEL_ERR;
482         }
483     }
484 
485     int err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH_RECOVER_KEY), data, reply,
486                           option);
487     if (err != E_OK) {
488         return err;
489     }
490 
491     return reply.ReadInt32();
492 }
493 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)494 int32_t StorageDaemonProxy::ActiveUserKey(uint32_t userId,
495                                           const std::vector<uint8_t> &token,
496                                           const std::vector<uint8_t> &secret)
497 {
498     MessageParcel data;
499     MessageParcel reply;
500     MessageOption option(MessageOption::TF_SYNC);
501 
502     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
503         return E_WRITE_DESCRIPTOR_ERR;
504     }
505 
506     if (!data.WriteUint32(userId)) {
507         return E_WRITE_PARCEL_ERR;
508     }
509     if (!data.WriteUInt8Vector(token)) {
510         return E_WRITE_PARCEL_ERR;
511     }
512     if (!data.WriteUInt8Vector(secret)) {
513         return E_WRITE_PARCEL_ERR;
514     }
515 
516     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY), data, reply, option);
517     if (err != E_OK) {
518         return err;
519     }
520 
521     return reply.ReadInt32();
522 }
523 
InactiveUserKey(uint32_t userId)524 int32_t StorageDaemonProxy::InactiveUserKey(uint32_t userId)
525 {
526     MessageParcel data;
527     MessageParcel reply;
528     MessageOption option(MessageOption::TF_SYNC);
529 
530     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
531         return E_WRITE_DESCRIPTOR_ERR;
532     }
533 
534     if (!data.WriteUint32(userId)) {
535         return E_WRITE_PARCEL_ERR;
536     }
537     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY), data, reply, option);
538     if (err != E_OK) {
539         return err;
540     }
541 
542     return reply.ReadInt32();
543 }
544 
LockUserScreen(uint32_t userId)545 int32_t StorageDaemonProxy::LockUserScreen(uint32_t userId)
546 {
547     MessageParcel data;
548     MessageParcel reply;
549     MessageOption option(MessageOption::TF_SYNC);
550 
551     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
552         return E_WRITE_DESCRIPTOR_ERR;
553     }
554 
555     if (!data.WriteUint32(userId)) {
556         return E_WRITE_PARCEL_ERR;
557     }
558     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::LOCK_USER_SCREEN), data, reply, option);
559     if (err != E_OK) {
560         return err;
561     }
562 
563     return reply.ReadInt32();
564 }
565 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)566 int32_t StorageDaemonProxy::UnlockUserScreen(uint32_t userId,
567                                              const std::vector<uint8_t> &token,
568                                              const std::vector<uint8_t> &secret)
569 {
570     MessageParcel data;
571     MessageParcel reply;
572     MessageOption option(MessageOption::TF_SYNC);
573 
574     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
575         return E_WRITE_DESCRIPTOR_ERR;
576     }
577 
578     if (!data.WriteUint32(userId)) {
579         return E_WRITE_PARCEL_ERR;
580     }
581     if (!data.WriteUInt8Vector(token)) {
582         return E_WRITE_PARCEL_ERR;
583     }
584     if (!data.WriteUInt8Vector(secret)) {
585         return E_WRITE_PARCEL_ERR;
586     }
587     int32_t err = SendRequest(
588         static_cast<int32_t>(StorageDaemonInterfaceCode::UNLOCK_USER_SCREEN), data, reply, option);
589     if (err != E_OK) {
590         return err;
591     }
592 
593     return reply.ReadInt32();
594 }
595 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)596 int32_t StorageDaemonProxy::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
597 {
598     MessageParcel data;
599     MessageParcel reply;
600     MessageOption option(MessageOption::TF_SYNC);
601 
602     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
603         return E_WRITE_DESCRIPTOR_ERR;
604     }
605 
606     if (!data.WriteUint32(userId)) {
607         return E_WRITE_PARCEL_ERR;
608     }
609     int32_t err = SendRequest(
610         static_cast<int32_t>(StorageDaemonInterfaceCode::LOCK_SCREEN_STATUS), data, reply, option);
611     if (err != E_OK) {
612         return err;
613     }
614     lockScreenStatus = reply.ReadBool();
615     return reply.ReadInt32();
616 }
617 
GenerateAppkey(uint32_t userId,uint32_t hashId,std::string & keyId)618 int32_t StorageDaemonProxy::GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId)
619 {
620     MessageParcel data;
621     MessageParcel reply;
622     MessageOption option(MessageOption::TF_SYNC);
623 
624     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
625         return E_WRITE_DESCRIPTOR_ERR;
626     }
627 
628     if (!data.WriteUint32(userId)) {
629         return E_WRITE_PARCEL_ERR;
630     }
631     if (!data.WriteUint32(hashId)) {
632         return E_WRITE_PARCEL_ERR;
633     }
634     int32_t err = SendRequest(
635         static_cast<int32_t>(StorageDaemonInterfaceCode::GENERATE_APP_KEY), data, reply, option);
636     if (err != E_OK) {
637         return err;
638     }
639     keyId = reply.ReadString();
640     return reply.ReadInt32();
641 }
642 
DeleteAppkey(uint32_t userId,const std::string & keyId)643 int32_t StorageDaemonProxy::DeleteAppkey(uint32_t userId, const std::string &keyId)
644 {
645     MessageParcel data;
646     MessageParcel reply;
647     MessageOption option(MessageOption::TF_SYNC);
648 
649     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
650         return E_WRITE_DESCRIPTOR_ERR;
651     }
652 
653     if (!data.WriteUint32(userId)) {
654         return E_WRITE_PARCEL_ERR;
655     }
656     if (!data.WriteString(keyId)) {
657         return E_WRITE_PARCEL_ERR;
658     }
659     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::DELETE_APP_KEY), data, reply, option);
660     if (err != E_OK) {
661         return err;
662     }
663     return reply.ReadInt32();
664 }
665 
CreateRecoverKey(uint32_t userId,uint32_t userType,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)666 int32_t StorageDaemonProxy::CreateRecoverKey(uint32_t userId,
667                                              uint32_t userType,
668                                              const std::vector<uint8_t> &token,
669                                              const std::vector<uint8_t> &secret)
670 {
671     MessageParcel data;
672     MessageParcel reply;
673     MessageOption option(MessageOption::TF_SYNC);
674 
675     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
676         return E_WRITE_DESCRIPTOR_ERR;
677     }
678 
679     if (!data.WriteUint32(userId)) {
680         return E_WRITE_PARCEL_ERR;
681     }
682     if (!data.WriteUint32(userType)) {
683         return E_WRITE_PARCEL_ERR;
684     }
685     if (!data.WriteUInt8Vector(token)) {
686         return E_WRITE_PARCEL_ERR;
687     }
688     if (!data.WriteUInt8Vector(secret)) {
689         return E_WRITE_PARCEL_ERR;
690     }
691     int err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::CREATE_RECOVER_KEY), data, reply, option);
692     if (err != E_OK) {
693         return err;
694     }
695 
696     return reply.ReadInt32();
697 }
698 
SetRecoverKey(const std::vector<uint8_t> & key)699 int32_t StorageDaemonProxy::SetRecoverKey(const std::vector<uint8_t> &key)
700 {
701     MessageParcel data;
702     MessageParcel reply;
703     MessageOption option(MessageOption::TF_SYNC);
704 
705     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
706         return E_WRITE_DESCRIPTOR_ERR;
707     }
708 
709     if (!data.WriteUInt8Vector(key)) {
710         return E_WRITE_PARCEL_ERR;
711     }
712     int err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::SET_RECOVER_KEY), data, reply, option);
713     if (err != E_OK) {
714         return err;
715     }
716 
717     return reply.ReadInt32();
718 }
719 
UpdateKeyContext(uint32_t userId,bool needRemoveTmpKey)720 int32_t StorageDaemonProxy::UpdateKeyContext(uint32_t userId, bool needRemoveTmpKey)
721 {
722     MessageParcel data;
723     MessageParcel reply;
724     MessageOption option(MessageOption::TF_SYNC);
725 
726     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
727         return E_WRITE_DESCRIPTOR_ERR;
728     }
729 
730     if (!data.WriteUint32(userId)) {
731         return E_WRITE_PARCEL_ERR;
732     }
733     if (!data.WriteBool(needRemoveTmpKey)) {
734         return E_WRITE_PARCEL_ERR;
735     }
736 
737     int32_t err = SendRequest(
738         static_cast<int32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT), data, reply, option);
739     if (err != E_OK) {
740         return err;
741     }
742 
743     return reply.ReadInt32();
744 }
745 
MountCryptoPathAgain(uint32_t userId)746 int32_t StorageDaemonProxy::MountCryptoPathAgain(uint32_t userId)
747 {
748     MessageParcel data;
749     MessageParcel reply;
750     MessageOption option(MessageOption::TF_SYNC);
751 
752     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
753         return E_WRITE_DESCRIPTOR_ERR;
754     }
755 
756     if (!data.WriteUint32(userId)) {
757         return E_WRITE_PARCEL_ERR;
758     }
759     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::MOUNT_CRYPTO_PATH_AGAIN),
760         data, reply, option);
761     if (err != E_OK) {
762         return err;
763     }
764 
765     return reply.ReadInt32();
766 }
767 
CreateShareFile(const std::vector<std::string> & uriList,uint32_t tokenId,uint32_t flag)768 std::vector<int32_t> StorageDaemonProxy::CreateShareFile(const std::vector<std::string> &uriList,
769                                                          uint32_t tokenId, uint32_t flag)
770 {
771     MessageParcel data;
772     MessageParcel reply;
773     MessageOption option(MessageOption::TF_SYNC);
774 
775     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
776         return std::vector<int32_t>{E_WRITE_DESCRIPTOR_ERR};
777     }
778 
779     if (!WriteBatchUris(data, uriList)) {
780         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
781     }
782 
783     if (!data.WriteUint32(tokenId)) {
784         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
785     }
786 
787     if (!data.WriteUint32(flag)) {
788         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
789     }
790 
791     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE), data, reply, option);
792     if (err != E_OK) {
793         return std::vector<int32_t>{err};
794     }
795 
796     std::vector<int32_t> retList;
797     if (!reply.ReadInt32Vector(&retList)) {
798         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
799     };
800     return retList;
801 }
802 
DeleteShareFile(uint32_t tokenId,const std::vector<std::string> & uriList)803 int32_t StorageDaemonProxy::DeleteShareFile(uint32_t tokenId, const std::vector<std::string> &uriList)
804 {
805     MessageParcel data;
806     MessageParcel reply;
807     MessageOption option(MessageOption::TF_ASYNC);
808 
809     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
810         return E_WRITE_DESCRIPTOR_ERR;
811     }
812 
813     if (!data.WriteUint32(tokenId)) {
814         return E_WRITE_PARCEL_ERR;
815     }
816 
817     if (!WriteBatchUris(data, uriList)) {
818         return E_WRITE_PARCEL_ERR;
819     }
820 
821     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE), data, reply, option);
822     if (err != E_OK) {
823         return err;
824     }
825 
826     return reply.ReadInt32();
827 }
828 
SetBundleQuota(const std::string & bundleName,int32_t uid,const std::string & bundleDataDirPath,int32_t limitSizeMb)829 int32_t StorageDaemonProxy::SetBundleQuota(const std::string &bundleName, int32_t uid,
830     const std::string &bundleDataDirPath, int32_t limitSizeMb)
831 {
832     MessageParcel data;
833     MessageParcel reply;
834     MessageOption option(MessageOption::TF_SYNC);
835     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
836         return E_WRITE_DESCRIPTOR_ERR;
837     }
838 
839     if (!data.WriteString(bundleName)) {
840         return E_WRITE_PARCEL_ERR;
841     }
842 
843     if (!data.WriteInt32(uid)) {
844         return E_WRITE_PARCEL_ERR;
845     }
846 
847     if (!data.WriteString(bundleDataDirPath)) {
848         return E_WRITE_PARCEL_ERR;
849     }
850 
851     if (!data.WriteInt32(limitSizeMb)) {
852         return E_WRITE_PARCEL_ERR;
853     }
854 
855     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA), data, reply, option);
856     if (err != E_OK) {
857         return err;
858     }
859 
860     return reply.ReadInt32();
861 }
862 
GetOccupiedSpace(int32_t idType,int32_t id,int64_t & size)863 int32_t StorageDaemonProxy::GetOccupiedSpace(int32_t idType, int32_t id, int64_t &size)
864 {
865     MessageParcel data;
866     MessageParcel reply;
867     MessageOption option(MessageOption::TF_SYNC);
868     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
869         return E_WRITE_DESCRIPTOR_ERR;
870     }
871 
872     if (!data.WriteInt32(idType)) {
873         return E_WRITE_PARCEL_ERR;
874     }
875 
876     if (!data.WriteInt32(id)) {
877         return E_WRITE_PARCEL_ERR;
878     }
879 
880     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::GET_SPACE), data, reply, option);
881     if (err != E_OK) {
882         return err;
883     }
884 
885     err = reply.ReadInt32();
886     if (err != E_OK) {
887         return err;
888     }
889     size = reply.ReadInt64();
890     return E_OK;
891 }
892 
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)893 int32_t StorageDaemonProxy::GetBundleStatsForIncrease(uint32_t userId, const std::vector<std::string> &bundleNames,
894     const std::vector<int64_t> &incrementalBackTimes, std::vector<int64_t> &pkgFileSizes,
895     std::vector<int64_t> &incPkgFileSizes)
896 {
897     MessageParcel data;
898     MessageParcel reply;
899     MessageOption option(MessageOption::TF_SYNC);
900     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
901         return E_WRITE_DESCRIPTOR_ERR;
902     }
903 
904     if (!data.WriteInt32(userId)) {
905         return E_WRITE_PARCEL_ERR;
906     }
907 
908     if (!data.WriteStringVector(bundleNames)) {
909         return E_WRITE_PARCEL_ERR;
910     }
911 
912     if (!data.WriteInt64Vector(incrementalBackTimes)) {
913         return E_WRITE_PARCEL_ERR;
914     }
915 
916     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::GET_BUNDLE_STATS_INCREASE), data, reply,
917         option);
918     if (err != E_OK) {
919         LOGE("StorageDaemonProxy::SendRequest call err = %{public}d", err);
920         return err;
921     }
922     err = reply.ReadInt32();
923     if (err != E_OK) {
924         LOGE("StorageDaemonProxy::SendRequest reply.ReadInt32() call err = %{public}d", err);
925         return err;
926     }
927     if (!reply.ReadInt64Vector(&pkgFileSizes)) {
928         LOGE("StorageDaemonProxy::SendRequest read pkgFileSizes");
929         return E_WRITE_REPLY_ERR;
930     }
931     if (!reply.ReadInt64Vector(&incPkgFileSizes)) {
932         LOGE("StorageDaemonProxy::SendRequest read incPkgFileSizes");
933         return E_WRITE_REPLY_ERR;
934     }
935 
936     return E_OK;
937 }
938 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)939 int32_t StorageDaemonProxy::MountDfsDocs(int32_t userId, const std::string &relativePath,
940     const std::string &networkId, const std::string &deviceId)
941 {
942     MessageParcel data;
943     MessageParcel reply;
944     MessageOption option(MessageOption::TF_SYNC);
945     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
946         return E_WRITE_DESCRIPTOR_ERR;
947     }
948     if (!data.WriteInt32(userId) || !data.WriteString(relativePath) ||
949         !data.WriteString(networkId) || !data.WriteString(deviceId)) {
950         return E_WRITE_PARCEL_ERR;
951     }
952 
953     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::MOUNT_DFS_DOCS), data, reply, option);
954     if (err != E_OK) {
955         return err;
956     }
957 
958     return reply.ReadInt32();
959 }
960 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)961 int32_t StorageDaemonProxy::UMountDfsDocs(int32_t userId, const std::string &relativePath,
962     const std::string &networkId, const std::string &deviceId)
963 {
964     MessageParcel data;
965     MessageParcel reply;
966     MessageOption option(MessageOption::TF_SYNC);
967     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
968         return E_WRITE_DESCRIPTOR_ERR;
969     }
970     if (!data.WriteInt32(userId) || !data.WriteString(relativePath) ||
971         !data.WriteString(networkId) || !data.WriteString(deviceId)) {
972         return E_WRITE_PARCEL_ERR;
973     }
974 
975     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS), data, reply, option);
976     if (err != E_OK) {
977         return err;
978     }
979 
980     return reply.ReadInt32();
981 }
982 
UpdateMemoryPara(int32_t size,int32_t & oldSize)983 int32_t StorageDaemonProxy::UpdateMemoryPara(int32_t size, int32_t &oldSize)
984 {
985     MessageParcel data;
986     MessageParcel reply;
987     MessageOption option(MessageOption::TF_SYNC);
988 
989     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
990         return E_WRITE_DESCRIPTOR_ERR;
991     }
992 
993     if (!data.WriteInt32(size)) {
994         return E_WRITE_PARCEL_ERR;
995     }
996 
997     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::UPDATE_MEM_PARA), data, reply, option);
998     if (err != E_OK) {
999         return err;
1000     }
1001     err = reply.ReadInt32();
1002     if (err != E_OK) {
1003         return err;
1004     }
1005     oldSize = reply.ReadInt32();
1006     return E_OK;
1007 }
1008 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1009 int32_t StorageDaemonProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1010 {
1011     sptr<IRemoteObject> remote = Remote();
1012     if (remote == nullptr) {
1013         LOGE("remote is nullptr, code = %{public}d", code);
1014         return E_REMOTE_IS_NULLPTR;
1015     }
1016 
1017     int32_t result = remote->SendRequest(code, data, reply, option);
1018     if (result != E_OK) {
1019         LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
1020         return result;
1021     }
1022 
1023     return E_OK;
1024 }
1025 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)1026 int32_t StorageDaemonProxy::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
1027 {
1028     MessageParcel data;
1029     MessageParcel reply;
1030     MessageOption option(MessageOption::TF_SYNC);
1031 
1032     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
1033         return E_WRITE_DESCRIPTOR_ERR;
1034     }
1035 
1036     if (!data.WriteUint32(userId)) {
1037         return E_WRITE_PARCEL_ERR;
1038     }
1039     if (!data.WriteBool(needCheckDirMount)) {
1040         return E_WRITE_PARCEL_ERR;
1041     }
1042     int32_t err =
1043         SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS), data, reply, option);
1044     if (err != E_OK) {
1045         return err;
1046     }
1047     isEncrypted = reply.ReadBool();
1048     return reply.ReadInt32();
1049 }
1050 
GetUserNeedActiveStatus(uint32_t userId,bool & needActive)1051 int32_t StorageDaemonProxy::GetUserNeedActiveStatus(uint32_t userId, bool &needActive)
1052 {
1053     MessageParcel data;
1054     MessageParcel reply;
1055     MessageOption option(MessageOption::TF_SYNC);
1056 
1057     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
1058         LOGE("WriteInterfaceToken failed");
1059         return E_WRITE_DESCRIPTOR_ERR;
1060     }
1061     if (!data.WriteInt32(userId)) {
1062         LOGE("write userId failed!");
1063         return E_WRITE_PARCEL_ERR;
1064     }
1065 
1066     int32_t err =
1067         SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::GET_USER_NEED_ACTIVE_STATUS), data, reply, option);
1068     if (err != E_OK) {
1069         return err;
1070     }
1071     needActive = reply.ReadBool();
1072     return reply.ReadInt32();
1073 }
1074 
MountMediaFuse(int32_t userId,int32_t & devFd)1075 int32_t StorageDaemonProxy::MountMediaFuse(int32_t userId, int32_t &devFd)
1076 {
1077 #ifdef STORAGE_SERVICE_MEDIA_FUSE
1078     MessageParcel data;
1079     MessageParcel reply;
1080     MessageOption option(MessageOption::TF_SYNC);
1081 
1082     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
1083         LOGE("WriteInterfaceToken failed");
1084         return E_WRITE_DESCRIPTOR_ERR;
1085     }
1086 
1087     if (!data.WriteInt32(userId)) {
1088         return E_WRITE_PARCEL_ERR;
1089     }
1090 
1091     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::MOUNT_MEDIA_FUSE), data, reply, option);
1092     if (err != E_OK) {
1093         return err;
1094     }
1095 
1096     int32_t ret = reply.ReadInt32();
1097     if (ret == E_OK) {
1098         devFd = reply.ReadFileDescriptor();
1099     }
1100 
1101     return ret;
1102 #else
1103     return E_OK;
1104 #endif
1105 }
1106 
UMountMediaFuse(int32_t userId)1107 int32_t StorageDaemonProxy::UMountMediaFuse(int32_t userId)
1108 {
1109 #ifdef STORAGE_SERVICE_MEDIA_FUSE
1110     MessageParcel data;
1111     MessageParcel reply;
1112     MessageOption option(MessageOption::TF_SYNC);
1113 
1114     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
1115         LOGE("WriteInterfaceToken failed");
1116         return E_WRITE_DESCRIPTOR_ERR;
1117     }
1118 
1119     if (!data.WriteInt32(userId)) {
1120         return E_WRITE_PARCEL_ERR;
1121     }
1122 
1123     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::UMOUNT_MEDIA_FUSE), data, reply, option);
1124     if (err != E_OK) {
1125         return err;
1126     }
1127 
1128     return reply.ReadInt32();
1129 #else
1130     return E_OK;
1131 #endif
1132 }
1133 
MountFileMgrFuse(int32_t userId,const std::string & path,int32_t & fuseFd)1134 int32_t StorageDaemonProxy::MountFileMgrFuse(int32_t userId, const std::string &path, int32_t &fuseFd)
1135 {
1136     MessageParcel data;
1137     MessageParcel reply;
1138     MessageOption option(MessageOption::TF_SYNC);
1139 
1140     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
1141         LOGE("WriteInterfaceToken failed");
1142         return E_WRITE_DESCRIPTOR_ERR;
1143     }
1144 
1145     if (!data.WriteInt32(userId) || !data.WriteString(path)) {
1146         return E_WRITE_PARCEL_ERR;
1147     }
1148 
1149     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::MOUNT_FILE_MGR_FUSE),
1150         data, reply, option);
1151     if (err != E_OK) {
1152         return err;
1153     }
1154     int32_t ret = reply.ReadInt32();
1155     if (ret == E_OK) {
1156         fuseFd = reply.ReadFileDescriptor();
1157     }
1158     return ret;
1159 }
1160 
UMountFileMgrFuse(int32_t userId,const std::string & path)1161 int32_t StorageDaemonProxy::UMountFileMgrFuse(int32_t userId, const std::string &path)
1162 {
1163     MessageParcel data;
1164     MessageParcel reply;
1165     MessageOption option(MessageOption::TF_SYNC);
1166 
1167     if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) {
1168         LOGE("WriteInterfaceToken failed");
1169         return E_WRITE_DESCRIPTOR_ERR;
1170     }
1171 
1172     if (!data.WriteInt32(userId) || !data.WriteString(path)) {
1173         return E_WRITE_PARCEL_ERR;
1174     }
1175 
1176     int32_t err = SendRequest(static_cast<int32_t>(StorageDaemonInterfaceCode::UMOUNT_FILE_MGR_FUSE),
1177         data, reply, option);
1178     if (err != E_OK) {
1179         return err;
1180     }
1181     return reply.ReadInt32();
1182 }
1183 } // StorageDaemon
1184 } // OHOS
1185