From c82aa95168db0a960c732ff001daaa5859b2317d Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Wed, 29 Sep 2021 13:44:55 +0200 Subject: [PATCH] Auto-rename of `db_dir/mainnet` to `db_dir/bitcoin` Since version 0.9 the `mainnet` subdir is called `bitcoin` we need to rename it if auto reindex is specified to fulfill the purpose of auto reindex. The logic in this change keeps the intention of disabled auto reindex causing no changes. --- src/bin/electrs.rs | 0 src/server.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) mode change 100644 => 100755 src/bin/electrs.rs diff --git a/src/bin/electrs.rs b/src/bin/electrs.rs old mode 100644 new mode 100755 diff --git a/src/server.rs b/src/server.rs index f50662f70..259a37033 100644 --- a/src/server.rs +++ b/src/server.rs @@ -60,10 +60,50 @@ pub fn run() -> Result<()> { result.context("electrs failed") } +fn rename_db_dir(config: &Config) -> Result<()> { + use std::{fs, io}; + + match (config.network, config.auto_reindex, config.db_path.parent()) { + (bitcoin::Network::Bitcoin, true, Some(db_parent)) => { + let old_dir = db_parent.join("mainnet"); + match fs::rename(&old_dir, &config.db_path) { + Ok(()) => Ok(()), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()), + Err(error) => Err(error).with_context(|| { + format!( + "failed to rename the old directory ({}) to {}", + old_dir.display(), + config.db_path.display() + ) + }), + } + } + (bitcoin::Network::Bitcoin, false, Some(db_parent)) => { + let old_dir = db_parent.join("mainnet"); + match fs::metadata(&old_dir) { + Ok(_) => Err(anyhow::anyhow!( + "The old directory {} exists but auto reindex was disabled", + old_dir.display() + )), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()), + Err(error) => Err(error).with_context(|| { + format!( + "failed to check whether the old directory ({}) exists", + old_dir.display() + ) + }), + } + } + _ => Ok(()), + } +} + fn serve() -> Result<()> { let config = Config::from_args(); let metrics = Metrics::new(config.monitoring_addr)?; + rename_db_dir(&config)?; + let (server_tx, server_rx) = unbounded(); if !config.disable_electrum_rpc { let listener = TcpListener::bind(config.electrum_rpc_addr)?;