Skip to content

Commit

Permalink
fix(windows): wrong prefix config file path (\\?\) when logged
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed May 30, 2022
1 parent 61e1f4d commit b73959f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
19 changes: 19 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
7 changes: 4 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions src/winservice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PathBuf>) -> Result {
let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
Expand All @@ -191,7 +180,7 @@ pub fn install_service(config_file: Option<PathBuf>) -> 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()));
}
Expand Down

0 comments on commit b73959f

Please sign in to comment.