1 /* 2 * Copyright (c) 2023-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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_WATCHER_MANAGER_H 17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_WATCHER_MANAGER_H 18 19 #include <mutex> 20 #include <sys/inotify.h> 21 #include <sys/time.h> 22 #include "account_error_no.h" 23 #include "account_file_operator.h" 24 #include "singleton.h" 25 26 namespace OHOS { 27 namespace AccountSA { 28 #ifdef ENABLE_FILE_WATCHER 29 #ifdef HAS_HUKS_PART 30 int32_t GenerateAccountInfoDigest(const std::string &inData, uint8_t* outData, uint32_t size); 31 #endif // HAS_HUKS_PART 32 33 using CheckNotifyEventCallbackFunc = std::function<bool(const std::string&, int32_t, uint32_t)>; 34 class FileWatcher { 35 public: 36 FileWatcher(int32_t id, const CheckNotifyEventCallbackFunc &checkCallbackFunc); 37 FileWatcher(int32_t id, const std::string &filePath, const CheckNotifyEventCallbackFunc &checkCallbackFunc); 38 ~FileWatcher(); 39 40 std::string GetFilePath() const; 41 int32_t GetLocalId() const; 42 int32_t GetWd() const; 43 44 bool StartNotify(int32_t fd, const uint32_t &watchEvents); 45 void CloseNotify(int32_t fd); 46 bool CheckNotifyEvent(uint32_t event); 47 48 private: 49 int32_t id_ = -1; 50 int32_t wd_ = -1; // generate from inotify_add_watch 51 std::string filePath_; 52 CheckNotifyEventCallbackFunc eventCallbackFunc_; 53 }; 54 55 class AccountFileWatcherMgr { 56 public: 57 static AccountFileWatcherMgr &GetInstance(); 58 void StartWatch(); 59 void AddFileWatcher( 60 int32_t id, CheckNotifyEventCallbackFunc checkCallbackFunc, const std::string &filePath = ""); 61 void RemoveFileWatcher(int32_t id, const std::string &filePath); 62 ErrCode GetAccountInfoDigestFromFile(const std::string &path, uint8_t *digest, uint32_t size); 63 ErrCode GenerateAccountInfoDigestStr( 64 const std::string &userInfoPath, const std::string &accountInfoStr, std::string &digestStr); 65 ErrCode AddAccountInfoDigest(const std::string accountInfo, const std::string &userInfoPath); 66 ErrCode DeleteAccountInfoDigest(const std::string &userInfoPath); 67 68 private: 69 void DealWithFileEvent(); 70 void GetNotifyEvent(); 71 AccountFileWatcherMgr(); 72 ~AccountFileWatcherMgr(); 73 DISALLOW_COPY_AND_MOVE(AccountFileWatcherMgr); 74 75 public: 76 int32_t inotifyFd_ = -1; 77 78 std::mutex accountInfoDigestFileLock_; 79 std::mutex fileWatcherMgrLock_; 80 std::shared_ptr<AccountFileOperator> accountFileOperator_; 81 std::unordered_map<int32_t, std::shared_ptr<FileWatcher>> fileNameMgrMap_; 82 fd_set fds_; 83 bool run_ = false; 84 }; 85 #endif // ENABLE_FILE_WATCHER 86 } // namespace AccountSA 87 } // namespace OHOS 88 89 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OSACCOUNT_OS_ACCOUNT_CONTROL_FILE_MANAGER_H 90