1 /*
2 * Copyright (c) 2022-2023 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_userinfo.h"
26 #include "hilog/log.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace HiAppEventClean {
31 namespace {
32 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_Clean" };
33
CreateCleaners(const std::string & dir,std::vector<std::shared_ptr<AppEventCleaner>> & cleaners)34 void CreateCleaners(const std::string& dir, std::vector<std::shared_ptr<AppEventCleaner>>& cleaners)
35 {
36 cleaners.push_back(std::make_shared<AppEventDbCleaner>(dir));
37 cleaners.push_back(std::make_shared<AppEventLogCleaner>(dir));
38 }
39
GetCurStorageSize(const std::string & dir)40 uint64_t GetCurStorageSize(const std::string& dir)
41 {
42 std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
43 CreateCleaners(dir, cleaners);
44 uint64_t curSize = 0;
45 for (auto& cleaner : cleaners) {
46 curSize += cleaner->GetFilesSize();
47 }
48 return curSize;
49 }
50 }
IsStorageSpaceFull(const std::string & dir,uint64_t maxSize)51 bool IsStorageSpaceFull(const std::string& dir, uint64_t maxSize)
52 {
53 return GetCurStorageSize(dir) > maxSize;
54 }
55
ReleaseSomeStorageSpace(const std::string & dir,uint64_t maxSize)56 bool ReleaseSomeStorageSpace(const std::string& dir, uint64_t maxSize)
57 {
58 HiLog::Info(LABEL, "start to clear the storage space");
59 std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
60 CreateCleaners(dir, cleaners);
61 auto curSize = GetCurStorageSize(dir);
62 for (auto it = cleaners.rbegin(); it != cleaners.rend(); ++it) { // clear the log space first
63 curSize = (*it)->ClearSpace(curSize, maxSize);
64 if (curSize <= maxSize) {
65 return true;
66 }
67 }
68 return curSize <= maxSize;
69 }
70
ClearData(const std::string & dir)71 void ClearData(const std::string& dir)
72 {
73 // reset the status of observers
74 AppEventObserverMgr::GetInstance().HandleClearUp();
75
76 // clear user ids and properties
77 HiAppEvent::UserInfo::GetInstance().ClearData();
78
79 // delete data files
80 std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
81 CreateCleaners(dir, cleaners);
82 for (auto& cleaner : cleaners) { // clear the db data first
83 cleaner->ClearData();
84 }
85 }
86 } // namespace HiAppEventClean
87 } // namespace HiviewDFX
88 } // namespace OHOS
89