• 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_manager_proxy.h"
17 #include "storage_service_errno.h"
18 #include "storage_service_log.h"
19 
20 namespace OHOS {
21 namespace StorageManager {
PrepareAddUser(int32_t userId,uint32_t flags)22 int32_t StorageManagerProxy::PrepareAddUser(int32_t userId, uint32_t flags)
23 {
24     LOGI("StorageManagerProxy::PrepareAddUser, userId:%{public}d", userId);
25     MessageParcel data;
26     MessageParcel reply;
27     MessageOption option(MessageOption::TF_SYNC);
28     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
29         LOGE("StorageManagerProxy::PrepareAddUser, WriteInterfaceToken failed");
30         return E_WRITE_DESCRIPTOR_ERR;
31     }
32     if (!data.WriteInt32(userId)) {
33         LOGE("StorageManagerProxy::PrepareAddUser, WriteInt32 failed");
34         return E_WRITE_PARCEL_ERR;
35     }
36     if (!data.WriteUint32(flags)) {
37         LOGE("StorageManagerProxy::PrepareAddUser, WriteUint32 failed");
38         return E_WRITE_PARCEL_ERR;
39     }
40     int err = SendRequest(PREPARE_ADD_USER, data, reply, option);
41     if (err != E_OK) {
42         return err;
43     }
44     return reply.ReadUint32();
45 }
46 
RemoveUser(int32_t userId,uint32_t flags)47 int32_t StorageManagerProxy::RemoveUser(int32_t userId, uint32_t flags)
48 {
49     LOGI("StorageManagerProxy::RemoveUser, userId:%{public}d", userId);
50     MessageParcel data;
51     MessageParcel reply;
52     MessageOption option(MessageOption::TF_SYNC);
53     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
54         LOGE("StorageManagerProxy::RemoveUser, WriteInterfaceToken failed");
55         return E_WRITE_DESCRIPTOR_ERR;
56     }
57     if (!data.WriteInt32(userId)) {
58         LOGE("StorageManagerProxy::RemoveUser, WriteInt32 failed");
59         return E_WRITE_PARCEL_ERR;
60     }
61     if (!data.WriteUint32(flags)) {
62         LOGE("StorageManagerProxy::RemoveUser, WriteUint32 failed");
63         return E_WRITE_PARCEL_ERR;
64     }
65     int err = SendRequest(REMOVE_USER, data, reply, option);
66     if (err != E_OK) {
67         return err;
68     }
69     return reply.ReadUint32();
70 }
71 
PrepareStartUser(int32_t userId)72 int32_t StorageManagerProxy::PrepareStartUser(int32_t userId)
73 {
74     LOGI("StorageManagerProxy::PrepareStartUser, userId:%{public}d", userId);
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option(MessageOption::TF_SYNC);
78     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
79         LOGE("StorageManagerProxy::PrepareStartUser, WriteInterfaceToken failed");
80         return E_WRITE_DESCRIPTOR_ERR;
81     }
82     if (!data.WriteInt32(userId)) {
83         LOGE("StorageManagerProxy::PrepareStartUser, WriteInt32 failed");
84         return E_WRITE_PARCEL_ERR;
85     }
86     int err = SendRequest(PREPARE_START_USER, data, reply, option);
87     if (err != E_OK) {
88         return err;
89     }
90     return reply.ReadUint32();
91 }
92 
StopUser(int32_t userId)93 int32_t StorageManagerProxy::StopUser(int32_t userId)
94 {
95     LOGI("StorageManagerProxy::StopUser, userId:%{public}d", userId);
96     MessageParcel data;
97     MessageParcel reply;
98     MessageOption option(MessageOption::TF_SYNC);
99     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
100         LOGE("StorageManagerProxy::StopUser, WriteInterfaceToken failed");
101         return E_WRITE_DESCRIPTOR_ERR;
102     }
103     if (!data.WriteInt32(userId)) {
104         LOGE("StorageManagerProxy::StopUser, WriteInt32 failed");
105         return E_WRITE_PARCEL_ERR;
106     }
107     int err = SendRequest(STOP_USER, data, reply, option);
108     if (err != E_OK) {
109         return err;
110     }
111     return reply.ReadUint32();
112 }
113 
GenerateUserKeys(uint32_t userId,uint32_t flags)114 int32_t StorageManagerProxy::GenerateUserKeys(uint32_t userId, uint32_t flags)
115 {
116     LOGI("user ID: %{public}u, flags: %{public}u", userId, flags);
117     MessageParcel data;
118     MessageParcel reply;
119     MessageOption option(MessageOption::TF_SYNC);
120     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
121         LOGE("WriteInterfaceToken failed");
122         return E_WRITE_DESCRIPTOR_ERR;
123     }
124     if (!data.WriteUint32(userId)) {
125         LOGE("Write user ID failed");
126         return E_WRITE_PARCEL_ERR;
127     }
128     if (!data.WriteUint32(flags)) {
129         LOGE("Write key flags failed");
130         return E_WRITE_PARCEL_ERR;
131     }
132     int err = SendRequest(CREATE_USER_KEYS, data, reply, option);
133     if (err != E_OK) {
134         return err;
135     }
136 
137     return reply.ReadInt32();
138 }
139 
DeleteUserKeys(uint32_t userId)140 int32_t StorageManagerProxy::DeleteUserKeys(uint32_t userId)
141 {
142     LOGI("user ID: %{public}u", userId);
143     MessageParcel data;
144     MessageParcel reply;
145     MessageOption option(MessageOption::TF_SYNC);
146     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
147         LOGE("WriteInterfaceToken failed");
148         return E_WRITE_DESCRIPTOR_ERR;
149     }
150     if (!data.WriteUint32(userId)) {
151         LOGE("Write user ID failed");
152         return E_WRITE_PARCEL_ERR;
153     }
154     int err = SendRequest(DELETE_USER_KEYS, data, reply, option);
155     if (err != E_OK) {
156         return err;
157     }
158 
159     return reply.ReadInt32();
160 }
161 
UpdateUserAuth(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)162 int32_t StorageManagerProxy::UpdateUserAuth(uint32_t userId,
163                                             const std::vector<uint8_t> &token,
164                                             const std::vector<uint8_t> &oldSecret,
165                                             const std::vector<uint8_t> &newSecret)
166 {
167     LOGI("user ID: %{public}u", userId);
168     MessageParcel data;
169     MessageParcel reply;
170     MessageOption option(MessageOption::TF_SYNC);
171     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
172         LOGE("WriteInterfaceToken failed");
173         return E_WRITE_DESCRIPTOR_ERR;
174     }
175     if (!data.WriteUint32(userId)) {
176         LOGE("Write user ID failed");
177         return E_WRITE_PARCEL_ERR;
178     }
179     if (!data.WriteUInt8Vector(token)) {
180         LOGE("Write token failed");
181         return E_WRITE_PARCEL_ERR;
182     }
183     if (!data.WriteUInt8Vector(oldSecret)) {
184         LOGE("Write oldSecret failed");
185         return E_WRITE_PARCEL_ERR;
186     }
187     if (!data.WriteUInt8Vector(newSecret)) {
188         LOGE("Write newSecret failed");
189         return E_WRITE_PARCEL_ERR;
190     }
191 
192     int err = SendRequest(UPDATE_USER_AUTH, data, reply, option);
193     if (err != E_OK) {
194         return err;
195     }
196 
197     return reply.ReadInt32();
198 }
199 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)200 int32_t StorageManagerProxy::ActiveUserKey(uint32_t userId,
201                                            const std::vector<uint8_t> &token,
202                                            const std::vector<uint8_t> &secret)
203 {
204     LOGI("user ID: %{public}u", userId);
205     MessageParcel data;
206     MessageParcel reply;
207     MessageOption option(MessageOption::TF_SYNC);
208     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
209         LOGE("WriteInterfaceToken failed");
210         return E_WRITE_DESCRIPTOR_ERR;
211     }
212     if (!data.WriteUint32(userId)) {
213         LOGE("Write user ID failed");
214         return E_WRITE_PARCEL_ERR;
215     }
216     if (!data.WriteUInt8Vector(token)) {
217         LOGE("Write token failed");
218         return E_WRITE_PARCEL_ERR;
219     }
220     if (!data.WriteUInt8Vector(secret)) {
221         LOGE("Write secret failed");
222         return E_WRITE_PARCEL_ERR;
223     }
224 
225     int err = SendRequest(ACTIVE_USER_KEY, data, reply, option);
226     if (err != E_OK) {
227         return err;
228     }
229 
230     return reply.ReadInt32();
231 }
232 
InactiveUserKey(uint32_t userId)233 int32_t StorageManagerProxy::InactiveUserKey(uint32_t userId)
234 {
235     LOGI("user ID: %{public}u", userId);
236     MessageParcel data;
237     MessageParcel reply;
238     MessageOption option(MessageOption::TF_SYNC);
239     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
240         LOGE("WriteInterfaceToken failed");
241         return E_WRITE_DESCRIPTOR_ERR;
242     }
243     if (!data.WriteUint32(userId)) {
244         LOGE("Write user ID failed");
245         return E_WRITE_PARCEL_ERR;
246     }
247     int err = SendRequest(INACTIVE_USER_KEY, data, reply, option);
248     if (err != E_OK) {
249         return err;
250     }
251 
252     return reply.ReadInt32();
253 }
254 
UpdateKeyContext(uint32_t userId)255 int32_t StorageManagerProxy::UpdateKeyContext(uint32_t userId)
256 {
257     LOGI("user ID: %{public}u", userId);
258     MessageParcel data;
259     MessageParcel reply;
260     MessageOption option(MessageOption::TF_SYNC);
261     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
262         LOGE("WriteInterfaceToken failed");
263         return E_WRITE_DESCRIPTOR_ERR;
264     }
265     if (!data.WriteUint32(userId)) {
266         LOGE("Write user ID failed");
267         return E_WRITE_PARCEL_ERR;
268     }
269     int err = SendRequest(UPDATE_KEY_CONTEXT, data, reply, option);
270     if (err != E_OK) {
271         return err;
272     }
273 
274     return reply.ReadInt32();
275 }
276 
GetFreeSizeOfVolume(std::string volumeUuid,int64_t & freeSize)277 int32_t StorageManagerProxy::GetFreeSizeOfVolume(std::string volumeUuid, int64_t &freeSize)
278 {
279     LOGI("StorageManagerProxy::GetFreeSizeOfVolume, volumeUuid:%{public}s", volumeUuid.c_str());
280     MessageParcel data;
281     MessageParcel reply;
282     MessageOption option(MessageOption::TF_SYNC);
283     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
284         LOGE("StorageManagerProxy::GetFreeSizeOfVolume, WriteInterfaceToken failed");
285         return E_WRITE_DESCRIPTOR_ERR;
286     }
287 
288     if (!data.WriteString(volumeUuid)) {
289         LOGE("StorageManagerProxy::GetFreeSizeOfVolume, WriteString failed");
290         return E_WRITE_PARCEL_ERR;
291     }
292     int err = SendRequest(GET_FREE, data, reply, option);
293     if (err != E_OK) {
294         return err;
295     }
296     err = reply.ReadInt32();
297     if (err != E_OK) {
298         return err;
299     }
300     freeSize = reply.ReadInt64();
301     return E_OK;
302 }
303 
GetTotalSizeOfVolume(std::string volumeUuid,int64_t & totalSize)304 int32_t StorageManagerProxy::GetTotalSizeOfVolume(std::string volumeUuid, int64_t &totalSize)
305 {
306     LOGI("StorageManagerProxy::GetTotalSizeOfVolume, volumeUuid:%{public}s", volumeUuid.c_str());
307     MessageParcel data;
308     MessageParcel reply;
309     MessageOption option(MessageOption::TF_SYNC);
310     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
311         LOGE("StorageManagerProxy::GetTotalSizeOfVolume, WriteInterfaceToken failed");
312         return E_WRITE_DESCRIPTOR_ERR;
313     }
314 
315     if (!data.WriteString(volumeUuid)) {
316         LOGE("StorageManagerProxy::GetTotalSizeOfVolume, WriteString failed");
317         return E_WRITE_PARCEL_ERR;
318     }
319     int err = SendRequest(GET_TOTAL, data, reply, option);
320     if (err != E_OK) {
321         return err;
322     }
323     err = reply.ReadInt32();
324     if (err != E_OK) {
325         return err;
326     }
327     totalSize = reply.ReadInt64();
328     return E_OK;
329 }
330 
GetBundleStats(std::string pkgName,BundleStats & bundleStats)331 int32_t StorageManagerProxy::GetBundleStats(std::string pkgName, BundleStats &bundleStats)
332 {
333     MessageParcel data;
334     MessageParcel reply;
335     MessageOption option(MessageOption::TF_SYNC);
336     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
337         return E_WRITE_DESCRIPTOR_ERR;
338     }
339 
340     if (!data.WriteString(pkgName)) {
341         return E_WRITE_PARCEL_ERR;
342     }
343     int err = SendRequest(GET_BUNDLE_STATUS, data, reply, option);
344     if (err != E_OK) {
345         return err;
346     }
347     err = reply.ReadInt32();
348     if (err != E_OK) {
349         return err;
350     }
351     bundleStats = *BundleStats::Unmarshalling(reply);
352     return E_OK;
353 }
354 
NotifyVolumeCreated(VolumeCore vc)355 int32_t StorageManagerProxy::NotifyVolumeCreated(VolumeCore vc)
356 {
357     LOGI("StorageManagerProxy::NotifyVolumeCreated, volumeUuid:%{public}s", vc.GetId().c_str());
358     MessageParcel data;
359     MessageParcel reply;
360     MessageOption option(MessageOption::TF_SYNC);
361     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
362         LOGE("StorageManagerProxy::NotifyVolumeCreated, WriteInterfaceToken failed");
363         return E_WRITE_DESCRIPTOR_ERR;
364     }
365 
366     if (!vc.Marshalling(data)) {
367         LOGE("StorageManagerProxy::NotifyVolumeCreated, WriteVolumeInfo failed");
368         return E_WRITE_PARCEL_ERR;
369     }
370 
371     int err = SendRequest(NOTIFY_VOLUME_CREATED, data, reply, option);
372     return err;
373 }
374 
NotifyVolumeMounted(std::string volumeId,int32_t fsType,std::string fsUuid,std::string path,std::string description)375 int32_t StorageManagerProxy::NotifyVolumeMounted(std::string volumeId, int32_t fsType, std::string fsUuid,
376     std::string path, std::string description)
377 {
378     LOGI("StorageManagerProxy::NotifyVolumeMounted, volumeUuid:%{public}s", volumeId.c_str());
379     MessageParcel data;
380     MessageParcel reply;
381     MessageOption option(MessageOption::TF_SYNC);
382     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
383         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteInterfaceToken failed");
384         return E_WRITE_DESCRIPTOR_ERR;
385     }
386 
387     if (!data.WriteString(volumeId)) {
388         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
389         return E_WRITE_PARCEL_ERR;
390     }
391 
392     if (!data.WriteInt32(fsType)) {
393         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteInt32 failed");
394         return E_WRITE_PARCEL_ERR;
395     }
396 
397     if (!data.WriteString(fsUuid)) {
398         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
399         return E_WRITE_PARCEL_ERR;
400     }
401 
402     if (!data.WriteString(path)) {
403         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
404         return E_WRITE_PARCEL_ERR;
405     }
406 
407     if (!data.WriteString(description)) {
408         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
409         return E_WRITE_PARCEL_ERR;
410     }
411 
412     int err = SendRequest(NOTIFY_VOLUME_MOUNTED, data, reply, option);
413     return err;
414 }
415 
NotifyVolumeDestroyed(std::string volumeId)416 int32_t StorageManagerProxy::NotifyVolumeDestroyed(std::string volumeId)
417 {
418     LOGI("StorageManagerProxy::NotifyVolumedestroyed, volumeId:%{public}s", volumeId.c_str());
419     MessageParcel data;
420     MessageParcel reply;
421     MessageOption option(MessageOption::TF_SYNC);
422     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
423         LOGE("StorageManagerProxy::NotifyVolumedestroyed, WriteInterfaceToken failed");
424         return E_WRITE_DESCRIPTOR_ERR;
425     }
426 
427     if (!data.WriteString(volumeId)) {
428         LOGE("StorageManagerProxy::NotifyVolumedestroyed, WriteString failed");
429         return E_WRITE_PARCEL_ERR;
430     }
431     int err = SendRequest(NOTIFY_VOLUME_DESTROYED, data, reply, option);
432     return err;
433 }
434 
Mount(std::string volumeId)435 int32_t StorageManagerProxy::Mount(std::string volumeId)
436 {
437     LOGI("StorageManagerProxy::Mount, volumeId:%{public}s", volumeId.c_str());
438     MessageParcel data;
439     MessageParcel reply;
440     MessageOption option(MessageOption::TF_SYNC);
441     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
442         LOGE("StorageManagerProxy::Mount, WriteInterfaceToken failed");
443         return E_WRITE_DESCRIPTOR_ERR;
444     }
445 
446     if (!data.WriteString(volumeId)) {
447         LOGE("StorageManagerProxy::Mount, WriteString failed");
448         return E_WRITE_PARCEL_ERR;
449     }
450     int err = SendRequest(MOUNT, data, reply, option);
451     if (err != E_OK) {
452         return err;
453     }
454     return reply.ReadInt32();
455 }
456 
Unmount(std::string volumeId)457 int32_t StorageManagerProxy::Unmount(std::string volumeId)
458 {
459     LOGI("StorageManagerProxy::Unmount, volumeId:%{public}s", volumeId.c_str());
460     MessageParcel data;
461     MessageParcel reply;
462     MessageOption option(MessageOption::TF_SYNC);
463     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
464         LOGE("StorageManagerProxy::Unmount, WriteInterfaceToken failed");
465         return E_WRITE_DESCRIPTOR_ERR;
466     }
467 
468     if (!data.WriteString(volumeId)) {
469         LOGE("StorageManagerProxy::Unmount, WriteString failed");
470         return E_WRITE_PARCEL_ERR;
471     }
472     int err = SendRequest(UNMOUNT, data, reply, option);
473     if (err != E_OK) {
474         return err;
475     }
476     return reply.ReadInt32();
477 }
478 
GetAllVolumes(std::vector<VolumeExternal> & vecOfVol)479 int32_t StorageManagerProxy::GetAllVolumes(std::vector<VolumeExternal> &vecOfVol)
480 {
481     LOGI("StorageManagerProxy::GetAllVolumes");
482     MessageParcel data;
483     MessageParcel reply;
484     MessageOption option(MessageOption::TF_SYNC);
485     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
486         LOGE("StorageManagerProxy::GetAllVolumes, WriteInterfaceToken failed");
487         return E_WRITE_DESCRIPTOR_ERR;
488     }
489 
490     int err = SendRequest(GET_ALL_VOLUMES, data, reply, option);
491     if (err != E_OK) {
492         return err;
493     }
494     err = reply.ReadInt32();
495     if (err != E_OK) {
496         return err;
497     }
498     uint size = reply.ReadUint32();
499     if (size == 0) {
500         return reply.ReadInt32();
501     }
502     for (uint i = 0; i < size; i++) {
503         std::unique_ptr<VolumeExternal> ve = VolumeExternal::Unmarshalling(reply);
504         LOGI("StorageManagerProxy::GetAllVolumes push %{public}s", ve->GetId().c_str());
505         vecOfVol.push_back(*ve);
506     }
507     return E_OK;
508 }
509 
NotifyDiskCreated(Disk disk)510 int32_t StorageManagerProxy::NotifyDiskCreated(Disk disk)
511 {
512     LOGI("StorageManagerProxy::NotifyDiskCreate, diskId:%{public}s", disk.GetDiskId().c_str());
513     MessageParcel data;
514     MessageParcel reply;
515     MessageOption option(MessageOption::TF_SYNC);
516     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
517         LOGE("StorageManagerProxy::NotifyDiskCreate, WriteInterfaceToken failed");
518         return E_WRITE_DESCRIPTOR_ERR;
519     }
520 
521     if (!disk.Marshalling(data)) {
522         LOGE("StorageManagerProxy::NotifyDiskCreate, WriteDiskInfo failed");
523         return E_WRITE_PARCEL_ERR;
524     }
525 
526     int err = SendRequest(NOTIFY_DISK_CREATED, data, reply, option);
527     return err;
528 }
529 
NotifyDiskDestroyed(std::string diskId)530 int32_t StorageManagerProxy::NotifyDiskDestroyed(std::string diskId)
531 {
532     LOGI("StorageManagerProxy::NotifyDiskDestroyed, diskId:%{public}s", diskId.c_str());
533     MessageParcel data;
534     MessageParcel reply;
535     MessageOption option(MessageOption::TF_SYNC);
536     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
537         LOGE("StorageManagerProxy::NotifyDiskDestroyed, WriteInterfaceToken failed");
538         return E_WRITE_DESCRIPTOR_ERR;
539     }
540     if (!data.WriteString(diskId)) {
541         LOGE("StorageManagerProxy::NotifyDiskDestroyed, WriteString failed");
542         return E_WRITE_PARCEL_ERR;
543     }
544     int err = SendRequest(NOTIFY_DISK_DESTROYED, data, reply, option);
545     return err;
546 }
547 
Partition(std::string diskId,int32_t type)548 int32_t StorageManagerProxy::Partition(std::string diskId, int32_t type)
549 {
550     LOGI("StorageManagerProxy::Partition, diskId:%{public}s", diskId.c_str());
551     MessageParcel data;
552     MessageParcel reply;
553     MessageOption option(MessageOption::TF_SYNC);
554     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
555         LOGE("StorageManagerProxy::Partition, WriteInterfaceToken failed");
556         return E_WRITE_DESCRIPTOR_ERR;
557     }
558     if (!data.WriteString(diskId)) {
559         LOGE("StorageManagerProxy::Partition, WriteString failed");
560         return E_WRITE_PARCEL_ERR;
561     }
562     if (!data.WriteInt32(type)) {
563         LOGE("StorageManagerProxy::Partition WriteInt32 failed");
564         return E_WRITE_PARCEL_ERR;
565     }
566     int err = SendRequest(PARTITION, data, reply, option);
567     if (err != E_OK) {
568         return err;
569     }
570     return reply.ReadInt32();
571 }
572 
GetAllDisks(std::vector<Disk> & vecOfDisk)573 int32_t StorageManagerProxy::GetAllDisks(std::vector<Disk> &vecOfDisk)
574 {
575     LOGI("StorageManagerProxy::GetAllDisks");
576     MessageParcel data;
577     MessageParcel reply;
578     MessageOption option(MessageOption::TF_SYNC);
579     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
580         LOGE("StorageManagerProxy::GetAllDisks, WriteInterfaceToken failed");
581         return E_WRITE_DESCRIPTOR_ERR;
582     }
583 
584     int err = SendRequest(GET_ALL_DISKS, data, reply, option);
585     if (err != E_OK) {
586         return err;
587     }
588     err = reply.ReadInt32();
589     if (err != E_OK) {
590         return err;
591     }
592     uint size = reply.ReadUint32();
593     if (size == 0) {
594         return reply.ReadInt32();
595     }
596     for (uint i = 0; i < size; i++) {
597         std::unique_ptr<Disk> disk = Disk::Unmarshalling(reply);
598         LOGI("StorageManagerProxy::GetAllDisks push %{public}s", disk->GetDiskId().c_str());
599         vecOfDisk.push_back(*disk);
600     }
601     return E_OK;
602 }
603 
GetSystemSize(int64_t & systemSize)604 int32_t StorageManagerProxy::GetSystemSize(int64_t &systemSize)
605 {
606     LOGI("StorageManagerProxy::GetSystemSize");
607     MessageParcel data;
608     MessageParcel reply;
609     MessageOption option(MessageOption::TF_SYNC);
610     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
611         LOGE("StorageManagerProxy::GetSystemSize WriteInterfaceToken failed");
612         return E_WRITE_DESCRIPTOR_ERR;
613     }
614 
615     int err = SendRequest(GET_SYSTEM_SIZE, data, reply, option);
616     if (err != E_OK) {
617         return err;
618     }
619     err = reply.ReadInt32();
620     if (err != E_OK) {
621         return err;
622     }
623     systemSize = reply.ReadInt64();
624     return E_OK;
625 }
626 
GetTotalSize(int64_t & totalSize)627 int32_t StorageManagerProxy::GetTotalSize(int64_t &totalSize)
628 {
629     LOGI("StorageManagerProxy::GetTotalSize");
630     MessageParcel data;
631     MessageParcel reply;
632     MessageOption option(MessageOption::TF_SYNC);
633     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
634         LOGE("StorageManagerProxy::GetTotalSize WriteInterfaceToken failed");
635         return E_WRITE_DESCRIPTOR_ERR;
636     }
637 
638     int err = SendRequest(GET_TOTAL_SIZE, data, reply, option);
639     if (err != E_OK) {
640         return err;
641     }
642     err = reply.ReadInt32();
643     if (err != E_OK) {
644         return err;
645     }
646     totalSize = reply.ReadInt64();
647     return E_OK;
648 }
649 
GetFreeSize(int64_t & freeSize)650 int32_t StorageManagerProxy::GetFreeSize(int64_t &freeSize)
651 {
652     LOGI("StorageManagerProxy::GetFreeSize");
653     MessageParcel data;
654     MessageParcel reply;
655     MessageOption option(MessageOption::TF_SYNC);
656     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
657         LOGE("StorageManagerProxy::GetFreeSize WriteInterfaceToken failed");
658         return E_WRITE_DESCRIPTOR_ERR;
659     }
660 
661     int err = SendRequest(GET_FREE_SIZE, data, reply, option);
662     if (err != E_OK) {
663         return err;
664     }
665     err = reply.ReadInt32();
666     if (err != E_OK) {
667         return err;
668     }
669     freeSize = reply.ReadInt64();
670     return E_OK;
671 }
672 
GetUserStorageStats(StorageStats & storageStats)673 int32_t StorageManagerProxy::GetUserStorageStats(StorageStats &storageStats)
674 {
675     StorageStats result;
676     MessageParcel data;
677     MessageParcel reply;
678     MessageOption option(MessageOption::TF_SYNC);
679     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
680         return E_WRITE_DESCRIPTOR_ERR;
681     }
682 
683     int err = SendRequest(GET_CURR_USER_STATS, data, reply, option);
684     if (err != E_OK) {
685         return err;
686     }
687     err = reply.ReadInt32();
688     if (err != E_OK) {
689         return err;
690     }
691     storageStats = *StorageStats::Unmarshalling(reply);
692     return E_OK;
693 }
694 
GetUserStorageStats(int32_t userId,StorageStats & storageStats)695 int32_t StorageManagerProxy::GetUserStorageStats(int32_t userId, StorageStats &storageStats)
696 {
697     StorageStats result;
698     MessageParcel data;
699     MessageParcel reply;
700     MessageOption option(MessageOption::TF_SYNC);
701     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
702         return E_WRITE_DESCRIPTOR_ERR;
703     }
704 
705     if (!data.WriteInt32(userId)) {
706         return E_WRITE_PARCEL_ERR;
707     }
708     int err = SendRequest(GET_USER_STATS, data, reply, option);
709     if (err != E_OK) {
710         return err;
711     }
712     err = reply.ReadInt32();
713     if (err != E_OK) {
714         return err;
715     }
716     storageStats = *StorageStats::Unmarshalling(reply);
717     return E_OK;
718 }
719 
GetCurrentBundleStats(BundleStats & bundleStats)720 int32_t StorageManagerProxy::GetCurrentBundleStats(BundleStats &bundleStats)
721 {
722     BundleStats result;
723     MessageParcel data;
724     MessageParcel reply;
725     MessageOption option(MessageOption::TF_SYNC);
726     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
727         return E_WRITE_DESCRIPTOR_ERR;
728     }
729 
730     int err = SendRequest(GET_CURR_BUNDLE_STATS, data, reply, option);
731     if (err != E_OK) {
732         return err;
733     }
734     err = reply.ReadInt32();
735     if (err != E_OK) {
736         return err;
737     }
738     bundleStats = *BundleStats::Unmarshalling(reply);
739     return E_OK;
740 }
741 
GetVolumeByUuid(std::string fsUuid,VolumeExternal & vc)742 int32_t StorageManagerProxy::GetVolumeByUuid(std::string fsUuid, VolumeExternal &vc)
743 {
744     MessageParcel data;
745     MessageParcel reply;
746     MessageOption option(MessageOption::TF_SYNC);
747     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
748         return E_WRITE_DESCRIPTOR_ERR;
749     }
750 
751     if (!data.WriteString(fsUuid)) {
752         return E_WRITE_PARCEL_ERR;
753     }
754 
755     int err = SendRequest(GET_VOL_BY_UUID, data, reply, option);
756     if (err != E_OK) {
757         return err;
758     }
759     vc = *VolumeExternal::Unmarshalling(reply);
760     return reply.ReadInt32();
761 }
762 
GetVolumeById(std::string volumeId,VolumeExternal & vc)763 int32_t StorageManagerProxy::GetVolumeById(std::string volumeId, VolumeExternal &vc)
764 {
765     MessageParcel data;
766     MessageParcel reply;
767     MessageOption option(MessageOption::TF_SYNC);
768     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
769         return E_WRITE_DESCRIPTOR_ERR;
770     }
771 
772     if (!data.WriteString(volumeId)) {
773         return E_WRITE_PARCEL_ERR;
774     }
775 
776     int err = SendRequest(GET_VOL_BY_ID, data, reply, option);
777     if (err != E_OK) {
778         return err;
779     }
780     vc = *VolumeExternal::Unmarshalling(reply);
781     return reply.ReadInt32();
782 }
783 
SetVolumeDescription(std::string fsUuid,std::string description)784 int32_t StorageManagerProxy::SetVolumeDescription(std::string fsUuid, std::string description)
785 {
786     MessageParcel data;
787     MessageParcel reply;
788     MessageOption option(MessageOption::TF_SYNC);
789     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
790         return E_WRITE_DESCRIPTOR_ERR;
791     }
792 
793     if (!data.WriteString(fsUuid)) {
794         return E_WRITE_PARCEL_ERR;
795     }
796 
797     if (!data.WriteString(description)) {
798         return E_WRITE_PARCEL_ERR;
799     }
800 
801     int err = SendRequest(SET_VOL_DESC, data, reply, option);
802     if (err != E_OK) {
803         return err;
804     }
805     return reply.ReadInt32();
806 }
807 
Format(std::string volumeId,std::string fsType)808 int32_t StorageManagerProxy::Format(std::string volumeId, std::string fsType)
809 {
810     MessageParcel data;
811     MessageParcel reply;
812     MessageOption option(MessageOption::TF_SYNC);
813     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
814         return E_WRITE_DESCRIPTOR_ERR;
815     }
816 
817     if (!data.WriteString(volumeId)) {
818         return E_WRITE_PARCEL_ERR;
819     }
820 
821     if (!data.WriteString(fsType)) {
822         return E_WRITE_PARCEL_ERR;
823     }
824 
825     int err = SendRequest(FORMAT, data, reply, option);
826     if (err != E_OK) {
827         return err;
828     }
829     return reply.ReadInt32();
830 }
831 
GetDiskById(std::string diskId,Disk & disk)832 int32_t StorageManagerProxy::GetDiskById(std::string diskId, Disk &disk)
833 {
834     MessageParcel data;
835     MessageParcel reply;
836     MessageOption option(MessageOption::TF_SYNC);
837     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
838         return E_WRITE_DESCRIPTOR_ERR;
839     }
840 
841     if (!data.WriteString(diskId)) {
842         return E_WRITE_PARCEL_ERR;
843     }
844 
845     int err = SendRequest(GET_DISK_BY_ID, data, reply, option);
846     if (err != E_OK) {
847         return err;
848     }
849     disk = *Disk::Unmarshalling(reply);
850     return reply.ReadInt32();
851 }
852 
CreateShareFile(std::string uri,uint32_t tokenId,uint32_t flag)853 int32_t StorageManagerProxy::CreateShareFile(std::string uri, uint32_t tokenId, uint32_t flag)
854 {
855     MessageParcel data;
856     MessageParcel reply;
857     MessageOption option(MessageOption::TF_SYNC);
858 
859     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
860         return E_WRITE_DESCRIPTOR_ERR;
861     }
862 
863     if (!data.WriteString(uri)) {
864         return E_WRITE_PARCEL_ERR;
865     }
866 
867     if (!data.WriteUint32(tokenId)) {
868         return E_WRITE_PARCEL_ERR;
869     }
870 
871     if (!data.WriteUint32(flag)) {
872         return E_WRITE_PARCEL_ERR;
873     }
874 
875     int err = SendRequest(CREATE_SHARE_FILE, data, reply, option);
876     if (err != E_OK) {
877         return err;
878     }
879 
880     return reply.ReadInt32();
881 }
882 
DeleteShareFile(uint32_t tokenId,std::vector<std::string> sharePathList)883 int32_t StorageManagerProxy::DeleteShareFile(uint32_t tokenId, std::vector<std::string>sharePathList)
884 {
885     MessageParcel data;
886     MessageParcel reply;
887     MessageOption option(MessageOption::TF_ASYNC);
888 
889     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
890         return E_WRITE_DESCRIPTOR_ERR;
891     }
892 
893     if (!data.WriteUint32(tokenId)) {
894         return E_WRITE_PARCEL_ERR;
895     }
896 
897     if (!data.WriteStringVector(sharePathList)) {
898         return E_WRITE_PARCEL_ERR;
899     }
900 
901     int err = SendRequest(DELETE_SHARE_FILE, data, reply, option);
902     if (err != E_OK) {
903         return err;
904     }
905 
906     return reply.ReadInt32();
907 }
908 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)909 int32_t StorageManagerProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
910     MessageOption &option)
911 {
912     sptr<IRemoteObject> remote = Remote();
913     if (remote == nullptr) {
914         LOGE("remote is nullptr, code = %{public}d", code);
915         return E_REMOTE_IS_NULLPTR;
916     }
917 
918     int32_t result = remote->SendRequest(code, data, reply, option);
919     if (result != E_OK) {
920         LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
921         return result;
922     }
923 
924     return E_OK;
925 }
926 } // StorageManager
927 } // OHOS
928