• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "sys_event_db_backup.h"
17 
18 #include <cinttypes>
19 
20 #include "file_util.h"
21 #include "logger.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 DEFINE_LOG_TAG("HiView-SysEventDbBak");
26 using EventStore::SysEventDao;
27 using EventStore::SysEventQuery;
28 using EventStore::EventCol;
29 using EventStore::Op;
30 using EventStore::ResultSet;
31 
SysEventDbBackup(EventStore::StoreType type)32 SysEventDbBackup::SysEventDbBackup(EventStore::StoreType type)
33     : type_(type)
34 {
35     dbFile_ = SysEventDao::GetDataFile(type);
36     dbBakFile_ = SysEventDao::GetBakFile(type);
37 }
38 
IsBroken()39 bool SysEventDbBackup::IsBroken()
40 {
41     auto sysEventQuery = SysEventDao::BuildQuery(dbFile_);
42     ResultSet result = (*sysEventQuery).Where(EventCol::TS, Op::GT, 0).And(EventCol::TS, Op::LT, 1000).Execute(1); // 1s
43     if (result.GetErrCode() != 0) {
44         HIVIEW_LOGE("sys event db=%{public}s is broken", dbFile_.c_str());
45         return true;
46     }
47     return false;
48 }
49 
BackupOnline()50 bool SysEventDbBackup::BackupOnline()
51 {
52     if (SysEventDao::BackupDB(type_) < 0) {
53         HIVIEW_LOGE("sys event backup db failed");
54         return false;
55     }
56     HIVIEW_LOGI("sys event backup db success");
57     return true;
58 }
59 
Recover()60 bool SysEventDbBackup::Recover()
61 {
62     HIVIEW_LOGW("start to recover db file=%{public}s", dbFile_.c_str());
63     return RecoverFromBackup() ? true : RecoverByRebuild();
64 }
65 
RecoverFromBackup()66 bool SysEventDbBackup::RecoverFromBackup()
67 {
68     if (!FileUtil::FileExists(dbBakFile_)) {
69         HIVIEW_LOGE("the backup file does not exists");
70         return false;
71     }
72     if (SysEventDao::DeleteDB(type_) < 0) {
73         HIVIEW_LOGE("can not delete db data");
74         return false;
75     }
76     RemoveDbFile();
77     if (!FileUtil::RenameFile(dbBakFile_, dbFile_)) {
78         HIVIEW_LOGE("failed to rename the backup file");
79         return false;
80     }
81     return true;
82 }
83 
RecoverByRebuild()84 bool SysEventDbBackup::RecoverByRebuild()
85 {
86     HIVIEW_LOGW("recover sys event db by rebuild");
87     if (SysEventDao::CloseDB(type_) < 0) {
88         return false;
89     }
90     RemoveDbFile();
91     return true;
92 }
93 
RemoveDbFile()94 void SysEventDbBackup::RemoveDbFile()
95 {
96     if (FileUtil::FileExists(dbFile_) && !FileUtil::RemoveFile(dbFile_)) {
97         HIVIEW_LOGW("failed to remove sys event wal db");
98     }
99 
100     std::string walDbFile = dbFile_ + "-wal";
101     if (FileUtil::FileExists(walDbFile) && !FileUtil::RemoveFile(walDbFile)) {
102         HIVIEW_LOGW("failed to remove sys event wal db");
103     }
104 }
105 } // namespace HiviewDFX
106 } // namespace OHOS