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 #include "event_db_file_util.h"
17
18 #include <vector>
19
20 #include "base_def.h"
21 #include "file_util.h"
22 #include "hisysevent.h"
23 #include "hiview_logger.h"
24 #include "string_util.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("HiView-EventDbFileUtil");
29 namespace {
30 constexpr char DB_NAME_CONCATE[] = "-";
31 constexpr size_t EVENT_NAME_INDEX = 0;
32 constexpr size_t EVENT_TYPE_INDEX = 1;
33 constexpr size_t EVENT_LEVEL_INDEX = 2;
34 constexpr size_t EVENT_SEQ_INDEX = 3;
35 constexpr size_t FILE_NAME_SPLIT_SIZE = 4;
36
IsValidDomainName(const std::string & content,const size_t lenLimit)37 bool IsValidDomainName(const std::string& content, const size_t lenLimit)
38 {
39 if (content.size() >= lenLimit) {
40 return false;
41 }
42 for (const auto& c : content) {
43 if (isdigit(c) || isalpha(c) || c == '_') {
44 continue;
45 } else {
46 return false;
47 }
48 }
49 return true;
50 }
51
IsValidType(int type)52 bool IsValidType(int type)
53 {
54 return type == HiSysEvent::EventType::FAULT || type == HiSysEvent::EventType::STATISTIC ||
55 type == HiSysEvent::EventType::SECURITY || type == HiSysEvent::EventType::BEHAVIOR;
56 }
57 }
58
IsValidDbDir(const std::string & dir)59 bool EventDbFileUtil::IsValidDbDir(const std::string& dir)
60 {
61 return IsValidDomainName(FileUtil::ExtractFileName(dir), MAX_DOMAIN_LEN);
62 }
63
IsValidDbFilePath(const std::string & filePath)64 bool EventDbFileUtil::IsValidDbFilePath(const std::string& filePath)
65 {
66 std::string fileName = FileUtil::ExtractFileName(filePath);
67
68 SplitedEventInfo eventInfo;
69 return ParseEventInfoFromDbFileName(fileName, eventInfo, ALL_INFO);
70 }
71
ParseEventInfoFromDbFileName(const std::string & fileName,SplitedEventInfo & info,ParseType parseType)72 bool EventDbFileUtil::ParseEventInfoFromDbFileName(const std::string& fileName, SplitedEventInfo& info,
73 ParseType parseType)
74 {
75 std::vector<std::string> eventInfoList;
76 StringUtil::SplitStr(fileName, DB_NAME_CONCATE, eventInfoList);
77
78 if (eventInfoList.size() != FILE_NAME_SPLIT_SIZE) {
79 HIVIEW_LOGW("invalid size=%{public}zu of event info list", eventInfoList.size());
80 return false;
81 }
82 // parse event name
83 if (parseType & NAME_ONLY) {
84 std::string name = eventInfoList[EVENT_NAME_INDEX];
85 if (!IsValidDomainName(name, MAX_EVENT_NAME_LEN)) {
86 HIVIEW_LOGW("invalid name: %{public}s", name.c_str());
87 return false;
88 }
89 info.name = name;
90 }
91 // parse event type
92 if (parseType & TYPE_ONLY) {
93 int type = 0;
94 StringUtil::ConvertStringTo(eventInfoList[EVENT_TYPE_INDEX], type);
95 if (!IsValidType(type)) {
96 HIVIEW_LOGW("invalid type: %{public}d", type);
97 return false;
98 }
99 info.type = type;
100 }
101 // parse event level
102 if (parseType & LEVEL_ONLY) {
103 std::string level = eventInfoList[EVENT_LEVEL_INDEX];
104 if (level != "CRITICAL" && level!= "MINOR") {
105 HIVIEW_LOGW("invalid level: %{public}s", level.c_str());
106 return false;
107 }
108 info.level = level;
109 }
110 // parse event sequence
111 if (parseType & SEQ_ONLY) {
112 StringUtil::ConvertStringTo(eventInfoList[EVENT_SEQ_INDEX], info.seq);
113 if (info.seq <= 0) {
114 HIVIEW_LOGW("invalid seq: %{public}" PRId64 "", info.seq);
115 return false;
116 }
117 }
118
119 return true;
120 }
121 }
122 }