@@ -218,14 +218,19 @@ impl Keyring {
218
218
check_call ( unsafe { keyctl_unlink ( keyring. id , self . id ) } )
219
219
}
220
220
221
- fn search_impl < K > ( & self , description : & str ) -> KeyringSerial
221
+ fn search_impl < K > ( & self , description : & str , destination : Option < & mut Keyring > ) -> KeyringSerial
222
222
where
223
223
K : KeyType ,
224
224
{
225
225
let type_cstr = CString :: new ( K :: name ( ) ) . unwrap ( ) ;
226
226
let desc_cstr = CString :: new ( description) . unwrap ( ) ;
227
227
into_serial ( unsafe {
228
- keyctl_search ( self . id , type_cstr. as_ptr ( ) , desc_cstr. as_ptr ( ) , self . id )
228
+ keyctl_search (
229
+ self . id ,
230
+ type_cstr. as_ptr ( ) ,
231
+ desc_cstr. as_ptr ( ) ,
232
+ destination. map ( |dest| dest. id ) ,
233
+ )
229
234
} )
230
235
}
231
236
@@ -234,12 +239,15 @@ impl Keyring {
234
239
/// If it is found, it is attached to the keyring (if `write` permission to the keyring and
235
240
/// `link` permission on the key exist) and return it. Requires the `search` permission on the
236
241
/// keyring. Any children keyrings without the `search` permission are ignored.
237
- pub fn search_for_key < K , D > ( & self , description : D ) -> Result < Key >
242
+ pub fn search_for_key < ' a , K , D , DK > ( & self , description : D , destination : DK ) -> Result < Key >
238
243
where
239
244
K : KeyType ,
240
245
D : Borrow < K :: Description > ,
246
+ DK : Into < Option < & ' a mut Keyring > > ,
241
247
{
242
- check_call_key ( self . search_impl :: < K > ( & description. borrow ( ) . description ( ) ) )
248
+ check_call_key (
249
+ self . search_impl :: < K > ( & description. borrow ( ) . description ( ) , destination. into ( ) ) ,
250
+ )
243
251
}
244
252
245
253
/// Recursively search the keyring for a keyring with the matching description.
@@ -248,13 +256,15 @@ impl Keyring {
248
256
/// `link` permission on the found keyring exist) and return it. Requires the `search`
249
257
/// permission on the keyring. Any children keyrings without the `search` permission are
250
258
/// ignored.
251
- pub fn search_for_keyring < D > ( & self , description : D ) -> Result < Self >
259
+ pub fn search_for_keyring < ' a , D , DK > ( & self , description : D , destination : DK ) -> Result < Self >
252
260
where
253
261
D : Borrow < <keytypes:: Keyring as KeyType >:: Description > ,
262
+ DK : Into < Option < & ' a mut Keyring > > ,
254
263
{
255
- check_call_keyring (
256
- self . search_impl :: < keytypes:: Keyring > ( & description. borrow ( ) . description ( ) ) ,
257
- )
264
+ check_call_keyring ( self . search_impl :: < keytypes:: Keyring > (
265
+ & description. borrow ( ) . description ( ) ,
266
+ destination. into ( ) ,
267
+ ) )
258
268
}
259
269
260
270
/// Return all immediate children of the keyring.
@@ -962,44 +972,4 @@ mod tests {
962
972
keyring. unlink_key ( & key) . unwrap ( ) ;
963
973
keyring. invalidate ( ) . unwrap ( ) ;
964
974
}
965
-
966
- #[ test]
967
- fn test_search_key ( ) {
968
- let mut keyring = utils:: new_test_keyring ( ) ;
969
- let mut new_keyring = keyring. add_keyring ( "new_keyring" ) . unwrap ( ) ;
970
- let mut new_inner_keyring = new_keyring. add_keyring ( "new_inner_keyring" ) . unwrap ( ) ;
971
-
972
- // Create the key.
973
- let description = "test:rust-keyutils:search_key" ;
974
- let payload = "payload" ;
975
- let key = new_inner_keyring
976
- . add_key :: < keytypes:: User , _ , _ > ( description, payload. as_bytes ( ) )
977
- . unwrap ( ) ;
978
-
979
- let found_key = keyring
980
- . search_for_key :: < keytypes:: User , _ > ( description)
981
- . unwrap ( ) ;
982
- assert_eq ! ( found_key, key) ;
983
-
984
- // Clean up.
985
- new_inner_keyring. unlink_key ( & key) . unwrap ( ) ;
986
- new_inner_keyring. invalidate ( ) . unwrap ( ) ;
987
- new_keyring. invalidate ( ) . unwrap ( ) ;
988
- keyring. invalidate ( ) . unwrap ( ) ;
989
- }
990
-
991
- #[ test]
992
- fn test_search_keyring ( ) {
993
- let mut keyring = utils:: new_test_keyring ( ) ;
994
- let mut new_keyring = keyring. add_keyring ( "new_keyring" ) . unwrap ( ) ;
995
- let new_inner_keyring = new_keyring. add_keyring ( "new_inner_keyring" ) . unwrap ( ) ;
996
-
997
- let found_keyring = keyring. search_for_keyring ( "new_inner_keyring" ) . unwrap ( ) ;
998
- assert_eq ! ( found_keyring, new_inner_keyring) ;
999
-
1000
- // Clean up.
1001
- new_inner_keyring. invalidate ( ) . unwrap ( ) ;
1002
- new_keyring. invalidate ( ) . unwrap ( ) ;
1003
- keyring. invalidate ( ) . unwrap ( ) ;
1004
- }
1005
975
}
0 commit comments