Skip to content

Commit b73959f

Browse files
committed
fix(windows): wrong prefix config file path (\\?\) when logged
1 parent 61e1f4d commit b73959f

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/helpers.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,22 @@ pub fn stringify(dst: &mut String, path: &serde_ignored::Path<'_>) {
7272
| Path::NewtypeStruct { parent } => stringify(dst, parent),
7373
}
7474
}
75+
76+
#[cfg(unix)]
77+
/// In Unix-like systems it just casts the `PathBuf` into an string.
78+
pub fn adjust_canonicalization(p: PathBuf) -> String {
79+
p.display().to_string()
80+
}
81+
82+
#[cfg(windows)]
83+
/// In Windows systems it adjusts the `PathBuf` stripping its `\\?\` prefix.
84+
pub fn adjust_canonicalization(p: PathBuf) -> String {
85+
const VERBATIM_PREFIX: &str = r#"\\?\"#;
86+
let p = p.to_str().unwrap_or_default();
87+
let p = if p.starts_with(VERBATIM_PREFIX) {
88+
p.strip_prefix(VERBATIM_PREFIX).unwrap_or_default()
89+
} else {
90+
p
91+
};
92+
p.to_owned()
93+
}

src/server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ impl Server {
8383
// Config-file "advanced" options
8484
let advanced_opts = self.opts.advanced;
8585

86-
// Config file
87-
if general.config_file.is_some() && general.config_file.is_some() {
88-
tracing::info!("config file: {}", general.config_file.unwrap().display());
86+
// Config file option
87+
if let Some(config_file) = general.config_file {
88+
let config_file = helpers::adjust_canonicalization(config_file);
89+
tracing::info!("config file: {}", config_file);
8990
}
9091

9192
// Determine TCP listener either file descriptor or TCP socket

src/winservice.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use windows_service::{
1616
service_manager::{ServiceManager, ServiceManagerAccess},
1717
};
1818

19-
use crate::{logger, Context, Result, Server, Settings};
19+
use crate::{helpers, logger, Context, Result, Server, Settings};
2020

2121
const SERVICE_NAME: &str = "static-web-server";
2222
const SERVICE_TYPE: ServiceType = ServiceType::OWN_PROCESS;
@@ -167,17 +167,6 @@ pub fn run_server_as_service() -> Result {
167167
Ok(())
168168
}
169169

170-
fn adjust_canonicalization(p: PathBuf) -> String {
171-
const VERBATIM_PREFIX: &str = r#"\\?\"#;
172-
let p = p.to_str().unwrap_or_default();
173-
let p = if p.starts_with(VERBATIM_PREFIX) {
174-
p.strip_prefix(VERBATIM_PREFIX).unwrap_or_default()
175-
} else {
176-
p
177-
};
178-
p.to_owned()
179-
}
180-
181170
/// Install a Windows Service for SWS.
182171
pub fn install_service(config_file: Option<PathBuf>) -> Result {
183172
let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
@@ -191,7 +180,7 @@ pub fn install_service(config_file: Option<PathBuf>) -> Result {
191180

192181
// Append a `--config-file` path to the binary arguments if present
193182
if let Some(f) = config_file {
194-
let f = adjust_canonicalization(f);
183+
let f = helpers::adjust_canonicalization(f);
195184
if !f.is_empty() {
196185
service_binary_arguments.push(OsString::from(["--config-file=", &f].concat()));
197186
}

0 commit comments

Comments
 (0)