From 9dccc273bfe1e9513881b2cfa95a9e0caa3882eb Mon Sep 17 00:00:00 2001 From: edouard Date: Fri, 7 Apr 2023 12:41:45 +0200 Subject: [PATCH] gui creates datadir for fresh install --- gui/src/installer/mod.rs | 9 +++++---- gui/src/logger.rs | 8 ++++++-- gui/src/main.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/gui/src/installer/mod.rs b/gui/src/installer/mod.rs index 8d388e59..5e610e78 100644 --- a/gui/src/installer/mod.rs +++ b/gui/src/installer/mod.rs @@ -211,10 +211,11 @@ pub fn daemon_check(cfg: liana::config::Config) -> Result<(), Error> { pub async fn install(ctx: Context) -> Result { let mut cfg: liana::config::Config = ctx.extract_daemon_config(); - let data_dir = - cfg.data_dir.unwrap().canonicalize().map_err(|e| { - Error::Unexpected(format!("Failed to canonicalize datadir path: {}", e)) - })?; + let data_dir = cfg.data_dir.unwrap(); + + let data_dir = data_dir + .canonicalize() + .map_err(|e| Error::Unexpected(format!("Failed to canonicalize datadir path: {}", e)))?; cfg.data_dir = Some(data_dir.clone()); daemon_check(cfg.clone())?; diff --git a/gui/src/logger.rs b/gui/src/logger.rs index d5d5ca5a..66ef12ca 100644 --- a/gui/src/logger.rs +++ b/gui/src/logger.rs @@ -97,8 +97,12 @@ impl Logger { pub fn remove_install_log_file(&self, mut datadir: PathBuf) { datadir.push(INSTALLER_LOG_FILE_NAME); - if let Err(e) = std::fs::remove_file(datadir) { - error!("Failed to remove installer log file: {:#?}", e); + if let Err(e) = std::fs::remove_file(&datadir) { + error!( + "Failed to remove installer log file {} error:{:#?}", + datadir.to_string_lossy(), + e + ); } } diff --git a/gui/src/main.rs b/gui/src/main.rs index 5a16e8e6..8c8c8f18 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -111,6 +111,18 @@ impl Application for GUI { ) } Config::Install(datadir_path, network) => { + if !datadir_path.exists() { + // datadir is created right before launching the installer + // so logs can go in /installer.log + if let Err(e) = create_datadir(&datadir_path) { + error!("Failed to create datadir: {}", e); + } else { + info!( + "Created a fresh data directory at {}", + &datadir_path.to_string_lossy() + ); + } + } logger.set_installer_mode(datadir_path.clone(), LevelFilter::INFO); let (install, command) = Installer::new(datadir_path, network); ( @@ -270,6 +282,25 @@ impl Application for GUI { } } +fn create_datadir(datadir_path: &std::path::Path) -> Result<(), Box> { + #[cfg(unix)] + return { + use std::fs::DirBuilder; + use std::os::unix::fs::DirBuilderExt; + + let mut builder = DirBuilder::new(); + builder.mode(0o700).recursive(true).create(datadir_path)?; + Ok(()) + }; + + // TODO: permissions on Windows.. + #[cfg(not(unix))] + return { + std::fs::create_dir_all(datadir_path)?; + Ok(()) + }; +} + pub enum Config { Run(PathBuf, app::Config, bitcoin::Network), Launcher(PathBuf),