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 #include "common_util.h"
17
18 #include <regex>
19
20 #include "file_util.h"
21 #include "hiview_logger.h"
22 #include "string_util.h"
23 #include "time_util.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace UCollectUtil {
28 namespace {
29 DEFINE_LOG_TAG("UCollectUtil-CommonUtil");
30 const std::string EXPORT_FILE_REGEX = "[0-9]{14}(.*)";
31 const std::string UNDERLINE = "_";
32 }
StrToNum(const std::string & sString,T & tX)33 template <typename T> bool CommonUtil::StrToNum(const std::string &sString, T &tX)
34 {
35 std::istringstream iStream(sString);
36 return (iStream >> tX) ? true : false;
37 }
38
ParseTypeAndValue(const std::string & str,std::string & type,int32_t & value)39 bool CommonUtil::ParseTypeAndValue(const std::string &str, std::string &type, int32_t &value)
40 {
41 std::string::size_type typePos = str.find(":");
42 if (typePos != std::string::npos) {
43 type = str.substr(0, typePos);
44 std::string valueStr = str.substr(typePos + 1);
45 std::string::size_type valuePos = valueStr.find("kB");
46 if (valuePos == std::string::npos) {
47 valuePos = valueStr.find("KB");
48 }
49 if (valuePos != std::string::npos) {
50 valueStr.resize(valuePos);
51 StrToNum(valueStr, value);
52 return true;
53 } else {
54 StrToNum(valueStr, value);
55 return true;
56 }
57 }
58 return false;
59 }
60
GetDirRegexFiles(const std::string & path,const std::string & pattern,std::vector<std::string> & files)61 void CommonUtil::GetDirRegexFiles(const std::string& path, const std::string& pattern,
62 std::vector<std::string>& files)
63 {
64 DIR* dir = opendir(path.c_str());
65 if (dir == nullptr) {
66 HIVIEW_LOGE("failed to open dir=%{public}s", path.c_str());
67 return;
68 }
69 std::regex reg = std::regex(pattern);
70 struct dirent* ptr = nullptr;
71 while ((ptr = readdir(dir)) != nullptr) {
72 if (ptr->d_type == DT_REG) {
73 if (regex_match(ptr->d_name, reg)) {
74 files.push_back(FileUtil::IncludeTrailingPathDelimiter(path) + std::string(ptr->d_name));
75 }
76 }
77 }
78 closedir(dir);
79 std::sort(files.begin(), files.end());
80 }
81
GetFileNameNum(const std::string & fileName,const std::string & ext)82 int CommonUtil::GetFileNameNum(const std::string& fileName, const std::string& ext)
83 {
84 int ret = 0;
85 auto startPos = fileName.find(UNDERLINE);
86 if (startPos == std::string::npos) {
87 return ret;
88 }
89 auto endPos = fileName.find(ext);
90 if (endPos == std::string::npos) {
91 return ret;
92 }
93 if (endPos <= startPos + 1) {
94 return ret;
95 }
96 return StringUtil::StrToInt(fileName.substr(startPos + 1, endPos - startPos - 1));
97 }
98
CreateExportFile(const std::string & path,int32_t maxFileNum,const std::string & prefix,const std::string & ext)99 std::string CommonUtil::CreateExportFile(const std::string& path, int32_t maxFileNum, const std::string& prefix,
100 const std::string& ext)
101 {
102 if (!FileUtil::IsDirectory(path) && !FileUtil::ForceCreateDirectory(path)) {
103 HIVIEW_LOGE("failed to create dir=%{public}s", path.c_str());
104 return "";
105 }
106
107 std::vector<std::string> files;
108 GetDirRegexFiles(path, prefix + EXPORT_FILE_REGEX, files);
109 if (files.size() >= static_cast<size_t>(maxFileNum)) {
110 for (size_t index = 0; index <= files.size() - static_cast<size_t>(maxFileNum); ++index) {
111 HIVIEW_LOGI("remove file=%{public}s", FileUtil::ExtractFileName(files[index]).c_str());
112 (void)FileUtil::RemoveFile(files[index]);
113 }
114 }
115
116 uint64_t fileTime = TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC;
117 std::string timeFormat = TimeUtil::TimestampFormatToDate(fileTime, "%Y%m%d%H%M%S");
118 std::string fileName;
119 fileName.append(path).append(prefix).append(timeFormat);
120 if (!files.empty()) {
121 auto startPos = files.back().find(timeFormat);
122 if (startPos != std::string::npos) {
123 int fileNameNum = GetFileNameNum(files.back().substr(startPos), ext); // yyyymmddHHMMSS_1.txt
124 fileName.append(UNDERLINE).append(std::to_string(++fileNameNum));
125 }
126 }
127 fileName.append(ext);
128 (void)FileUtil::CreateFile(fileName);
129 HIVIEW_LOGI("create file=%{public}s", FileUtil::ExtractFileName(fileName).c_str());
130 return fileName;
131 }
132 } // UCollectUtil
133 } // HiViewDFX
134 } // OHOS
135