@@ -18,63 +18,88 @@ use std::net::Ipv4Addr;
18
18
use std:: path:: { Path , PathBuf } ;
19
19
use std:: process:: Command ;
20
20
use std:: str:: FromStr ;
21
- use sysinfo:: { ProcessExt , RefreshKind , System , SystemExt } ;
21
+ use sysinfo:: { ProcessExt , ProcessRefreshKind , RefreshKind , System , SystemExt } ;
22
22
use users:: { get_current_uid, get_user_by_uid} ;
23
23
use walkdir:: WalkDir ;
24
24
25
25
pub fn config_dir ( ) -> anyhow:: Result < PathBuf > {
26
- let mut pathbuf = PathBuf :: new ( ) ;
27
- let _res: ( ) = if let Ok ( home) = std:: env:: var ( "HOME" ) {
28
- let confpath = format ! ( "{}/.config" , home) ;
29
- let path = Path :: new ( & confpath) ;
30
- debug ! (
31
- "Using config dir from $HOME config: {}" ,
32
- path. to_string_lossy( )
33
- ) ;
34
- if path. exists ( ) {
35
- pathbuf. push ( path) ;
36
- Ok ( ( ) )
37
- } else {
38
- Err ( anyhow ! ( "Could not find valid config directory!" ) )
39
- }
40
- } else if let Ok ( user) = std:: env:: var ( "SUDO_USER" ) {
41
- // TODO: DRY
42
- let confpath = format ! ( "/home/{}/.config" , user) ;
43
- let path = Path :: new ( & confpath) ;
44
- debug ! (
45
- "Using config dir from $SUDO_USER config: {}" ,
46
- path. to_string_lossy( )
47
- ) ;
48
- if path. exists ( ) {
49
- pathbuf. push ( path) ;
50
- Ok ( ( ) )
51
- } else {
52
- Err ( anyhow ! ( "Could not find valid config directory!" ) )
53
- }
54
- } else if let Some ( base_dirs) = BaseDirs :: new ( ) {
55
- debug ! (
56
- "Using config dir from XDG dirs: {}" ,
57
- base_dirs. config_dir( ) . to_string_lossy( )
58
- ) ;
59
- pathbuf. push ( base_dirs. config_dir ( ) ) ;
60
- Ok ( ( ) )
61
- } else if let Some ( user) = get_user_by_uid ( get_current_uid ( ) ) {
62
- let confpath = format ! ( "/home/{}/.config" , user. name( ) . to_str( ) . unwrap( ) ) ;
63
- let path = Path :: new ( & confpath) ;
64
- debug ! (
65
- "Using config dir from current user config: {}" ,
66
- path. to_string_lossy( )
67
- ) ;
68
- if path. exists ( ) {
69
- pathbuf. push ( path) ;
70
- Ok ( ( ) )
71
- } else {
72
- Err ( anyhow ! ( "Could not find valid config directory!" ) )
73
- }
74
- } else {
75
- Err ( anyhow ! ( "Could not find valid config directory!" ) )
76
- } ?;
77
- Ok ( pathbuf)
26
+ let path: Option < PathBuf > = None
27
+ . or_else ( || {
28
+ if let Ok ( home) = std:: env:: var ( "HOME" ) {
29
+ let confpath = format ! ( "{}/.config" , home) ;
30
+ let path = Path :: new ( & confpath) ;
31
+ debug ! (
32
+ "Using config dir from $HOME config: {}" ,
33
+ path. to_string_lossy( )
34
+ ) ;
35
+ if path. exists ( ) {
36
+ // Work-around for case when root $HOME is set but user's is not
37
+ // It seems we cannot distinguish these cases
38
+ if path. to_string_lossy ( ) . contains ( "/root" ) {
39
+ None
40
+ } else {
41
+ Some ( path. into ( ) )
42
+ }
43
+ } else {
44
+ None
45
+ }
46
+ } else {
47
+ None
48
+ }
49
+ } )
50
+ . or_else ( || {
51
+ if let Ok ( user) = std:: env:: var ( "SUDO_USER" ) {
52
+ // TODO: DRY
53
+ let confpath = format ! ( "/home/{}/.config" , user) ;
54
+ let path = Path :: new ( & confpath) ;
55
+ debug ! (
56
+ "Using config dir from $SUDO_USER config: {}" ,
57
+ path. to_string_lossy( )
58
+ ) ;
59
+ if path. exists ( ) {
60
+ Some ( path. into ( ) )
61
+ } else {
62
+ None
63
+ }
64
+ } else {
65
+ None
66
+ }
67
+ } )
68
+ . or_else ( || {
69
+ if let Some ( base_dirs) = BaseDirs :: new ( ) {
70
+ debug ! (
71
+ "Using config dir from XDG dirs: {}" ,
72
+ base_dirs. config_dir( ) . to_string_lossy( )
73
+ ) ;
74
+ Some ( base_dirs. config_dir ( ) . into ( ) )
75
+ } else {
76
+ None
77
+ }
78
+ } )
79
+ . or_else ( || {
80
+ if let Some ( user) = get_user_by_uid ( get_current_uid ( ) ) {
81
+ // Handles case when run as root directly
82
+ let confpath = if get_current_uid ( ) == 0 {
83
+ "/root/.config" . to_string ( )
84
+ } else {
85
+ format ! ( "/home/{}/.config" , user. name( ) . to_str( ) . unwrap( ) )
86
+ } ;
87
+ let path = Path :: new ( & confpath) ;
88
+ debug ! (
89
+ "Using config dir from current user config: {}" ,
90
+ path. to_string_lossy( )
91
+ ) ;
92
+ if path. exists ( ) {
93
+ Some ( path. into ( ) )
94
+ } else {
95
+ None
96
+ }
97
+ } else {
98
+ None
99
+ }
100
+ } ) ;
101
+
102
+ path. ok_or_else ( || anyhow ! ( "Could not find valid config directory!" ) )
78
103
}
79
104
80
105
pub fn vopono_dir ( ) -> anyhow:: Result < PathBuf > {
@@ -161,17 +186,20 @@ pub fn get_existing_namespaces() -> anyhow::Result<Vec<String>> {
161
186
}
162
187
163
188
pub fn check_process_running ( pid : u32 ) -> bool {
164
- let s = System :: new_with_specifics ( RefreshKind :: new ( ) . with_processes ( ) ) ;
189
+ let s =
190
+ System :: new_with_specifics ( RefreshKind :: new ( ) . with_processes ( ProcessRefreshKind :: new ( ) ) ) ;
165
191
s. process ( pid as i32 ) . is_some ( )
166
192
}
167
193
168
194
pub fn get_all_running_pids ( ) -> Vec < u32 > {
169
- let s = System :: new_with_specifics ( RefreshKind :: new ( ) . with_processes ( ) ) ;
195
+ let s =
196
+ System :: new_with_specifics ( RefreshKind :: new ( ) . with_processes ( ProcessRefreshKind :: new ( ) ) ) ;
170
197
s. processes ( ) . keys ( ) . map ( |x| * x as u32 ) . collect ( )
171
198
}
172
199
173
200
pub fn get_all_running_process_names ( ) -> Vec < String > {
174
- let s = System :: new_with_specifics ( RefreshKind :: new ( ) . with_processes ( ) ) ;
201
+ let s =
202
+ System :: new_with_specifics ( RefreshKind :: new ( ) . with_processes ( ProcessRefreshKind :: new ( ) ) ) ;
175
203
s. processes ( )
176
204
. values ( )
177
205
. map ( |x| x. name ( ) . to_string ( ) )
0 commit comments