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 DISTRIBUTEDDATAMGR_RDB_FAULT_HIVIEW_REPORTER_H
17 #define DISTRIBUTEDDATAMGR_RDB_FAULT_HIVIEW_REPORTER_H
18 
19 #include <ctime>
20 #include <fcntl.h>
21 #include <string>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
25 #include "connection.h"
26 #include "rdb_store_config.h"
27 #include "rdb_types.h"
28 namespace OHOS::NativeRdb {
29 using DebugInfo = OHOS::DistributedRdb::RdbDebugInfo;
30 struct RdbCorruptedEvent {
31     std::string bundleName;
32     std::string moduleName;
33     std::string storeType;
34     std::string storeName;
35     uint32_t securityLevel;
36     uint32_t pathArea;
37     uint32_t encryptStatus;
38     uint32_t integrityCheck;
39     uint32_t errorCode;
40     int32_t systemErrorNo;
41     std::string appendix;
42     time_t errorOccurTime;
43     std::string path;
44     std::map<std::string, DebugInfo> debugInfos;
45 };
46 
47 class RdbFaultHiViewReporter {
48 public:
49     static RdbCorruptedEvent Create(const RdbStoreConfig &config, int32_t errCode, const std::string &appendix = "");
50     static bool RegCollector(Connection::Collector collector);
51     static void Report(const RdbCorruptedEvent &eventInfo);
52     static void ReportFault(const RdbCorruptedEvent &eventInfo);
53     static void ReportRestore(const RdbCorruptedEvent &eventInfo, bool repair = true);
54     static std::string Format(const std::map<std::string, DebugInfo> &debugs, const std::string &header);
55     static std::string FormatBrief(const std::map<std::string, DebugInfo> &debugs, const std::string &header);
56     static bool IsReportCorruptedFault(const std::string &dbPath);
57 
58 private:
59     static void Update(RdbCorruptedEvent &eventInfo, const std::map<std::string, DebugInfo> &infos);
60     static std::string GetFileStatInfo(const DebugInfo &debugInfo);
61     static void CreateCorruptedFlag(const std::string &dbPath);
62     static void DeleteCorruptedFlag(const std::string &dbPath);
63     static std::string GetTimeWithMilliseconds(time_t sec, int64_t nsec);
64     static std::string GetBundleName(const RdbCorruptedEvent &eventInfo);
65     static Connection::Collector collector_;
66 };
67 } // namespace OHOS::NativeRdb
68 #endif //DISTRIBUTEDDATAMGR_RDB_FAULT_HIVIEW_REPORTER_H
69