index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2022-05-30 21:30:24.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2022-05-30 21:35:17.0 +00:00:00
commit
b73959fc0e40774e63840c62ca5bc7a621f28d3a [patch]
tree
4cfa7c2fb948add03a910141380ff1013cf776c8
parent
61e1f4d3cd54d79dbe32e1b4172a445e2b6cde7f
download
b73959fc0e40774e63840c62ca5bc7a621f28d3a.tar.gz

fix(windows): wrong prefix config file path (`\\?\`) when logged



Diff

 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<PathBuf>) -> Result {
    let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
@@ -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()));
        }