• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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_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-EventExportFlow");
32 namespace {
33 constexpr uint64_t DAY_TO_SECONDS = 24 * 60 * 60;
34 
GetExpiredFileNames(std::vector<std::string> & dest,const std::string & scanDir,uint8_t storedDayCnt)35 void GetExpiredFileNames(std::vector<std::string>& dest, const std::string& scanDir, uint8_t storedDayCnt)
36 {
37     std::vector<std::string> scannedFiles;
38     FileUtil::GetDirFiles(scanDir, scannedFiles);
39     if (scannedFiles.empty()) {
40         HIVIEW_LOGW("no packaged export event file found.");
41         return;
42     }
43     std::regex reg { ".*/HSE_.*\\.zip$" };
44     std::smatch match;
45     for (const auto& scannedFile : scannedFiles) {
46         HIVIEW_LOGD("scannedFile is %{public}s", StringUtil::HideDeviceIdInfo(scannedFile).c_str());
47         if (!std::regex_match(scannedFile, match, reg)) {
48             continue;
49         }
50         auto fileModifyTime = FileUtil::GetLastModifiedTimeStamp(scannedFile);
51         auto currentTime = static_cast<int64_t>(TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC);
52         HIVIEW_LOGD("current time: %{public}" PRIu64 ", file last modified time: %{public}" PRIu64 "", currentTime,
53             fileModifyTime);
54         if (static_cast<uint64_t>(std::abs(fileModifyTime - currentTime)) > storedDayCnt * DAY_TO_SECONDS) {
55             dest.emplace_back(scannedFile);
56         }
57     }
58 }
59 }
60 
HandleRequest(RequestPtr req)61 bool EventScanHandler::HandleRequest(RequestPtr req)
62 {
63     auto scanReq = BaseRequest::DownCastTo<EventScanRequest>(req);
64     return Scan(scanReq->moduleName, scanReq->scanDir, scanReq->storedDayCnt);
65 }
66 
Scan(const std::string & moduleName,const std::string & scanDir,uint8_t storedDayCnt)67 bool EventScanHandler::Scan(const std::string& moduleName, const std::string& scanDir, uint8_t storedDayCnt)
68 {
69     auto delReq = std::make_shared<EventDelRequest>();
70     delReq->moduleName = moduleName;
71     GetExpiredFileNames(delReq->files, scanDir, storedDayCnt);
72     if (delReq->files.empty()) {
73         HIVIEW_LOGI("no expired event file found");
74         return true;
75     }
76     HIVIEW_LOGI("count of export event zip file is %{public}zu", delReq->files.size());
77     if (nextHandler_ != nullptr) {
78         return nextHandler_->HandleRequest(delReq);
79     }
80     return true;
81 }
82 } // HiviewDFX
83 } // OHOS
84