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_expire_task.h"
17 
18 #include "file_util.h"
19 #include "hiview_logger.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 DEFINE_LOG_TAG("HiView-EventExpireTask");
24 namespace {
25 constexpr char SYSEVENT_EXPORT_TMP_DIR[] = "tmp";
26 
CreateExpireFileScanDir(const std::string & baseDir,const std::string & moduleName)27 std::string CreateExpireFileScanDir(const std::string& baseDir, const std::string& moduleName)
28 {
29     std::string dir = FileUtil::IncludeTrailingPathDelimiter(baseDir);
30     dir = FileUtil::IncludeTrailingPathDelimiter(dir.append(SYSEVENT_EXPORT_TMP_DIR));
31     dir = FileUtil::IncludeTrailingPathDelimiter(dir.append(moduleName));
32     if (!FileUtil::IsDirectory(dir) && !FileUtil::ForceCreateDirectory(dir)) {
33         return "";
34     }
35     HIVIEW_LOGD("scan directory is %{public}s", dir.c_str());
36     return dir;
37 }
38 }
OnTaskRun()39 void EventExpireTask::OnTaskRun()
40 {
41     if (config_ == nullptr || dbMgr_ == nullptr) {
42         HIVIEW_LOGE("config manager or db manager is invalid");
43         return;
44     }
45     // init delete handler
46     auto deleteHandler = std::make_shared<EventDeleteHandler>();
47     // init scan handler
48     auto scanHandler = std::make_shared<EventScanHandler>();
49     // init handler chain
50     scanHandler->SetNextHandler(deleteHandler);
51     // start handler chain, try to scan expired event file
52     auto scanReq = std::make_shared<EventScanRequest>();
53     scanReq->moduleName = config_->moduleName;
54     scanReq->scanDir = CreateExpireFileScanDir(config_->exportDir, config_->moduleName);
55     scanReq->storedDayCnt = static_cast<uint8_t>(config_->dayCnt);
56     if (!scanHandler->HandleRequest(scanReq)) {
57         HIVIEW_LOGE("some error occured during event expring");
58         return;
59     }
60     HIVIEW_LOGI("task of expiring events finished");
61 }
62 } // HiviewDFX
63 } // OHOS