mirror of
https://github.com/Tencent/MMKV.git
synced 2026-10-17 04:30:56 +08:00
add experimental disk corrupt detech on Android/Linux
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user