1 /*
2 * Copyright (c) 2021 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 "RdbStoreConfig"
16 #include "rdb_store_config.h"
17
18 #include <sstream>
19
20 #include "logger.h"
21 #include "rdb_errno.h"
22 #include "rdb_security_manager.h"
23 #include "string_utils.h"
24 #include "sqlite_global_config.h"
25 #include "sqlite_utils.h"
26 #include "rdb_fault_hiview_reporter.h"
27
28 namespace OHOS::NativeRdb {
29 using namespace OHOS::Rdb;
30
RdbStoreConfig(const std::string & name,StorageMode storageMode,bool isReadOnly,const std::vector<uint8_t> & encryptKey,const std::string & journalMode,const std::string & syncMode,const std::string & databaseFileType,SecurityLevel securityLevel,bool isCreateNecessary,bool autoCheck,int journalSize,int pageSize)31 RdbStoreConfig::RdbStoreConfig(const std::string &name, StorageMode storageMode, bool isReadOnly,
32 const std::vector<uint8_t> &encryptKey, const std::string &journalMode, const std::string &syncMode,
33 const std::string &databaseFileType, SecurityLevel securityLevel, bool isCreateNecessary, bool autoCheck,
34 int journalSize, int pageSize)
35 : readOnly_(isReadOnly), isCreateNecessary_(isCreateNecessary), autoCheck_(autoCheck), journalSize_(journalSize),
36 pageSize_(pageSize), securityLevel_(securityLevel), storageMode_(storageMode), path_(name),
37 journalMode_(journalMode), syncMode_(syncMode), databaseFileType(databaseFileType)
38 {
39 name_ = StringUtils::ExtractFileName(name);
40 cryptoParam_.encryptKey_ = encryptKey;
41 walLimitSize_ = GlobalExpr::DB_WAL_DEFAULT_SIZE;
42 checkpointSize_ = GlobalExpr::DB_WAL_WARNING_SIZE;
43 startCheckpointSize_ = GlobalExpr::DB_WAL_SIZE_LIMIT_MIN;
44 clearMemorySize_ = GlobalExpr::CLEAR_MEMORY_SIZE;
45 }
46
~RdbStoreConfig()47 RdbStoreConfig::~RdbStoreConfig()
48 {
49 ClearEncryptKey();
50 }
51
52 /**
53 * Obtains the database name.
54 */
GetName() const55 std::string RdbStoreConfig::GetName() const
56 {
57 return name_;
58 }
59
60 /**
61 * Obtains the database path.
62 */
GetPath() const63 std::string RdbStoreConfig::GetPath() const
64 {
65 return path_;
66 }
67
68 /**
69 * Obtains the storage mode.
70 */
GetStorageMode() const71 StorageMode RdbStoreConfig::GetStorageMode() const
72 {
73 return storageMode_;
74 }
75
76 /**
77 * Obtains the journal mode in this {@code StoreConfig} object.
78 */
GetJournalMode() const79 std::string RdbStoreConfig::GetJournalMode() const
80 {
81 return journalMode_;
82 }
83
84 /**
85 * Obtains the synchronization mode in this {@code StoreConfig} object.
86 */
GetSyncMode() const87 std::string RdbStoreConfig::GetSyncMode() const
88 {
89 return syncMode_;
90 }
91
92 /**
93 * Checks whether the database is read-only.
94 */
IsReadOnly() const95 bool RdbStoreConfig::IsReadOnly() const
96 {
97 return readOnly_;
98 }
99
100 /**
101 * Checks whether the database is memory.
102 */
IsMemoryRdb() const103 bool RdbStoreConfig::IsMemoryRdb() const
104 {
105 return GetStorageMode() == StorageMode::MODE_MEMORY;
106 }
107
108 /**
109 * Obtains the database file type in this {@code StoreConfig} object.
110 */
GetDatabaseFileType() const111 std::string RdbStoreConfig::GetDatabaseFileType() const
112 {
113 return databaseFileType;
114 }
115
SetName(std::string name)116 void RdbStoreConfig::SetName(std::string name)
117 {
118 this->name_ = std::move(name);
119 }
120
121 /**
122 * Sets the journal mode for the object.
123 */
SetJournalMode(JournalMode journalMode)124 void RdbStoreConfig::SetJournalMode(JournalMode journalMode)
125 {
126 this->journalMode_ = GetJournalModeValue(journalMode);
127 }
128
SetJournalMode(const std::string & journalMode)129 void RdbStoreConfig::SetJournalMode(const std::string &journalMode)
130 {
131 this->journalMode_ = journalMode;
132 }
133
SetDatabaseFileType(DatabaseFileType type)134 void RdbStoreConfig::SetDatabaseFileType(DatabaseFileType type)
135 {
136 this->databaseFileType = GetDatabaseFileTypeValue(type);
137 }
138
139 /**
140 * Sets the path for the object.
141 */
SetPath(std::string path)142 void RdbStoreConfig::SetPath(std::string path)
143 {
144 this->path_ = std::move(path);
145 }
146
SetStorageMode(StorageMode storageMode)147 void RdbStoreConfig::SetStorageMode(StorageMode storageMode)
148 {
149 this->storageMode_ = storageMode;
150 }
151
IsAutoCheck() const152 bool RdbStoreConfig::IsAutoCheck() const
153 {
154 return autoCheck_;
155 }
SetAutoCheck(bool autoCheck)156 void RdbStoreConfig::SetAutoCheck(bool autoCheck)
157 {
158 this->autoCheck_ = autoCheck;
159 }
GetJournalSize() const160 int RdbStoreConfig::GetJournalSize() const
161 {
162 return journalSize_;
163 }
SetJournalSize(int journalSize)164 void RdbStoreConfig::SetJournalSize(int journalSize)
165 {
166 this->journalSize_ = journalSize;
167 }
GetPageSize() const168 int RdbStoreConfig::GetPageSize() const
169 {
170 return pageSize_;
171 }
SetPageSize(int pageSize)172 void RdbStoreConfig::SetPageSize(int pageSize)
173 {
174 this->pageSize_ = pageSize;
175 }
GetEncryptAlgo() const176 EncryptAlgo RdbStoreConfig::GetEncryptAlgo() const
177 {
178 return static_cast<EncryptAlgo>(cryptoParam_.encryptAlgo);
179 }
SetEncryptAlgo(EncryptAlgo encryptAlgo)180 void RdbStoreConfig::SetEncryptAlgo(EncryptAlgo encryptAlgo)
181 {
182 this->cryptoParam_.encryptAlgo = static_cast<int32_t>(encryptAlgo);
183 }
184
SetReadOnly(bool readOnly)185 void RdbStoreConfig::SetReadOnly(bool readOnly)
186 {
187 this->readOnly_ = readOnly;
188 }
189
SetDistributedType(DistributedType type)190 int RdbStoreConfig::SetDistributedType(DistributedType type)
191 {
192 if (type != DistributedType::RDB_DEVICE_COLLABORATION) {
193 LOG_ERROR("type is invalid.");
194 return E_ERROR;
195 }
196 distributedType_ = type;
197 return E_OK;
198 }
199
GetDistributedType() const200 DistributedType RdbStoreConfig::GetDistributedType() const
201 {
202 return distributedType_;
203 }
204
SetBundleName(const std::string & bundleName)205 int RdbStoreConfig::SetBundleName(const std::string &bundleName)
206 {
207 if (bundleName.empty()) {
208 LOG_ERROR("bundleName is empty.");
209 return E_ERROR;
210 }
211 bundleName_ = bundleName;
212 return E_OK;
213 }
214
GetBundleName() const215 std::string RdbStoreConfig::GetBundleName() const
216 {
217 return bundleName_;
218 }
219
SetModuleName(const std::string & moduleName)220 void RdbStoreConfig::SetModuleName(const std::string &moduleName)
221 {
222 moduleName_ = moduleName;
223 }
224
GetModuleName() const225 std::string RdbStoreConfig::GetModuleName() const
226 {
227 return moduleName_;
228 }
229
SetServiceName(const std::string & serviceName)230 void RdbStoreConfig::SetServiceName(const std::string &serviceName)
231 {
232 SetBundleName(serviceName);
233 }
234
SetArea(int32_t area)235 void RdbStoreConfig::SetArea(int32_t area)
236 {
237 area_ = area + 1;
238 }
239
GetArea() const240 int32_t RdbStoreConfig::GetArea() const
241 {
242 return area_;
243 }
244
GetJournalModeValue(JournalMode journalMode)245 std::string RdbStoreConfig::GetJournalModeValue(JournalMode journalMode)
246 {
247 std::string value = "";
248
249 switch (journalMode) {
250 case JournalMode::MODE_DELETE:
251 return "DELETE";
252 case JournalMode::MODE_TRUNCATE:
253 return "TRUNCATE";
254 case JournalMode::MODE_PERSIST:
255 return "PERSIST";
256 case JournalMode::MODE_MEMORY:
257 return "MEMORY";
258 case JournalMode::MODE_WAL:
259 return "WAL";
260 case JournalMode::MODE_OFF:
261 return "OFF";
262 default:
263 break;
264 }
265 return value;
266 }
267
GetSyncModeValue(SyncMode syncMode)268 std::string RdbStoreConfig::GetSyncModeValue(SyncMode syncMode)
269 {
270 std::string value = "";
271 switch (syncMode) {
272 case SyncMode::MODE_OFF:
273 return "MODE_OFF";
274 case SyncMode::MODE_NORMAL:
275 return "MODE_NORMAL";
276 case SyncMode::MODE_FULL:
277 return "MODE_FULL";
278 case SyncMode::MODE_EXTRA:
279 return "MODE_EXTRA";
280 default:
281 break;
282 }
283
284 return value;
285 }
286
GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)287 std::string RdbStoreConfig::GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)
288 {
289 std::string value = "";
290 switch (databaseFileType) {
291 case DatabaseFileType::NORMAL:
292 return "db";
293 case DatabaseFileType::BACKUP:
294 return "backup";
295 case DatabaseFileType::CORRUPT:
296 return "corrupt";
297 default:
298 break;
299 }
300
301 return value;
302 }
303
SetSecurityLevel(SecurityLevel sl)304 void RdbStoreConfig::SetSecurityLevel(SecurityLevel sl)
305 {
306 securityLevel_ = sl;
307 }
308
GetSecurityLevel() const309 SecurityLevel RdbStoreConfig::GetSecurityLevel() const
310 {
311 return securityLevel_;
312 }
313
SetEncryptStatus(const bool status)314 void RdbStoreConfig::SetEncryptStatus(const bool status)
315 {
316 this->isEncrypt_ = status;
317 }
318
IsEncrypt() const319 bool RdbStoreConfig::IsEncrypt() const
320 {
321 return isEncrypt_ || !cryptoParam_.encryptKey_.empty();
322 }
323
IsCreateNecessary() const324 bool RdbStoreConfig::IsCreateNecessary() const
325 {
326 return isCreateNecessary_;
327 }
328
SetCreateNecessary(bool isCreateNecessary)329 void RdbStoreConfig::SetCreateNecessary(bool isCreateNecessary)
330 {
331 isCreateNecessary_ = isCreateNecessary;
332 }
333
GetReadConSize() const334 int RdbStoreConfig::GetReadConSize() const
335 {
336 return readConSize_;
337 }
338
SetReadConSize(int readConSize)339 void RdbStoreConfig::SetReadConSize(int readConSize)
340 {
341 readConSize_ = readConSize;
342 }
343
SetEncryptKey(const std::vector<uint8_t> & encryptKey)344 void RdbStoreConfig::SetEncryptKey(const std::vector<uint8_t> &encryptKey)
345 {
346 cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
347 cryptoParam_.encryptKey_ = encryptKey;
348 }
349
RestoreEncryptKey(const std::vector<uint8_t> & encryptKey) const350 void RdbStoreConfig::RestoreEncryptKey(const std::vector<uint8_t> &encryptKey) const
351 {
352 RdbSecurityManager::GetInstance().RestoreKeyFile(GetPath(), encryptKey);
353 cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
354 newEncryptKey_.assign(newEncryptKey_.size(), 0);
355 cryptoParam_.encryptKey_ = encryptKey;
356 }
357
GetEncryptKey() const358 std::vector<uint8_t> RdbStoreConfig::GetEncryptKey() const
359 {
360 return cryptoParam_.encryptKey_;
361 }
362
ChangeEncryptKey() const363 void RdbStoreConfig::ChangeEncryptKey() const
364 {
365 RdbSecurityManager::GetInstance().ChangeKeyFile(GetPath());
366 if (newEncryptKey_.empty()) {
367 return;
368 }
369 cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
370 cryptoParam_.encryptKey_.assign(newEncryptKey_.data(), newEncryptKey_.data() + newEncryptKey_.size());
371 newEncryptKey_.assign(newEncryptKey_.size(), 0);
372 newEncryptKey_.resize(0);
373 }
374
GetNewEncryptKey() const375 std::vector<uint8_t> RdbStoreConfig::GetNewEncryptKey() const
376 {
377 return newEncryptKey_;
378 }
379
Initialize() const380 int32_t RdbStoreConfig::Initialize() const
381 {
382 return GenerateEncryptedKey();
383 }
384
GenerateEncryptedKey() const385 int32_t RdbStoreConfig::GenerateEncryptedKey() const
386 {
387 if (!isEncrypt_ || !cryptoParam_.encryptKey_.empty()) {
388 return E_OK;
389 }
390
391 auto name = bundleName_;
392 if (name.empty()) {
393 name = std::string(path_).substr(0, path_.rfind("/") + 1);
394 }
395 using KeyFileType = RdbSecurityManager::KeyFileType;
396 auto errCode = RdbSecurityManager::GetInstance().Init(name);
397 if (errCode != E_OK) {
398 RdbFaultHiViewReporter::ReportFault(RdbFaultDbFileEvent(FT_OPEN, E_ROOT_KEY_FAULT, *this,
399 "gen root key fail ret=" + std::to_string(errCode)));
400 LOG_ERROR("generate root encrypt key failed, bundleName_:%{public}s", bundleName_.c_str());
401 return errCode;
402 }
403 auto rdbPwd = RdbSecurityManager::GetInstance().GetRdbPassword(path_, KeyFileType::PUB_KEY_FILE);
404 if (rdbPwd.IsValid()) {
405 cryptoParam_.encryptKey_ = std::vector<uint8_t>(rdbPwd.GetData(), rdbPwd.GetData() + rdbPwd.GetSize());
406 }
407 rdbPwd.Clear();
408 if ((rdbPwd.isKeyExpired && autoRekey_) ||
409 RdbSecurityManager::GetInstance().IsKeyFileExists(path_, KeyFileType::PUB_KEY_FILE_NEW_KEY)) {
410 auto rdbNewPwd = RdbSecurityManager::GetInstance().GetRdbPassword(path_, KeyFileType::PUB_KEY_FILE_NEW_KEY);
411 if (rdbNewPwd.IsValid()) {
412 newEncryptKey_ = std::vector<uint8_t>(rdbNewPwd.GetData(), rdbNewPwd.GetData() + rdbNewPwd.GetSize());
413 }
414 rdbPwd.Clear();
415 }
416 if (cryptoParam_.encryptKey_.empty() && newEncryptKey_.empty()) {
417 LOG_WARN("key is inValid, bundleName_:%{public}s", bundleName_.c_str());
418 }
419 return E_OK;
420 }
421
ClearEncryptKey()422 void RdbStoreConfig::ClearEncryptKey()
423 {
424 cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
425 newEncryptKey_.assign(newEncryptKey_.size(), 0);
426 }
427
SetScalarFunction(const std::string & functionName,int argc,ScalarFunction function)428 void RdbStoreConfig::SetScalarFunction(const std::string &functionName, int argc, ScalarFunction function)
429 {
430 customScalarFunctions_.try_emplace(functionName, ScalarFunctionInfo(function, argc));
431 }
432
SetScalarFunctions(const std::map<std::string,ScalarFunctionInfo> functions)433 void RdbStoreConfig::SetScalarFunctions(const std::map<std::string, ScalarFunctionInfo> functions)
434 {
435 customScalarFunctions_ = functions;
436 }
437
GetScalarFunctions() const438 std::map<std::string, ScalarFunctionInfo> RdbStoreConfig::GetScalarFunctions() const
439 {
440 return customScalarFunctions_;
441 }
442
SetDataGroupId(const std::string & DataGroupId)443 void RdbStoreConfig::SetDataGroupId(const std::string &DataGroupId)
444 {
445 dataGroupId_ = DataGroupId;
446 }
447
GetDataGroupId() const448 std::string RdbStoreConfig::GetDataGroupId() const
449 {
450 return dataGroupId_;
451 }
452
SetAutoClean(bool isAutoClean)453 void RdbStoreConfig::SetAutoClean(bool isAutoClean)
454 {
455 isAutoClean_ = isAutoClean;
456 }
457
GetAutoClean() const458 bool RdbStoreConfig::GetAutoClean() const
459 {
460 return isAutoClean_;
461 }
462
SetIsVector(bool isVector)463 void RdbStoreConfig::SetIsVector(bool isVector)
464 {
465 isVector_ = isVector;
466 isVector ? SetDBType(DB_VECTOR) : SetDBType(DB_SQLITE);
467 }
468
IsVector() const469 bool RdbStoreConfig::IsVector() const
470 {
471 return isVector_;
472 }
473
SetCustomDir(const std::string & customDir)474 void RdbStoreConfig::SetCustomDir(const std::string &customDir)
475 {
476 customDir_ = customDir;
477 }
478
GetCustomDir() const479 std::string RdbStoreConfig::GetCustomDir() const
480 {
481 return customDir_;
482 }
483
SetVisitorDir(const std::string & visitorDir)484 void RdbStoreConfig::SetVisitorDir(const std::string &visitorDir)
485 {
486 visitorDir_ = visitorDir;
487 }
488
GetVisitorDir() const489 std::string RdbStoreConfig::GetVisitorDir() const
490 {
491 return visitorDir_;
492 }
493
IsSearchable() const494 bool RdbStoreConfig::IsSearchable() const
495 {
496 return isSearchable_;
497 }
498
SetSearchable(bool isSearchable)499 void RdbStoreConfig::SetSearchable(bool isSearchable)
500 {
501 isSearchable_ = isSearchable;
502 }
503
GetWriteTime() const504 int RdbStoreConfig::GetWriteTime() const
505 {
506 return writeTimeout_;
507 }
508
SetWriteTime(int timeout)509 void RdbStoreConfig::SetWriteTime(int timeout)
510 {
511 writeTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
512 }
513
GetReadTime() const514 int RdbStoreConfig::GetReadTime() const
515 {
516 return readTimeout_;
517 }
518
SetReadTime(int timeout)519 void RdbStoreConfig::SetReadTime(int timeout)
520 {
521 readTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
522 }
523
SetRoleType(RoleType role)524 void RdbStoreConfig::SetRoleType(RoleType role)
525 {
526 role_ = role;
527 }
528
GetRoleType() const529 uint32_t RdbStoreConfig::GetRoleType() const
530 {
531 return role_;
532 }
533
SetDBType(int32_t dbType)534 void RdbStoreConfig::SetDBType(int32_t dbType)
535 {
536 dbType_ = dbType;
537 }
538
GetDBType() const539 int32_t RdbStoreConfig::GetDBType() const
540 {
541 return dbType_;
542 }
543
SetAllowRebuild(bool allowRebuild)544 void RdbStoreConfig::SetAllowRebuild(bool allowRebuild)
545 {
546 this->allowRebuilt_ = allowRebuild;
547 }
548
GetAllowRebuild() const549 bool RdbStoreConfig::GetAllowRebuild() const
550 {
551 return allowRebuilt_;
552 }
553
SetIntegrityCheck(IntegrityCheck checkType)554 void RdbStoreConfig::SetIntegrityCheck(IntegrityCheck checkType)
555 {
556 checkType_ = checkType;
557 }
558
GetIntegrityCheck() const559 IntegrityCheck RdbStoreConfig::GetIntegrityCheck() const
560 {
561 return checkType_;
562 }
563
SetPluginLibs(const std::vector<std::string> & pluginLibs)564 void RdbStoreConfig::SetPluginLibs(const std::vector<std::string> &pluginLibs)
565 {
566 pluginLibs_ = pluginLibs;
567 }
568
GetPluginLibs() const569 std::vector<std::string> RdbStoreConfig::GetPluginLibs() const
570 {
571 return pluginLibs_;
572 }
573
GetIter() const574 int32_t RdbStoreConfig::GetIter() const
575 {
576 return cryptoParam_.iterNum;
577 }
578
SetIter(int32_t iter) const579 void RdbStoreConfig::SetIter(int32_t iter) const
580 {
581 cryptoParam_.iterNum = iter;
582 }
583
GetHaMode() const584 int32_t RdbStoreConfig::GetHaMode() const
585 {
586 return haMode_;
587 }
588
SetHaMode(int32_t haMode)589 void RdbStoreConfig::SetHaMode(int32_t haMode)
590 {
591 haMode_ = haMode;
592 }
593
GetPromiseInfo() const594 PromiseInfo RdbStoreConfig::GetPromiseInfo() const
595 {
596 return promiseInfo_;
597 }
598
SetPromiseInfo(PromiseInfo promiseInfo)599 void RdbStoreConfig::SetPromiseInfo(PromiseInfo promiseInfo)
600 {
601 promiseInfo_ = promiseInfo;
602 }
603
604
GetTokenizer() const605 Tokenizer RdbStoreConfig::GetTokenizer() const
606 {
607 return tokenizer_;
608 }
609
SetTokenizer(Tokenizer tokenizer)610 void RdbStoreConfig::SetTokenizer(Tokenizer tokenizer)
611 {
612 tokenizer_ = tokenizer;
613 }
614
GetWalLimitSize() const615 ssize_t RdbStoreConfig::GetWalLimitSize() const
616 {
617 return walLimitSize_;
618 }
619
SetWalLimitSize(ssize_t size)620 void RdbStoreConfig::SetWalLimitSize(ssize_t size)
621 {
622 if (size < GlobalExpr::DB_WAL_DEFAULT_SIZE) {
623 size = GlobalExpr::DB_WAL_DEFAULT_SIZE;
624 }
625 if (size > GlobalExpr::DB_WAL_SIZE_LIMIT_MAX) {
626 size = GlobalExpr::DB_WAL_SIZE_LIMIT_MAX;
627 }
628 walLimitSize_ = size;
629 // '(size >> 1) + (size >> 2)' Size of the WAL file that does not checkpoint within 5 minutes when sqlite_busy
630 checkpointSize_ = (size >> 1) + (size >> 2);
631 // '(size >> 5) + (size >> 7)' Size of the WAL file for starting checkpoint.
632 startCheckpointSize_ = (size >> 5) + (size >> 7);
633 }
634
GetClearMemorySize() const635 int32_t RdbStoreConfig::GetClearMemorySize() const
636 {
637 return clearMemorySize_;
638 }
639
SetClearMemorySize(int32_t size)640 void RdbStoreConfig::SetClearMemorySize(int32_t size)
641 {
642 if (size < 0 || size > GlobalExpr::CLEAR_MEMORY_SIZE) {
643 size = GlobalExpr::CLEAR_MEMORY_SIZE;
644 }
645 clearMemorySize_ = size;
646 }
647
GetCheckpointSize() const648 ssize_t RdbStoreConfig::GetCheckpointSize() const
649 {
650 return checkpointSize_;
651 }
652
GetStartCheckpointSize() const653 ssize_t RdbStoreConfig::GetStartCheckpointSize() const
654 {
655 return startCheckpointSize_;
656 }
657
GetSubUser() const658 int32_t RdbStoreConfig::GetSubUser() const
659 {
660 return subUser_;
661 }
662
SetSubUser(int32_t subUser)663 void RdbStoreConfig::SetSubUser(int32_t subUser)
664 {
665 subUser_ = subUser;
666 }
667
EnableRekey(bool enable)668 void RdbStoreConfig::EnableRekey(bool enable)
669 {
670 autoRekey_ = enable;
671 }
672
SetCryptoParam(RdbStoreConfig::CryptoParam cryptoParam)673 void RdbStoreConfig::SetCryptoParam(RdbStoreConfig::CryptoParam cryptoParam)
674 {
675 cryptoParam_ = cryptoParam;
676 }
677
GetCryptoParam() const678 RdbStoreConfig::CryptoParam RdbStoreConfig::GetCryptoParam() const
679 {
680 return cryptoParam_;
681 }
682
GetNcandidates() const683 int RdbStoreConfig::GetNcandidates() const
684 {
685 return ncandidates_;
686 }
687
SetNcandidates(int ncandidates)688 void RdbStoreConfig::SetNcandidates(int ncandidates)
689 {
690 ncandidates_ = ncandidates;
691 }
692
693 RdbStoreConfig::CryptoParam::CryptoParam() = default;
694
~CryptoParam()695 RdbStoreConfig::CryptoParam::~CryptoParam()
696 {
697 encryptKey_.assign(encryptKey_.size(), 0);
698 }
699
IsValid() const700 bool RdbStoreConfig::CryptoParam::IsValid() const
701 {
702 int32_t count = iterNum;
703 if (count < 0) {
704 return false;
705 }
706
707 if (encryptAlgo != AES_256_CBC && encryptAlgo != AES_256_GCM) {
708 return false;
709 }
710
711 if (hmacAlgo < SHA1 || hmacAlgo > SHA512) {
712 return false;
713 }
714
715 if (kdfAlgo < KDF_SHA1 || kdfAlgo > KDF_SHA512) {
716 return false;
717 }
718
719 return (cryptoPageSize != 0) && ((cryptoPageSize & DB_INVALID_CRYPTO_PAGE_SIZE_MASK) == 0) &&
720 (cryptoPageSize & (cryptoPageSize - 1)) == 0;
721 }
722
ToString() const723 std::string RdbStoreConfig::ToString() const
724 {
725 std::stringstream oss;
726 oss << " bundleName:" << bundleName_ << ",";
727 oss << " moduleName:" << moduleName_ << ",";
728 oss << " dataGroupId:" << dataGroupId_ << ",";
729 oss << " path:" << SqliteUtils::Anonymous(path_) << ",";
730 oss << " storageMode:" << static_cast<int32_t>(storageMode_) << ",";
731 oss << " journalMode:" << journalMode_ << ",";
732 oss << " syncMode:" << syncMode_ << ",";
733 oss << " databaseFileType:" << databaseFileType << ",";
734 oss << " isEncrypt:" << IsEncrypt() << ",";
735 oss << " isSearchable:" << IsSearchable() << ",";
736 oss << " readOnly_:" << readOnly_ << ",";
737 oss << " securityLevel:" << static_cast<int32_t>(securityLevel_) << ",";
738 oss << " journalSize:" << journalSize_ << ",";
739 oss << " pageSize:" << pageSize_ << ",";
740 oss << " dbType:" << dbType_ << ",";
741 oss << " customDir:" << SqliteUtils::Anonymous(customDir_) << ",";
742 oss << " haMode:" << haMode_ << ",";
743 oss << " pluginLibs size:" << pluginLibs_.size() << ",";
744 oss << " area:" << area_ << ",";
745 return oss.str();
746 }
747
FormatCfg(const RdbStoreConfig & first,const RdbStoreConfig & second)748 std::string RdbStoreConfig::FormatCfg(const RdbStoreConfig &first, const RdbStoreConfig &second)
749 {
750 std::stringstream oss;
751 oss << " storageMode:" << static_cast<int32_t>(first.storageMode_) << "->"
752 << static_cast<int32_t>(second.storageMode_) << ",";
753 oss << " journalMode:" << first.journalMode_ << "->" << second.journalMode_ << ",";
754 oss << " syncMode:" << first.syncMode_ << "->" << second.syncMode_ << ",";
755 oss << " databaseFileType:" << first.databaseFileType << "->" << second.databaseFileType << ",";
756 oss << " isEncrypt:" << first.IsEncrypt() << "->" << second.IsEncrypt() << ",";
757 oss << " isSearchable:" << first.IsSearchable() << "->" << second.IsSearchable() << ",";
758 oss << " readOnly_:" << first.readOnly_ << "->" << second.readOnly_ << ",";
759 oss << " securityLevel:" << static_cast<int32_t>(first.securityLevel_) << "->"
760 << static_cast<int32_t>(second.securityLevel_) << ",";
761 oss << " journalSize:" << first.journalSize_ << "->" << second.journalSize_ << ",";
762 oss << " pageSize:" << first.pageSize_ << "->" << second.pageSize_ << ",";
763 oss << " dbType:" << first.dbType_ << "->" << second.dbType_ << ",";
764 oss << " customDir:" << SqliteUtils::Anonymous(first.customDir_) << "->"
765 << SqliteUtils::Anonymous(second.customDir_) << ",";
766 oss << " haMode:" << first.haMode_ << "->" << second.haMode_ << ",";
767 oss << " pluginLibs size:" << first.pluginLibs_.size() << "->" << second.pluginLibs_.size() << ",";
768 oss << " area:" << first.area_ << "->" << second.area_ << ",";
769 return oss.str();
770 }
771
SetRegisterInfo(RegisterType type,bool state) const772 void RdbStoreConfig::SetRegisterInfo(RegisterType type, bool state) const
773 {
774 registerInfo_.Set(type, state);
775 }
776
GetRegisterInfo(RegisterType type) const777 bool RdbStoreConfig::GetRegisterInfo(RegisterType type) const
778 {
779 return registerInfo_.Get(type);
780 }
781
IsEqualRegisterInfo(const RdbStoreConfig & config) const782 bool RdbStoreConfig::IsEqualRegisterInfo(const RdbStoreConfig& config) const
783 {
784 return registerInfo_ == config.registerInfo_;
785 }
786 } // namespace OHOS::NativeRdb
787