Skip to content

Commit

Permalink
Auto-rename of db_dir/mainnet to db_dir/bitcoin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kixunil committed Feb 21, 2022
1 parent a8575b6 commit 5ac416f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Empty file modified src/bin/electrs.rs
100644 → 100755
Empty file.
44 changes: 44 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,54 @@ 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)?;
Expand Down

0 comments on commit 5ac416f

Please sign in to comment.