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