• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 <mutex>
17 #include <chrono>
18 #include <unistd.h>
19 #include "rdb_adapter.h"
20 #include "rdb_errno.h"
21 #include "distributed_device_profile_constants.h"
22 #include "distributed_device_profile_errors.h"
23 #include "distributed_device_profile_log.h"
24 
25 namespace OHOS {
26 namespace DistributedDeviceProfile {
27 using namespace std::chrono_literals;
28 namespace {
29     const std::set<std::string> TABLES = {
30         "trust_device_table",
31         "accesser_table",
32         "accessee_table",
33         "access_control_table",
34         "subscribe_trust_info_table"
35     };
36     const std::string TAG = "rdbAdapter";
37 }
38 
RdbAdapter()39 RdbAdapter::RdbAdapter()
40 {
41     HILOGI("rdbAdapter constructor");
42 }
43 
~RdbAdapter()44 RdbAdapter::~RdbAdapter()
45 {
46     HILOGI("rdbAdapter destructor");
47 }
48 
Init()49 int32_t RdbAdapter::Init()
50 {
51     int32_t retryTimes = RDB_INIT_MAX_TIMES;
52     while (retryTimes > 0) {
53         if (GetRDBPtr() == DP_SUCCESS) {
54             HILOGI("rdbAdapter init success");
55             return DP_SUCCESS;
56         }
57         usleep(RDB_INIT_INTERVAL_TIME);
58         retryTimes--;
59     }
60     HILOGE("rdbAdapter init failed");
61     return DP_RDBADAPTER_INIT_FAIL;
62 }
63 
UnInit()64 int32_t RdbAdapter::UnInit()
65 {
66     HILOGI("rdbAdapter unInit");
67     {
68         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
69         store_ = nullptr;
70     }
71     return DP_SUCCESS;
72 }
73 
Put(int64_t & outRowId,const std::string & table,const ValuesBucket & values)74 int32_t RdbAdapter::Put(int64_t& outRowId, const std::string& table, const ValuesBucket& values)
75 {
76     if (TABLES.find(table) == TABLES.end()) {
77         HILOGE("table does not exist");
78         return DP_RDBADAPTER_TABLE_NOT_EXIST;
79     }
80     {
81         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
82         if (store_ == nullptr) {
83             HILOGE("RDBStore_ is null");
84             return DP_RDB_DB_PTR_NULL;
85         }
86         int32_t ret = store_->Insert(outRowId, table, values);
87         if (ret == E_SQLITE_CORRUPT) {
88             HILOGE("database corrupt ret:%{public}d", ret);
89             int32_t restoreRet = store_->Restore("");
90             if (restoreRet != E_OK) {
91                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
92                 return DP_RDB_DATABASE_RESTORE_FAIL;
93             }
94             ret = store_->Insert(outRowId, table, values);
95         }
96         if (ret != E_OK) {
97             HILOGE("rdbAdapter put failed ret:%{public}d", ret);
98             return DP_RDBADAPTER_PUT_FAIL;
99         }
100     }
101     return DP_SUCCESS;
102 }
103 
Delete(int32_t & deleteRows,const std::string & table,const std::string & whereClause,const std::vector<ValueObject> & bindArgs)104 int32_t RdbAdapter::Delete(int32_t& deleteRows, const std::string& table, const std::string& whereClause,
105     const std::vector<ValueObject>& bindArgs)
106 {
107     if (TABLES.find(table) == TABLES.end()) {
108         HILOGE("table does not exist");
109         return DP_RDBADAPTER_TABLE_NOT_EXIST;
110     }
111     {
112         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
113         if (store_ == nullptr) {
114             HILOGE("RDBStore_ is null");
115             return DP_RDB_DB_PTR_NULL;
116         }
117         int32_t ret = store_->Delete(deleteRows, table, whereClause, bindArgs);
118         if (ret == E_SQLITE_CORRUPT) {
119             HILOGE("database corrupt ret:%{public}d", ret);
120             int32_t restoreRet = store_->Restore("");
121             if (restoreRet != E_OK) {
122                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
123                 return DP_RDB_DATABASE_RESTORE_FAIL;
124             }
125             ret = store_->Delete(deleteRows, table, whereClause, bindArgs);
126         }
127         if (ret != E_OK) {
128             HILOGE("rdbAdapter delete failed ret:%{public}d", ret);
129             return DP_RDBADAPTER_DELETE_FAIL;
130         }
131     }
132     return DP_SUCCESS;
133 }
134 
Update(int32_t & changedRows,const std::string & table,const ValuesBucket & values,const std::string & whereClause,const std::vector<ValueObject> & bindArgs)135 int32_t RdbAdapter::Update(int32_t& changedRows, const std::string& table, const ValuesBucket& values,
136     const std::string& whereClause, const std::vector<ValueObject>& bindArgs)
137 {
138     if (TABLES.find(table) == TABLES.end()) {
139         HILOGE("table does not exist");
140         return DP_RDBADAPTER_TABLE_NOT_EXIST;
141     }
142     {
143         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
144         if (store_ == nullptr) {
145             HILOGE("RDBStore_ is null");
146             return DP_RDB_DB_PTR_NULL;
147         }
148         int32_t ret = store_->Update(changedRows, table, values, whereClause, bindArgs);
149         if (ret == E_SQLITE_CORRUPT) {
150             HILOGE("database corrupt ret:%{public}d", ret);
151             int32_t restoreRet = store_->Restore("");
152             if (restoreRet != E_OK) {
153                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
154                 return DP_RDB_DATABASE_RESTORE_FAIL;
155             }
156             ret = store_->Update(changedRows, table, values, whereClause, bindArgs);
157         }
158         if (ret != E_OK) {
159             HILOGE("rdbAdapter update failed ret:%{public}d", ret);
160             return DP_RDBADAPTER_UPDATE_FAIL;
161         }
162     }
163     return DP_SUCCESS;
164 }
165 
Get(const std::string & sql,const std::vector<ValueObject> & args)166 std::shared_ptr<ResultSet> RdbAdapter::Get(const std::string& sql, const std::vector<ValueObject>& args)
167 {
168     std::shared_ptr<ResultSet> resultSet = nullptr;
169     {
170         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
171         if (store_ == nullptr) {
172             HILOGE("RDBStore_ is null");
173             return nullptr;
174         }
175         resultSet = store_->QueryByStep(sql, args);
176         if (resultSet == nullptr) {
177             HILOGE("resultSet is null");
178             return nullptr;
179         }
180         int32_t rowCount = ROWCOUNT_INIT;
181         int32_t ret = resultSet->GetRowCount(rowCount);
182         if (ret == E_SQLITE_CORRUPT) {
183             HILOGE("database corrupt ret:%{public}d", ret);
184             resultSet->Close();
185             ret = store_->Restore("");
186             if (ret != E_OK) {
187                 HILOGE("Restore failed ret:%{public}d", ret);
188                 return nullptr;
189             }
190             resultSet = store_->QueryByStep(sql, args);
191         }
192     }
193     return resultSet;
194 }
195 
GetRDBPtr()196 int32_t RdbAdapter::GetRDBPtr()
197 {
198     int32_t version = RDB_VERSION_5_1_2;
199     OpenCallback helper;
200     RdbStoreConfig config(RDB_PATH + DATABASE_NAME);
201     config.SetHaMode(HAMode::MAIN_REPLICA);
202     config.SetAllowRebuild(true);
203     int32_t errCode = E_OK;
204     {
205         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
206         store_ = RdbHelper::GetRdbStore(config, version, helper, errCode);
207         if (store_ == nullptr) {
208             HILOGE("RDBStore_ is null");
209             return DP_RDB_DB_PTR_NULL;
210         }
211         NativeRdb::RebuiltType rebuiltType = NativeRdb::RebuiltType::NONE;
212         errCode = store_->GetRebuilt(rebuiltType);
213         if (errCode != E_OK) {
214             HILOGE("getRDBPtr failed errCode:%{public}d", errCode);
215             return DP_GET_RDBSTORE_FAIL;
216         }
217         if (rebuiltType == NativeRdb::RebuiltType::REBUILT) {
218             HILOGE("database corrupt");
219             int32_t restoreRet = store_->Restore("");
220             if (restoreRet != E_OK) {
221                 HILOGE("Restore failed restoreRet:%{public}d", restoreRet);
222                 return DP_RDB_DATABASE_RESTORE_FAIL;
223             }
224         }
225     }
226     return DP_SUCCESS;
227 }
228 
CreateTable(const std::string & sql)229 int32_t RdbAdapter::CreateTable(const std::string& sql)
230 {
231     {
232         std::lock_guard<std::mutex> lock(rdbAdapterMtx_);
233         if (store_ == nullptr) {
234             HILOGE("RDBStore_ is null");
235             return DP_RDB_DB_PTR_NULL;
236         }
237         if (store_->ExecuteSql(sql) != E_OK) {
238             HILOGE("rdbAdapter create table failed");
239             return DP_RDBADAPTER_CREATE_TABLE_FAIL;
240         }
241     }
242     return DP_SUCCESS;
243 }
244 
OnCreate(RdbStore & store)245 int32_t OpenCallback::OnCreate(RdbStore& store)
246 {
247     HILOGI("rdbStore create");
248     if (CreateTable(store) != DP_SUCCESS) {
249         HILOGE("CreateTable failed");
250         return DP_CREATE_TABLE_FAIL;
251     }
252     if (CreateUniqueIndex(store) != DP_SUCCESS) {
253         HILOGE("CreateUniqueIndex failed");
254         return DP_CREATE_UNIQUE_INDEX_FAIL;
255     }
256     return DP_SUCCESS;
257 }
258 
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)259 int32_t OpenCallback::OnUpgrade(RdbStore& store, int oldVersion, int newVersion)
260 {
261     HILOGI("rdbStore upgrade, oldVersion : %{public}d, newVersion : %{public}d", oldVersion, newVersion);
262     if (oldVersion == RDB_VERSION && newVersion == RDB_VERSION_5_1) {
263         UpgradeVersionOneToTwo(store);
264     }
265     if (oldVersion == RDB_VERSION_5_1 && newVersion == RDB_VERSION_5_1_2) {
266         UpgradeVersionTwoToThree(store);
267     }
268     if (oldVersion == RDB_VERSION && newVersion == RDB_VERSION_5_1_2) {
269         UpgradeVersionOneToTwo(store);
270         UpgradeVersionTwoToThree(store);
271     }
272     return DP_SUCCESS;
273 }
274 
CreateTable(RdbStore & store)275 int32_t OpenCallback::CreateTable(RdbStore& store)
276 {
277     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
278     if (store.ExecuteSql(CREATE_TURST_DEVICE_TABLE_SQL) != NativeRdb::E_OK) {
279         HILOGE("trustdevice_table create failed");
280         return DP_CREATE_TABLE_FAIL;
281     }
282     if (store.ExecuteSql(CREATE_ACCESS_CONTROL_TABLE_SQL) != NativeRdb::E_OK) {
283         HILOGE("accesscontrol_table create failed");
284         return DP_CREATE_TABLE_FAIL;
285     }
286     if (store.ExecuteSql(CREATE_ACCESSER_TABLE_SQL) != NativeRdb::E_OK) {
287         HILOGE("accessertable create failed");
288         return DP_CREATE_TABLE_FAIL;
289     }
290     if (store.ExecuteSql(CREATE_ACCESSEE_TABLE_SQL) != NativeRdb::E_OK) {
291         HILOGE("accesseetable create failed");
292         return DP_CREATE_TABLE_FAIL;
293     }
294     return DP_SUCCESS;
295 }
296 
CreateUniqueIndex(RdbStore & store)297 int32_t OpenCallback::CreateUniqueIndex(RdbStore& store)
298 {
299     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
300     if (store.ExecuteSql(CREATE_TURST_DEVICE_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
301         HILOGE("trustdevice_table unique index create failed");
302         return DP_CREATE_UNIQUE_INDEX_FAIL;
303     }
304     if (store.ExecuteSql(CREATE_ACCESS_CONTROL_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
305         HILOGE("accesscontrol_table unique index create failed");
306         return DP_CREATE_UNIQUE_INDEX_FAIL;
307     }
308     if (store.ExecuteSql(CREATE_ACCESSER_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
309         HILOGE("accessertable unique index create failed");
310         return DP_CREATE_UNIQUE_INDEX_FAIL;
311     }
312     if (store.ExecuteSql(CREATE_ACCESSEE_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
313         HILOGE("accesseetable unique index create failed");
314         return DP_CREATE_UNIQUE_INDEX_FAIL;
315     }
316     return DP_SUCCESS;
317 }
318 
UpgradeVersionOneToTwo(RdbStore & store)319 int32_t OpenCallback::UpgradeVersionOneToTwo(RdbStore& store)
320 {
321     int32_t ret = AddAcerColumnOneToTwo(store);
322     if (ret != DP_SUCCESS) {
323         HILOGE("AddAcerColumn failed");
324         return ret;
325     }
326     ret = AddAceeColumnOneToTwo(store);
327     if (ret != DP_SUCCESS) {
328         HILOGE("AddAceeColumn failed");
329         return ret;
330     }
331     ret = DropAndRebuildIndex(store);
332     if (ret != DP_SUCCESS) {
333         HILOGE("DropAndRebuildIndex failed");
334         return ret;
335     }
336     return DP_SUCCESS;
337 }
338 
UpgradeVersionTwoToThree(RdbStore & store)339 int32_t OpenCallback::UpgradeVersionTwoToThree(RdbStore &store)
340 {
341     int32_t ret = AddAclColumnTwoToThree(store);
342     if (ret != DP_SUCCESS) {
343         HILOGE("AddAclColumnTwoToThree failed");
344         return ret;
345     }
346     ret = AddAcerColumnTwoToThree(store);
347     if (ret != DP_SUCCESS) {
348         HILOGE("AddAcerColumnTwoToThree failed");
349         return ret;
350     }
351     ret = AddAceeColumnTwoToThree(store);
352     if (ret != DP_SUCCESS) {
353         HILOGE("AddAceeColumnTwoToThree failed");
354         return ret;
355     }
356     ret = DropAndRebuildIndex(store);
357     if (ret != DP_SUCCESS) {
358         HILOGE("DropAndRebuildIndex failed");
359         return ret;
360     }
361     return DP_SUCCESS;
362 }
363 
AddAcerColumnOneToTwo(RdbStore & store)364 int32_t OpenCallback::AddAcerColumnOneToTwo(RdbStore& store)
365 {
366     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
367     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_DEVICE_NAME) != NativeRdb::E_OK) {
368         HILOGE("add column accesserDeviceName to acer table failed");
369         return DP_CREATE_TABLE_FAIL;
370     }
371     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_SERVICE_NAME) != NativeRdb::E_OK) {
372         HILOGE("add column accesserServiceName to acer table failed");
373         return DP_CREATE_TABLE_FAIL;
374     }
375     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_CREDENTIAL_ID) != NativeRdb::E_OK) {
376         HILOGE("add column accesserCredentialId to acer table failed");
377         return DP_CREATE_TABLE_FAIL;
378     }
379     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_STATUS) != NativeRdb::E_OK) {
380         HILOGE("add column accesserStatus to acer table failed");
381         return DP_CREATE_TABLE_FAIL;
382     }
383     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_SESSION_KEY_ID) != NativeRdb::E_OK) {
384         HILOGE("add column accesserSessionKeyId to acer table failed");
385         return DP_CREATE_TABLE_FAIL;
386     }
387     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_SESSION_KEY_TIMESTAMP) != NativeRdb::E_OK) {
388         HILOGE("add column accesserSessionKeyTimeStamp to acer table failed");
389         return DP_CREATE_TABLE_FAIL;
390     }
391     return DP_SUCCESS;
392 }
393 
AddAceeColumnOneToTwo(RdbStore & store)394 int32_t OpenCallback::AddAceeColumnOneToTwo(RdbStore &store)
395 {
396     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
397     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_DEVICE_NAME) != NativeRdb::E_OK) {
398         HILOGE("add column accesseeDeviceName to acee table failed");
399         return DP_CREATE_TABLE_FAIL;
400     }
401     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_SERVICE_NAME) != NativeRdb::E_OK) {
402         HILOGE("add column accesseeServiceName to acee table failed");
403         return DP_CREATE_TABLE_FAIL;
404     }
405     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_CREDENTIAL_ID) != NativeRdb::E_OK) {
406         HILOGE("add column accesseeCredentialId to acee table failed");
407         return DP_CREATE_TABLE_FAIL;
408     }
409     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_STATUS) != NativeRdb::E_OK) {
410         HILOGE("add column accesseeStatus to acee table failed");
411         return DP_CREATE_TABLE_FAIL;
412     }
413     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_SESSION_KEY_ID) != NativeRdb::E_OK) {
414         HILOGE("add column accesseeSessionKeyId to acee table failed");
415         return DP_CREATE_TABLE_FAIL;
416     }
417     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_SESSION_KEY_TIMESTAMP) != NativeRdb::E_OK) {
418         HILOGE("add column accesseeSessionKeyTimeStamp to acee table failed");
419         return DP_CREATE_TABLE_FAIL;
420     }
421     return DP_SUCCESS;
422 }
423 
AddAclColumnTwoToThree(RdbStore & store)424 int32_t OpenCallback::AddAclColumnTwoToThree(RdbStore &store)
425 {
426     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
427     if (store.ExecuteSql(ALTER_TABLE_ACCESS_CONTROL_ADD_COLUMN_EXTRA_DATA) != NativeRdb::E_OK) {
428         HILOGE("add column extraData to acL table failed");
429         return DP_CREATE_TABLE_FAIL;
430     }
431     return DP_SUCCESS;
432 }
433 
AddAcerColumnTwoToThree(RdbStore & store)434 int32_t OpenCallback::AddAcerColumnTwoToThree(RdbStore &store)
435 {
436     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
437     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_CREDENTIAL_ID_STR) != NativeRdb::E_OK) {
438         HILOGE("add column accesserCredentialIdStr to acer table failed");
439         return DP_CREATE_TABLE_FAIL;
440     }
441     if (store.ExecuteSql(ALTER_TABLE_ACER_ADD_COLUMN_ACER_EXTRA_DATA) != NativeRdb::E_OK) {
442         HILOGE("add column accesserExtraData to acer table failed");
443         return DP_CREATE_TABLE_FAIL;
444     }
445     return DP_SUCCESS;
446 }
447 
AddAceeColumnTwoToThree(RdbStore & store)448 int32_t OpenCallback::AddAceeColumnTwoToThree(RdbStore &store)
449 {
450     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
451     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_CREDENTIAL_ID_STR) != NativeRdb::E_OK) {
452         HILOGE("add column accesseeCredentialIdStr to acee table failed");
453         return DP_CREATE_TABLE_FAIL;
454     }
455     if (store.ExecuteSql(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_EXTRA_DATA) != NativeRdb::E_OK) {
456         HILOGE("add column accesseeExtraData to acee table failed");
457         return DP_CREATE_TABLE_FAIL;
458     }
459     return DP_SUCCESS;
460 }
461 
DropAndRebuildIndex(RdbStore & store)462 int32_t OpenCallback::DropAndRebuildIndex(RdbStore& store)
463 {
464     std::lock_guard<std::mutex> lock(rdbStoreMtx_);
465     if (store.ExecuteSql(DROP_OLD_UNIQUE_INDEX_ON_ACER) != NativeRdb::E_OK) {
466         HILOGE("delete old unique index to acer table failed");
467         return DP_CREATE_TABLE_FAIL;
468     }
469     if (store.ExecuteSql(DROP_OLD_UNIQUE_INDEX_ON_ACEE) != NativeRdb::E_OK) {
470         HILOGE("delete old unique index to acee table failed");
471         return DP_CREATE_TABLE_FAIL;
472     }
473     if (store.ExecuteSql(CREATE_ACCESSER_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
474         HILOGE("create new unique index to acer table failed");
475         return DP_CREATE_TABLE_FAIL;
476     }
477     if (store.ExecuteSql(CREATE_ACCESSEE_TABLE_UNIQUE_INDEX_SQL) != NativeRdb::E_OK) {
478         HILOGE("create new unique index to acee table failed");
479         return DP_CREATE_TABLE_FAIL;
480     }
481     return DP_SUCCESS;
482 }
483 } // namespace DistributedDeviceProfile
484 } // namespace OHOS
485