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 void AddListenerFile(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 GetNotifyEvent(); 69 void ReadNotifyEvent(); 70 std::tuple<bool, int, bool> HandleProgress(inotify_event *event); 71 bool CheckFileValid(const std::string &filePath); 72 int UpdateProgressSize(const std::string &filePath, std::shared_ptr<ReceiveInfo> receivedInfo); 73 74 private: 75 bool isFile_ = false; 76 int32_t notifyFd_ = -1; 77 int32_t eventFd_ = -1; 78 uint64_t totalSize_ = 0; 79 uint64_t progressSize_ = 0; 80 std::chrono::steady_clock::time_point notifyTime_; 81 std::mutex wdsMutex_; 82 std::map<int, std::shared_ptr<ReceiveInfo>> wds_; 83 84 std::thread notifyHandler_; 85 std::mutex filePathsMutex_; 86 std::set<std::string> filePaths_; 87 ProcessCallback processCallback_; 88 int32_t errorCode_ = 0; 89 }; 90 } // namespace DistributedFile 91 } // namespace Storage 92 } // namespace OHOS 93 94 #endif // DISTRIBUTED_FILE_COPY_LISTENER_H 95