@@ -2,12 +2,18 @@ use bluer::Address;
2
2
use serde:: de:: { self , Deserializer , Error as DeError , Visitor } ;
3
3
use serde:: { Deserialize , Serialize } ;
4
4
use simplelog:: * ;
5
- use std:: fmt:: { self , Display } ;
6
- use std:: fs;
7
- use std:: path:: PathBuf ;
8
- use std:: str:: FromStr ;
5
+ use std:: {
6
+ fmt:: { self , Display } ,
7
+ fs, io,
8
+ path:: PathBuf ,
9
+ process:: { Command , Stdio } ,
10
+ str:: FromStr ,
11
+ } ;
9
12
use toml_edit:: { value, DocumentMut } ;
10
13
14
+ // module name for logging engine
15
+ const NAME : & str = "<i><bright-black> config: </>" ;
16
+
11
17
#[ derive(
12
18
clap:: ValueEnum , Default , Debug , PartialEq , PartialOrd , Clone , Copy , Deserialize , Serialize ,
13
19
) ]
@@ -170,6 +176,34 @@ impl Default for AppConfig {
170
176
}
171
177
}
172
178
179
+ /// Remount `/` as readonly (`lock = true`) or read-write (`lock = false`)
180
+ fn remount_root ( lock : bool ) -> io:: Result < ( ) > {
181
+ let mode = if lock { "remount,ro" } else { "remount,rw" } ;
182
+
183
+ let status = Command :: new ( "mount" )
184
+ . args ( & [ "-o" , mode, "/" ] )
185
+ . stdout ( Stdio :: null ( ) )
186
+ . stderr ( Stdio :: null ( ) )
187
+ . status ( ) ?;
188
+
189
+ if status. success ( ) {
190
+ info ! (
191
+ "{} Remount as {} successful" ,
192
+ NAME ,
193
+ if lock { "read-only" } else { "read-write" }
194
+ ) ;
195
+ } else {
196
+ error ! (
197
+ "{} Remount as {} failed: {:?}" ,
198
+ NAME ,
199
+ if lock { "read-only" } else { "read-write" } ,
200
+ status
201
+ ) ;
202
+ }
203
+
204
+ Ok ( ( ) )
205
+ }
206
+
173
207
impl AppConfig {
174
208
pub fn load ( config_file : PathBuf ) -> Result < Self , Box < dyn std:: error:: Error > > {
175
209
use :: config:: File ;
@@ -235,6 +269,13 @@ impl AppConfig {
235
269
doc[ "ev_battery_capacity" ] = value ( self . ev_battery_capacity as i64 ) ;
236
270
doc[ "ev_factor" ] = value ( self . ev_factor as f64 ) ;
237
271
272
+ let _ = remount_root ( false ) ;
273
+ info ! (
274
+ "{} Saving new configuration to file: {}" ,
275
+ NAME ,
276
+ config_file. display( )
277
+ ) ;
238
278
let _ = fs:: write ( config_file, doc. to_string ( ) ) ;
279
+ let _ = remount_root ( true ) ;
239
280
}
240
281
}
0 commit comments