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 16 #ifndef DISTRIBUTED_FILE_COPY_MANAGER_H 17 #define DISTRIBUTED_FILE_COPY_MANAGER_H 18 19 #include <chrono> 20 #include <condition_variable> 21 #include <memory> 22 23 #include "copy/distributed_file_fd_guard.h" 24 #include "copy/file_copy_listener.h" 25 #include "copy/trans_listener.h" 26 #include "iremote_broker.h" 27 #include "refbase.h" 28 #include "uv.h" 29 30 namespace OHOS { 31 namespace Storage { 32 namespace DistributedFile { 33 34 class FileIoToken : public IRemoteBroker { 35 public: 36 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileio.open"); 37 38 FileIoToken() = default; 39 virtual ~FileIoToken() noexcept = default; 40 }; 41 42 struct FileInfos { 43 std::string srcUri; 44 std::string destUri; 45 std::string srcPath; 46 std::string destPath; 47 bool srcUriIsFile = false; 48 std::shared_ptr<FileCopyLocalListener> localListener = nullptr; 49 sptr<TransListener> transListener = nullptr; 50 std::atomic_bool needCancel{ false }; 51 std::mutex subDirsMutex; 52 std::set<std::string> subDirs; 53 54 bool operator==(const FileInfos &infos) const 55 { 56 return (srcUri == infos.srcUri && destUri == infos.destUri); 57 } 58 bool operator<(const FileInfos &infos) const 59 { 60 if (srcUri == infos.srcUri) { 61 return destUri < infos.destUri; 62 } 63 return srcUri < infos.srcUri; 64 } 65 }; 66 67 class FileCopyManager final { 68 public: 69 using ProcessCallback = std::function<void (uint64_t processSize, uint64_t totalSize)>; 70 static std::shared_ptr<FileCopyManager> GetInstance(); 71 int32_t Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback &processCallback); 72 int32_t Cancel(const std::string &srcUri, const std::string &destUri); 73 int32_t Cancel(); 74 75 private: 76 static std::shared_ptr<FileCopyManager> instance_; 77 std::mutex FileInfosVecMutex_; 78 std::vector<std::shared_ptr<FileInfos>> FileInfosVec_; 79 80 private: 81 // operator of local copy 82 int32_t ExecLocal(std::shared_ptr<FileInfos> infos); 83 int32_t CopyFile(const std::string &src, const std::string &dest, std::shared_ptr<FileInfos> infos); 84 int32_t SendFileCore(std::shared_ptr<FDGuard> srcFdg, std::shared_ptr<FDGuard> destFdg, 85 std::shared_ptr<FileInfos> infos); 86 int32_t CopyDirFunc(const std::string &src, const std::string &dest, std::shared_ptr<FileInfos> infos); 87 int32_t CopySubDir(const std::string &srcPath, const std::string &destPath, std::shared_ptr<FileInfos> infos); 88 int32_t RecurCopyDir(const std::string &srcPath, const std::string &destPath, std::shared_ptr<FileInfos> infos); 89 int32_t OpenSrcFile(const std::string &srcPth, std::shared_ptr<FileInfos> infos, int32_t &srcFd); 90 91 // operator of remote copy 92 int32_t ExecRemote(std::shared_ptr<FileInfos> infos, ProcessCallback &processCallback); 93 94 // operator of file 95 int32_t CreateFileInfos(const std::string &srcUri, const std::string &destUri, std::shared_ptr<FileInfos> &infos); 96 int32_t CheckOrCreatePath(const std::string &destPath); 97 int MakeDir(const std::string &path); 98 bool IsRemoteUri(const std::string &uri); 99 bool IsMediaUri(const std::string &uriPath); 100 void AddFileInfos(std::shared_ptr<FileInfos> infos); 101 void RemoveFileInfos(std::shared_ptr<FileInfos> infos); 102 void DeleteResFile(std::shared_ptr<FileInfos> infos); 103 }; 104 } // namespace DistributedFile 105 } // namespace Storage 106 } // namespace OHOS 107 108 #endif // DISTRIBUTED_FILE_COPY_MANAGER_H 109