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 #define LOG_TAG "KVDBExporter"
16 #include "kvdb_exporter.h"
17
18 #include "backup_manager.h"
19 #include "dfx/reporter.h"
20 #include "directory/directory_manager.h"
21 #include "kvdb_general_store.h"
22 #include "log_print.h"
23 #include "utils/anonymous.h"
24 namespace OHOS::DistributedKv {
25 using namespace OHOS::DistributedData;
26 using namespace OHOS::DistributedDataDfx;
27 __attribute__((used)) KVDBExporter KVDBExporter::instance_;
28
Exporter(const StoreMetaData & meta,const std::string & backupPath,bool & result)29 void KVDBExporter::Exporter(const StoreMetaData &meta, const std::string &backupPath, bool &result)
30 {
31 DBManager manager(meta.appId, meta.user);
32 auto path = DirectoryManager::GetInstance().GetStorePath(meta);
33 manager.SetKvStoreConfig({ path });
34 auto dbPassword = KVDBGeneralStore::GetDBPassword(meta);
35 auto dbOption = KVDBGeneralStore::GetDBOption(meta, dbPassword);
36
37 manager.GetKvStore(meta.storeId, dbOption, [&manager, &backupPath, &dbPassword, &result]
38 (DistributedDB::DBStatus dbStatus, DistributedDB::KvStoreNbDelegate *delegate) {
39 if (delegate == nullptr) {
40 ZLOGE("Auto backup delegate is null");
41 result = false;
42 return;
43 }
44 dbStatus = delegate->CheckIntegrity();
45 if (dbStatus != DistributedDB::DBStatus::OK) {
46 ZLOGE("CheckIntegrity fail, dbStatus:%{public}d, backupPath:%{public}s", dbStatus,
47 Anonymous::Change(backupPath).c_str());
48 result = false;
49 return;
50 }
51 dbStatus = delegate->Export(backupPath, dbPassword);
52 result = (dbStatus == DistributedDB::DBStatus::OK) ? true : false;
53 manager.CloseKvStore(delegate);
54 });
55 std::string message;
56 message.append(" backup name [")
57 .append(backupPath)
58 .append("], isEncrypt [")
59 .append(std::to_string(meta.isEncrypt))
60 .append("]")
61 .append("], backup result [")
62 .append(std::to_string(result))
63 .append("]");
64 Reporter::GetInstance()->GetBehaviourReporter()->Report(
65 { meta.account, meta.appId, Anonymous::Change(meta.storeId), BehaviourType::DATABASE_BACKUP, message });
66 }
67
KVDBExporter()68 KVDBExporter::KVDBExporter() noexcept
69 {
70 BackupManager::GetInstance().RegisterExporter(KvStoreType::SINGLE_VERSION, Exporter);
71 BackupManager::GetInstance().RegisterExporter(KvStoreType::DEVICE_COLLABORATION, Exporter);
72 }
73 } // namespace OHOS::DistributedKv
74