1+ use std:: ffi:: CStr ;
12use std:: ffi:: OsStr ;
23use std:: fs;
34use std:: fs:: read_dir;
45use std:: io:: Result ;
6+ use std:: os:: unix:: ffi:: OsStrExt ;
57use std:: path:: Path ;
68use std:: path:: PathBuf ;
79use std:: process:: Command ;
810use 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) ]
2914pub 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
4833impl 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