Skip to content

Commit 1aa4336

Browse files
committed
fix: use binded const str
1 parent 3f94301 commit 1aa4336

File tree

3 files changed

+32
-55
lines changed

3 files changed

+32
-55
lines changed

Diff for: nginx-sys/build.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,14 @@ fn main() -> Result<(), Box<dyn StdError>> {
132132
}
133133
println!("Verified GPG permissions");
134134
// Configure and Compile NGINX
135-
let (nginx_install_dir, nginx_src_dir) = compile_nginx(&cache_dir)?;
135+
let (_, nginx_src_dir) = compile_nginx(&cache_dir)?;
136136
// Hint cargo to rebuild if any of the these environment variables values change
137137
// because they will trigger a recompilation of NGINX with different parameters
138138
for var in ENV_VARS_TRIGGERING_RECOMPILE {
139139
println!("cargo::rerun-if-env-changed={var}");
140140
}
141141
println!("cargo::rerun-if-changed=build.rs");
142142
println!("cargo::rerun-if-changed=wrapper.h");
143-
// Provide build metadata for integration tests
144-
println!(
145-
"cargo::rustc-env=NGINX_SYS_NGINX_INSTALL_DIR={}",
146-
nginx_install_dir.display()
147-
);
148143
// Read autoconf generated makefile for NGINX and generate Rust bindings based on its includes
149144
generate_binding(nginx_src_dir);
150145
Ok(())

Diff for: nginx-sys/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,3 @@ pub unsafe fn add_to_ngx_table(
206206
table.lowcase_key = str_to_uchar(pool, String::from(key).to_ascii_lowercase().as_str());
207207
})
208208
}
209-
210-
/// Provide build metadatas.
211-
pub mod metadata {
212-
/// The path to the Nginx directory used to generate bindings. This constants is aimed at integration tests.
213-
pub const NGINX_INSTALL_DIR: &str = env!("NGINX_SYS_NGINX_INSTALL_DIR");
214-
}

Diff for: src/test_util.rs

+31-43
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1+
use std::ffi::CStr;
12
use std::ffi::OsStr;
23
use std::fs;
34
use std::fs::read_dir;
45
use std::io::Result;
6+
use std::os::unix::ffi::OsStrExt;
57
use std::path::Path;
68
use std::path::PathBuf;
79
use std::process::Command;
810
use std::process::Output;
911

10-
const NGINX_PREFIX: &str = nginx_sys::metadata::NGINX_INSTALL_DIR;
11-
12-
const NGINX_SBIN_SUFFIX: &str = "sbin/nginx";
13-
const NGINX_MODULES_SUFFIX: &str = "modules";
14-
const NGINX_CONF_SUFFIX: &str = "conf/nginx.conf";
15-
const NGINX_CONF_PREFIX_SUFFIX: &str = "conf";
16-
const NGINX_ERROR_LOG_SUFFIX: &str = "logs/error.log";
17-
const NGINX_PID_SUFFIX: &str = "logs/nginx.pid";
18-
const NGINX_LOCK_SUFFIX: &str = "logs/nginx.lock";
19-
20-
const NGINX_HTTP_LOG_SUFFIX: &str = "logs/access.log";
21-
const NGINX_HTTP_CLIENT_BODY_SUFFIX: &str = "client_body_temp";
22-
const NGINX_HTTP_PROXY_TEMP_SUFFIX: &str = "proxy_temp";
23-
const NGINX_HTTP_FASTCGI_TEMP_SUFFIX: &str = "fastcgi_temp";
24-
const NGINX_HTTP_UWSGI_TEMP_SUFFIX: &str = "uwsgi_temp";
25-
const NGINX_HTTP_SCGI_TEMP_SUFFIX: &str = "scgi_temp";
26-
2712
/// harness to test nginx
2813
#[allow(dead_code)]
2914
pub struct Nginx {
3015
// these paths have options to change them from default paths (in prefix dir)
3116
// most of them are not used, but keep them for future uses
3217
prefix: PathBuf,
3318
sbin_path: PathBuf,
34-
modules_path: PathBuf,
19+
modules_path: PathBuf, // and only this path is not embedded in bindings.rs, since the module root is same to the prefix
3520
conf_path: PathBuf,
3621
conf_prefix: PathBuf,
3722
error_log_path: PathBuf,
@@ -47,31 +32,34 @@ pub struct Nginx {
4732

4833
impl Default for Nginx {
4934
fn default() -> Nginx {
50-
Self::new_with_prefix(NGINX_PREFIX.into())
51-
}
52-
}
53-
54-
impl Nginx {
55-
/// create nginx with prefix only
56-
pub fn new_with_prefix(prefix: PathBuf) -> Nginx {
35+
// we load consts in bindings.rs here, since join() and from_bytes() is not const fn for now
36+
fn from_bytes_with_nul(slice: &[u8]) -> &OsStr {
37+
OsStr::from_bytes(CStr::from_bytes_with_nul(slice).unwrap().to_bytes())
38+
}
39+
let prefix: PathBuf = from_bytes_with_nul(nginx_sys::NGX_PREFIX).into();
40+
fn concat_slice(prefix: &PathBuf, slice: &[u8]) -> PathBuf {
41+
prefix.join(from_bytes_with_nul(slice))
42+
}
5743
Nginx {
58-
sbin_path: prefix.join(NGINX_SBIN_SUFFIX),
59-
modules_path: prefix.join(NGINX_MODULES_SUFFIX),
60-
conf_path: prefix.join(NGINX_CONF_SUFFIX),
61-
conf_prefix: prefix.join(NGINX_CONF_PREFIX_SUFFIX),
62-
error_log_path: prefix.join(NGINX_ERROR_LOG_SUFFIX),
63-
pid_path: prefix.join(NGINX_PID_SUFFIX),
64-
lock_path: prefix.join(NGINX_LOCK_SUFFIX),
65-
http_log_path: prefix.join(NGINX_HTTP_LOG_SUFFIX),
66-
http_client_body_temp_path: prefix.join(NGINX_HTTP_CLIENT_BODY_SUFFIX),
67-
http_proxy_temp_path: prefix.join(NGINX_HTTP_PROXY_TEMP_SUFFIX),
68-
http_fastcgi_temp_path: prefix.join(NGINX_HTTP_FASTCGI_TEMP_SUFFIX),
69-
http_uwsgi_temp_path: prefix.join(NGINX_HTTP_UWSGI_TEMP_SUFFIX),
70-
http_scgi_temp_path: prefix.join(NGINX_HTTP_SCGI_TEMP_SUFFIX),
44+
sbin_path: concat_slice(&prefix, nginx_sys::NGX_SBIN_PATH),
45+
modules_path: prefix.join("modules"),
46+
conf_path: concat_slice(&prefix, nginx_sys::NGX_CONF_PATH),
47+
conf_prefix: concat_slice(&prefix, nginx_sys::NGX_CONF_PREFIX),
48+
error_log_path: concat_slice(&prefix, nginx_sys::NGX_ERROR_LOG_PATH),
49+
pid_path: concat_slice(&prefix, nginx_sys::NGX_PID_PATH),
50+
lock_path: concat_slice(&prefix, nginx_sys::NGX_LOCK_PATH),
51+
http_log_path: concat_slice(&prefix, nginx_sys::NGX_HTTP_LOG_PATH),
52+
http_client_body_temp_path: concat_slice(&prefix, nginx_sys::NGX_HTTP_CLIENT_TEMP_PATH),
53+
http_proxy_temp_path: concat_slice(&prefix, nginx_sys::NGX_HTTP_PROXY_TEMP_PATH),
54+
http_fastcgi_temp_path: concat_slice(&prefix, nginx_sys::NGX_HTTP_FASTCGI_TEMP_PATH),
55+
http_uwsgi_temp_path: concat_slice(&prefix, nginx_sys::NGX_HTTP_UWSGI_TEMP_PATH),
56+
http_scgi_temp_path: concat_slice(&prefix, nginx_sys::NGX_HTTP_SCGI_TEMP_PATH),
7157
prefix,
7258
}
7359
}
60+
}
7461

62+
impl Nginx {
7563
/// execute nginx process with arguments
7664
pub fn cmd(&mut self, args: &[&str]) -> Result<Output> {
7765
let result = Command::new(&self.sbin_path).args(args).output();
@@ -112,8 +100,8 @@ impl Nginx {
112100
.join(conf_path_from.file_name().unwrap_or(OsStr::new("unknown_conf")));
113101
println!(
114102
"copying config from: {} to: {}",
115-
conf_path_to.display(),
116-
conf_path_from.display()
103+
conf_path_from.display(),
104+
conf_path_to.display()
117105
); // replace with logging
118106
fs::copy(conf_path_from, conf_path_to)
119107
}
@@ -127,7 +115,7 @@ impl Nginx {
127115
); // replace with logging
128116
fs::write(conf_path_to, conf_content)
129117
}
130-
/// ensure the existance module dir
118+
/// ensure the existance of module dir
131119
fn ensure_module_dir(&mut self) -> Result<()> {
132120
fs::create_dir_all(&self.modules_path)
133121
}
@@ -139,8 +127,8 @@ impl Nginx {
139127
.join(module_path_from.file_name().unwrap_or(OsStr::new("unknown_module")));
140128
println!(
141129
"copying module from: {} to: {}",
142-
module_path_to.display(),
143-
module_path_from.display()
130+
module_path_from.display(),
131+
module_path_to.display()
144132
); // replace with logging
145133
fs::copy(module_path_from, module_path_to)
146134
}

0 commit comments

Comments
 (0)