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_LISTENER_H 17 #define DISTRIBUTED_FILE_COPY_LISTENER_H 18 19 #include <fcntl.h> 20 #include <filesystem> 21 22 #include <map> 23 #include <memory> 24 #include <poll.h> 25 #include <set> 26 #include <string> 27 #include <sys/eventfd.h> 28 #include <sys/inotify.h> 29 #include <sys/prctl.h> 30 #include <sys/types.h> 31 #include <thread> 32 #include <tuple> 33 34 namespace OHOS { 35 namespace Storage { 36 namespace DistributedFile { 37 38 struct ReceiveInfo { 39 std::string path; // dir name 40 std::map<std::string, uint64_t> fileList; // filename, proceededSize 41 }; 42 43 using ProcessCallback = std::function<void (uint64_t processSize, uint64_t totalSize)>; 44 class FileCopyLocalListener { 45 public: 46 FileCopyLocalListener(const FileCopyLocalListener&) = delete; 47 FileCopyLocalListener& operator=(const FileCopyLocalListener&) = delete; 48 FileCopyLocalListener(const std::string &srcPath, 49 bool isFile, const ProcessCallback &processCallback); 50 ~FileCopyLocalListener(); 51 static std::shared_ptr<FileCopyLocalListener> GetLocalListener(const std::string &srcPath, 52 bool isFile, const ProcessCallback &processCallback); 53 54 public: 55 void StartListener(); 56 void StopListener(); 57 void AddFile(const std::string &fileName); 58 int32_t AddListenerFile(const std::string &srcPath, const std::string &destPath, uint32_t mode); GetFilePath()59 std::set<std::string> GetFilePath() 60 { 61 std::lock_guard<std::mutex> lock(filePathsMutex_); 62 return filePaths_; 63 } GetResult()64 int32_t GetResult() { return errorCode_; }; 65 66 private: 67 void CloseNotifyFd(); 68 void CloseNotifyFdLocked(); 69 void GetNotifyEvent(); 70 void ReadNotifyEvent(); 71 void ReadNotifyEventLocked(); 72 std::tuple<bool, int, bool> HandleProgress(inotify_event *event); 73 bool CheckFileValid(const std::string &filePath); 74 int UpdateProgressSize(const std::string &filePath, std::shared_ptr<ReceiveInfo> receivedInfo); 75 76 private: 77 bool isFile_ = false; 78 int32_t notifyFd_ = -1; 79 int32_t eventFd_ = -1; 80 uint64_t totalSize_ = 0; 81 uint64_t progressSize_ = 0; 82 std::chrono::steady_clock::time_point notifyTime_; 83 std::mutex wdsMutex_; 84 std::map<int, std::shared_ptr<ReceiveInfo>> wds_; 85 86 std::thread notifyHandler_; 87 std::mutex filePathsMutex_; 88 std::set<std::string> filePaths_; 89 ProcessCallback processCallback_; 90 int32_t errorCode_ = 0; 91 92 std::mutex readMutex_; 93 bool readFlag_ = false; 94 bool readClosed_ = false; 95 std::atomic<bool> notifyRun_ {true}; 96 97 std::condition_variable notifyCv_; 98 std::mutex cvLock_; 99 std::mutex processMutex_; 100 }; 101 } // namespace DistributedFile 102 } // namespace Storage 103 } // namespace OHOS 104 105 #endif // DISTRIBUTED_FILE_COPY_LISTENER_H 106