From b73959fc0e40774e63840c62ca5bc7a621f28d3a Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Mon, 30 May 2022 23:30:24 +0200 Subject: [PATCH] fix(windows): wrong prefix config file path (`\\?\`) when logged --- src/helpers.rs | 19 +++++++++++++++++++ src/server.rs | 7 ++++--- src/winservice.rs | 15 ++------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index c30f2e2..5862ba4 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -72,3 +72,22 @@ pub fn stringify(dst: &mut String, path: &serde_ignored::Path<'_>) { | Path::NewtypeStruct { parent } => stringify(dst, parent), } } + +#[cfg(unix)] +/// In Unix-like systems it just casts the `PathBuf` into an string. +pub fn adjust_canonicalization(p: PathBuf) -> String { + p.display().to_string() +} + +#[cfg(windows)] +/// In Windows systems it adjusts the `PathBuf` stripping its `\\?\` prefix. +pub fn adjust_canonicalization(p: PathBuf) -> String { + const VERBATIM_PREFIX: &str = r#"\\?\"#; + let p = p.to_str().unwrap_or_default(); + let p = if p.starts_with(VERBATIM_PREFIX) { + p.strip_prefix(VERBATIM_PREFIX).unwrap_or_default() + } else { + p + }; + p.to_owned() +} diff --git a/src/server.rs b/src/server.rs index f4bec7b..9cde513 100644 --- a/src/server.rs +++ b/src/server.rs @@ -83,9 +83,10 @@ impl Server { // Config-file "advanced" options let advanced_opts = self.opts.advanced; - // Config file - if general.config_file.is_some() && general.config_file.is_some() { - tracing::info!("config file: {}", general.config_file.unwrap().display()); + // Config file option + if let Some(config_file) = general.config_file { + let config_file = helpers::adjust_canonicalization(config_file); + tracing::info!("config file: {}", config_file); } // Determine TCP listener either file descriptor or TCP socket diff --git a/src/winservice.rs b/src/winservice.rs index 1a03308..2a81207 100644 --- a/src/winservice.rs +++ b/src/winservice.rs @@ -16,7 +16,7 @@ use windows_service::{ service_manager::{ServiceManager, ServiceManagerAccess}, }; -use crate::{logger, Context, Result, Server, Settings}; +use crate::{helpers, logger, Context, Result, Server, Settings}; const SERVICE_NAME: &str = "static-web-server"; const SERVICE_TYPE: ServiceType = ServiceType::OWN_PROCESS; @@ -167,17 +167,6 @@ pub fn run_server_as_service() -> Result { Ok(()) } -fn adjust_canonicalization(p: PathBuf) -> String { - const VERBATIM_PREFIX: &str = r#"\\?\"#; - let p = p.to_str().unwrap_or_default(); - let p = if p.starts_with(VERBATIM_PREFIX) { - p.strip_prefix(VERBATIM_PREFIX).unwrap_or_default() - } else { - p - }; - p.to_owned() -} - /// Install a Windows Service for SWS. pub fn install_service(config_file: Option) -> Result { let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE; @@ -191,7 +180,7 @@ pub fn install_service(config_file: Option) -> Result { // Append a `--config-file` path to the binary arguments if present if let Some(f) = config_file { - let f = adjust_canonicalization(f); + let f = helpers::adjust_canonicalization(f); if !f.is_empty() { service_binary_arguments.push(OsString::from(["--config-file=", &f].concat())); } -- libgit2 1.7.2