@@ -2051,6 +2051,8 @@ fn show_dir_name(
2051
2051
struct ListState < ' a > {
2052
2052
out : BufWriter < Stdout > ,
2053
2053
style_manager : Option < StyleManager < ' a > > ,
2054
+ uid_cache : HashMap < u32 , String > ,
2055
+ gid_cache : HashMap < u32 , String > ,
2054
2056
}
2055
2057
2056
2058
#[ allow( clippy:: cognitive_complexity) ]
@@ -2063,6 +2065,8 @@ pub fn list(locs: Vec<&Path>, config: &Config) -> UResult<()> {
2063
2065
let mut state = ListState {
2064
2066
out : BufWriter :: new ( stdout ( ) ) ,
2065
2067
style_manager : config. color . as_ref ( ) . map ( StyleManager :: new) ,
2068
+ uid_cache : HashMap :: new ( ) ,
2069
+ gid_cache : HashMap :: new ( ) ,
2066
2070
} ;
2067
2071
2068
2072
for loc in locs {
@@ -2397,10 +2401,10 @@ fn get_metadata_with_deref_opt(p_buf: &Path, dereference: bool) -> std::io::Resu
2397
2401
fn display_dir_entry_size (
2398
2402
entry : & PathData ,
2399
2403
config : & Config ,
2400
- out : & mut BufWriter < Stdout > ,
2404
+ state : & mut ListState ,
2401
2405
) -> ( usize , usize , usize , usize , usize , usize ) {
2402
2406
// TODO: Cache/memorize the display_* results so we don't have to recalculate them.
2403
- if let Some ( md) = entry. get_metadata ( out) {
2407
+ if let Some ( md) = entry. get_metadata ( & mut state . out ) {
2404
2408
let ( size_len, major_len, minor_len) = match display_len_or_rdev ( md, config) {
2405
2409
SizeOrDeviceId :: Device ( major, minor) => {
2406
2410
( major. len ( ) + minor. len ( ) + 2usize , major. len ( ) , minor. len ( ) )
@@ -2409,8 +2413,8 @@ fn display_dir_entry_size(
2409
2413
} ;
2410
2414
(
2411
2415
display_symlink_count ( md) . len ( ) ,
2412
- display_uname ( md, config) . len ( ) ,
2413
- display_group ( md, config) . len ( ) ,
2416
+ display_uname ( md, config, state ) . len ( ) ,
2417
+ display_group ( md, config, state ) . len ( ) ,
2414
2418
size_len,
2415
2419
major_len,
2416
2420
minor_len,
@@ -2523,7 +2527,7 @@ fn display_items(
2523
2527
} ) ;
2524
2528
2525
2529
if config. format == Format :: Long {
2526
- let padding_collection = calculate_padding_collection ( items, config, & mut state. out ) ;
2530
+ let padding_collection = calculate_padding_collection ( items, config, state) ;
2527
2531
2528
2532
for item in items {
2529
2533
#[ cfg( unix) ]
@@ -2561,7 +2565,7 @@ fn display_items(
2561
2565
None
2562
2566
} ;
2563
2567
2564
- let padding = calculate_padding_collection ( items, config, & mut state. out ) ;
2568
+ let padding = calculate_padding_collection ( items, config, state) ;
2565
2569
2566
2570
// we need to apply normal color to non filename output
2567
2571
if let Some ( style_manager) = & mut state. style_manager {
@@ -2813,12 +2817,12 @@ fn display_item_long(
2813
2817
2814
2818
if config. long . owner {
2815
2819
output_display. extend ( b" " ) ;
2816
- output_display. extend_pad_right ( & display_uname ( md, config) , padding. uname ) ;
2820
+ output_display. extend_pad_right ( & display_uname ( md, config, state ) , padding. uname ) ;
2817
2821
}
2818
2822
2819
2823
if config. long . group {
2820
2824
output_display. extend ( b" " ) ;
2821
- output_display. extend_pad_right ( & display_group ( md, config) , padding. group ) ;
2825
+ output_display. extend_pad_right ( & display_group ( md, config, state ) , padding. group ) ;
2822
2826
}
2823
2827
2824
2828
if config. context {
@@ -2830,7 +2834,7 @@ fn display_item_long(
2830
2834
// the owner, since GNU/Hurd is not currently supported by Rust.
2831
2835
if config. long . author {
2832
2836
output_display. extend ( b" " ) ;
2833
- output_display. extend_pad_right ( & display_uname ( md, config) , padding. uname ) ;
2837
+ output_display. extend_pad_right ( & display_uname ( md, config, state ) , padding. uname ) ;
2834
2838
}
2835
2839
2836
2840
match display_len_or_rdev ( md, config) {
@@ -3002,67 +3006,49 @@ fn get_inode(metadata: &Metadata) -> String {
3002
3006
// Currently getpwuid is `linux` target only. If it's broken state.out into
3003
3007
// a posix-compliant attribute this can be updated...
3004
3008
#[ cfg( unix) ]
3005
- use std:: sync:: LazyLock ;
3006
- #[ cfg( unix) ]
3007
- use std:: sync:: Mutex ;
3008
- #[ cfg( unix) ]
3009
3009
use uucore:: entries;
3010
3010
use uucore:: fs:: FileInformation ;
3011
3011
3012
3012
#[ cfg( unix) ]
3013
- fn cached_uid2usr ( uid : u32 ) -> String {
3014
- static UID_CACHE : LazyLock < Mutex < HashMap < u32 , String > > > =
3015
- LazyLock :: new ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
3016
-
3017
- let mut uid_cache = UID_CACHE . lock ( ) . unwrap ( ) ;
3018
- uid_cache
3019
- . entry ( uid)
3020
- . or_insert_with ( || entries:: uid2usr ( uid) . unwrap_or_else ( |_| uid. to_string ( ) ) )
3021
- . clone ( )
3022
- }
3023
-
3024
- #[ cfg( unix) ]
3025
- fn display_uname ( metadata : & Metadata , config : & Config ) -> String {
3013
+ fn display_uname ( metadata : & Metadata , config : & Config , state : & mut ListState ) -> String {
3014
+ let uid = metadata. uid ( ) ;
3026
3015
if config. long . numeric_uid_gid {
3027
- metadata . uid ( ) . to_string ( )
3016
+ uid. to_string ( )
3028
3017
} else {
3029
- cached_uid2usr ( metadata. uid ( ) )
3018
+ state
3019
+ . uid_cache
3020
+ . entry ( uid)
3021
+ . or_insert_with ( || entries:: uid2usr ( uid) . unwrap_or_else ( |_| uid. to_string ( ) ) )
3022
+ . clone ( )
3030
3023
}
3031
3024
}
3032
3025
3033
3026
#[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
3034
- fn cached_gid2grp ( gid : u32 ) -> String {
3035
- static GID_CACHE : LazyLock < Mutex < HashMap < u32 , String > > > =
3036
- LazyLock :: new ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
3037
-
3038
- let mut gid_cache = GID_CACHE . lock ( ) . unwrap ( ) ;
3039
- gid_cache
3040
- . entry ( gid)
3041
- . or_insert_with ( || entries:: gid2grp ( gid) . unwrap_or_else ( |_| gid. to_string ( ) ) )
3042
- . clone ( )
3043
- }
3044
-
3045
- #[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
3046
- fn display_group ( metadata : & Metadata , config : & Config ) -> String {
3027
+ fn display_group ( metadata : & Metadata , config : & Config , state : & mut ListState ) -> String {
3028
+ let gid = metadata. gid ( ) ;
3047
3029
if config. long . numeric_uid_gid {
3048
- metadata . gid ( ) . to_string ( )
3030
+ gid. to_string ( )
3049
3031
} else {
3050
- cached_gid2grp ( metadata. gid ( ) )
3032
+ state
3033
+ . gid_cache
3034
+ . entry ( gid)
3035
+ . or_insert_with ( || entries:: gid2grp ( gid) . unwrap_or_else ( |_| gid. to_string ( ) ) )
3036
+ . clone ( )
3051
3037
}
3052
3038
}
3053
3039
3054
3040
#[ cfg( target_os = "redox" ) ]
3055
- fn display_group ( metadata : & Metadata , _config : & Config ) -> String {
3041
+ fn display_group ( metadata : & Metadata , _config : & Config , _state : & mut ListState ) -> String {
3056
3042
metadata. gid ( ) . to_string ( )
3057
3043
}
3058
3044
3059
3045
#[ cfg( not( unix) ) ]
3060
- fn display_uname ( _metadata : & Metadata , _config : & Config ) -> String {
3046
+ fn display_uname ( _metadata : & Metadata , _config : & Config , _state : & mut ListState ) -> String {
3061
3047
"somebody" . to_string ( )
3062
3048
}
3063
3049
3064
3050
#[ cfg( not( unix) ) ]
3065
- fn display_group ( _metadata : & Metadata , _config : & Config ) -> String {
3051
+ fn display_group ( _metadata : & Metadata , _config : & Config , _state : & mut ListState ) -> String {
3066
3052
"somegroup" . to_string ( )
3067
3053
}
3068
3054
@@ -3439,7 +3425,7 @@ fn get_security_context(config: &Config, p_buf: &Path, must_dereference: bool) -
3439
3425
fn calculate_padding_collection (
3440
3426
items : & [ PathData ] ,
3441
3427
config : & Config ,
3442
- out : & mut BufWriter < Stdout > ,
3428
+ state : & mut ListState ,
3443
3429
) -> PaddingCollection {
3444
3430
let mut padding_collections = PaddingCollection {
3445
3431
inode : 1 ,
@@ -3456,7 +3442,7 @@ fn calculate_padding_collection(
3456
3442
for item in items {
3457
3443
#[ cfg( unix) ]
3458
3444
if config. inode {
3459
- let inode_len = if let Some ( md) = item. get_metadata ( out) {
3445
+ let inode_len = if let Some ( md) = item. get_metadata ( & mut state . out ) {
3460
3446
display_inode ( md) . len ( )
3461
3447
} else {
3462
3448
continue ;
@@ -3465,7 +3451,7 @@ fn calculate_padding_collection(
3465
3451
}
3466
3452
3467
3453
if config. alloc_size {
3468
- if let Some ( md) = item. get_metadata ( out) {
3454
+ if let Some ( md) = item. get_metadata ( & mut state . out ) {
3469
3455
let block_size_len = display_size ( get_block_size ( md, config) , config) . len ( ) ;
3470
3456
padding_collections. block_size = block_size_len. max ( padding_collections. block_size ) ;
3471
3457
}
@@ -3474,7 +3460,7 @@ fn calculate_padding_collection(
3474
3460
if config. format == Format :: Long {
3475
3461
let context_len = item. security_context . len ( ) ;
3476
3462
let ( link_count_len, uname_len, group_len, size_len, major_len, minor_len) =
3477
- display_dir_entry_size ( item, config, out ) ;
3463
+ display_dir_entry_size ( item, config, state ) ;
3478
3464
padding_collections. link_count = link_count_len. max ( padding_collections. link_count ) ;
3479
3465
padding_collections. uname = uname_len. max ( padding_collections. uname ) ;
3480
3466
padding_collections. group = group_len. max ( padding_collections. group ) ;
0 commit comments