add experimental disk corrupt detech on Android/Linux

This commit is contained in:
guoling
2025-01-15 11:55:21 +08:00
parent aad8e484c0
commit ead3e4668f
5 changed files with 36 additions and 9 deletions
+25
View File
@@ -123,6 +123,31 @@ bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD, bool nee
return ret;
}
bool isDiskOfMMAPFileCorrupted(MemoryFile *file, bool &needReportReadFail) {
// TODO: maybe we need reading a larger chunk than 4 byte in Android/Linux
uint32_t info;
auto fd = file->getFd();
auto path = file->getPath().c_str();
auto oldPos = lseek(fd, 0, SEEK_CUR);
lseek(fd, 0, SEEK_SET);
auto size = read(fd, &info, sizeof(info));
auto err = errno;
lseek(fd, oldPos, SEEK_SET);
if (size <= 0) {
needReportReadFail = true;
MMKVError("fail to read [%s] from fd [%d], errno: %d (%s)", path, fd, err, strerror(err));
if (err == EIO || err == EILSEQ || err == EINVAL || err == ENXIO) {
MMKVWarning("file fail to read, consider it illegal, delete now: [%s]", path);
return true;
}
}
// we don't rollout mayfly fd (yet)
// file->cleanMayflyFD();
return false;
}
} // namespace mmkv
#endif // defined(MMKV_ANDROID) || defined(MMKV_LINUX)