1
+ use std:: ffi:: CStr ;
1
2
use std:: ffi:: OsStr ;
2
3
use std:: fs;
3
4
use std:: fs:: read_dir;
4
5
use std:: io:: Result ;
6
+ use std:: os:: unix:: ffi:: OsStrExt ;
5
7
use std:: path:: Path ;
6
8
use std:: path:: PathBuf ;
7
9
use std:: process:: Command ;
8
10
use std:: process:: Output ;
9
11
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
-
27
12
/// harness to test nginx
28
13
#[ allow( dead_code) ]
29
14
pub struct Nginx {
30
15
// these paths have options to change them from default paths (in prefix dir)
31
16
// most of them are not used, but keep them for future uses
32
17
prefix : PathBuf ,
33
18
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
35
20
conf_path : PathBuf ,
36
21
conf_prefix : PathBuf ,
37
22
error_log_path : PathBuf ,
@@ -47,31 +32,34 @@ pub struct Nginx {
47
32
48
33
impl Default for Nginx {
49
34
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
+ }
57
43
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 ) ,
71
57
prefix,
72
58
}
73
59
}
60
+ }
74
61
62
+ impl Nginx {
75
63
/// execute nginx process with arguments
76
64
pub fn cmd ( & mut self , args : & [ & str ] ) -> Result < Output > {
77
65
let result = Command :: new ( & self . sbin_path ) . args ( args) . output ( ) ;
@@ -112,8 +100,8 @@ impl Nginx {
112
100
. join ( conf_path_from. file_name ( ) . unwrap_or ( OsStr :: new ( "unknown_conf" ) ) ) ;
113
101
println ! (
114
102
"copying config from: {} to: {}" ,
115
- conf_path_to . display( ) ,
116
- conf_path_from . display( )
103
+ conf_path_from . display( ) ,
104
+ conf_path_to . display( )
117
105
) ; // replace with logging
118
106
fs:: copy ( conf_path_from, conf_path_to)
119
107
}
@@ -127,7 +115,7 @@ impl Nginx {
127
115
) ; // replace with logging
128
116
fs:: write ( conf_path_to, conf_content)
129
117
}
130
- /// ensure the existance module dir
118
+ /// ensure the existance of module dir
131
119
fn ensure_module_dir ( & mut self ) -> Result < ( ) > {
132
120
fs:: create_dir_all ( & self . modules_path )
133
121
}
@@ -139,8 +127,8 @@ impl Nginx {
139
127
. join ( module_path_from. file_name ( ) . unwrap_or ( OsStr :: new ( "unknown_module" ) ) ) ;
140
128
println ! (
141
129
"copying module from: {} to: {}" ,
142
- module_path_to . display( ) ,
143
- module_path_from . display( )
130
+ module_path_from . display( ) ,
131
+ module_path_to . display( )
144
132
) ; // replace with logging
145
133
fs:: copy ( module_path_from, module_path_to)
146
134
}
0 commit comments