• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "hiappevent_base.h"
24 #include "hilog/log.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace HiAppEventClean {
29 namespace {
30 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_Clean" };
31 
CreateCleaners(const std::string & dir,std::vector<std::shared_ptr<AppEventCleaner>> & cleaners)32 void CreateCleaners(const std::string& dir, std::vector<std::shared_ptr<AppEventCleaner>>& cleaners)
33 {
34     cleaners.push_back(std::make_shared<AppEventDbCleaner>(dir));
35     cleaners.push_back(std::make_shared<AppEventLogCleaner>(dir));
36 }
37 
GetCurStorageSize(const std::string & dir)38 uint64_t GetCurStorageSize(const std::string& dir)
39 {
40     std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
41     CreateCleaners(dir, cleaners);
42     uint64_t curSize = 0;
43     for (auto& cleaner : cleaners) {
44         curSize += cleaner->GetFilesSize();
45     }
46     return curSize;
47 }
48 }
IsStorageSpaceFull(const std::string & dir,uint64_t maxSize)49 bool IsStorageSpaceFull(const std::string& dir, uint64_t maxSize)
50 {
51     return GetCurStorageSize(dir) > maxSize;
52 }
53 
ReleaseSomeStorageSpace(const std::string & dir,uint64_t maxSize)54 bool ReleaseSomeStorageSpace(const std::string& dir, uint64_t maxSize)
55 {
56     HiLog::Info(LABEL, "start to clear the storage space");
57     std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
58     CreateCleaners(dir, cleaners);
59     auto curSize = GetCurStorageSize(dir);
60     for (auto it = cleaners.rbegin(); it != cleaners.rend(); ++it) { // clear the log space first
61         curSize = (*it)->ClearSpace(curSize, maxSize);
62         if (curSize <= maxSize) {
63             return true;
64         }
65     }
66     return curSize <= maxSize;
67 }
68 
ClearData(const std::string & dir)69 void ClearData(const std::string& dir)
70 {
71     std::vector<std::shared_ptr<AppEventCleaner>> cleaners;
72     CreateCleaners(dir, cleaners);
73     for (auto& cleaner : cleaners) { // clear the db data first
74         cleaner->ClearData();
75     }
76 }
77 } // namespace HiAppEventClean
78 } // namespace HiviewDFX
79 } // namespace OHOS
80