33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6- use super :: { Matcher , MatcherIO , WalkEntry } ;
6+ use super :: { ComparableValue , Matcher , MatcherIO , WalkEntry } ;
77
88#[ cfg( unix) ]
99use nix:: unistd:: Group ;
1010#[ cfg( unix) ]
1111use std:: os:: unix:: fs:: MetadataExt ;
1212
1313pub struct GroupMatcher {
14- gid : Option < u32 > ,
14+ gid : ComparableValue ,
1515}
1616
1717impl GroupMatcher {
1818 #[ cfg( unix) ]
19- pub fn from_group_name ( group : & str ) -> Self {
19+ pub fn from_group_name ( group : & str ) -> Option < Self > {
2020 // get gid from group name
21- let Ok ( group) = Group :: from_name ( group) else {
22- return Self { gid : None } ;
23- } ;
24-
25- let Some ( group) = group else {
26- // This if branch is to determine whether a certain group exists in the system.
27- // If a certain group does not exist in the system,
28- // the result will need to be returned according to
29- // the flag bit of whether to invert the result.
30- return Self { gid : None } ;
31- } ;
32-
33- Self {
34- gid : Some ( group. gid . as_raw ( ) ) ,
35- }
21+ let group = Group :: from_name ( group) . ok ( ) ??;
22+ let gid = group. gid . as_raw ( ) ;
23+ Some ( Self :: from_gid ( gid) )
3624 }
3725
38- #[ cfg( unix) ]
3926 pub fn from_gid ( gid : u32 ) -> Self {
40- Self { gid : Some ( gid) }
27+ Self :: from_comparable ( ComparableValue :: EqualTo ( gid as u64 ) )
4128 }
4229
43- #[ cfg( windows) ]
44- pub fn from_group_name ( _group : & str ) -> GroupMatcher {
45- GroupMatcher { gid : None }
30+ pub fn from_comparable ( gid : ComparableValue ) -> Self {
31+ Self { gid }
4632 }
4733
4834 #[ cfg( windows) ]
49- pub fn from_gid ( _gid : u32 ) -> GroupMatcher {
50- GroupMatcher { gid : None }
51- }
52-
53- pub fn gid ( & self ) -> & Option < u32 > {
54- & self . gid
35+ pub fn from_group_name ( _group : & str ) -> Option < Self > {
36+ None
5537 }
5638}
5739
5840impl Matcher for GroupMatcher {
5941 #[ cfg( unix) ]
6042 fn matches ( & self , file_info : & WalkEntry , _: & mut MatcherIO ) -> bool {
61- let Ok ( metadata) = file_info. metadata ( ) else {
62- return false ;
63- } ;
64-
65- let file_gid = metadata. gid ( ) ;
66-
67- // When matching the -group parameter in find/matcher/mod.rs,
68- // it has been judged that the group does not exist and an error is returned.
69- // So use unwarp() directly here.
70- self . gid . unwrap ( ) == file_gid
43+ match file_info. metadata ( ) {
44+ Ok ( metadata) => self . gid . matches ( metadata. gid ( ) . into ( ) ) ,
45+ Err ( _) => false ,
46+ }
7147 }
7248
7349 #[ cfg( windows) ]
@@ -136,7 +112,8 @@ mod tests {
136112 . unwrap ( )
137113 . name ;
138114
139- let matcher = super :: GroupMatcher :: from_group_name ( file_group. as_str ( ) ) ;
115+ let matcher =
116+ super :: GroupMatcher :: from_group_name ( file_group. as_str ( ) ) . expect ( "group should exist" ) ;
140117 assert ! (
141118 matcher. matches( & file_info, & mut matcher_io) ,
142119 "group should match"
@@ -146,18 +123,13 @@ mod tests {
146123 let time_string = Local :: now ( ) . format ( "%Y%m%d%H%M%S" ) . to_string ( ) ;
147124 let matcher = GroupMatcher :: from_group_name ( time_string. as_str ( ) ) ;
148125 assert ! (
149- matcher. gid ( ) . is_none( ) ,
126+ matcher. is_none( ) ,
150127 "group name {} should not exist" ,
151128 time_string
152129 ) ;
153130
154131 // Testing group id
155132 let matcher = GroupMatcher :: from_gid ( file_gid) ;
156- assert ! (
157- matcher. gid( ) . is_some( ) ,
158- "group id {} should exist" ,
159- file_gid
160- ) ;
161133 assert ! (
162134 matcher. matches( & file_info, & mut matcher_io) ,
163135 "group id should match"
0 commit comments