From 3abe6be384c31492565414c97604e63552f2d150 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 17 Dec 2023 18:50:53 +0900 Subject: [PATCH] index: propagate DefaultIndexStore::init/reinit() errors --- cli/src/commands/debug.rs | 4 +++- lib/src/default_index/mod.rs | 2 +- lib/src/default_index/store.rs | 35 ++++++++++++++++++++++------------ lib/src/repo.rs | 2 +- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/cli/src/commands/debug.rs b/cli/src/commands/debug.rs index fe5bf02f4..6ce2d2c9d 100644 --- a/cli/src/commands/debug.rs +++ b/cli/src/commands/debug.rs @@ -237,7 +237,9 @@ fn cmd_debug_reindex( let default_index_store: Option<&DefaultIndexStore> = repo.index_store().as_any().downcast_ref(); if let Some(default_index_store) = default_index_store { - default_index_store.reinit(); + default_index_store + .reinit() + .map_err(|err| CommandError::InternalError(err.to_string()))?; let repo = repo.reload_at(repo.operation())?; let default_index: &DefaultReadonlyIndex = repo .readonly_index() diff --git a/lib/src/default_index/mod.rs b/lib/src/default_index/mod.rs index 6076c6240..5d092c1a1 100644 --- a/lib/src/default_index/mod.rs +++ b/lib/src/default_index/mod.rs @@ -28,7 +28,7 @@ pub use self::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError}; pub use self::rev_walk::{ RevWalk, RevWalkDescendants, RevWalkDescendantsGenerationRange, RevWalkGenerationRange, }; -pub use self::store::{DefaultIndexStore, DefaultIndexStoreError}; +pub use self::store::{DefaultIndexStore, DefaultIndexStoreError, DefaultIndexStoreInitError}; #[cfg(test)] mod tests { diff --git a/lib/src/default_index/store.rs b/lib/src/default_index/store.rs index 94de84cfc..cc60785ca 100644 --- a/lib/src/default_index/store.rs +++ b/lib/src/default_index/store.rs @@ -26,10 +26,10 @@ use thiserror::Error; use super::mutable::DefaultMutableIndex; use super::readonly::{DefaultReadonlyIndex, ReadonlyIndexLoadError, ReadonlyIndexSegment}; -use crate::backend::{BackendError, CommitId, ObjectId}; +use crate::backend::{BackendError, BackendInitError, CommitId, ObjectId}; use crate::commit::CommitByCommitterTimestamp; use crate::dag_walk; -use crate::file_util::persist_content_addressed_temp_file; +use crate::file_util::{persist_content_addressed_temp_file, IoResultExt as _, PathError}; use crate::index::{ Index, IndexReadError, IndexStore, IndexWriteError, MutableIndex, ReadonlyIndex, }; @@ -37,6 +37,17 @@ use crate::op_store::{OpStoreError, OperationId}; use crate::operation::Operation; use crate::store::Store; +/// Error that may occur during `DefaultIndexStore` initialization. +#[derive(Debug, Error)] +#[error("Failed to initialize index store: {0}")] +pub struct DefaultIndexStoreInitError(#[from] pub PathError); + +impl From for BackendInitError { + fn from(err: DefaultIndexStoreInitError) -> Self { + BackendInitError(err.into()) + } +} + #[derive(Debug, Error)] pub enum DefaultIndexStoreError { #[error( @@ -69,11 +80,12 @@ impl DefaultIndexStore { "default" } - pub fn init(dir: &Path) -> Self { - std::fs::create_dir(dir.join("operations")).unwrap(); - DefaultIndexStore { + pub fn init(dir: &Path) -> Result { + let op_dir = dir.join("operations"); + std::fs::create_dir(&op_dir).context(&op_dir)?; + Ok(DefaultIndexStore { dir: dir.to_owned(), - } + }) } pub fn load(dir: &Path) -> DefaultIndexStore { @@ -82,10 +94,11 @@ impl DefaultIndexStore { } } - pub fn reinit(&self) { + pub fn reinit(&self) -> Result<(), DefaultIndexStoreInitError> { let op_dir = self.dir.join("operations"); - std::fs::remove_dir_all(&op_dir).unwrap(); - std::fs::create_dir(op_dir).unwrap(); + std::fs::remove_dir_all(&op_dir).context(&op_dir)?; + std::fs::create_dir(&op_dir).context(&op_dir)?; + Ok(()) } fn load_index_at_operation( @@ -256,9 +269,7 @@ impl IndexStore for DefaultIndexStore { // we just reindex. // TODO: Move this message to a callback or something. println!("The index was corrupt (maybe the format has changed). Reindexing..."); - // TODO: propagate error - std::fs::remove_dir_all(self.dir.join("operations")).unwrap(); - std::fs::create_dir(self.dir.join("operations")).unwrap(); + self.reinit().map_err(|err| IndexReadError(err.into()))?; self.index_at_operation(store, op) } result => result, diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 593223210..64457dc84 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -132,7 +132,7 @@ impl ReadonlyRepo { } pub fn default_index_store_initializer() -> &'static IndexStoreInitializer<'static> { - &|_settings, store_path| Ok(Box::new(DefaultIndexStore::init(store_path))) + &|_settings, store_path| Ok(Box::new(DefaultIndexStore::init(store_path)?)) } pub fn default_submodule_store_initializer() -> &'static SubmoduleStoreInitializer<'static> {