From 38ce9143211364c996fba93ee491ef580cb9ea18 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 17 Dec 2023 14:28:53 +0900 Subject: [PATCH] index: reindex on content-related I/O errors If read_exact() or read_u32() reached to EOF, the index file should be considered corrupted. File not found error is also treated as data corruption because an invalid file name could be read from the child segment file. It can't handle special file names like "..", though. --- lib/src/default_index/readonly.rs | 9 +++++++-- lib/src/default_index/store.rs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index 69d274e35..5bd98960b 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -63,8 +63,13 @@ impl ReadonlyIndexLoadError { } /// Returns true if the underlying error suggests data corruption. - pub(super) fn is_corrupt(&self) -> bool { - matches!(self.error.kind(), io::ErrorKind::InvalidData) + pub(super) fn is_corrupt_or_not_found(&self) -> bool { + // If the parent file name field is corrupt, the file wouldn't be found. + // And there's no need to distinguish it from an empty file. + matches!( + self.error.kind(), + io::ErrorKind::NotFound | io::ErrorKind::InvalidData | io::ErrorKind::UnexpectedEof + ) } } diff --git a/lib/src/default_index/store.rs b/lib/src/default_index/store.rs index 42a5516cb..4c59d709c 100644 --- a/lib/src/default_index/store.rs +++ b/lib/src/default_index/store.rs @@ -243,7 +243,7 @@ impl IndexStore for DefaultIndexStore { store.change_id_length(), op.id(), ) { - Err(DefaultIndexStoreError::LoadIndex(err)) if err.is_corrupt() => { + Err(DefaultIndexStoreError::LoadIndex(err)) if err.is_corrupt_or_not_found() => { // If the index was corrupt (maybe it was written in a different format), // we just reindex. // TODO: Move this message to a callback or something.