| 1652 | } |
| 1653 | |
| 1654 | bool MMKV::isFileValid(const string &mmapID, const MMKVPath_t *relatePath) { |
| 1655 | if (!g_instanceLock) { |
| 1656 | return false; |
| 1657 | } |
| 1658 | SCOPED_LOCK(g_instanceLock); |
| 1659 | |
| 1660 | std::string realID, mmapKey; |
| 1661 | auto [kvPath, crcPath] = getStorage(mmapID, relatePath, realID, mmapKey); |
| 1662 | if (kvPath.empty()) { |
| 1663 | return true; |
| 1664 | } |
| 1665 | if (crcPath.empty()) { |
| 1666 | return false; |
| 1667 | } |
| 1668 | |
| 1669 | uint32_t crcFile = 0; |
| 1670 | MMBuffer *data = readWholeFile(crcPath); |
| 1671 | if (data) { |
| 1672 | if (data->getPtr()) { |
| 1673 | MMKVMetaInfo metaInfo; |
| 1674 | metaInfo.read(data->getPtr()); |
| 1675 | crcFile = metaInfo.m_crcDigest; |
| 1676 | } |
| 1677 | delete data; |
| 1678 | } else { |
| 1679 | return false; |
| 1680 | } |
| 1681 | |
| 1682 | uint32_t crcDigest = 0; |
| 1683 | MMBuffer *fileData = readWholeFile(kvPath); |
| 1684 | if (fileData) { |
| 1685 | if (fileData->getPtr() && (fileData->length() >= Fixed32Size)) { |
| 1686 | uint32_t actualSize = 0; |
| 1687 | memcpy(&actualSize, fileData->getPtr(), Fixed32Size); |
| 1688 | if (actualSize > (fileData->length() - Fixed32Size)) { |
| 1689 | delete fileData; |
| 1690 | return false; |
| 1691 | } |
| 1692 | |
| 1693 | crcDigest = (uint32_t) CRC32(0, (const uint8_t *) fileData->getPtr() + Fixed32Size, (uint32_t) actualSize); |
| 1694 | } |
| 1695 | delete fileData; |
| 1696 | return crcFile == crcDigest; |
| 1697 | } else { |
| 1698 | return false; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | bool MMKV::removeStorage(const std::string &mmapID, const MMKVPath_t *relatePath) { |
| 1703 | if (!g_instanceLock) { |
no test coverage detected