| 369 | } |
| 370 | |
| 371 | void UserDefault::lazyInit() |
| 372 | { |
| 373 | if (_initialized) |
| 374 | return; |
| 375 | |
| 376 | #if !USER_DEFAULT_PLAIN_MODE |
| 377 | _filePath = FileUtils::getInstance()->getNativeWritableAbsolutePath() + _userDefaultFileName; |
| 378 | |
| 379 | // construct file mapping |
| 380 | if (!_fileStream.open(_filePath, IFileStream::Mode::OVERLAPPED)) |
| 381 | { |
| 382 | AXLOGW("UserDefault::init open storage file '{}' failed!", _filePath); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | int filesize = static_cast<int>(_fileStream.size()); |
| 387 | |
| 388 | if (filesize < _curMapSize) |
| 389 | { // construct a empty file mapping |
| 390 | if (!_fileStream.resize(_curMapSize)) |
| 391 | { |
| 392 | AXLOGW("UserDefault::init failed to truncate '{}'.", _filePath); |
| 393 | return; |
| 394 | } |
| 395 | _rwmmap = std::make_shared<mio::mmap_sink>(_fileStream.nativeHandle(), 0, _curMapSize); |
| 396 | } |
| 397 | else |
| 398 | { /// load to memory _values |
| 399 | _rwmmap = std::make_shared<mio::mmap_sink>(_fileStream.nativeHandle(), 0, mio::map_entire_file); |
| 400 | if (_rwmmap->is_mapped()) |
| 401 | { // no error |
| 402 | yasio::ibstream_view ibs(_rwmmap->data(), _rwmmap->length()); |
| 403 | |
| 404 | if (ibs.length() > 0) |
| 405 | { |
| 406 | // read count of keyvals. |
| 407 | int count = ibs.read<int>(); |
| 408 | for (auto i = 0; i < count; ++i) |
| 409 | { |
| 410 | if (_encryptEnabled) |
| 411 | { |
| 412 | std::string key(ibs.read_v()); |
| 413 | std::string value(ibs.read_v()); |
| 414 | this->encrypt(key, AES_DECRYPT); |
| 415 | this->encrypt(value, AES_DECRYPT); |
| 416 | updateValueForKey(key, value); |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | std::string_view key(ibs.read_v()); |
| 421 | std::string_view value(ibs.read_v()); |
| 422 | updateValueForKey(key, value); |
| 423 | } |
| 424 | } |
| 425 | _realSize = static_cast<int>(ibs.seek(0, SEEK_CUR) - sizeof(udflen_t)); |
| 426 | } |
| 427 | } |
| 428 | else |
nothing calls this directly
no test coverage detected