@@ -4,12 +4,19 @@ use failure::Error;
44use std:: env;
55use std:: ffi:: OsString ;
66use std:: path;
7- use std:: process:: Command ;
7+
8+ #[ derive( Debug , Clone , Copy ) ]
9+ pub struct User {
10+ /// The users uid
11+ pub uid : nix:: unistd:: Uid ,
12+ /// The users gid
13+ pub gid : nix:: unistd:: Gid ,
14+ }
815
916#[ derive( Debug ) ]
1017pub struct Environment {
1118 /// User uid and gid
12- pub user_details : ( String , String ) ,
19+ pub user_details : User ,
1320 /// The directory floki was launched in
1421 pub current_directory : path:: PathBuf ,
1522 /// The root directory for floki (may be different from
@@ -28,30 +35,24 @@ impl Environment {
2835 /// Gather information on the environment floki is running in
2936 pub fn gather ( config_file : & Option < path:: PathBuf > ) -> Result < Self , Error > {
3037 let ( floki_root, config_path) = resolve_floki_root_and_config ( config_file) ?;
38+ let user = get_user_details ( ) ;
3139 Ok ( Environment {
32- user_details : get_user_details ( ) ? ,
40+ user_details : user ,
3341 current_directory : get_current_working_directory ( ) ?,
3442 floki_root : floki_root,
3543 config_file : normalize_path ( config_path) ?,
3644 ssh_agent_socket : get_ssh_agent_socket_path ( ) ,
37- floki_workspace : get_floki_work_path ( ) ? ,
45+ floki_workspace : get_floki_work_path ( user . uid ) ,
3846 } )
3947 }
4048}
4149
42- /// Run a command and extract stdout as a String
43- fn run_and_get_raw_output ( cmd : & mut Command ) -> Result < String , Error > {
44- let output = String :: from_utf8 ( cmd. output ( ) ?. stdout ) ?;
45- Ok ( output. trim_end ( ) . into ( ) )
46- }
47-
4850/// Get the user and group ids of the current user
49- fn get_user_details ( ) -> Result < ( String , String ) , Error > {
50- let user = run_and_get_raw_output ( Command :: new ( "id" ) . arg ( "-u" ) ) ?;
51- debug ! ( "User's current id: {:?}" , user) ;
52- let group = run_and_get_raw_output ( Command :: new ( "id" ) . arg ( "-g" ) ) ?;
53- debug ! ( "User's current group: {:?}" , group) ;
54- Ok ( ( user, group) )
51+ fn get_user_details ( ) -> User {
52+ let uid = nix:: unistd:: getuid ( ) ;
53+ let gid = nix:: unistd:: getgid ( ) ;
54+ debug ! ( "Current user has uid {} and group {}" , uid, gid) ;
55+ User { uid, gid }
5556}
5657
5758/// Get the current working directory as a String
@@ -100,11 +101,9 @@ fn resolve_floki_root_and_config(
100101}
101102
102103/// Resolve a directory for floki to use for user-global file (caches etc)
103- fn get_floki_work_path ( ) -> Result < path:: PathBuf , Error > {
104- let root: path:: PathBuf = env:: var ( "HOME" )
105- . unwrap_or ( format ! ( "/tmp/{}/" , get_user_details( ) ?. 0 ) )
106- . into ( ) ;
107- Ok ( root. join ( ".floki" ) )
104+ fn get_floki_work_path ( uid : nix:: unistd:: Uid ) -> path:: PathBuf {
105+ let root: path:: PathBuf = env:: var ( "HOME" ) . unwrap_or ( format ! ( "/tmp/{}/" , uid) ) . into ( ) ;
106+ root. join ( ".floki" )
108107}
109108
110109/// Normalize the filepath - this turns a relative path into an absolute one - to
0 commit comments