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