@@ -9,13 +9,14 @@ mod install;
9
9
mod serve;
10
10
pub mod util;
11
11
12
+ use anyhow:: Result ;
12
13
use clap:: { Args , Parser , Subcommand } ;
13
14
use std:: io:: { BufRead , Write } ;
14
15
use std:: net:: SocketAddr ;
15
16
use std:: path:: { Path , PathBuf } ;
16
17
17
18
pub trait Running {
18
- fn run ( self ) -> anyhow :: Result < ( ) > ;
19
+ fn run ( self ) -> Result < ( ) > ;
19
20
}
20
21
21
22
#[ derive( Parser ) ]
@@ -69,7 +70,7 @@ impl InstallConfig {
69
70
const PATH : & ' static str = "/etc/.thunder" ;
70
71
71
72
/// Remove config file
72
- pub fn remove_file ( self ) -> anyhow :: Result < ( ) > {
73
+ pub fn remove_file ( self ) -> Result < ( ) > {
73
74
let path = Path :: new ( Self :: PATH ) ;
74
75
if path. exists ( ) {
75
76
std:: fs:: remove_file ( & Self :: PATH ) ?;
@@ -78,7 +79,7 @@ impl InstallConfig {
78
79
}
79
80
80
81
/// Write to file
81
- fn write_to_file ( & self ) -> anyhow :: Result < ( ) > {
82
+ fn write_to_file ( & self ) -> Result < ( ) > {
82
83
let path = Path :: new ( Self :: PATH ) ;
83
84
if !path. exists ( ) {
84
85
let mut file = std:: fs:: File :: create ( path) ?;
@@ -175,7 +176,7 @@ pub struct ServeConfig {
175
176
tls_key : Option < PathBuf > ,
176
177
}
177
178
178
- fn main ( ) -> anyhow :: Result < ( ) > {
179
+ fn main ( ) -> Result < ( ) > {
179
180
let opt = Opt :: parse ( ) ;
180
181
181
182
match opt. commands {
@@ -187,12 +188,16 @@ fn main() -> anyhow::Result<()> {
187
188
let install_config = InstallConfig :: read_from_file ( ) . map_or ( None , |v| Some ( v) ) ;
188
189
install:: XunleiUninstall ( install_config) . run ( ) ?;
189
190
}
190
- Commands :: Run ( config) => {
191
- serve:: Serve :: new ( config, InstallConfig :: read_from_file ( ) ?) . run ( ) ?;
191
+ Commands :: Run ( server_config) => {
192
+ let install_config = InstallConfig :: read_from_file ( ) ?;
193
+ before_action ( & install_config) ?;
194
+ serve:: Serve :: new ( server_config, install_config) . run ( ) ?;
192
195
}
193
- Commands :: Start ( config) => {
196
+ Commands :: Start ( server_config) => {
197
+ let install_config = InstallConfig :: read_from_file ( ) ?;
198
+ before_action ( & install_config) ?;
194
199
daemon:: start ( ) ?;
195
- serve:: Serve :: new ( config , InstallConfig :: read_from_file ( ) ? ) . run ( ) ?;
200
+ serve:: Serve :: new ( server_config , install_config ) . run ( ) ?;
196
201
}
197
202
Commands :: Stop => {
198
203
daemon:: stop ( ) ?;
@@ -206,3 +211,29 @@ fn main() -> anyhow::Result<()> {
206
211
}
207
212
Ok ( ( ) )
208
213
}
214
+
215
+ /// Running before the daemon starts, execute the following code
216
+ fn before_action ( install_config : & InstallConfig ) -> Result < ( ) > {
217
+ #[ cfg( target_os = "linux" ) ]
218
+ use nix:: mount:: MsFlags ;
219
+
220
+ #[ cfg( target_os = "linux" ) ]
221
+ let _ = nix:: mount:: umount ( & install_config. mount_bind_download_path ) ;
222
+ #[ cfg( target_os = "linux" ) ]
223
+ if nix:: mount:: mount (
224
+ Some ( & install_config. download_path ) ,
225
+ & install_config. mount_bind_download_path ,
226
+ <Option < & ' static [ u8 ] > >:: None ,
227
+ MsFlags :: MS_BIND ,
228
+ <Option < & ' static [ u8 ] > >:: None ,
229
+ )
230
+ . is_err ( )
231
+ {
232
+ anyhow:: bail!(
233
+ "Mount {} to {} failed" ,
234
+ install_config. download_path. display( ) ,
235
+ install_config. mount_bind_download_path. display( )
236
+ ) ;
237
+ }
238
+ Ok ( ( ) )
239
+ }
0 commit comments