Jelajahi Sumber

fix "~/.config" path expansion

ghassmo 4 tahun lalu
induk
melakukan
17ac6cefdb
4 mengubah file dengan 31 tambahan dan 16 penghapusan
  1. 12 12
      src/bin/cashierd.rs
  2. 3 3
      src/bin/darkfid.rs
  3. 2 1
      src/rpc/rpcserver.rs
  4. 14 0
      src/util.rs

+ 12 - 12
src/bin/cashierd.rs

@@ -9,7 +9,7 @@ use drk::{
     },
     },
     serial::{deserialize, serialize},
     serial::{deserialize, serialize},
     service::{bridge, bridge::Bridge},
     service::{bridge, bridge::Bridge},
-    util::join_config_path,
+    util::{expand_path, join_config_path},
     wallet::{CashierDb, WalletDb},
     wallet::{CashierDb, WalletDb},
     Error, Result,
     Error, Result,
 };
 };
@@ -70,16 +70,16 @@ impl Cashierd {
         let config: CashierdConfig = Config::<CashierdConfig>::load(config_path)?;
         let config: CashierdConfig = Config::<CashierdConfig>::load(config_path)?;
 
 
         let cashier_wallet = CashierDb::new(
         let cashier_wallet = CashierDb::new(
-            &PathBuf::from(config.cashier_wallet_path.clone()),
+            expand_path(&config.cashier_wallet_path.clone())?.as_path(),
             config.cashier_wallet_password.clone(),
             config.cashier_wallet_password.clone(),
         )?;
         )?;
 
 
         let client_wallet = WalletDb::new(
         let client_wallet = WalletDb::new(
-            &PathBuf::from(config.client_wallet_path.clone()),
+            expand_path(&config.client_wallet_path.clone())?.as_path(),
             config.client_wallet_password.clone(),
             config.client_wallet_password.clone(),
         )?;
         )?;
 
 
-        let rocks = Rocks::new(&PathBuf::from(config.database_path.clone()))?;
+        let rocks = Rocks::new(expand_path(&config.database_path.clone())?.as_path())?;
 
 
         let client = Client::new(
         let client = Client::new(
             rocks,
             rocks,
@@ -88,8 +88,8 @@ impl Cashierd {
                 config.gateway_subscriber_url.parse()?,
                 config.gateway_subscriber_url.parse()?,
             ),
             ),
             (
             (
-                PathBuf::from(config.mint_params.clone()),
-                PathBuf::from(config.spend_params.clone()),
+                expand_path(&config.mint_params.clone())?,
+                expand_path(&config.spend_params.clone())?,
             ),
             ),
             client_wallet.clone(),
             client_wallet.clone(),
         )?;
         )?;
@@ -151,11 +151,11 @@ impl Cashierd {
         let (notify, recv_coin) = async_channel::unbounded::<(jubjub::SubgroupPoint, u64)>();
         let (notify, recv_coin) = async_channel::unbounded::<(jubjub::SubgroupPoint, u64)>();
         let cashier_client_subscriber_task =
         let cashier_client_subscriber_task =
             smol::spawn(Client::connect_to_subscriber_from_cashier(
             smol::spawn(Client::connect_to_subscriber_from_cashier(
-                    self.client.clone(),
-                    executor.clone(),
-                    self.cashier_wallet.clone(),
-                    notify.clone(),
-                ));
+                self.client.clone(),
+                executor.clone(),
+                self.cashier_wallet.clone(),
+                notify.clone(),
+            ));
 
 
         let cashier_wallet = self.cashier_wallet.clone();
         let cashier_wallet = self.cashier_wallet.clone();
         let listen_for_receiving_coins_task = smol::spawn(async move {
         let listen_for_receiving_coins_task = smol::spawn(async move {
@@ -173,7 +173,7 @@ impl Cashierd {
         let cfg = RpcServerConfig {
         let cfg = RpcServerConfig {
             socket_addr: self.config.clone().listen_url,
             socket_addr: self.config.clone().listen_url,
             use_tls: self.config.serve_tls,
             use_tls: self.config.serve_tls,
-            identity_path: self.config.clone().tls_identity_path,
+            identity_path: expand_path(&self.config.clone().tls_identity_path)?,
             identity_pass: self.config.clone().tls_identity_password,
             identity_pass: self.config.clone().tls_identity_password,
         };
         };
 
 

+ 3 - 3
src/bin/darkfid.rs

@@ -13,7 +13,7 @@ use drk::{
         rpcserver::{listen_and_serve, RequestHandler, RpcServerConfig},
         rpcserver::{listen_and_serve, RequestHandler, RpcServerConfig},
     },
     },
     serial::serialize,
     serial::serialize,
-    util::join_config_path,
+    util::{expand_path, join_config_path},
     wallet::WalletDb,
     wallet::WalletDb,
     Result,
     Result,
 };
 };
@@ -54,7 +54,7 @@ impl Darkfid {
     fn new(config_path: PathBuf) -> Result<Self> {
     fn new(config_path: PathBuf) -> Result<Self> {
         let config: DarkfidConfig = Config::<DarkfidConfig>::load(config_path)?;
         let config: DarkfidConfig = Config::<DarkfidConfig>::load(config_path)?;
         let wallet = WalletDb::new(
         let wallet = WalletDb::new(
-            &PathBuf::from(&config.wallet_path),
+            expand_path(&config.wallet_path)?.as_path(),
             config.wallet_password.clone(),
             config.wallet_password.clone(),
         )?;
         )?;
         // TODO: FIXME
         // TODO: FIXME
@@ -303,7 +303,7 @@ async fn main() -> Result<()> {
     let server_config = RpcServerConfig {
     let server_config = RpcServerConfig {
         socket_addr: darkfid.config.clone().listen_address,
         socket_addr: darkfid.config.clone().listen_address,
         use_tls: darkfid.config.serve_tls,
         use_tls: darkfid.config.serve_tls,
-        identity_path: darkfid.config.clone().tls_identity_path,
+        identity_path: expand_path(&darkfid.config.clone().tls_identity_path)?,
         identity_pass: darkfid.config.clone().tls_identity_password,
         identity_pass: darkfid.config.clone().tls_identity_password,
     };
     };
 
 

+ 2 - 1
src/rpc/rpcserver.rs

@@ -1,6 +1,7 @@
 use std::net::{SocketAddr, TcpListener, TcpStream};
 use std::net::{SocketAddr, TcpListener, TcpStream};
 use std::str::FromStr;
 use std::str::FromStr;
 use std::sync::Arc;
 use std::sync::Arc;
+use std::path::PathBuf;
 
 
 use async_native_tls::{Identity, TlsAcceptor};
 use async_native_tls::{Identity, TlsAcceptor};
 use async_trait::async_trait;
 use async_trait::async_trait;
@@ -16,7 +17,7 @@ use crate::Result;
 pub struct RpcServerConfig {
 pub struct RpcServerConfig {
     pub socket_addr: String,
     pub socket_addr: String,
     pub use_tls: bool,
     pub use_tls: bool,
-    pub identity_path: String,
+    pub identity_path: PathBuf,
     pub identity_pass: String,
     pub identity_pass: String,
 }
 }
 
 

+ 14 - 0
src/util.rs

@@ -8,6 +8,20 @@ use crate::{
     Result,
     Result,
 };
 };
 
 
+pub fn expand_path(path: &str) -> Result<PathBuf> {
+    let ret: PathBuf;
+
+    if path.starts_with("~") {
+        let homedir = dirs::home_dir().unwrap();
+        let remains = PathBuf::from(path.strip_prefix("~/").unwrap());
+        ret = [homedir, remains].iter().collect();
+    } else {
+        ret = PathBuf::from(path);
+    }
+
+    Ok(ret)
+}
+
 pub fn join_config_path(file: &Path) -> Result<PathBuf> {
 pub fn join_config_path(file: &Path) -> Result<PathBuf> {
     let mut path = PathBuf::new();
     let mut path = PathBuf::new();
     let dfi_path = Path::new("darkfi");
     let dfi_path = Path::new("darkfi");