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