1 /* 2 * Copyright (c) 2024 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 TEMP_FILE_MANAGER_H_ 17 #define TEMP_FILE_MANAGER_H_ 18 19 #include <filesystem> 20 #include <list> 21 #include <string> 22 #include <utility> 23 24 #include "epoll_manager.h" 25 #include "fault_logger_config.h" 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 30 class TempFileManager { 31 public: 32 explicit TempFileManager(EpollManager& epollManager); 33 34 bool Init(); 35 36 static int32_t CreateFileDescriptor(int32_t type, int32_t pid, int32_t tid, uint64_t time); 37 #ifndef is_ohos_lite 38 static bool CheckCrashFileRecord(int32_t pid); 39 static void RecordFileCreation(int32_t type, int32_t pid); 40 #endif 41 42 private: 43 class TempFileWatcher : public EpollListener { 44 public: 45 static std::unique_ptr<TempFileWatcher> CreateInstance(TempFileManager& tempFileManager); 46 bool AddWatchEvent(const char* watchPath, uint32_t watchEvent); 47 void OnEventPoll() override; 48 private: 49 TempFileWatcher(TempFileManager& tempFileManager, SmartFd fd); 50 void HandleEvent(uint32_t eventMask, const std::string& filePath, const SingleFileConfig& fileConfig); 51 void HandleFileCreate(const std::string& filePath, const SingleFileConfig& fileConfig); 52 void HandleFileDeleteOrMove(const std::string& filePath, const SingleFileConfig& fileConfig); 53 void HandleDirRemoved(); 54 static void HandleFileWrite(const std::string& filePath, const SingleFileConfig& fileConfig); 55 TempFileManager &tempFileManager_; 56 }; 57 bool InitTempFileWatcher(); 58 void ScanTempFilesOnStart(); 59 void ClearBigFilesOnStart(bool isSizeOverLimit, std::list<std::string>& files); 60 61 #ifndef is_ohos_lite 62 static void ClearTimeOutRecords(); 63 #endif 64 65 EpollManager& epollManager_; 66 int32_t& GetTargetFileCount(int32_t type); 67 std::vector<std::pair<int32_t, int32_t>> fileCounts_; 68 #ifndef is_ohos_lite 69 static std::list<std::pair<int32_t, int64_t>> crashFileRecords_; 70 #endif 71 }; 72 } 73 } 74 #endif // TEMP_FILE_MANAGER_H_ 75