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 "external_storage_oper.h"
17
18 #include <vector>
19
20 #include "cmd_response.h"
21 #include "external_storage_utils.h"
22 #include "file_info.h"
23 #include "file_manager_service_def.h"
24 #include "file_manager_service_errno.h"
25 #include "log.h"
26
27 using namespace std;
28 namespace OHOS {
29 namespace FileManagerService {
OperProcess(uint32_t code,MessageParcel & data,MessageParcel & reply) const30 int ExternalStorageOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const
31 {
32 DEBUG_LOG("ExternalStorageOper::OperProcess");
33 int errCode = SUCCESS;
34 switch (code) {
35 case Operation::LIST_FILE: {
36 std::string devName = data.ReadString();
37 std::string devPath = data.ReadString();
38 std::string type = data.ReadString();
39 std::string path = data.ReadString();
40 int64_t offset = data.ReadInt64();
41 int64_t count = data.ReadInt64();
42
43 CmdOptions option(devName, devPath, offset, count, true);
44 errCode = this->ListFile(type, path, option, reply);
45 break;
46 }
47 case Operation::CREATE_FILE: {
48 std::string name = data.ReadString();
49 std::string uri = data.ReadString();
50 errCode = this->CreateFile(uri, name, reply);
51 break;
52 }
53 case Operation::GET_ROOT: {
54 string path = data.ReadString();
55 // name for extension
56 string name = "name";
57 errCode = GetRoot(name, path, reply);
58 break;
59 }
60 default: {
61 DEBUG_LOG("not valid code %{public}d.", code);
62 break;
63 }
64 }
65 return errCode;
66 }
67
GetRoot(const std::string & name,const std::string & path,MessageParcel & reply) const68 int ExternalStorageOper::GetRoot(const std::string &name, const std::string &path, MessageParcel &reply) const
69 {
70 std::vector<std::shared_ptr<FileInfo>> fileList;
71 int ret = ExternalStorageUtils::DoGetRoot(name, path, fileList);
72 CmdResponse cmdResponse;
73 cmdResponse.SetErr(ret);
74 cmdResponse.SetFileInfoList(fileList);
75 if (!reply.WriteParcelable(&cmdResponse)) {
76 ERR_LOG("reply write err parcel capacity%{public}d", reply.GetDataCapacity());
77 }
78 return ret;
79 }
80
CreateFile(const std::string & uri,const std::string & name,MessageParcel & reply) const81 int ExternalStorageOper::CreateFile(const std::string &uri, const std::string &name, MessageParcel &reply) const
82 {
83 std::string resultUir;
84 int ret = ExternalStorageUtils::DoCreateFile(uri, name, resultUir);
85 CmdResponse cmdResponse;
86 cmdResponse.SetErr(ret);
87 cmdResponse.SetUri(resultUir);
88 if (!reply.WriteParcelable(&cmdResponse)) {
89 ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity());
90 }
91 return ret;
92 }
93
ListFile(const std::string & type,const std::string & uri,const CmdOptions & option,MessageParcel & reply) const94 int ExternalStorageOper::ListFile(const std::string &type, const std::string &uri, const CmdOptions &option,
95 MessageParcel &reply) const
96 {
97 std::vector<std::shared_ptr<FileInfo>> fileList;
98 int ret = ExternalStorageUtils::DoListFile(type, uri, option, fileList);
99 CmdResponse cmdResponse;
100 cmdResponse.SetErr(ret);
101 cmdResponse.SetFileInfoList(fileList);
102 if (!reply.WriteParcelable(&cmdResponse)) {
103 ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity());
104 }
105 return ret;
106 }
107 } // namespace FileManagerService
108 } // namespace OHOS