• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int callingUid {};
49     std::shared_ptr<FileCopyLocalListener> localListener = nullptr;
50     sptr<TransListener> transListener = nullptr;
51     std::atomic_bool needCancel{ false };
52     std::mutex subDirsMutex;
53     std::set<std::string> subDirs;
54 
55     bool operator==(const FileInfos &infos) const
56     {
57         return (srcUri == infos.srcUri && destUri == infos.destUri);
58     }
59     bool operator<(const FileInfos &infos) const
60     {
61         if (srcUri == infos.srcUri) {
62             return destUri < infos.destUri;
63         }
64         return srcUri < infos.srcUri;
65     }
66 };
67 
68 class FileCopyManager final {
69 public:
70     using ProcessCallback = std::function<void (uint64_t processSize, uint64_t totalSize)>;
71     static std::shared_ptr<FileCopyManager> GetInstance();
72     int32_t Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback &processCallback);
73     int32_t Cancel(const std::string &srcUri, const std::string &destUri, const bool isKeepFiles = false);
74     int32_t Cancel(const bool isKeepFiles = false);
75     // operator of local copy
76     int32_t ExecLocal(std::shared_ptr<FileInfos> infos);
77 
78 private:
79     static std::shared_ptr<FileCopyManager> instance_;
80     std::mutex FileInfosVecMutex_;
81     std::vector<std::shared_ptr<FileInfos>> FileInfosVec_;
82 
83 private:
84     int32_t CopyFile(const std::string &src, const std::string &dest, std::shared_ptr<FileInfos> infos);
85     int32_t SendFileCore(std::shared_ptr<FDGuard> srcFdg, std::shared_ptr<FDGuard> destFdg,
86         std::shared_ptr<FileInfos> infos);
87     int32_t CopyDirFunc(const std::string &src, const std::string &dest, std::shared_ptr<FileInfos> infos);
88     int32_t CopySubDir(const std::string &srcPath, const std::string &destPath, std::shared_ptr<FileInfos> infos);
89     int32_t RecurCopyDir(const std::string &srcPath, const std::string &destPath, std::shared_ptr<FileInfos> infos);
90     int32_t OpenSrcFile(const std::string &srcPth, std::shared_ptr<FileInfos> infos, int32_t &srcFd);
91 
92     // operator of remote copy
93     int32_t ExecRemote(std::shared_ptr<FileInfos> infos, ProcessCallback &processCallback);
94 
95     // operator of file
96     int32_t CreateFileInfos(const std::string &srcUri, const std::string &destUri, std::shared_ptr<FileInfos> &infos);
97     int32_t CheckOrCreatePath(const std::string &destPath);
98     int32_t ExecCopy(std::shared_ptr<FileInfos> infos);
99     int MakeDir(const std::string &path);
100     bool IsRemoteUri(const std::string &uri);
101     bool IsMediaUri(const std::string &uriPath);
102     void AddFileInfos(std::shared_ptr<FileInfos> infos);
103     void RemoveFileInfos(std::shared_ptr<FileInfos> infos);
104     void DeleteResFile(std::shared_ptr<FileInfos> infos);
105     bool IsDirectory(const std::string &path);
106     bool IsFile(const std::string &path);
107 };
108 } // namespace DistributedFile
109 } // namespace Storage
110 } // namespace OHOS
111 
112 #endif // DISTRIBUTED_FILE_COPY_MANAGER_H
113