From 0707ef3afb6fcff7a028a6edd1e5e424faf14562 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Wed, 11 Apr 2018 19:44:30 +0200 Subject: [PATCH 1/6] start --- parity/run.rs | 5 ++--- parity/url.rs | 36 +++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/parity/run.rs b/parity/run.rs index 6ccd4caf562..f07f67caa83 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -145,7 +145,7 @@ pub fn open_ui(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration, l let token = signer::generate_token_and_url(ws_conf, ui_conf, logger_config)?; // Open a browser - url::open(&token.url); + url::open(&token.url).map_err(|e| format!("{}", e))?; // Print a message println!("{}", token.message); Ok(()) @@ -157,10 +157,9 @@ pub fn open_dapp(dapps_conf: &dapps::Configuration, rpc_conf: &rpc::HttpConfigur } let url = format!("http://{}:{}/{}/", rpc_conf.interface, rpc_conf.port, dapp); - url::open(&url); + url::open(&url).map_err(|e| format!("{}", e))?; Ok(()) } - // node info fetcher for the local store. struct FullNodeInfo { miner: Option>, // TODO: only TXQ needed, just use that after decoupling. diff --git a/parity/url.rs b/parity/url.rs index 4189ae24184..5481b4ef427 100644 --- a/parity/url.rs +++ b/parity/url.rs @@ -16,8 +16,29 @@ //! Cross-platform open url in default browser +use std::{io, fmt, process}; + +#[derive(Debug)] +pub enum Error { + ProcessError(io::Error), +} + +impl From for Error { + fn from(err: io::Error) -> Self { + Error::ProcessError(err) + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self { + Error::ProcessError(ref e) => write!(f, "{}", e), + } + } +} + #[cfg(windows)] -pub fn open(url: &str) { +pub fn open(url: &str) -> Result<(), Error> { use std::ffi::CString; use std::ptr; use winapi::um::shellapi::ShellExecuteA; @@ -31,18 +52,19 @@ pub fn open(url: &str) { ptr::null(), Normal); } + Ok(()) } #[cfg(any(target_os="macos", target_os="freebsd"))] -pub fn open(url: &str) { - use std; - let _ = std::process::Command::new("open").arg(url).spawn(); +pub fn open(url: &str) -> Result<(), Error> { + let _ = process::Command::new("open").arg(url).spawn()?; + Ok(()) } #[cfg(target_os="linux")] -pub fn open(url: &str) { - use std; - let _ = std::process::Command::new("xdg-open").arg(url).spawn(); +pub fn open(url: &str) -> Result<(), Error> { + let _ = process::Command::new("xdg-open").arg(url).spawn()?; + Ok(()) } #[cfg(target_os="android")] From 01b8d2dafd20514e9c269a5952145c08d2b0866a Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Thu, 12 Apr 2018 00:02:07 +0200 Subject: [PATCH 2/6] add error handling for winapi --- parity/url.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/parity/url.rs b/parity/url.rs index 5481b4ef427..979f36d2d5c 100644 --- a/parity/url.rs +++ b/parity/url.rs @@ -16,11 +16,13 @@ //! Cross-platform open url in default browser -use std::{io, fmt, process}; +use std::{io, fmt, process, ffi}; -#[derive(Debug)] +#[allow(unused)] pub enum Error { ProcessError(io::Error), + FfiNull(ffi::NulError), + WindowsShellExecute, } impl From for Error { @@ -29,10 +31,18 @@ impl From for Error { } } +impl From for Error { + fn from(err: ffi::NulError) -> Self { + Error::FfiNull(err) + } +} + impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { Error::ProcessError(ref e) => write!(f, "{}", e), + Error::FfiNull(ref e) => write!(f, "{}", e), + Error::WindowsShellExecute => write!(f, "WindowsShellExecute failed"), } } } @@ -43,16 +53,21 @@ pub fn open(url: &str) -> Result<(), Error> { use std::ptr; use winapi::um::shellapi::ShellExecuteA; use winapi::um::winuser::SW_SHOWNORMAL as Normal; + use winapi::shared::minwindef::INT; - unsafe { + const WINDOWS_SHELL_EXECUTE_SUCCESS: isize = 32; + + let h_instance = unsafe { ShellExecuteA(ptr::null_mut(), - CString::new("open").unwrap().as_ptr(), - CString::new(url.to_owned().replace("\n", "%0A")).unwrap().as_ptr(), + CString::new("open")?.as_ptr(), + CString::new(url.to_owned().replace("\n", "%0A"))?.as_ptr(), ptr::null(), ptr::null(), - Normal); - } - Ok(()) + Normal) as INT + }; + // https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx + // `ShellExecute` returns a value greater than 32 on success + if h_instance > WINDOWS_SHELL_EXECUTE_SUCCESS { Ok(()) } else { Err(Error::WindowsShellExecute) } } #[cfg(any(target_os="macos", target_os="freebsd"))] From 8029d6efc9457edb6628ff788fbb978d2d8648fe Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Thu, 12 Apr 2018 00:04:11 +0200 Subject: [PATCH 3/6] fix typo --- parity/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parity/main.rs b/parity/main.rs index 24b53ae2c92..a3701d3c80c 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -232,7 +232,7 @@ fn take_spec_name_override() -> Option { #[cfg(windows)] fn global_cleanup() { // We need to cleanup all sockets before spawning another Parity process. This makes shure everything is cleaned up. - // The loop is required because of internal refernce counter for winsock dll. We don't know how many crates we use do + // The loop is required because of internal reference counter for winsock dll. We don't know how many crates we use do // initialize it. There's at least 2 now. for _ in 0.. 10 { unsafe { ::winapi::um::winsock2::WSACleanup(); } From be75230e16cc2940b373b40551e81c4d02c349e7 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Thu, 12 Apr 2018 00:19:23 +0200 Subject: [PATCH 4/6] fix warnings and windows errors --- parity/url.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/parity/url.rs b/parity/url.rs index 979f36d2d5c..5a7eada6ef7 100644 --- a/parity/url.rs +++ b/parity/url.rs @@ -16,29 +16,29 @@ //! Cross-platform open url in default browser -use std::{io, fmt, process, ffi}; +use std; #[allow(unused)] pub enum Error { - ProcessError(io::Error), - FfiNull(ffi::NulError), + ProcessError(std::io::Error), + FfiNull(std::ffi::NulError), WindowsShellExecute, } -impl From for Error { - fn from(err: io::Error) -> Self { +impl From for Error { + fn from(err: std::io::Error) -> Self { Error::ProcessError(err) } } -impl From for Error { - fn from(err: ffi::NulError) -> Self { +impl From for Error { + fn from(err: std::ffi::NulError) -> Self { Error::FfiNull(err) } } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { match *self { Error::ProcessError(ref e) => write!(f, "{}", e), Error::FfiNull(ref e) => write!(f, "{}", e), @@ -55,7 +55,7 @@ pub fn open(url: &str) -> Result<(), Error> { use winapi::um::winuser::SW_SHOWNORMAL as Normal; use winapi::shared::minwindef::INT; - const WINDOWS_SHELL_EXECUTE_SUCCESS: isize = 32; + const WINDOWS_SHELL_EXECUTE_SUCCESS: i32 = 32; let h_instance = unsafe { ShellExecuteA(ptr::null_mut(), @@ -72,13 +72,13 @@ pub fn open(url: &str) -> Result<(), Error> { #[cfg(any(target_os="macos", target_os="freebsd"))] pub fn open(url: &str) -> Result<(), Error> { - let _ = process::Command::new("open").arg(url).spawn()?; + let _ = std::process::Command::new("open").arg(url).spawn()?; Ok(()) } #[cfg(target_os="linux")] pub fn open(url: &str) -> Result<(), Error> { - let _ = process::Command::new("xdg-open").arg(url).spawn()?; + let _ = std::process::Command::new("xdg-open").arg(url).spawn()?; Ok(()) } From bf4081b882a077efdb8c232b4c47d1412aa499c4 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Thu, 12 Apr 2018 09:07:57 +0200 Subject: [PATCH 5/6] formatting --- parity/url.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/parity/url.rs b/parity/url.rs index 5a7eada6ef7..fa82fbb590d 100644 --- a/parity/url.rs +++ b/parity/url.rs @@ -26,25 +26,25 @@ pub enum Error { } impl From for Error { - fn from(err: std::io::Error) -> Self { - Error::ProcessError(err) - } + fn from(err: std::io::Error) -> Self { + Error::ProcessError(err) + } } impl From for Error { - fn from(err: std::ffi::NulError) -> Self { - Error::FfiNull(err) - } + fn from(err: std::ffi::NulError) -> Self { + Error::FfiNull(err) + } } impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - match *self { - Error::ProcessError(ref e) => write!(f, "{}", e), - Error::FfiNull(ref e) => write!(f, "{}", e), + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + match *self { + Error::ProcessError(ref e) => write!(f, "{}", e), + Error::FfiNull(ref e) => write!(f, "{}", e), Error::WindowsShellExecute => write!(f, "WindowsShellExecute failed"), - } - } + } + } } #[cfg(windows)] @@ -64,7 +64,7 @@ pub fn open(url: &str) -> Result<(), Error> { ptr::null(), ptr::null(), Normal) as INT - }; +}; // https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx // `ShellExecute` returns a value greater than 32 on success if h_instance > WINDOWS_SHELL_EXECUTE_SUCCESS { Ok(()) } else { Err(Error::WindowsShellExecute) } From dacf9dc53722573f0b3a1190109e51052181f6e5 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Fri, 13 Apr 2018 17:18:07 +0200 Subject: [PATCH 6/6] Address review comments --- parity/url.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/parity/url.rs b/parity/url.rs index fa82fbb590d..41c4e545831 100644 --- a/parity/url.rs +++ b/parity/url.rs @@ -17,12 +17,12 @@ //! Cross-platform open url in default browser use std; +use std::os::raw::c_int; #[allow(unused)] pub enum Error { ProcessError(std::io::Error), - FfiNull(std::ffi::NulError), - WindowsShellExecute, + WindowsShellExecute(c_int), } impl From for Error { @@ -31,18 +31,11 @@ impl From for Error { } } -impl From for Error { - fn from(err: std::ffi::NulError) -> Self { - Error::FfiNull(err) - } -} - impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { match *self { Error::ProcessError(ref e) => write!(f, "{}", e), - Error::FfiNull(ref e) => write!(f, "{}", e), - Error::WindowsShellExecute => write!(f, "WindowsShellExecute failed"), + Error::WindowsShellExecute(e) => write!(f, "WindowsShellExecute error: {}", e), } } } @@ -53,21 +46,25 @@ pub fn open(url: &str) -> Result<(), Error> { use std::ptr; use winapi::um::shellapi::ShellExecuteA; use winapi::um::winuser::SW_SHOWNORMAL as Normal; - use winapi::shared::minwindef::INT; - const WINDOWS_SHELL_EXECUTE_SUCCESS: i32 = 32; + const WINDOWS_SHELL_EXECUTE_SUCCESS: c_int = 32; let h_instance = unsafe { ShellExecuteA(ptr::null_mut(), - CString::new("open")?.as_ptr(), - CString::new(url.to_owned().replace("\n", "%0A"))?.as_ptr(), + CString::new("open").unwrap().as_ptr(), + CString::new(url.to_owned().replace("\n", "%0A")).unwrap().as_ptr(), ptr::null(), ptr::null(), - Normal) as INT -}; + Normal) as c_int + }; + // https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx // `ShellExecute` returns a value greater than 32 on success - if h_instance > WINDOWS_SHELL_EXECUTE_SUCCESS { Ok(()) } else { Err(Error::WindowsShellExecute) } + if h_instance > WINDOWS_SHELL_EXECUTE_SUCCESS { + Ok(()) + } else { + Err(Error::WindowsShellExecute(h_instance)) + } } #[cfg(any(target_os="macos", target_os="freebsd"))]