1 /*
2 * Copyright (C) 2025 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 #define MLOG_TAG "MtpPtpProxy"
16
17 #include "mtp_ptp_proxy.h"
18 #include "medialibrary_errno.h"
19 #include "media_log.h"
20 #include "media_mtp_utils.h"
21 #include "mtp_ptp_const.h"
22 #include "mtp_file_observer.h"
23 #include "mtp_manager.h"
24 #include "mtp_media_library.h"
25 #include "mtp_medialibrary_manager.h"
26 #include "mtp_storage_manager.h"
27
28 namespace OHOS {
29 namespace Media {
30 namespace {
31 std::shared_ptr<MtpMedialibraryManager> g_mtpMedialibraryManager = nullptr;
32 std::shared_ptr<MtpMediaLibrary> g_mtpMediaLibrary = nullptr;
33 }
34
GetInstance()35 MtpPtpProxy &MtpPtpProxy::GetInstance()
36 {
37 static MtpPtpProxy instance;
38 return instance;
39 }
40
Init(const sptr<OHOS::IRemoteObject> & token,Context & context)41 void MtpPtpProxy::Init(const sptr<OHOS::IRemoteObject> &token, Context &context)
42 {
43 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
44 g_mtpMediaLibrary = MtpMediaLibrary::GetInstance();
45 g_mtpMedialibraryManager = MtpMedialibraryManager::GetInstance();
46 CHECK_AND_RETURN_LOG(g_mtpMedialibraryManager != nullptr, "g_mtpMedialibraryManager is null");
47 g_mtpMedialibraryManager->Init(token, context);
48 }
49
GetGalleryName()50 static inline std::string GetGalleryName()
51 {
52 auto manager = MtpStorageManager::GetInstance();
53 CHECK_AND_RETURN_RET_LOG(manager != nullptr, "", "manager is null");
54 std::string language = manager->GetSystemLanguage();
55 if (language.empty()) {
56 language = CHINESE_ABBREVIATION;
57 }
58 return language == CHINESE_ABBREVIATION ? PTP_DOC_NAME_CN : PTP_DOC_NAME_EN;
59 }
60
IsMtpMode()61 static inline bool IsMtpMode()
62 {
63 return MtpManager::GetInstance().IsMtpMode();
64 }
65
GetHandles(Context & context,std::shared_ptr<UInt32List> & outHandles,bool isMac)66 int32_t MtpPtpProxy::GetHandles(Context &context, std::shared_ptr<UInt32List> &outHandles, bool isMac)
67 {
68 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
69 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
70 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
71 "g_mtpMediaLibrary is null");
72 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
73 "g_mtpMedialibraryManager is null");
74
75 CHECK_AND_RETURN_RET(IsMtpMode(), isMac ? g_mtpMedialibraryManager->GetAllHandles(context, outHandles) :
76 g_mtpMedialibraryManager->GetHandles(context, outHandles));
77
78 bool isRoot = context->parent == DEFAULT_PARENT_ROOT || context->parent == ALL_HANDLE_ID;
79 bool isMtp = context->parent > PTP_IN_MTP_ID;
80 if (isRoot || isMtp) {
81 int32_t errorCode = g_mtpMediaLibrary->GetHandles(context, outHandles);
82 CHECK_AND_EXECUTE(!isRoot, outHandles->push_back(PTP_IN_MTP_ID));
83
84 std::string path("");
85 std::string realPath("");
86 g_mtpMediaLibrary->GetPathByContextParent(context, path);
87 g_mtpMediaLibrary->GetRealPath(path, realPath);
88 MtpFileObserver::GetInstance().AddFileInotify(path, realPath, context);
89 return errorCode;
90 }
91 return g_mtpMedialibraryManager->GetHandles(context, outHandles);
92 }
93
GetObjectInfo(Context & context,std::shared_ptr<ObjectInfo> & objectInfo)94 int32_t MtpPtpProxy::GetObjectInfo(Context &context, std::shared_ptr<ObjectInfo> &objectInfo)
95 {
96 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
97 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
98 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
99 "g_mtpMediaLibrary is null");
100 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
101 "g_mtpMedialibraryManager is null");
102
103 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->GetObjectInfo(context, objectInfo));
104
105 if (context->handle < PTP_IN_MTP_ID) {
106 return g_mtpMedialibraryManager->GetObjectInfo(context, objectInfo);
107 } else if (context->handle == PTP_IN_MTP_ID) {
108 std::string name = GetGalleryName();
109 return g_mtpMediaLibrary->GetGalleryObjectInfo(context, objectInfo, std::move(name));
110 } else {
111 return g_mtpMediaLibrary->GetObjectInfo(context, objectInfo);
112 }
113 }
114
GetObjectPropValue(Context & context,uint64_t & intVal,uint128_t & longVal,std::string & strVal)115 int32_t MtpPtpProxy::GetObjectPropValue(Context &context, uint64_t &intVal, uint128_t &longVal, std::string &strVal)
116 {
117 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
118 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
119 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
120 "g_mtpMediaLibrary is null");
121 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
122 "g_mtpMedialibraryManager is null");
123
124 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->GetObjectPropValue(context, intVal, longVal, strVal));
125
126 if (context->handle < PTP_IN_MTP_ID) {
127 return g_mtpMedialibraryManager->GetObjectPropValue(context, intVal, longVal, strVal);
128 } else if (context->handle == PTP_IN_MTP_ID) {
129 std::string name = GetGalleryName();
130 return g_mtpMediaLibrary->GetGalleryPropValue(context, intVal, longVal, strVal, std::move(name));
131 } else {
132 return g_mtpMediaLibrary->GetObjectPropValue(context, intVal, longVal, strVal);
133 }
134 }
135
SetObjectPropValue(Context & context)136 int32_t MtpPtpProxy::SetObjectPropValue(Context &context)
137 {
138 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
139 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
140 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
141 "g_mtpMediaLibrary is null");
142 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
143 "g_mtpMedialibraryManager is null");
144
145 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->SetObjectPropValue(context));
146
147 if (context->handle <= PTP_IN_MTP_ID) {
148 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
149 return MTP_ERROR_ACCESS_DENIED;
150 } else {
151 return g_mtpMediaLibrary->SetObjectPropValue(context);
152 }
153 }
154
GetObjectPropList(Context & context,std::shared_ptr<std::vector<Property>> & outProps)155 int32_t MtpPtpProxy::GetObjectPropList(Context &context, std::shared_ptr<std::vector<Property>> &outProps)
156 {
157 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
158 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
159 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
160 "g_mtpMediaLibrary is null");
161 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
162 "g_mtpMedialibraryManager is null");
163
164 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->GetObjectPropList(context, outProps));
165
166 if (context->handle < PTP_IN_MTP_ID) {
167 return g_mtpMedialibraryManager->GetObjectPropList(context, outProps);
168 } else if (context->handle == PTP_IN_MTP_ID) {
169 std::string name = GetGalleryName();
170 return g_mtpMediaLibrary->GetGalleryObjectPropList(context, outProps, std::move(name));
171 } else {
172 return g_mtpMediaLibrary->GetObjectPropList(context, outProps);
173 }
174 }
175
IsMtpExistObject(Context & context)176 bool MtpPtpProxy::IsMtpExistObject(Context &context)
177 {
178 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
179 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
180 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
181 "g_mtpMediaLibrary is null");
182 if (context->handle <= PTP_IN_MTP_ID) {
183 return true;
184 }
185 return g_mtpMediaLibrary->IsExistObject(context);
186 }
187
GetReadFd(Context & context,int32_t & fd)188 int32_t MtpPtpProxy::GetReadFd(Context &context, int32_t &fd)
189 {
190 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
191 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
192 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
193 "g_mtpMediaLibrary is null");
194 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
195 "g_mtpMedialibraryManager is null");
196
197 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->GetFd(context, fd, FILEMODE_READONLY));
198
199 if (context->handle < PTP_IN_MTP_ID) {
200 return g_mtpMedialibraryManager->GetFd(context, fd, FILEMODE_READONLY);
201 } else if (context->handle == PTP_IN_MTP_ID) {
202 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
203 return MTP_ERROR_ACCESS_DENIED;
204 } else {
205 return g_mtpMediaLibrary->GetFd(context, fd);
206 }
207 }
208
CloseReadFd(Context & context,int32_t fd)209 int32_t MtpPtpProxy::CloseReadFd(Context &context, int32_t fd)
210 {
211 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
212 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
213 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
214 "g_mtpMediaLibrary is null");
215 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
216 "g_mtpMedialibraryManager is null");
217
218 return IsMtpMode() ? g_mtpMediaLibrary->CloseFd(context, fd) : g_mtpMedialibraryManager->CloseFdForGet(context, fd);
219 }
220
CloseWriteFd(Context & context,int32_t fd)221 int32_t MtpPtpProxy::CloseWriteFd(Context &context, int32_t fd)
222 {
223 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
224 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
225 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
226 "g_mtpMediaLibrary is null");
227 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
228 "g_mtpMedialibraryManager is null");
229
230 return IsMtpMode() ? g_mtpMediaLibrary->CloseFd(context, fd) : g_mtpMedialibraryManager->CloseFd(context, fd);
231 }
232
GetModifyObjectInfoPathById(const int32_t handle,std::string & path)233 int32_t MtpPtpProxy::GetModifyObjectInfoPathById(const int32_t handle, std::string &path)
234 {
235 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
236 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
237 "g_mtpMediaLibrary is null");
238 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
239 "g_mtpMedialibraryManager is null");
240
241 if (IsMtpMode()) {
242 g_mtpMediaLibrary->GetPathById(handle, path);
243 } else {
244 g_mtpMedialibraryManager->GetPathById(handle, path);
245 path = g_mtpMedialibraryManager->GetHmdfsPath(path);
246 }
247 return MTP_SUCCESS;
248 }
249
GetWriteFd(Context & context,int32_t & fd)250 int32_t MtpPtpProxy::GetWriteFd(Context &context, int32_t &fd)
251 {
252 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
253 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
254 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
255 "g_mtpMediaLibrary is null");
256 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
257 "g_mtpMedialibraryManager is null");
258
259 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->GetFdByOpenFile(context, fd));
260
261 if (context->handle <= PTP_IN_MTP_ID) {
262 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
263 return MTP_ERROR_ACCESS_DENIED;
264 }
265 return g_mtpMediaLibrary->GetFd(context, fd, true);
266 }
267
GetMtpPathById(const int32_t handle,std::string & outPath)268 int32_t MtpPtpProxy::GetMtpPathById(const int32_t handle, std::string &outPath)
269 {
270 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
271 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
272 "g_mtpMediaLibrary is null");
273 return g_mtpMediaLibrary->GetPathById(handle, outPath);
274 }
275
DeleteCanceledObject(const std::string & path,const uint32_t handle)276 void MtpPtpProxy::DeleteCanceledObject(const std::string &path, const uint32_t handle)
277 {
278 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
279 CHECK_AND_RETURN_LOG(g_mtpMediaLibrary != nullptr, "g_mtpMediaLibrary is null");
280 CHECK_AND_RETURN_LOG(g_mtpMedialibraryManager != nullptr, "g_mtpMedialibraryManager is null");
281
282 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->DeleteCanceledObject(handle));
283
284 CHECK_AND_RETURN_LOG(handle > PTP_IN_MTP_ID, "MTP_ERROR_ACCESS_DENIED");
285 return g_mtpMediaLibrary->DeleteHandlePathMap(path, handle);
286 }
287
GetThumb(Context & context,std::shared_ptr<UInt8List> & outThumb)288 int32_t MtpPtpProxy::GetThumb(Context &context, std::shared_ptr<UInt8List> &outThumb)
289 {
290 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
291 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
292 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
293 "g_mtpMediaLibrary is null");
294 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
295 "g_mtpMedialibraryManager is null");
296
297 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->GetThumb(context, outThumb));
298
299 if (context->handle < PTP_IN_MTP_ID) {
300 return g_mtpMedialibraryManager->GetThumb(context, outThumb);
301 } else if (context->handle == PTP_IN_MTP_ID) {
302 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
303 return MTP_ERROR_ACCESS_DENIED;
304 } else {
305 return g_mtpMediaLibrary->GetThumb(context, outThumb);
306 }
307 }
308
SendObjectInfo(Context & context,uint32_t & storageID,uint32_t & parent,uint32_t & handle)309 int32_t MtpPtpProxy::SendObjectInfo(Context &context, uint32_t &storageID, uint32_t &parent, uint32_t &handle)
310 {
311 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
312 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
313 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
314 "g_mtpMediaLibrary is null");
315 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
316 "g_mtpMedialibraryManager is null");
317
318 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->SendObjectInfo(context, storageID, parent, handle));
319
320 bool isToPtp = context->parent <= PTP_IN_MTP_ID && context->parent != DEFAULT_PARENT_ROOT;
321 if (isToPtp) {
322 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
323 return MTP_ERROR_ACCESS_DENIED;
324 }
325 return g_mtpMediaLibrary->SendObjectInfo(context, storageID, parent, handle);
326 }
327
DeleteObject(Context & context)328 int32_t MtpPtpProxy::DeleteObject(Context &context)
329 {
330 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
331 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
332 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
333 "g_mtpMediaLibrary is null");
334 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
335 "g_mtpMedialibraryManager is null");
336
337 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->DeleteObject(context));
338
339 bool isPtp = context->handle <= PTP_IN_MTP_ID && context->handle != DEFAULT_PARENT_ROOT;
340 if (isPtp) {
341 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
342 return MTP_ERROR_ACCESS_DENIED;
343 }
344 return g_mtpMediaLibrary->DeleteObject(context);
345 }
346
MoveObject(Context & context,uint32_t & repeatHandle)347 int32_t MtpPtpProxy::MoveObject(Context &context, uint32_t &repeatHandle)
348 {
349 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
350 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
351 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
352 "g_mtpMediaLibrary is null");
353 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
354 "g_mtpMedialibraryManager is null");
355
356 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->MoveObject(context));
357 // context->handle: from, context->parent: to
358 // disable move to ptp and disable move from ptp
359 bool isFromPtp = context->handle <= PTP_IN_MTP_ID && context->handle != DEFAULT_PARENT_ROOT;
360 bool isToPtp = context->parent <= PTP_IN_MTP_ID && context->parent != DEFAULT_PARENT_ROOT;
361 if (isFromPtp || isToPtp) {
362 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
363 return MTP_ERROR_ACCESS_DENIED;
364 }
365 return g_mtpMediaLibrary->MoveObject(context, repeatHandle);
366 }
367
CopyObject(Context & context,uint32_t & outObjectHandle,uint32_t & oldHandle)368 int32_t MtpPtpProxy::CopyObject(Context &context, uint32_t &outObjectHandle, uint32_t &oldHandle)
369 {
370 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
371 CHECK_AND_RETURN_RET_LOG(context != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE, "context is null");
372 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
373 "g_mtpMediaLibrary is null");
374 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
375 "g_mtpMedialibraryManager is null");
376
377 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->CopyObject(context, outObjectHandle, false));
378
379 // context->handle: from, context->parent: to
380 // disable copy to mtp and disable copy from mtp root (gallery)
381 bool isFromPtpRoot = context->handle == PTP_IN_MTP_ID;
382 bool isToPtp = context->parent <= PTP_IN_MTP_ID && context->parent != DEFAULT_PARENT_ROOT;
383 if (isFromPtpRoot || isToPtp) {
384 MEDIA_WARN_LOG("MtpPtpProxy::%{public}s *MTP_ERROR_ACCESS_DENIED*", __func__);
385 return MTP_ERROR_ACCESS_DENIED;
386 }
387 // copy from ptp to mtp
388 bool isFromPtp = context->handle < PTP_IN_MTP_ID && context->handle != DEFAULT_PARENT_ROOT;
389 bool isToMtp = context->parent > PTP_IN_MTP_ID || context->parent == DEFAULT_PARENT_ROOT;
390 if (isFromPtp && isToMtp) {
391 PathMap paths;
392 auto errCode = g_mtpMedialibraryManager->GetCopyObjectPath(context->handle, paths);
393 CHECK_AND_RETURN_RET_LOG(errCode == MTP_SUCCESS, MTP_ERROR_INVALID_OBJECTHANDLE, "GetCopyObjectPath failed");
394 // copy album
395 if (context->handle < COMMON_PHOTOS_OFFSET) {
396 std::string albumName("");
397 errCode = g_mtpMedialibraryManager->GetAlbumName(context->handle, albumName);
398 CHECK_AND_RETURN_RET_LOG(errCode == MTP_SUCCESS, MTP_ERROR_INVALID_OBJECTHANDLE, "GetAlbumName failed");
399 return g_mtpMediaLibrary->CopyGalleryAlbum(context, albumName, paths, outObjectHandle);
400 }
401 // copy photo
402 return g_mtpMediaLibrary->CopyGalleryPhoto(context, paths, outObjectHandle);
403 }
404 return g_mtpMediaLibrary->CopyObject(context, outObjectHandle, oldHandle);
405 }
406
GetMtpStorageIds()407 int32_t MtpPtpProxy::GetMtpStorageIds()
408 {
409 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
410 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
411 "g_mtpMediaLibrary is null");
412 return g_mtpMediaLibrary->GetStorageIds();
413 }
414
GetIdByPath(const std::string & path,uint32_t & outId)415 int32_t MtpPtpProxy::GetIdByPath(const std::string &path, uint32_t &outId)
416 {
417 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
418 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
419 "g_mtpMediaLibrary is null");
420 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
421 "g_mtpMedialibraryManager is null");
422
423 CHECK_AND_RETURN_RET(IsMtpMode(), g_mtpMedialibraryManager->GetIdByPath(path, outId));
424
425 return g_mtpMediaLibrary->GetIdByPath(path, outId);
426 }
427
GetPathByHandle(uint32_t handle,std::string & path,std::string & realPath)428 int32_t MtpPtpProxy::GetPathByHandle(uint32_t handle, std::string &path, std::string &realPath)
429 {
430 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
431 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
432 "g_mtpMediaLibrary is null");
433 CHECK_AND_RETURN_RET_LOG(g_mtpMedialibraryManager != nullptr, MTP_ERROR_INVALID_OBJECTHANDLE,
434 "g_mtpMedialibraryManager is null");
435
436 if (IsMtpMode()) {
437 g_mtpMediaLibrary->GetPathById(handle, path);
438 g_mtpMediaLibrary->GetRealPath(path, realPath);
439 return MTP_SUCCESS;
440 }
441
442 g_mtpMedialibraryManager->GetPathById(handle, path);
443
444 size_t position = path.find(PATH_LOCAL_STR);
445 std::string real = std::to_string(getuid() / BASE_USER_RANGE);
446 if (position != std::string::npos) {
447 realPath = path.substr(0, position + 1) + real + path.substr(position);
448 }
449 MEDIA_DEBUG_LOG("GetPathByHandle new %{private}s", realPath.c_str());
450 return MTP_SUCCESS;
451 }
452
MtpTryAddExternalStorage(const std::string & fsUuid,uint32_t & storageId)453 bool MtpPtpProxy::MtpTryAddExternalStorage(const std::string &fsUuid, uint32_t &storageId)
454 {
455 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
456 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, false, "g_mtpMediaLibrary is null");
457 CHECK_AND_RETURN_RET_LOG(IsMtpMode(), false, "not in mtp mode");
458 return g_mtpMediaLibrary->TryAddExternalStorage(fsUuid, storageId);
459 }
460
MtpTryRemoveExternalStorage(const std::string & fsUuid,uint32_t & storageId)461 bool MtpPtpProxy::MtpTryRemoveExternalStorage(const std::string &fsUuid, uint32_t &storageId)
462 {
463 MEDIA_DEBUG_LOG("%{public}s is called", __func__);
464 CHECK_AND_RETURN_RET_LOG(g_mtpMediaLibrary != nullptr, false, "g_mtpMediaLibrary is null");
465 CHECK_AND_RETURN_RET_LOG(IsMtpMode(), false, "not in mtp mode");
466 return g_mtpMediaLibrary->TryRemoveExternalStorage(fsUuid, storageId);
467 }
468 } // namespace Media
469 } // namespace OHOS
470