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 #define MLOG_TAG "MediaLibraryRestore"
16 
17 #include "acl.h"
18 #include "medialibrary_restore.h"
19 #include "dfx_utils.h"
20 #include "medialibrary_data_manager.h"
21 #include "medialibrary_tracer.h"
22 #include "media_file_utils.h"
23 #include "media_log.h"
24 #include "parameter.h"
25 #include "parameters.h"
26 #include "post_event_utils.h"
27 #ifdef CLOUD_SYNC_MANAGER
28 #include "cloud_sync_manager.h"
29 #endif
30 
31 namespace OHOS {
32 namespace Media {
33 namespace {
34     const std::string SWITCH_STATUS_KEY = "persist.multimedia.medialibrary.rdb_switch_status";
35     const std::string CLOUD_STOP_FLAG_K = "persist.kernel.medialibrarydata.stopflag";
36     const std::string CLOUD_STOP_FLAG_V = "1";
37     constexpr int PARAMETER_E_OK        = 0;
38     constexpr int WAIT_SECONDS          = 120;
39     enum HA_SWITCH_STATUS : uint32_t {
40         HA_SWITCH_READY = 0,
41         HA_SWITCHING = 1
42     };
43 } // namespace
44 
GetInstance()45 MediaLibraryRestore &MediaLibraryRestore::GetInstance()
46 {
47     static MediaLibraryRestore instance;
48     return instance;
49 }
50 
SaveHAModeSwitchStatusToPara(const int64_t & status)51 void MediaLibraryRestore::SaveHAModeSwitchStatusToPara(const int64_t &status)
52 {
53     int ret = SetParameter(SWITCH_STATUS_KEY.c_str(), std::to_string(status).c_str());
54     CHECK_AND_RETURN_LOG((ret == PARAMETER_E_OK), "MediaLibraryRestore SetParameter switch error");
55 }
56 
CheckRestore(const int32_t & errCode)57 void MediaLibraryRestore::CheckRestore(const int32_t &errCode)
58 {
59     MEDIA_DEBUG_LOG("CheckRestore is called, errcode=%{public}d", errCode);
60     if (errCode != NativeRdb::E_SQLITE_CORRUPT) {
61         return;
62     }
63     MEDIA_INFO_LOG("Restore is called");
64     CHECK_AND_RETURN_LOG((!isRestoring_), "RdbStore is restoring");
65 
66     std::string date = DfxUtils::GetCurrentDateMillisecond();
67     VariantMap map = {{KEY_DB_CORRUPT, std::move(date)}};
68     PostEventUtils::GetInstance().PostErrorProcess(ErrType::DB_CORRUPT_ERR, map);
69 
70     isRestoring_ = true;
71     std::thread([&] {
72         MEDIA_INFO_LOG("MediaLibraryRestore::Restore [start]");
73         auto rdb = MediaLibraryDataManager::GetInstance()->rdbStore_;
74         if (rdb == nullptr) {
75             isRestoring_ = false;
76             MEDIA_ERR_LOG("Restore rdbStore is nullptr");
77             return;
78         }
79 
80         int errCode = rdb->Restore("");
81         MEDIA_INFO_LOG("MediaLibraryRestore::Restore [end]. errCode = %{public}d", errCode);
82         isRestoring_ = false;
83     }).detach();
84 }
85 
86 #ifdef CLOUD_SYNC_MANAGER
StopCloudSync()87 void MediaLibraryRestore::StopCloudSync()
88 {
89     CHECK_AND_RETURN_LOG((isBackuping_.load()), "StopCloudSync: backuping is false, return");
90     MediaLibraryTracer tracer;
91     tracer.Start("MediaLibraryRestore::StopCloudSync");
92     FileManagement::CloudSync::CloudSyncManager::GetInstance().StopSync(BUNDLE_NAME, true);
93     uint32_t times = 0;
94     int ret = WaitParameter(CLOUD_STOP_FLAG_K.c_str(), CLOUD_STOP_FLAG_V.c_str(), WAIT_SECONDS);
95     if (ret == PARAMETER_E_OK) {
96         MEDIA_INFO_LOG("StopCloudSync success end");
97         return;
98     }
99     isBackuping_ = false;
100     MEDIA_INFO_LOG("StopCloudSync timeout error, set backup false");
101 }
102 
StartCloudSync()103 void MediaLibraryRestore::StartCloudSync()
104 {
105     MediaLibraryTracer tracer;
106     tracer.Start("MediaLibraryRestore::StartCloudSync");
107     int32_t ret = FileManagement::CloudSync::CloudSyncManager::GetInstance().StartSync(BUNDLE_NAME);
108     if (ret != 0) {
109         MEDIA_ERR_LOG("StartCloudSync fail, errcode=%{public}d", ret);
110     }
111 }
112 #endif
113 
CheckBackup()114 void MediaLibraryRestore::CheckBackup()
115 {
116     MEDIA_INFO_LOG("CheckBackup is called");
117     CHECK_AND_RETURN_LOG((!isRestoring_), "CheckBackup: is restoring, return");
118     CHECK_AND_RETURN_LOG((!isBackuping_.load()), "CheckBackup: is backuping, return");
119 
120     auto rdb = MediaLibraryDataManager::GetInstance()->rdbStore_;
121     CHECK_AND_RETURN_LOG((rdb != nullptr), "CheckBackup: rdbStore is nullptr");
122     if (!rdb->IsSlaveDiffFromMaster()) {
123         MEDIA_INFO_LOG("CheckBackup: isSlaveDiffFromMaster [false], return");
124         return;
125     }
126     MEDIA_INFO_LOG("CheckBackup: isSlaveDiffFromMaster [true]");
127     DoRdbBackup();
128 }
129 
ResetHAModeSwitchStatus()130 void MediaLibraryRestore::ResetHAModeSwitchStatus()
131 {
132     auto switchStatus = HA_SWITCH_READY;
133     SaveHAModeSwitchStatusToPara(std::move(switchStatus));
134     isBackuping_ = false;
135 }
136 
DoRdbBackup()137 void MediaLibraryRestore::DoRdbBackup()
138 {
139     isBackuping_ = true;
140     CHECK_AND_RETURN_LOG((!isWaiting_.load()), "waiting stop cloudsync");
141     auto currentTime = MediaFileUtils::UTCTimeSeconds();
142     SaveHAModeSwitchStatusToPara(currentTime);
143     std::thread([&] {
144         MEDIA_INFO_LOG("DoRdbBackup: Backup [start]");
145 #ifdef CLOUD_SYNC_MANAGER
146         MEDIA_INFO_LOG("DoRdbBackup: Call CloudSync start [isBackuping=%{public}d]", isBackuping_.load());
147         isWaiting_ = true;
148         StopCloudSync();
149         isWaiting_ = false;
150         MEDIA_INFO_LOG("DoRdbBackup: Call CloudSync end");
151         if (!isBackuping_.load()) {
152             ResetHAModeSwitchStatus();
153             StartCloudSync();
154             MEDIA_INFO_LOG("DoRdbBackup: isbackuping fasle, return");
155             return;
156         }
157 #endif
158         auto rdb = MediaLibraryDataManager::GetInstance()->rdbStore_;
159         if (rdb == nullptr) {
160             ResetHAModeSwitchStatus();
161 #ifdef CLOUD_SYNC_MANAGER
162             StartCloudSync();
163 #endif
164             MEDIA_ERR_LOG("DoRdbBackup: rdbStore is nullptr");
165             return;
166         }
167 
168         if (isInterrupting_.load() || !isBackuping_.load()) {
169             ResetHAModeSwitchStatus();
170 #ifdef CLOUD_SYNC_MANAGER
171             StartCloudSync();
172 #endif
173             MEDIA_INFO_LOG("DoRdbBackup: Interrupt or isbackuping false, return");
174             return;
175         }
176         MediaLibraryTracer tracer;
177         tracer.Start("MediaLibraryRestore::DoRdbBackup Backup");
178         int errCode = rdb->Backup("");
179         if (errCode == NativeRdb::E_OK) {
180             Acl::AclSetSlaveDatabase();
181         }
182         MEDIA_INFO_LOG("DoRdbBackup: Backup [end]. errCode = %{public}d", errCode);
183         ResetHAModeSwitchStatus();
184 #ifdef CLOUD_SYNC_MANAGER
185         StartCloudSync();
186 #endif
187     }).detach();
188 }
189 
InterruptBackup()190 void MediaLibraryRestore::InterruptBackup()
191 {
192     if (!isBackuping_.load()) {
193         MEDIA_INFO_LOG("rdb is not backuping, no need to interrupt");
194         return;
195     }
196     if (isWaiting_.load()) {
197         isBackuping_ = false;
198         MEDIA_INFO_LOG("InterruptBackup: isWaiting, return");
199         return;
200     }
201     auto rdb = MediaLibraryDataManager::GetInstance()->rdbStore_;
202     CHECK_AND_RETURN_LOG((rdb != nullptr), "[InterruptBackup] rdbStore is nullptr");
203     isInterrupting_ = true;
204     int errCode = rdb->InterruptBackup();
205     isInterrupting_ = false;
206     isBackuping_ = false;
207     MEDIA_INFO_LOG("InterruptBackup [end]. errCode = %{public}d", errCode);
208     if (errCode == NativeRdb::E_OK) {
209         ResetHAModeSwitchStatus();
210     }
211 }
212 
CheckResultSet(const std::shared_ptr<NativeRdb::ResultSet> & resultSet)213 void MediaLibraryRestore::CheckResultSet(const std::shared_ptr<NativeRdb::ResultSet> &resultSet)
214 {
215     MediaLibraryTracer tracer;
216     tracer.Start("CheckResultSet");
217     if (resultSet == nullptr) {
218         return;
219     }
220     int count;
221     int errCode = resultSet->GetRowCount(count);
222     CheckRestore(errCode);
223 }
224 
IsRestoring() const225 bool MediaLibraryRestore::IsRestoring() const
226 {
227     return isRestoring_;
228 }
229 
IsBackuping() const230 bool MediaLibraryRestore::IsBackuping() const
231 {
232     return isBackuping_;
233 }
234 
IsWaiting() const235 bool MediaLibraryRestore::IsWaiting() const
236 {
237     return isWaiting_;
238 }
239 } // namespace Media
240 } // namespace OHOS
241