1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "storage_manager_proxy.h"
17 #include "hitrace_meter.h"
18 #include "storage_manager_ipc_interface_code.h"
19 #include "storage_service_errno.h"
20 #include "storage_service_log.h"
21 #include "utils/storage_utils.h"
22 
23 namespace OHOS {
24 namespace StorageManager {
PrepareAddUser(int32_t userId,uint32_t flags)25 int32_t StorageManagerProxy::PrepareAddUser(int32_t userId, uint32_t flags)
26 {
27     LOGI("StorageManagerProxy::PrepareAddUser, userId:%{public}d", userId);
28     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
29     MessageParcel data;
30     MessageParcel reply;
31     MessageOption option(MessageOption::TF_SYNC);
32     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
33         LOGE("StorageManagerProxy::PrepareAddUser, WriteInterfaceToken failed");
34         return E_WRITE_DESCRIPTOR_ERR;
35     }
36     if (!data.WriteInt32(userId)) {
37         LOGE("StorageManagerProxy::PrepareAddUser, WriteInt32 failed");
38         return E_WRITE_PARCEL_ERR;
39     }
40     if (!data.WriteUint32(flags)) {
41         LOGE("StorageManagerProxy::PrepareAddUser, WriteUint32 failed");
42         return E_WRITE_PARCEL_ERR;
43     }
44     int32_t err = SendRequest(
45         static_cast<int32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER), data, reply, option);
46     if (err != E_OK) {
47         return err;
48     }
49     return reply.ReadUint32();
50 }
51 
RemoveUser(int32_t userId,uint32_t flags)52 int32_t StorageManagerProxy::RemoveUser(int32_t userId, uint32_t flags)
53 {
54     LOGI("StorageManagerProxy::RemoveUser, userId:%{public}d", userId);
55     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
56     MessageParcel data;
57     MessageParcel reply;
58     MessageOption option(MessageOption::TF_SYNC);
59     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
60         LOGE("StorageManagerProxy::RemoveUser, WriteInterfaceToken failed");
61         return E_WRITE_DESCRIPTOR_ERR;
62     }
63     if (!data.WriteInt32(userId)) {
64         LOGE("StorageManagerProxy::RemoveUser, WriteInt32 failed");
65         return E_WRITE_PARCEL_ERR;
66     }
67     if (!data.WriteUint32(flags)) {
68         LOGE("StorageManagerProxy::RemoveUser, WriteUint32 failed");
69         return E_WRITE_PARCEL_ERR;
70     }
71     int32_t err = SendRequest(
72         static_cast<int32_t>(StorageManagerInterfaceCode::REMOVE_USER), data, reply, option);
73     if (err != E_OK) {
74         return err;
75     }
76     return reply.ReadUint32();
77 }
78 
PrepareStartUser(int32_t userId)79 int32_t StorageManagerProxy::PrepareStartUser(int32_t userId)
80 {
81     LOGI("StorageManagerProxy::PrepareStartUser, userId:%{public}d", userId);
82     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
83     MessageParcel data;
84     MessageParcel reply;
85     MessageOption option(MessageOption::TF_SYNC);
86     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
87         LOGE("StorageManagerProxy::PrepareStartUser, WriteInterfaceToken failed");
88         return E_WRITE_DESCRIPTOR_ERR;
89     }
90     if (!data.WriteInt32(userId)) {
91         LOGE("StorageManagerProxy::PrepareStartUser, WriteInt32 failed");
92         return E_WRITE_PARCEL_ERR;
93     }
94     int32_t err = SendRequest(
95         static_cast<int32_t>(StorageManagerInterfaceCode::PREPARE_START_USER), data, reply, option);
96     if (err != E_OK) {
97         return err;
98     }
99     return reply.ReadUint32();
100 }
101 
StopUser(int32_t userId)102 int32_t StorageManagerProxy::StopUser(int32_t userId)
103 {
104     LOGI("StorageManagerProxy::StopUser, userId:%{public}d", userId);
105     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
106     MessageParcel data;
107     MessageParcel reply;
108     MessageOption option(MessageOption::TF_SYNC);
109     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
110         LOGE("StorageManagerProxy::StopUser, WriteInterfaceToken failed");
111         return E_WRITE_DESCRIPTOR_ERR;
112     }
113     if (!data.WriteInt32(userId)) {
114         LOGE("StorageManagerProxy::StopUser, WriteInt32 failed");
115         return E_WRITE_PARCEL_ERR;
116     }
117     int32_t err = SendRequest(
118         static_cast<int32_t>(StorageManagerInterfaceCode::STOP_USER), data, reply, option);
119     if (err != E_OK) {
120         return err;
121     }
122     return reply.ReadUint32();
123 }
124 
CompleteAddUser(int32_t userId)125 int32_t StorageManagerProxy::CompleteAddUser(int32_t userId)
126 {
127     LOGI("StorageManagerProxy::CompleteAddUser, userId:%{public}d", userId);
128     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
129     MessageParcel data;
130     MessageParcel reply;
131     MessageOption option(MessageOption::TF_SYNC);
132     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
133         LOGE("StorageManagerProxy::CompleteAddUser, WriteInterfaceToken failed");
134         return E_WRITE_DESCRIPTOR_ERR;
135     }
136     if (!data.WriteInt32(userId)) {
137         LOGE("StorageManagerProxy::CompleteAddUser, WriteInt32 failed");
138         return E_WRITE_PARCEL_ERR;
139     }
140     int32_t err = SendRequest(
141         static_cast<int32_t>(StorageManagerInterfaceCode::COMPLETE_ADD_USER), data, reply, option);
142     if (err != E_OK) {
143         return err;
144     }
145     return reply.ReadUint32();
146 }
147 
GenerateUserKeys(uint32_t userId,uint32_t flags)148 int32_t StorageManagerProxy::GenerateUserKeys(uint32_t userId, uint32_t flags)
149 {
150     LOGI("user ID: %{public}u, flags: %{public}u", userId, flags);
151     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
152     MessageParcel data;
153     MessageParcel reply;
154     MessageOption option(MessageOption::TF_SYNC);
155     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
156         LOGE("WriteInterfaceToken failed");
157         return E_WRITE_DESCRIPTOR_ERR;
158     }
159     if (!data.WriteUint32(userId)) {
160         LOGE("Write user ID failed");
161         return E_WRITE_PARCEL_ERR;
162     }
163     if (!data.WriteUint32(flags)) {
164         LOGE("Write key flags failed");
165         return E_WRITE_PARCEL_ERR;
166     }
167     int32_t err = SendRequest(
168         static_cast<int32_t>(StorageManagerInterfaceCode::CREATE_USER_KEYS), data, reply, option);
169     if (err != E_OK) {
170         return err;
171     }
172 
173     return reply.ReadInt32();
174 }
175 
DeleteUserKeys(uint32_t userId)176 int32_t StorageManagerProxy::DeleteUserKeys(uint32_t userId)
177 {
178     LOGI("user ID: %{public}u", userId);
179     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
180     MessageParcel data;
181     MessageParcel reply;
182     MessageOption option(MessageOption::TF_SYNC);
183     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
184         LOGE("WriteInterfaceToken failed");
185         return E_WRITE_DESCRIPTOR_ERR;
186     }
187     if (!data.WriteUint32(userId)) {
188         LOGE("Write user ID failed");
189         return E_WRITE_PARCEL_ERR;
190     }
191     int32_t err = SendRequest(
192         static_cast<int32_t>(StorageManagerInterfaceCode::DELETE_USER_KEYS), data, reply, option);
193     if (err != E_OK) {
194         return err;
195     }
196 
197     return reply.ReadInt32();
198 }
199 
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)200 int32_t StorageManagerProxy::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
201                                             const std::vector<uint8_t> &token,
202                                             const std::vector<uint8_t> &oldSecret,
203                                             const std::vector<uint8_t> &newSecret)
204 {
205     LOGI("user ID: %{public}u", userId);
206     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
207     MessageParcel data;
208     MessageParcel reply;
209     MessageOption option(MessageOption::TF_SYNC);
210     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
211         LOGE("WriteInterfaceToken failed");
212         return E_WRITE_DESCRIPTOR_ERR;
213     }
214     if (!data.WriteUint32(userId)) {
215         LOGE("Write user ID failed");
216         return E_WRITE_PARCEL_ERR;
217     }
218     if (!data.WriteUint64(secureUid)) {
219         LOGE("Write user ID failed");
220         return E_WRITE_PARCEL_ERR;
221     }
222     if (!data.WriteUInt8Vector(token)) {
223         LOGE("Write token failed");
224         return E_WRITE_PARCEL_ERR;
225     }
226     if (!data.WriteUInt8Vector(oldSecret)) {
227         LOGE("Write oldSecret failed");
228         return E_WRITE_PARCEL_ERR;
229     }
230     if (!data.WriteUInt8Vector(newSecret)) {
231         LOGE("Write newSecret failed");
232         return E_WRITE_PARCEL_ERR;
233     }
234 
235     int32_t err = SendRequest(
236         static_cast<int32_t>(StorageManagerInterfaceCode::UPDATE_USER_AUTH), data, reply, option);
237     if (err != E_OK) {
238         return err;
239     }
240 
241     return reply.ReadInt32();
242 }
243 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)244 int32_t StorageManagerProxy::ActiveUserKey(uint32_t userId,
245                                            const std::vector<uint8_t> &token,
246                                            const std::vector<uint8_t> &secret)
247 {
248     LOGI("user ID: %{public}u", userId);
249     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
250     MessageParcel data;
251     MessageParcel reply;
252     MessageOption option(MessageOption::TF_SYNC);
253     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
254         LOGE("WriteInterfaceToken failed");
255         return E_WRITE_DESCRIPTOR_ERR;
256     }
257     if (!data.WriteUint32(userId)) {
258         LOGE("Write user ID failed");
259         return E_WRITE_PARCEL_ERR;
260     }
261     if (!data.WriteUInt8Vector(token)) {
262         LOGE("Write token failed");
263         return E_WRITE_PARCEL_ERR;
264     }
265     if (!data.WriteUInt8Vector(secret)) {
266         LOGE("Write secret failed");
267         return E_WRITE_PARCEL_ERR;
268     }
269 
270     int32_t err = SendRequest(
271         static_cast<int32_t>(StorageManagerInterfaceCode::ACTIVE_USER_KEY), data, reply, option);
272     if (err != E_OK) {
273         return err;
274     }
275 
276     return reply.ReadInt32();
277 }
278 
InactiveUserKey(uint32_t userId)279 int32_t StorageManagerProxy::InactiveUserKey(uint32_t userId)
280 {
281     LOGI("user ID: %{public}u", userId);
282     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
283     MessageParcel data;
284     MessageParcel reply;
285     MessageOption option(MessageOption::TF_SYNC);
286     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
287         LOGE("WriteInterfaceToken failed");
288         return E_WRITE_DESCRIPTOR_ERR;
289     }
290     if (!data.WriteUint32(userId)) {
291         LOGE("Write user ID failed");
292         return E_WRITE_PARCEL_ERR;
293     }
294     int32_t err = SendRequest(
295         static_cast<int32_t>(StorageManagerInterfaceCode::INACTIVE_USER_KEY), data, reply, option);
296     if (err != E_OK) {
297         return err;
298     }
299 
300     return reply.ReadInt32();
301 }
302 
LockUserScreen(uint32_t userId)303 int32_t StorageManagerProxy::LockUserScreen(uint32_t userId)
304 {
305     LOGI("user ID: %{public}u", userId);
306     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
307     MessageParcel data;
308     MessageParcel reply;
309     MessageOption option(MessageOption::TF_SYNC);
310     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
311         LOGE("WriteInterfaceToken failed");
312         return E_WRITE_DESCRIPTOR_ERR;
313     }
314     if (!data.WriteUint32(userId)) {
315         LOGE("Write user ID failed");
316         return E_WRITE_PARCEL_ERR;
317     }
318     int32_t err = SendRequest(
319         static_cast<int32_t>(StorageManagerInterfaceCode::LOCK_USER_SCREEN), data, reply, option);
320     if (err != E_OK) {
321         return err;
322     }
323 
324     return reply.ReadInt32();
325 }
326 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)327 int32_t StorageManagerProxy::UnlockUserScreen(uint32_t userId,
328                                               const std::vector<uint8_t> &token,
329                                               const std::vector<uint8_t> &secret)
330 {
331     LOGI("user ID: %{public}u", userId);
332     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
333     MessageParcel data;
334     MessageParcel reply;
335     MessageOption option(MessageOption::TF_SYNC);
336     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
337         LOGE("WriteInterfaceToken failed");
338         return E_WRITE_DESCRIPTOR_ERR;
339     }
340     if (!data.WriteUint32(userId)) {
341         LOGE("Write user ID failed");
342         return E_WRITE_PARCEL_ERR;
343     }
344     if (!data.WriteUInt8Vector(token)) {
345         LOGE("Write token failed");
346         return E_WRITE_PARCEL_ERR;
347     }
348     if (!data.WriteUInt8Vector(secret)) {
349         LOGE("Write secret failed");
350         return E_WRITE_PARCEL_ERR;
351     }
352     int32_t err = SendRequest(
353         static_cast<int32_t>(StorageManagerInterfaceCode::UNLOCK_USER_SCREEN), data, reply, option);
354     if (err != E_OK) {
355         return err;
356     }
357 
358     return reply.ReadInt32();
359 }
360 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)361 int32_t StorageManagerProxy::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
362 {
363     LOGI("user ID: %{public}u", userId);
364     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
365     MessageParcel data;
366     MessageParcel reply;
367     MessageOption option(MessageOption::TF_SYNC);
368     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
369         LOGE("WriteInterfaceToken failed");
370         return E_WRITE_DESCRIPTOR_ERR;
371     }
372     if (!data.WriteUint32(userId)) {
373         LOGE("Write user ID failed");
374         return E_WRITE_PARCEL_ERR;
375     }
376     int32_t err = SendRequest(
377         static_cast<int32_t>(StorageManagerInterfaceCode::LOCK_SCREEN_STATUS), data, reply, option);
378     if (err != E_OK) {
379         return err;
380     }
381     lockScreenStatus = reply.ReadBool();
382     return reply.ReadInt32();
383 }
384 
UpdateKeyContext(uint32_t userId)385 int32_t StorageManagerProxy::UpdateKeyContext(uint32_t userId)
386 {
387     LOGI("user ID: %{public}u", userId);
388     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
389     MessageParcel data;
390     MessageParcel reply;
391     MessageOption option(MessageOption::TF_SYNC);
392     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
393         LOGE("WriteInterfaceToken failed");
394         return E_WRITE_DESCRIPTOR_ERR;
395     }
396     if (!data.WriteUint32(userId)) {
397         LOGE("Write user ID failed");
398         return E_WRITE_PARCEL_ERR;
399     }
400     int32_t err = SendRequest(
401         static_cast<int32_t>(StorageManagerInterfaceCode::UPDATE_KEY_CONTEXT), data, reply, option);
402     if (err != E_OK) {
403         return err;
404     }
405 
406     return reply.ReadInt32();
407 }
408 
GenerateAppkey(uint32_t hashId,uint32_t userId,std::string & keyId)409 int32_t StorageManagerProxy::GenerateAppkey(uint32_t hashId, uint32_t userId, std::string &keyId)
410 {
411     LOGI("userId ID: %{public}u", userId);
412     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
413     MessageParcel data;
414     MessageParcel reply;
415     MessageOption option(MessageOption::TF_SYNC);
416     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
417         LOGE("WriteInterfaceToken failed");
418         return E_WRITE_DESCRIPTOR_ERR;
419     }
420     if (!data.WriteUint32(hashId)) {
421         LOGE("Write hashId failed");
422         return E_WRITE_PARCEL_ERR;
423     }
424     if (!data.WriteUint32(userId)) {
425         LOGE("Write appUid failed");
426         return E_WRITE_PARCEL_ERR;
427     }
428     int32_t err = SendRequest(
429         static_cast<int32_t>(StorageManagerInterfaceCode::GENERATE_APP_KEY), data, reply, option);
430     if (err != E_OK) {
431         return err;
432     }
433     keyId = reply.ReadString();
434     return reply.ReadInt32();
435 }
436 
DeleteAppkey(const std::string keyId)437 int32_t StorageManagerProxy::DeleteAppkey(const std::string keyId)
438 {
439     LOGI("DeleteAppkey enter ");
440     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
441     MessageParcel data;
442     MessageParcel reply;
443     MessageOption option(MessageOption::TF_SYNC);
444     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
445         LOGE("WriteInterfaceToken failed");
446         return E_WRITE_DESCRIPTOR_ERR;
447     }
448     if (!data.WriteString(keyId)) {
449         LOGE("Write key ID failed");
450         return E_WRITE_PARCEL_ERR;
451     }
452     int32_t err = SendRequest(
453         static_cast<int32_t>(StorageManagerInterfaceCode::DELETE_APP_KEY), data, reply, option);
454     if (err != E_OK) {
455         return err;
456     }
457 
458     return reply.ReadInt32();
459 }
460 
GetFreeSizeOfVolume(std::string volumeUuid,int64_t & freeSize)461 int32_t StorageManagerProxy::GetFreeSizeOfVolume(std::string volumeUuid, int64_t &freeSize)
462 {
463     LOGI("StorageManagerProxy::GetFreeSizeOfVolume, volumeUuid:%{public}s",
464         GetAnonyString(volumeUuid).c_str());
465     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
466     MessageParcel data;
467     MessageParcel reply;
468     MessageOption option(MessageOption::TF_SYNC);
469     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
470         LOGE("StorageManagerProxy::GetFreeSizeOfVolume, WriteInterfaceToken failed");
471         return E_WRITE_DESCRIPTOR_ERR;
472     }
473 
474     if (!data.WriteString(volumeUuid)) {
475         LOGE("StorageManagerProxy::GetFreeSizeOfVolume, WriteString failed");
476         return E_WRITE_PARCEL_ERR;
477     }
478     int32_t err = SendRequest(
479         static_cast<int32_t>(StorageManagerInterfaceCode::GET_FREE), data, reply, option);
480     if (err != E_OK) {
481         return err;
482     }
483     err = reply.ReadInt32();
484     if (err != E_OK) {
485         return err;
486     }
487     freeSize = reply.ReadInt64();
488     return E_OK;
489 }
490 
GetTotalSizeOfVolume(std::string volumeUuid,int64_t & totalSize)491 int32_t StorageManagerProxy::GetTotalSizeOfVolume(std::string volumeUuid, int64_t &totalSize)
492 {
493     LOGI("StorageManagerProxy::GetTotalSizeOfVolume, volumeUuid:%{public}s",
494         GetAnonyString(volumeUuid).c_str());
495     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
496     MessageParcel data;
497     MessageParcel reply;
498     MessageOption option(MessageOption::TF_SYNC);
499     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
500         LOGE("StorageManagerProxy::GetTotalSizeOfVolume, WriteInterfaceToken failed");
501         return E_WRITE_DESCRIPTOR_ERR;
502     }
503 
504     if (!data.WriteString(volumeUuid)) {
505         LOGE("StorageManagerProxy::GetTotalSizeOfVolume, WriteString failed");
506         return E_WRITE_PARCEL_ERR;
507     }
508     int32_t err = SendRequest(
509         static_cast<int32_t>(StorageManagerInterfaceCode::GET_TOTAL), data, reply, option);
510     if (err != E_OK) {
511         return err;
512     }
513     err = reply.ReadInt32();
514     if (err != E_OK) {
515         return err;
516     }
517     totalSize = reply.ReadInt64();
518     return E_OK;
519 }
520 
GetBundleStats(std::string pkgName,BundleStats & bundleStats,int32_t appIndex,uint32_t statFlag)521 int32_t StorageManagerProxy::GetBundleStats(std::string pkgName, BundleStats &bundleStats,
522     int32_t appIndex, uint32_t statFlag)
523 {
524     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
525     MessageParcel data;
526     MessageParcel reply;
527     MessageOption option(MessageOption::TF_SYNC);
528     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
529         LOGE("StorageManagerProxy::GetBundleStats, WriteInterfaceToken failed");
530         return E_WRITE_DESCRIPTOR_ERR;
531     }
532 
533     if (!data.WriteString(pkgName)) {
534         LOGE("StorageManagerProxy::GetBundleStats, WriteString failed");
535         return E_WRITE_PARCEL_ERR;
536     }
537 
538     if (!data.WriteInt32(appIndex)) {
539         LOGE("StorageManagerProxy::GetBundleStats, WriteInt32 failed");
540         return E_WRITE_PARCEL_ERR;
541     }
542 
543     if (!data.WriteUint32(statFlag)) {
544         LOGE("StorageManagerProxy::GetBundleStats, WriteInt32 failed");
545         return E_WRITE_PARCEL_ERR;
546     }
547 
548     int32_t err = SendRequest(
549         static_cast<int32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATUS), data, reply, option);
550     if (err != E_OK) {
551         return err;
552     }
553     err = reply.ReadInt32();
554     if (err != E_OK) {
555         return err;
556     }
557     bundleStats = *BundleStats::Unmarshalling(reply);
558     return E_OK;
559 }
560 
NotifyVolumeCreated(VolumeCore vc)561 int32_t StorageManagerProxy::NotifyVolumeCreated(VolumeCore vc)
562 {
563     LOGI("StorageManagerProxy::NotifyVolumeCreated, volumeUuid:%{public}s",
564         GetAnonyString(vc.GetId()).c_str());
565     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
566     MessageParcel data;
567     MessageParcel reply;
568     MessageOption option(MessageOption::TF_SYNC);
569     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
570         LOGE("StorageManagerProxy::NotifyVolumeCreated, WriteInterfaceToken failed");
571         return E_WRITE_DESCRIPTOR_ERR;
572     }
573 
574     if (!vc.Marshalling(data)) {
575         LOGE("StorageManagerProxy::NotifyVolumeCreated, WriteVolumeInfo failed");
576         return E_WRITE_PARCEL_ERR;
577     }
578 
579     return SendRequest(
580         static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_CREATED), data, reply, option);
581 }
582 
NotifyVolumeMounted(std::string volumeId,int32_t fsType,std::string fsUuid,std::string path,std::string description)583 int32_t StorageManagerProxy::NotifyVolumeMounted(std::string volumeId, int32_t fsType, std::string fsUuid,
584     std::string path, std::string description)
585 {
586     LOGI("StorageManagerProxy::NotifyVolumeMounted, volumeUuid:%{public}s",
587         GetAnonyString(volumeId).c_str());
588     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
589     MessageParcel data;
590     MessageParcel reply;
591     MessageOption option(MessageOption::TF_SYNC);
592     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
593         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteInterfaceToken failed");
594         return E_WRITE_DESCRIPTOR_ERR;
595     }
596 
597     if (!data.WriteString(volumeId)) {
598         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
599         return E_WRITE_PARCEL_ERR;
600     }
601 
602     if (!data.WriteInt32(fsType)) {
603         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteInt32 failed");
604         return E_WRITE_PARCEL_ERR;
605     }
606 
607     if (!data.WriteString(fsUuid)) {
608         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
609         return E_WRITE_PARCEL_ERR;
610     }
611 
612     if (!data.WriteString(path)) {
613         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
614         return E_WRITE_PARCEL_ERR;
615     }
616 
617     if (!data.WriteString(description)) {
618         LOGE("StorageManagerProxy::NotifyVolumeMounted, WriteString failed");
619         return E_WRITE_PARCEL_ERR;
620     }
621 
622     return SendRequest(
623         static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_MOUNTED), data, reply, option);
624 }
625 
NotifyVolumeStateChanged(std::string volumeId,VolumeState state)626 int32_t StorageManagerProxy::NotifyVolumeStateChanged(std::string volumeId, VolumeState state)
627 {
628     LOGI("StorageManagerProxy::NotifyVolumeStateChanged, volumeId:%{public}s", volumeId.c_str());
629     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
630     MessageParcel data;
631     MessageParcel reply;
632     MessageOption option(MessageOption::TF_SYNC);
633     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
634         LOGE("StorageManagerProxy::NotifyVolumeStateChanged, WriteInterfaceToken failed");
635         return E_WRITE_DESCRIPTOR_ERR;
636     }
637 
638     if (!data.WriteString(volumeId)) {
639         LOGE("StorageManagerProxy::NotifyVolumeStateChanged, WriteString failed");
640         return E_WRITE_PARCEL_ERR;
641     }
642 
643     if (!data.WriteInt32(state)) {
644         LOGE("StorageManagerProxy::NotifyVolumeStateChanged, WriteInt failed");
645         return E_WRITE_PARCEL_ERR;
646     }
647 
648     return SendRequest(
649         static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_STATE_CHANGED), data, reply,
650         option);
651 }
652 
Mount(std::string volumeId)653 int32_t StorageManagerProxy::Mount(std::string volumeId)
654 {
655     LOGI("StorageManagerProxy::Mount, volumeId:%{public}s", volumeId.c_str());
656     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
657     MessageParcel data;
658     MessageParcel reply;
659     MessageOption option(MessageOption::TF_SYNC);
660     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
661         LOGE("StorageManagerProxy::Mount, WriteInterfaceToken failed");
662         return E_WRITE_DESCRIPTOR_ERR;
663     }
664 
665     if (!data.WriteString(volumeId)) {
666         LOGE("StorageManagerProxy::Mount, WriteString failed");
667         return E_WRITE_PARCEL_ERR;
668     }
669     int32_t err = SendRequest(
670         static_cast<int32_t>(StorageManagerInterfaceCode::MOUNT), data, reply, option);
671     if (err != E_OK) {
672         return err;
673     }
674     return reply.ReadInt32();
675 }
676 
Unmount(std::string volumeId)677 int32_t StorageManagerProxy::Unmount(std::string volumeId)
678 {
679     LOGI("StorageManagerProxy::Unmount, volumeId:%{public}s", volumeId.c_str());
680     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
681     MessageParcel data;
682     MessageParcel reply;
683     MessageOption option(MessageOption::TF_SYNC);
684     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
685         LOGE("StorageManagerProxy::Unmount, WriteInterfaceToken failed");
686         return E_WRITE_DESCRIPTOR_ERR;
687     }
688 
689     if (!data.WriteString(volumeId)) {
690         LOGE("StorageManagerProxy::Unmount, WriteString failed");
691         return E_WRITE_PARCEL_ERR;
692     }
693     int32_t err = SendRequest(
694         static_cast<int32_t>(StorageManagerInterfaceCode::UNMOUNT), data, reply, option);
695     if (err != E_OK) {
696         return err;
697     }
698     return reply.ReadInt32();
699 }
700 
GetAllVolumes(std::vector<VolumeExternal> & vecOfVol)701 int32_t StorageManagerProxy::GetAllVolumes(std::vector<VolumeExternal> &vecOfVol)
702 {
703     LOGI("StorageManagerProxy::GetAllVolumes");
704     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
705     MessageParcel data;
706     MessageParcel reply;
707     MessageOption option(MessageOption::TF_SYNC);
708     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
709         LOGE("StorageManagerProxy::GetAllVolumes, WriteInterfaceToken failed");
710         return E_WRITE_DESCRIPTOR_ERR;
711     }
712 
713     int32_t err = SendRequest(
714         static_cast<int32_t>(StorageManagerInterfaceCode::GET_ALL_VOLUMES), data, reply, option);
715     if (err != E_OK) {
716         return err;
717     }
718     err = reply.ReadInt32();
719     if (err != E_OK) {
720         return err;
721     }
722     uint size = reply.ReadUint32();
723     if (size == 0) {
724         return reply.ReadInt32();
725     }
726     for (uint32_t i = 0; i < size; i++) {
727         std::unique_ptr<VolumeExternal> ve = VolumeExternal::Unmarshalling(reply);
728         LOGI("StorageManagerProxy::GetAllVolumes push %{public}s", ve->GetId().c_str());
729         vecOfVol.push_back(*ve);
730     }
731     return E_OK;
732 }
733 
NotifyDiskCreated(Disk disk)734 int32_t StorageManagerProxy::NotifyDiskCreated(Disk disk)
735 {
736     LOGI("StorageManagerProxy::NotifyDiskCreate, diskId:%{public}s", GetAnonyString(disk.GetDiskId()).c_str());
737     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
738     MessageParcel data;
739     MessageParcel reply;
740     MessageOption option(MessageOption::TF_SYNC);
741     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
742         LOGE("StorageManagerProxy::NotifyDiskCreate, WriteInterfaceToken failed");
743         return E_WRITE_DESCRIPTOR_ERR;
744     }
745 
746     if (!disk.Marshalling(data)) {
747         LOGE("StorageManagerProxy::NotifyDiskCreate, WriteDiskInfo failed");
748         return E_WRITE_PARCEL_ERR;
749     }
750 
751     return SendRequest(static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_CREATED), data, reply, option);
752 }
753 
NotifyDiskDestroyed(std::string diskId)754 int32_t StorageManagerProxy::NotifyDiskDestroyed(std::string diskId)
755 {
756     LOGI("StorageManagerProxy::NotifyDiskDestroyed, diskId:%{public}s", GetAnonyString(diskId).c_str());
757     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
758     MessageParcel data;
759     MessageParcel reply;
760     MessageOption option(MessageOption::TF_SYNC);
761     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
762         LOGE("StorageManagerProxy::NotifyDiskDestroyed, WriteInterfaceToken failed");
763         return E_WRITE_DESCRIPTOR_ERR;
764     }
765     if (!data.WriteString(diskId)) {
766         LOGE("StorageManagerProxy::NotifyDiskDestroyed, WriteString failed");
767         return E_WRITE_PARCEL_ERR;
768     }
769     return SendRequest(static_cast<int32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_DESTROYED), data, reply, option);
770 }
771 
Partition(std::string diskId,int32_t type)772 int32_t StorageManagerProxy::Partition(std::string diskId, int32_t type)
773 {
774     LOGI("StorageManagerProxy::Partition, diskId:%{public}s", GetAnonyString(diskId).c_str());
775     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
776     MessageParcel data;
777     MessageParcel reply;
778     MessageOption option(MessageOption::TF_SYNC);
779     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
780         LOGE("StorageManagerProxy::Partition, WriteInterfaceToken failed");
781         return E_WRITE_DESCRIPTOR_ERR;
782     }
783     if (!data.WriteString(diskId)) {
784         LOGE("StorageManagerProxy::Partition, WriteString failed");
785         return E_WRITE_PARCEL_ERR;
786     }
787     if (!data.WriteInt32(type)) {
788         LOGE("StorageManagerProxy::Partition WriteInt32 failed");
789         return E_WRITE_PARCEL_ERR;
790     }
791     int32_t err = SendRequest(
792         static_cast<int32_t>(StorageManagerInterfaceCode::PARTITION), data, reply, option);
793     if (err != E_OK) {
794         return err;
795     }
796     return reply.ReadInt32();
797 }
798 
GetAllDisks(std::vector<Disk> & vecOfDisk)799 int32_t StorageManagerProxy::GetAllDisks(std::vector<Disk> &vecOfDisk)
800 {
801     LOGI("StorageManagerProxy::GetAllDisks");
802     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
803     MessageParcel data;
804     MessageParcel reply;
805     MessageOption option(MessageOption::TF_SYNC);
806     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
807         LOGE("StorageManagerProxy::GetAllDisks, WriteInterfaceToken failed");
808         return E_WRITE_DESCRIPTOR_ERR;
809     }
810 
811     int32_t err = SendRequest(
812         static_cast<int32_t>(StorageManagerInterfaceCode::GET_ALL_DISKS), data, reply, option);
813     if (err != E_OK) {
814         return err;
815     }
816     err = reply.ReadInt32();
817     if (err != E_OK) {
818         return err;
819     }
820     uint size = reply.ReadUint32();
821     if (size == 0) {
822         return reply.ReadInt32();
823     }
824     for (uint32_t i = 0; i < size; i++) {
825         std::unique_ptr<Disk> disk = Disk::Unmarshalling(reply);
826         LOGI("StorageManagerProxy::GetAllDisks push %{public}s", GetAnonyString(disk->GetDiskId()).c_str());
827         vecOfDisk.push_back(*disk);
828     }
829     return E_OK;
830 }
831 
GetSystemSize(int64_t & systemSize)832 int32_t StorageManagerProxy::GetSystemSize(int64_t &systemSize)
833 {
834     LOGI("StorageManagerProxy::GetSystemSize");
835     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
836     MessageParcel data;
837     MessageParcel reply;
838     MessageOption option(MessageOption::TF_SYNC);
839     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
840         LOGE("StorageManagerProxy::GetSystemSize WriteInterfaceToken failed");
841         return E_WRITE_DESCRIPTOR_ERR;
842     }
843 
844     int32_t err = SendRequest(
845         static_cast<int32_t>(StorageManagerInterfaceCode::GET_SYSTEM_SIZE), data, reply, option);
846     if (err != E_OK) {
847         return err;
848     }
849     err = reply.ReadInt32();
850     if (err != E_OK) {
851         return err;
852     }
853     systemSize = reply.ReadInt64();
854     return E_OK;
855 }
856 
GetTotalSize(int64_t & totalSize)857 int32_t StorageManagerProxy::GetTotalSize(int64_t &totalSize)
858 {
859     LOGI("StorageManagerProxy::GetTotalSize");
860     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
861     MessageParcel data;
862     MessageParcel reply;
863     MessageOption option(MessageOption::TF_SYNC);
864     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
865         LOGE("StorageManagerProxy::GetTotalSize WriteInterfaceToken failed");
866         return E_WRITE_DESCRIPTOR_ERR;
867     }
868 
869     int32_t err = SendRequest(
870         static_cast<int32_t>(StorageManagerInterfaceCode::GET_TOTAL_SIZE), data, reply, option);
871     if (err != E_OK) {
872         return err;
873     }
874     err = reply.ReadInt32();
875     if (err != E_OK) {
876         return err;
877     }
878     totalSize = reply.ReadInt64();
879     return E_OK;
880 }
881 
GetFreeSize(int64_t & freeSize)882 int32_t StorageManagerProxy::GetFreeSize(int64_t &freeSize)
883 {
884     LOGI("StorageManagerProxy::GetFreeSize");
885     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
886     MessageParcel data;
887     MessageParcel reply;
888     MessageOption option(MessageOption::TF_SYNC);
889     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
890         LOGE("StorageManagerProxy::GetFreeSize WriteInterfaceToken failed");
891         return E_WRITE_DESCRIPTOR_ERR;
892     }
893 
894     int32_t err = SendRequest(
895         static_cast<int32_t>(StorageManagerInterfaceCode::GET_FREE_SIZE), data, reply, option);
896     if (err != E_OK) {
897         return err;
898     }
899     err = reply.ReadInt32();
900     if (err != E_OK) {
901         return err;
902     }
903     freeSize = reply.ReadInt64();
904     return E_OK;
905 }
906 
GetUserStorageStats(StorageStats & storageStats)907 int32_t StorageManagerProxy::GetUserStorageStats(StorageStats &storageStats)
908 {
909     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
910     StorageStats result;
911     MessageParcel data;
912     MessageParcel reply;
913     MessageOption option(MessageOption::TF_SYNC);
914     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
915         return E_WRITE_DESCRIPTOR_ERR;
916     }
917 
918     int32_t err = SendRequest(
919         static_cast<int32_t>(StorageManagerInterfaceCode::GET_CURR_USER_STATS), data, reply, option);
920     if (err != E_OK) {
921         return err;
922     }
923     err = reply.ReadInt32();
924     if (err != E_OK) {
925         return err;
926     }
927     storageStats = *StorageStats::Unmarshalling(reply);
928     return E_OK;
929 }
930 
GetUserStorageStats(int32_t userId,StorageStats & storageStats)931 int32_t StorageManagerProxy::GetUserStorageStats(int32_t userId, StorageStats &storageStats)
932 {
933     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
934     StorageStats result;
935     MessageParcel data;
936     MessageParcel reply;
937     MessageOption option(MessageOption::TF_SYNC);
938     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
939         return E_WRITE_DESCRIPTOR_ERR;
940     }
941 
942     if (!data.WriteInt32(userId)) {
943         return E_WRITE_PARCEL_ERR;
944     }
945     int32_t err = SendRequest(
946         static_cast<int32_t>(StorageManagerInterfaceCode::GET_USER_STATS), data, reply, option);
947     if (err != E_OK) {
948         return err;
949     }
950     err = reply.ReadInt32();
951     if (err != E_OK) {
952         return err;
953     }
954     storageStats = *StorageStats::Unmarshalling(reply);
955     return E_OK;
956 }
957 
GetCurrentBundleStats(BundleStats & bundleStats,uint32_t statFlag)958 int32_t StorageManagerProxy::GetCurrentBundleStats(BundleStats &bundleStats, uint32_t statFlag)
959 {
960     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
961     BundleStats result;
962     MessageParcel data;
963     MessageParcel reply;
964     MessageOption option(MessageOption::TF_SYNC);
965     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
966         return E_WRITE_DESCRIPTOR_ERR;
967     }
968 
969     if (!data.WriteUint32(statFlag)) {
970         return E_WRITE_PARCEL_ERR;
971     }
972 
973     int32_t err = SendRequest(static_cast<int32_t>(StorageManagerInterfaceCode::GET_CURR_BUNDLE_STATS), data, reply,
974         option);
975     if (err != E_OK) {
976         return err;
977     }
978     err = reply.ReadInt32();
979     if (err != E_OK) {
980         return err;
981     }
982     bundleStats = *BundleStats::Unmarshalling(reply);
983     return E_OK;
984 }
985 
GetVolumeByUuid(std::string fsUuid,VolumeExternal & vc)986 int32_t StorageManagerProxy::GetVolumeByUuid(std::string fsUuid, VolumeExternal &vc)
987 {
988     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
989     MessageParcel data;
990     MessageParcel reply;
991     MessageOption option(MessageOption::TF_SYNC);
992     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
993         return E_WRITE_DESCRIPTOR_ERR;
994     }
995 
996     if (!data.WriteString(fsUuid)) {
997         return E_WRITE_PARCEL_ERR;
998     }
999 
1000     int32_t err = SendRequest(
1001         static_cast<int32_t>(StorageManagerInterfaceCode::GET_VOL_BY_UUID), data, reply, option);
1002     if (err != E_OK) {
1003         return err;
1004     }
1005     vc = *VolumeExternal::Unmarshalling(reply);
1006     return reply.ReadInt32();
1007 }
1008 
GetVolumeById(std::string volumeId,VolumeExternal & vc)1009 int32_t StorageManagerProxy::GetVolumeById(std::string volumeId, VolumeExternal &vc)
1010 {
1011     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1012     MessageParcel data;
1013     MessageParcel reply;
1014     MessageOption option(MessageOption::TF_SYNC);
1015     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1016         return E_WRITE_DESCRIPTOR_ERR;
1017     }
1018 
1019     if (!data.WriteString(volumeId)) {
1020         return E_WRITE_PARCEL_ERR;
1021     }
1022 
1023     int32_t err = SendRequest(
1024         static_cast<int32_t>(StorageManagerInterfaceCode::GET_VOL_BY_ID), data, reply, option);
1025     if (err != E_OK) {
1026         return err;
1027     }
1028     vc = *VolumeExternal::Unmarshalling(reply);
1029     return reply.ReadInt32();
1030 }
1031 
SetVolumeDescription(std::string fsUuid,std::string description)1032 int32_t StorageManagerProxy::SetVolumeDescription(std::string fsUuid, std::string description)
1033 {
1034     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1035     MessageParcel data;
1036     MessageParcel reply;
1037     MessageOption option(MessageOption::TF_SYNC);
1038     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1039         return E_WRITE_DESCRIPTOR_ERR;
1040     }
1041 
1042     if (!data.WriteString(fsUuid)) {
1043         return E_WRITE_PARCEL_ERR;
1044     }
1045 
1046     if (!data.WriteString(description)) {
1047         return E_WRITE_PARCEL_ERR;
1048     }
1049 
1050     int32_t err = SendRequest(
1051         static_cast<int32_t>(StorageManagerInterfaceCode::SET_VOL_DESC), data, reply, option);
1052     if (err != E_OK) {
1053         return err;
1054     }
1055     return reply.ReadInt32();
1056 }
1057 
Format(std::string volumeId,std::string fsType)1058 int32_t StorageManagerProxy::Format(std::string volumeId, std::string fsType)
1059 {
1060     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1061     MessageParcel data;
1062     MessageParcel reply;
1063     MessageOption option(MessageOption::TF_SYNC);
1064     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1065         return E_WRITE_DESCRIPTOR_ERR;
1066     }
1067 
1068     if (!data.WriteString(volumeId)) {
1069         return E_WRITE_PARCEL_ERR;
1070     }
1071 
1072     if (!data.WriteString(fsType)) {
1073         return E_WRITE_PARCEL_ERR;
1074     }
1075 
1076     int32_t err = SendRequest(
1077         static_cast<int32_t>(StorageManagerInterfaceCode::FORMAT), data, reply, option);
1078     if (err != E_OK) {
1079         return err;
1080     }
1081     return reply.ReadInt32();
1082 }
1083 
GetDiskById(std::string diskId,Disk & disk)1084 int32_t StorageManagerProxy::GetDiskById(std::string diskId, Disk &disk)
1085 {
1086     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1087     MessageParcel data;
1088     MessageParcel reply;
1089     MessageOption option(MessageOption::TF_SYNC);
1090     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1091         return E_WRITE_DESCRIPTOR_ERR;
1092     }
1093 
1094     if (!data.WriteString(diskId)) {
1095         return E_WRITE_PARCEL_ERR;
1096     }
1097 
1098     int32_t err = SendRequest(
1099         static_cast<int32_t>(StorageManagerInterfaceCode::GET_DISK_BY_ID), data, reply, option);
1100     if (err != E_OK) {
1101         return err;
1102     }
1103     disk = *Disk::Unmarshalling(reply);
1104     return reply.ReadInt32();
1105 }
1106 
CreateShareFile(const std::vector<std::string> & uriList,uint32_t tokenId,uint32_t flag)1107 std::vector<int32_t> StorageManagerProxy::CreateShareFile(const std::vector<std::string> &uriList,
1108                                                           uint32_t tokenId, uint32_t flag)
1109 {
1110     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1111     MessageParcel data;
1112     MessageParcel reply;
1113     MessageOption option(MessageOption::TF_SYNC);
1114 
1115     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1116         return std::vector<int32_t>{E_WRITE_DESCRIPTOR_ERR};
1117     }
1118 
1119     if (!data.WriteStringVector(uriList)) {
1120         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
1121     }
1122 
1123     if (!data.WriteUint32(tokenId)) {
1124         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
1125     }
1126 
1127     if (!data.WriteUint32(flag)) {
1128         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
1129     }
1130 
1131     int32_t err = SendRequest(
1132         static_cast<int32_t>(StorageManagerInterfaceCode::CREATE_SHARE_FILE), data, reply, option);
1133     if (err != E_OK) {
1134         return std::vector<int32_t>{err};
1135     }
1136 
1137     std::vector<int32_t> retList;
1138     if (!reply.ReadInt32Vector(&retList)) {
1139         return std::vector<int32_t>{E_WRITE_PARCEL_ERR};
1140     };
1141     return retList;
1142 }
1143 
DeleteShareFile(uint32_t tokenId,const std::vector<std::string> & uriList)1144 int32_t StorageManagerProxy::DeleteShareFile(uint32_t tokenId, const std::vector<std::string> &uriList)
1145 {
1146     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1147     MessageParcel data;
1148     MessageParcel reply;
1149     MessageOption option(MessageOption::TF_ASYNC);
1150 
1151     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1152         return E_WRITE_DESCRIPTOR_ERR;
1153     }
1154 
1155     if (!data.WriteUint32(tokenId)) {
1156         return E_WRITE_PARCEL_ERR;
1157     }
1158 
1159     if (!data.WriteStringVector(uriList)) {
1160         return E_WRITE_PARCEL_ERR;
1161     }
1162 
1163     int32_t err = SendRequest(
1164         static_cast<int32_t>(StorageManagerInterfaceCode::DELETE_SHARE_FILE), data, reply, option);
1165     if (err != E_OK) {
1166         return err;
1167     }
1168 
1169     return reply.ReadInt32();
1170 }
SetBundleQuota(const std::string & bundleName,int32_t uid,const std::string & bundleDataDirPath,int32_t limitSizeMb)1171 int32_t StorageManagerProxy::SetBundleQuota(const std::string &bundleName, int32_t uid,
1172     const std::string &bundleDataDirPath, int32_t limitSizeMb)
1173 {
1174     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1175     MessageParcel data;
1176     MessageParcel reply;
1177     MessageOption option(MessageOption::TF_SYNC);
1178 
1179     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1180         return E_WRITE_DESCRIPTOR_ERR;
1181     }
1182 
1183     if (!data.WriteString(bundleName)) {
1184         return E_WRITE_PARCEL_ERR;
1185     }
1186 
1187     if (!data.WriteInt32(uid)) {
1188         return E_WRITE_PARCEL_ERR;
1189     }
1190 
1191     if (!data.WriteString(bundleDataDirPath)) {
1192         return E_WRITE_PARCEL_ERR;
1193     }
1194 
1195     if (!data.WriteInt32(limitSizeMb)) {
1196         return E_WRITE_PARCEL_ERR;
1197     }
1198 
1199     int32_t err = SendRequest(
1200         static_cast<int32_t>(StorageManagerInterfaceCode::SET_BUNDLE_QUOTA), data, reply, option);
1201     if (err != E_OK) {
1202         return err;
1203     }
1204 
1205     return reply.ReadInt32();
1206 }
1207 
1208 
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)1209 int32_t StorageManagerProxy::GetBundleStatsForIncrease(uint32_t userId, const std::vector<std::string> &bundleNames,
1210     const std::vector<int64_t> &incrementalBackTimes, std::vector<int64_t> &pkgFileSizes,
1211     std::vector<int64_t> &incPkgFileSizes)
1212 {
1213     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1214     MessageParcel data;
1215     MessageParcel reply;
1216     MessageOption option(MessageOption::TF_SYNC);
1217     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1218         return E_WRITE_DESCRIPTOR_ERR;
1219     }
1220 
1221     if (!data.WriteInt32(userId)) {
1222         return E_WRITE_PARCEL_ERR;
1223     }
1224 
1225     if (!data.WriteStringVector(bundleNames)) {
1226         return E_WRITE_PARCEL_ERR;
1227     }
1228 
1229     if (!data.WriteInt64Vector(incrementalBackTimes)) {
1230         return E_WRITE_PARCEL_ERR;
1231     }
1232 
1233     int32_t err = SendRequest(
1234         static_cast<int32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATS_INCREASE), data, reply,
1235         option);
1236     if (err != E_OK) {
1237         return err;
1238     }
1239     err = reply.ReadInt32();
1240     if (err != E_OK) {
1241         return err;
1242     }
1243     if (!reply.ReadInt64Vector(&pkgFileSizes)) {
1244         LOGE("StorageManagerProxy::SendRequest read pkgFileSizes");
1245         return E_WRITE_REPLY_ERR;
1246     }
1247     if (!reply.ReadInt64Vector(&incPkgFileSizes)) {
1248         LOGE("StorageManagerProxy::SendRequest read incPkgFileSizes");
1249         return E_WRITE_REPLY_ERR;
1250     }
1251 
1252     return E_OK;
1253 }
1254 
UpdateMemoryPara(int32_t size,int32_t & oldSize)1255 int32_t StorageManagerProxy::UpdateMemoryPara(int32_t size, int32_t &oldSize)
1256 {
1257     MessageParcel data;
1258     MessageParcel reply;
1259     MessageOption option(MessageOption::TF_SYNC);
1260     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1261         return E_WRITE_DESCRIPTOR_ERR;
1262     }
1263     if (!data.WriteInt32(size)) {
1264         return E_WRITE_PARCEL_ERR;
1265     }
1266 
1267     int32_t err = SendRequest(
1268         static_cast<int32_t>(StorageManagerInterfaceCode::UPDATE_MEM_PARA), data, reply, option);
1269     if (err != E_OK) {
1270         return err;
1271     }
1272     err = reply.ReadInt32();
1273     if (err != E_OK) {
1274         return err;
1275     }
1276     oldSize = reply.ReadInt32();
1277 
1278     return E_OK;
1279 }
1280 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1281 int32_t StorageManagerProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
1282     MessageOption &option)
1283 {
1284     sptr<IRemoteObject> remote = Remote();
1285     if (remote == nullptr) {
1286         LOGE("remote is nullptr, code = %{public}d", code);
1287         return E_REMOTE_IS_NULLPTR;
1288     }
1289 
1290     int32_t result = remote->SendRequest(code, data, reply, option);
1291     if (result != E_OK) {
1292         LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
1293         return result;
1294     }
1295 
1296     return E_OK;
1297 }
1298 
GetUserStorageStatsByType(int32_t userId,StorageStats & storageStats,std::string type)1299 int32_t StorageManagerProxy::GetUserStorageStatsByType(int32_t userId, StorageStats &storageStats, std::string type)
1300 {
1301     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1302     StorageStats result;
1303     MessageParcel data;
1304     MessageParcel reply;
1305     MessageOption option(MessageOption::TF_SYNC);
1306     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1307         return E_WRITE_DESCRIPTOR_ERR;
1308     }
1309 
1310     if (!data.WriteInt32(userId)) {
1311         return E_WRITE_PARCEL_ERR;
1312     }
1313 
1314     if (!data.WriteString(type)) {
1315         return E_WRITE_PARCEL_ERR;
1316     }
1317     int32_t err = SendRequest(
1318         static_cast<int32_t>(StorageManagerInterfaceCode::GET_USER_STATS_BY_TYPE), data, reply,
1319         option);
1320     if (err != E_OK) {
1321         return err;
1322     }
1323     err = reply.ReadInt32();
1324     if (err != E_OK) {
1325         return err;
1326     }
1327     storageStats = *StorageStats::Unmarshalling(reply);
1328     return E_OK;
1329 }
1330 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)1331 int32_t StorageManagerProxy::MountDfsDocs(int32_t userId, const std::string &relativePath,
1332     const std::string &networkId, const std::string &deviceId)
1333 {
1334     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1335     MessageParcel data;
1336     MessageParcel reply;
1337     MessageOption option(MessageOption::TF_SYNC);
1338 
1339     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1340         LOGE("StorageManagerProxy::MountDfsDocs, WriteInterfaceToken failed");
1341         return E_WRITE_DESCRIPTOR_ERR;
1342     }
1343     if (!data.WriteInt32(userId) || !data.WriteString(relativePath) ||
1344         !data.WriteString(networkId) || !data.WriteString(deviceId)) {
1345         LOGE("StorageManagerProxy::MountDfsDocs, Write failed");
1346         return E_WRITE_PARCEL_ERR;
1347     }
1348 
1349     int32_t err = SendRequest(
1350         static_cast<int32_t>(StorageManagerInterfaceCode::MOUNT_DFS_DOCS), data, reply, option);
1351     if (err != E_OK) {
1352         return err;
1353     }
1354 
1355     return reply.ReadInt32();
1356 }
1357 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)1358 int32_t StorageManagerProxy::UMountDfsDocs(int32_t userId, const std::string &relativePath,
1359     const std::string &networkId, const std::string &deviceId)
1360 {
1361     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1362     MessageParcel data;
1363     MessageParcel reply;
1364     MessageOption option(MessageOption::TF_SYNC);
1365 
1366     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1367         LOGE("StorageManagerProxy::UMountDfsDocs, WriteInterfaceToken failed");
1368         return E_WRITE_DESCRIPTOR_ERR;
1369     }
1370     if (!data.WriteInt32(userId) || !data.WriteString(relativePath) ||
1371         !data.WriteString(networkId) || !data.WriteString(deviceId)) {
1372         LOGE("StorageManagerProxy::UMountDfsDocs, Write failed");
1373         return E_WRITE_PARCEL_ERR;
1374     }
1375 
1376     int32_t err = SendRequest(
1377         static_cast<int32_t>(StorageManagerInterfaceCode::UMOUNT_DFS_DOCS), data, reply, option);
1378     if (err != E_OK) {
1379         return err;
1380     }
1381 
1382     return reply.ReadInt32();
1383 }
1384 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)1385 int32_t StorageManagerProxy::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
1386 {
1387     LOGI("user ID: %{public}u", userId);
1388     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
1389     MessageParcel data;
1390     MessageParcel reply;
1391     MessageOption option(MessageOption::TF_SYNC);
1392     if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) {
1393         LOGE("WriteInterfaceToken failed");
1394         return E_WRITE_DESCRIPTOR_ERR;
1395     }
1396     if (!data.WriteUint32(userId)) {
1397         LOGE("Write user ID failed");
1398         return E_WRITE_PARCEL_ERR;
1399     }
1400     if (!data.WriteBool(needCheckDirMount)) {
1401         LOGE("Write user ID failed");
1402         return E_WRITE_PARCEL_ERR;
1403     }
1404     int32_t err = SendRequest(
1405         static_cast<int32_t>(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS), data, reply, option);
1406     if (err != E_OK) {
1407         return err;
1408     }
1409     isEncrypted = reply.ReadBool();
1410     return reply.ReadInt32();
1411 }
1412 } // StorageManager
1413 } // OHOS
1414