From da0050172be22463bb39dd043da9a8081c463f88 Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Fri, 11 Apr 2025 17:21:52 +0200 Subject: [PATCH] Some last fixes to format directory paths using OS seperator everwhere in all warning messages --- impls/src/lifecycle/default.rs | 25 ++++++++++++---- impls/src/lifecycle/seed.rs | 53 +++++++++++++++++++++++++++------- src/bin/grin-wallet.rs | 13 +++++++-- 3 files changed, 72 insertions(+), 19 deletions(-) diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index a0e624e..4ea83ee 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -27,6 +27,15 @@ use crate::LMDBBackend; use grin_util::logger::LoggingConfig; use std::fs; use std::path::PathBuf; +use std::path::MAIN_SEPARATOR; + +// Helper fuction to format paths according to OS, avoids bugs on Linux +pub fn fmt_path(path: String) -> String { + let sep = &MAIN_SEPARATOR.to_string(); + let path = path.replace("/", &sep); + let path = path.replace("\\", &sep); + path +} pub struct DefaultLCProvider<'a, C, K> where @@ -64,7 +73,13 @@ where } fn get_top_level_directory(&self) -> Result { - Ok(self.data_dir.to_owned()) + let sep = &MAIN_SEPARATOR.to_string(); + let data_dir = self + .data_dir + .to_owned() + .replace("/", &sep) + .replace("\\", &sep); + Ok(data_dir) } fn create_config( @@ -183,11 +198,11 @@ where ) -> Result<(), Error> { let mut data_dir_name = PathBuf::from(self.data_dir.clone()); data_dir_name.push(GRIN_WALLET_DIR); - let data_dir_name = data_dir_name.to_str().unwrap(); + let data_dir_name = fmt_path((data_dir_name.to_str().unwrap()).to_string()); let exists = WalletSeed::seed_file_exists(&data_dir_name); if !test_mode { if let Ok(true) = exists { - let msg = format!("Wallet seed already exists at: {}", data_dir_name); + let msg = format!("Wallet seed already exists at4565: {}", data_dir_name); return Err(Error::WalletSeedExists(msg)); } } @@ -231,7 +246,7 @@ where ) -> Result, Error> { let mut data_dir_name = PathBuf::from(self.data_dir.clone()); data_dir_name.push(GRIN_WALLET_DIR); - let data_dir_name = data_dir_name.to_str().unwrap(); + let data_dir_name = fmt_path(data_dir_name.to_str().unwrap().to_string()); let mut wallet: LMDBBackend<'a, C, K> = match LMDBBackend::new(&data_dir_name, self.node_client.clone()) { Err(e) => { @@ -276,7 +291,7 @@ where ) -> Result { let mut data_dir_name = PathBuf::from(self.data_dir.clone()); data_dir_name.push(GRIN_WALLET_DIR); - let data_dir_name = data_dir_name.to_str().unwrap(); + let data_dir_name = fmt_path(data_dir_name.display().to_string()); let wallet_seed = WalletSeed::from_file(&data_dir_name, password) .map_err(|_| Error::Lifecycle("Error opening wallet seed file".into()))?; let res = wallet_seed diff --git a/impls/src/lifecycle/seed.rs b/impls/src/lifecycle/seed.rs index e7b6b46..59dcd34 100644 --- a/impls/src/lifecycle/seed.rs +++ b/impls/src/lifecycle/seed.rs @@ -12,16 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. +use crate::blake2; use core::num::NonZeroU32; +use rand::{thread_rng, Rng}; +use serde_json; use std::fs::{self, File}; use std::io::{Read, Write}; use std::path::Path; use std::path::MAIN_SEPARATOR; -use crate::blake2; -use rand::{thread_rng, Rng}; -use serde_json; - use ring::aead; use ring::pbkdf2; @@ -57,6 +56,13 @@ impl WalletSeed { self.0.to_vec().to_hex() } + // Helper fuction to format paths according to OS, avoids bugs on Linux + pub fn fmt_path(path: String) -> String { + let sep = &MAIN_SEPARATOR.to_string(); + let path = path.replace("/", &sep); + let path = path.replace("\\", &sep); + path + } pub fn to_mnemonic(&self) -> Result { let result = mnemonic::from_entropy(&self.0); match result { @@ -97,7 +103,12 @@ impl WalletSeed { } pub fn seed_file_exists(data_file_dir: &str) -> Result { - let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,); + let seed_file_path = &format!( + "{}{}{}", + Self::fmt_path(data_file_dir.to_string()), + MAIN_SEPARATOR, + SEED_FILE, + ); debug!("Seed file path: {}", seed_file_path); if Path::new(seed_file_path).exists() { Ok(true) @@ -132,7 +143,12 @@ impl WalletSeed { word_list: util::ZeroingString, password: util::ZeroingString, ) -> Result<(), Error> { - let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,); + let seed_file_path = &format!( + "{}{}{}", + Self::fmt_path(data_file_dir.to_string()), + MAIN_SEPARATOR, + SEED_FILE, + ); debug!("data file dir: {}", data_file_dir); if let Ok(true) = WalletSeed::seed_file_exists(data_file_dir) { debug!("seed file exists"); @@ -166,10 +182,15 @@ impl WalletSeed { // create directory if it doesn't exist fs::create_dir_all(data_file_dir).map_err(|_| Error::IO)?; - let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,); - + let seed_file_path = &format!( + "{}{}{}", + Self::fmt_path(data_file_dir.to_string()), + MAIN_SEPARATOR, + SEED_FILE, + ); + let data_file_dir = Self::fmt_path(data_file_dir.to_string()); warn!("Generating wallet seed file at: {}", seed_file_path); - let exists = WalletSeed::seed_file_exists(data_file_dir)?; + let exists = WalletSeed::seed_file_exists(&data_file_dir)?; if exists && !test_mode { let msg = format!("Wallet seed already exists at: {}", data_file_dir); error!("{}", msg); @@ -196,7 +217,12 @@ impl WalletSeed { // create directory if it doesn't exist fs::create_dir_all(data_file_dir).map_err(|_| Error::IO)?; - let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,); + let seed_file_path = &format!( + "{}{}{}", + Self::fmt_path(data_file_dir.to_string()), + MAIN_SEPARATOR, + SEED_FILE, + ); debug!("Using wallet seed file at: {}", seed_file_path); @@ -219,7 +245,12 @@ impl WalletSeed { } pub fn delete_seed_file(data_file_dir: &str) -> Result<(), Error> { - let seed_file_path = &format!("{}{}{}", data_file_dir, MAIN_SEPARATOR, SEED_FILE,); + let seed_file_path = &format!( + "{}{}{}", + Self::fmt_path(data_file_dir.to_string()), + MAIN_SEPARATOR, + SEED_FILE, + ); if Path::new(seed_file_path).exists() { debug!("Deleting wallet seed file at: {}", seed_file_path); fs::remove_file(seed_file_path).map_err(|_| Error::IO)?; diff --git a/src/bin/grin-wallet.rs b/src/bin/grin-wallet.rs index 36d0385..fb23f9a 100644 --- a/src/bin/grin-wallet.rs +++ b/src/bin/grin-wallet.rs @@ -24,12 +24,12 @@ use crate::util::init_logger; use clap::App; use grin_core as core; use grin_util as util; +use grin_wallet::cmd; use grin_wallet_config as config; use grin_wallet_impls::HTTPNodeClient; use std::env; use std::path::PathBuf; - -use grin_wallet::cmd; +use std::path::MAIN_SEPARATOR; // include build information pub mod built_info { @@ -55,6 +55,13 @@ pub fn info_strings() -> (String, String) { ) } +// Helper fuction to format paths according to OS, avoids bugs on Linux +pub fn fmt_path(path: String) -> String { + let sep = &MAIN_SEPARATOR.to_string(); + let path = path.replace("/", &sep).replace("\\", &sep); + path +} + fn log_build_info() { let (basic_info, detailed_info) = info_strings(); info!("{}", basic_info); @@ -86,7 +93,7 @@ fn real_main() -> i32 { let res = args.value_of("top_level_dir"); match res { Some(d) => { - let d = d.to_owned().replace("/", "\\"); // Fix for fs to work with paths on Linux + let d = fmt_path(d.to_owned().to_string()); // Fix for fs to work with paths on Linu current_dir = Some(PathBuf::from(d)); } None => {