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 "media_file_oper.h"
17
18 #include <vector>
19
20 #include "cmd_options.h"
21 #include "cmd_response.h"
22 #include "file_info.h"
23 #include "file_manager_service_def.h"
24 #include "file_manager_service_errno.h"
25 #include "ipc_types.h"
26 #include "iremote_broker.h"
27 #include "iremote_proxy.h"
28 #include "iremote_stub.h"
29 #include "log.h"
30 #include "media_data_ability_const.h"
31 #include "media_file_utils.h"
32
33 using namespace std;
34
35 namespace OHOS {
36 namespace FileManagerService {
OperProcess(uint32_t code,MessageParcel & data,MessageParcel & reply) const37 int MediaFileOper::OperProcess(uint32_t code, MessageParcel &data, MessageParcel &reply) const
38 {
39 int errCode = SUCCESS;
40 // media process
41 switch (code) {
42 case Operation::MAKE_DIR: {
43 string name = data.ReadString();
44 string path = data.ReadString();
45 errCode = Mkdir(name, path);
46 break;
47 }
48 case Operation::GET_ROOT: {
49 string path = data.ReadString();
50 // name for extension
51 string name = "name";
52 errCode = GetRoot(name, path, reply);
53 break;
54 }
55 case Operation::LIST_FILE: {
56 string devName = data.ReadString();
57 string devPath = data.ReadString();
58 string type = data.ReadString();
59 string path = data.ReadString();
60 int off = data.ReadInt64();
61 int count = data.ReadInt64();
62 // put fileInfo into reply
63 errCode = ListFile(type, path, off, count, reply);
64 break;
65 }
66 case Operation::CREATE_FILE: {
67 string name = data.ReadString();
68 string path = data.ReadString();
69 errCode = CreateFile(name, path, reply);
70 break;
71 }
72 default: {
73 DEBUG_LOG("not valid code %{public}d.", code);
74 break;
75 }
76 }
77 return errCode;
78 }
79
CreateFile(const std::string & name,const std::string & path,MessageParcel & reply) const80 int MediaFileOper::CreateFile(const std::string &name, const std::string &path, MessageParcel &reply) const
81 {
82 string type = "file";
83 std::string uri;
84 int ret = MediaFileUtils::DoInsert(name, path, type, uri);
85 CmdResponse cmdResponse;
86 cmdResponse.SetErr(ret);
87 cmdResponse.SetUri(uri);
88 if (!reply.WriteParcelable(&cmdResponse)) {
89 ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity());
90 }
91 return ret;
92 }
93
GetRoot(const std::string & name,const std::string & path,MessageParcel & reply) const94 int MediaFileOper::GetRoot(const std::string &name, const std::string &path, MessageParcel &reply) const
95 {
96 std::vector<std::shared_ptr<FileInfo>> fileList;
97 int ret = MediaFileUtils::DoGetRoot(name, path, fileList);
98 CmdResponse cmdResponse;
99 cmdResponse.SetErr(ret);
100 cmdResponse.SetFileInfoList(fileList);
101 if (!reply.WriteParcelable(&cmdResponse)) {
102 ERR_LOG("reply write err parcel capacity%{public}d", reply.GetDataCapacity());
103 }
104 return ret;
105 }
106
ListFile(const string & type,const string & path,int offset,int count,MessageParcel & reply) const107 int MediaFileOper::ListFile(const string &type, const string &path, int offset, int count, MessageParcel &reply) const
108 {
109 shared_ptr<NativeRdb::AbsSharedResultSet> result;
110 int res = MediaFileUtils::DoListFile(type, path, offset, count, result);
111 if (res != SUCCESS) {
112 return res;
113 }
114
115 std::vector<std::shared_ptr<FileInfo>> fileList;
116 res = MediaFileUtils::GetFileInfoFromResult(result, fileList);
117 CmdResponse cmdResponse;
118 cmdResponse.SetErr(res);
119 cmdResponse.SetFileInfoList(fileList);
120 if (!reply.WriteParcelable(&cmdResponse)) {
121 ERR_LOG("reply write err parcel capacity:%{public}d", reply.GetDataCapacity());
122 }
123 return res;
124 }
125
Mkdir(const string & name,const string & path) const126 int MediaFileOper::Mkdir(const string &name, const string &path) const
127 {
128 DEBUG_LOG("MediaFileOper::mkdir path %{public}s.", path.c_str());
129 return SUCCESS;
130 }
131 } // namespace FileManagerService
132 } // namespace OHOS