1 /*
2 * Copyright (c) 2022-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 "hiappevent_clean.h"
17
18 #include <memory>
19 #include <vector>
20
21 #include "app_event_db_cleaner.h"
22 #include "app_event_log_cleaner.h"
23 #include "app_event_observer_mgr.h"
24 #include "hiappevent_base.h"
25 #include "hiappevent_config.h"
26 #include "hiappevent_userinfo.h"
27 #include "hilog/log.h"
28
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD002D07
31
32 #undef LOG_TAG
33 #define LOG_TAG "Clean"
34
35 namespace OHOS {
36 namespace HiviewDFX {
37 namespace HiAppEventClean {
38 namespace {
39 constexpr int EVENT_COUNT_OF_CHECK_SPACE = 1000;
40
41 static int g_eventCount = -1;
42
CreateCleaners(const std::string & dir,std::vector<std::shared_ptr<AppEventCleaner>> & cleaners)43 void CreateCleaners(const std::string& dir, std::vector<std::shared_ptr<AppEventCleaner>>& cleaners)
44 {
45 cleaners.push_back(std::make_shared<AppEventDbCleaner>(dir));
46 cleaners.push_back(std::make_shared<AppEventLogCleaner>(dir));
47 }
48
GetCurStorageSize(const std::string & dir)49 uint64_t GetCurStorageSize(const std::string& dir)
50 {
51 std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
52 CreateCleaners(dir, cleaners);
53 uint64_t curSize = 0;
54 for (auto& cleaner : cleaners) {
55 curSize += cleaner->GetFilesSize();
56 }
57 return curSize;
58 }
59 }
IsStorageSpaceFull(const std::string & dir,uint64_t maxSize)60 bool IsStorageSpaceFull(const std::string& dir, uint64_t maxSize)
61 {
62 return GetCurStorageSize(dir) > maxSize;
63 }
64
ReleaseSomeStorageSpace(const std::string & dir,uint64_t maxSize)65 bool ReleaseSomeStorageSpace(const std::string& dir, uint64_t maxSize)
66 {
67 HILOG_INFO(LOG_CORE, "start to clear the storage space");
68 std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
69 CreateCleaners(dir, cleaners);
70 auto curSize = GetCurStorageSize(dir);
71 for (auto it = cleaners.rbegin(); it != cleaners.rend(); ++it) { // clear the log space first
72 curSize = (*it)->ClearSpace(curSize, maxSize);
73 if (curSize <= maxSize) {
74 return true;
75 }
76 }
77 return curSize <= maxSize;
78 }
79
ClearData(const std::string & dir)80 void ClearData(const std::string& dir)
81 {
82 // reset the status of observers
83 AppEventObserverMgr::GetInstance().HandleClearUp();
84
85 // clear user ids and properties
86 HiAppEvent::UserInfo::GetInstance().ClearData();
87
88 // delete data files
89 std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
90 CreateCleaners(dir, cleaners);
91 for (auto& cleaner : cleaners) { // clear the db data first
92 cleaner->ClearData();
93 }
94 }
95
CheckStorageSpace()96 void CheckStorageSpace()
97 {
98 if (g_eventCount == -1 || g_eventCount >= (EVENT_COUNT_OF_CHECK_SPACE - 1)) {
99 std::string dir = HiAppEventConfig::GetInstance().GetStorageDir();
100 auto maxSize = HiAppEventConfig::GetInstance().GetMaxStorageSize();
101 if (!IsStorageSpaceFull(dir, maxSize)) {
102 return;
103 }
104 HILOG_INFO(LOG_CORE, "hiappevent dir space is full, start to clean");
105 ReleaseSomeStorageSpace(dir, maxSize);
106 }
107 g_eventCount = (++g_eventCount) % EVENT_COUNT_OF_CHECK_SPACE;
108 }
109 } // namespace HiAppEventClean
110 } // namespace HiviewDFX
111 } // namespace OHOS
112