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 #include "event_scan_handler.h"
17
18 #include <cstdlib>
19 #include <regex>
20 #include <fstream>
21 #include <sys/stat.h>
22
23 #include "event_delete_handler.h"
24 #include "hiview_logger.h"
25 #include "file_util.h"
26 #include "string_util.h"
27 #include "time_util.h"
28
29 namespace OHOS {
30 namespace HiviewDFX {
31 DEFINE_LOG_TAG("HiView-EventScanHandler");
32 namespace {
33 constexpr uint64_t DAY_TO_SECONDS = 24 * 60 * 60;
34
GetFileLastModifiedTime(const std::string & file)35 uint64_t GetFileLastModifiedTime(const std::string& file)
36 {
37 struct stat fileInfo {0};
38 if (stat(file.c_str(), &fileInfo) != ERR_OK) {
39 HIVIEW_LOGW("failed to get file info %{public}s", StringUtil::HideDeviceIdInfo(file).c_str());
40 return 0;
41 }
42 return fileInfo.st_mtime;
43 }
44
GetDuration(uint64_t from,uint64_t to)45 uint64_t GetDuration(uint64_t from, uint64_t to)
46 {
47 return ((from >= to) ? (from - to) : (to - from));
48 }
49
GetExpiredFileNames(std::vector<std::string> & dest,const std::string & scanDir,uint8_t storedDayCnt)50 void GetExpiredFileNames(std::vector<std::string>& dest, const std::string& scanDir, uint8_t storedDayCnt)
51 {
52 std::vector<std::string> scannedFiles;
53 FileUtil::GetDirFiles(scanDir, scannedFiles);
54 if (scannedFiles.empty()) {
55 HIVIEW_LOGW("no packaged export event file found.");
56 return;
57 }
58 std::regex reg { ".*/HSE_.*\\.zip$" };
59 std::smatch match;
60 for (const auto& scannedFile : scannedFiles) {
61 HIVIEW_LOGD("scannedFile is %{public}s", scannedFile.c_str());
62 if (!std::regex_match(scannedFile, match, reg)) {
63 continue;
64 }
65 auto fileModifyTime = GetFileLastModifiedTime(scannedFile);
66 auto currentTime = TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC;
67 HIVIEW_LOGD("current time: %{public}" PRIu64 ", file last modified time: %{public}" PRIu64 "", currentTime,
68 fileModifyTime);
69 if (GetDuration(currentTime, fileModifyTime) > storedDayCnt * DAY_TO_SECONDS) {
70 dest.emplace_back(scannedFile);
71 }
72 }
73 }
74 }
75
HandleRequest(RequestPtr req)76 bool EventScanHandler::HandleRequest(RequestPtr req)
77 {
78 auto scanReq = BaseRequest::DownCastTo<EventScanRequest>(req);
79 return Scan(scanReq->moduleName, scanReq->scanDir, scanReq->storedDayCnt);
80 }
81
Scan(const std::string & moduleName,const std::string & scanDir,uint8_t storedDayCnt)82 bool EventScanHandler::Scan(const std::string& moduleName, const std::string& scanDir, uint8_t storedDayCnt)
83 {
84 auto delReq = std::make_shared<EventDelRequest>();
85 delReq->moduleName = moduleName;
86 GetExpiredFileNames(delReq->files, scanDir, storedDayCnt);
87 if (delReq->files.empty()) {
88 HIVIEW_LOGW("no expired event file found");
89 return false;
90 }
91 HIVIEW_LOGW("count of export event zip file is %{public}zu", delReq->files.size());
92 if (nextHandler_ != nullptr) {
93 nextHandler_->HandleRequest(delReq);
94 }
95 return true;
96 }
97 } // HiviewDFX
98 } // OHOS