• 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 #include "file_manager_proxy.h"
16 
17 #include "cmd_response.h"
18 #include "file_info.h"
19 #include "file_manager_service_def.h"
20 #include "file_manager_service_errno.h"
21 #include "file_manager_service_stub.h"
22 #include "log.h"
23 #include "media_file_utils.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace FileManagerService {
GetCmdResponse(MessageParcel & reply,sptr<CmdResponse> & cmdResponse)29 static int GetCmdResponse(MessageParcel &reply, sptr<CmdResponse> &cmdResponse)
30 {
31     cmdResponse = reply.ReadParcelable<CmdResponse>();
32     if (cmdResponse == nullptr) {
33         ERR_LOG("Unmarshalling cmdResponse fail");
34         return FAIL;
35     }
36     return cmdResponse->GetErr();
37 }
38 
FileManagerProxy(const sptr<IRemoteObject> & impl)39 FileManagerProxy::FileManagerProxy(const sptr<IRemoteObject> &impl)
40     : IRemoteProxy<IFileManagerService>(impl) {}
GetRoot(const CmdOptions & option,vector<shared_ptr<FileInfo>> & fileRes)41 int FileManagerProxy::GetRoot(const CmdOptions &option, vector<shared_ptr<FileInfo>> &fileRes)
42 {
43     CmdOptions op(option);
44     int code = Operation::GET_ROOT;
45     if (op.GetDevInfo().GetName() == "external_storage") {
46         code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::GET_ROOT;
47     }
48     MessageParcel data;
49     data.WriteInterfaceToken(GetDescriptor());
50     data.WriteString(op.GetDevInfo().GetName());
51     MessageParcel reply;
52     MessageOption messageOption;
53     int err = Remote()->SendRequest(code, data, reply, messageOption);
54     if (err != ERR_NONE) {
55         ERR_LOG("GetRoot inner error send request fail %{public}d", err);
56         return FAIL;
57     }
58     sptr<CmdResponse> cmdResponse;
59     err = GetCmdResponse(reply, cmdResponse);
60     if (err != ERR_NONE) {
61         return err;
62     }
63     fileRes = cmdResponse->GetFileInfoList();
64     return err;
65 }
66 
CreateFile(const std::string & path,const std::string & fileName,const CmdOptions & option,std::string & uri)67 int FileManagerProxy::CreateFile(const std::string &path, const std::string &fileName,
68     const CmdOptions &option, std::string &uri)
69 {
70     MessageParcel data;
71     data.WriteInterfaceToken(GetDescriptor());
72     data.WriteString(fileName);
73     data.WriteString(path);
74     MessageParcel reply;
75     MessageOption messageOption;
76     int code = Operation::CREATE_FILE;
77     CmdOptions op(option);
78     if (op.GetDevInfo().GetName() == "external_storage") {
79         code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::CREATE_FILE;
80     }
81     int err = Remote()->SendRequest(code, data, reply, messageOption);
82     if (err != ERR_NONE) {
83         ERR_LOG("inner error send request fail %{public}d", err);
84         return FAIL;
85     }
86     sptr<CmdResponse> cmdResponse;
87     err = GetCmdResponse(reply, cmdResponse);
88     if (err != ERR_NONE) {
89         return err;
90     }
91     uri = cmdResponse->GetUri();
92     return err;
93 }
94 
GetFmsInstance()95 IFmsClient *IFmsClient::GetFmsInstance()
96 {
97     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
98     if (samgr == nullptr) {
99         ERR_LOG("samgr object is NULL.");
100         return nullptr;
101     }
102     sptr<IRemoteObject> object = samgr->GetSystemAbility(FILE_MANAGER_SERVICE_ID);
103     if (object == nullptr) {
104         ERR_LOG("FileManager Service object is NULL.");
105         return nullptr;
106     }
107     static FileManagerProxy proxy = FileManagerProxy(object);
108     return &proxy;
109 }
110 
ListFile(const std::string & type,const std::string & path,const CmdOptions & option,std::vector<std::shared_ptr<FileInfo>> & fileRes)111 int FileManagerProxy::ListFile(const std::string &type, const std::string &path, const CmdOptions &option,
112     std::vector<std::shared_ptr<FileInfo>> &fileRes)
113 {
114     CmdOptions op(option);
115     std::string devName(op.GetDevInfo().GetName());
116     std::string devPath(op.GetDevInfo().GetPath());
117     int64_t offset = op.GetOffset();
118     int64_t count = op.GetCount();
119     MessageParcel data;
120     data.WriteInterfaceToken(GetDescriptor());
121     data.WriteString(devName);
122     data.WriteString(devPath);
123     data.WriteString(type);
124     data.WriteString(path);
125     data.WriteInt64(offset);
126     data.WriteInt64(count);
127     MessageParcel reply;
128     MessageOption messageOption;
129     uint32_t code = Operation::LIST_FILE;
130     if (op.GetDevInfo().GetName() == "external_storage") {
131         code = (Equipment::EXTERNAL_STORAGE << EQUIPMENT_SHIFT) | Operation::LIST_FILE;
132     }
133     int err = Remote()->SendRequest(code, data, reply, messageOption);
134     if (err != ERR_NONE) {
135         ERR_LOG("inner error send request fail %{public}d", err);
136         return FAIL;
137     }
138     sptr<CmdResponse> cmdResponse;
139     err = GetCmdResponse(reply, cmdResponse);
140     if (err != ERR_NONE) {
141         return err;
142     }
143     fileRes = cmdResponse->GetFileInfoList();
144     return err;
145 }
146 
Mkdir(const string & name,const string & path)147 int FileManagerProxy::Mkdir(const string &name, const string &path)
148 {
149     MessageParcel data;
150     data.WriteInterfaceToken(GetDescriptor());
151     data.WriteString(name);
152     data.WriteString(path);
153     MessageParcel reply;
154     MessageOption option;
155     int err = Remote()->SendRequest(Operation::MAKE_DIR, data, reply, option);
156     if (err != ERR_NONE) {
157         ERR_LOG("inner error send request fail %{public}d", err);
158         return FAIL;
159     }
160     reply.ReadInt32(err);
161     return err;
162 }
163 } // FileManagerService
164 } // namespace OHOS