@@ -7,6 +7,7 @@ use std::rc::Rc;
7
7
use std:: task:: Poll ;
8
8
9
9
use anyhow:: { bail, format_err, Context as _} ;
10
+ use cargo_util:: paths;
10
11
use ops:: FilterRule ;
11
12
use serde:: { Deserialize , Serialize } ;
12
13
@@ -319,6 +320,20 @@ impl InstallTracker {
319
320
self . v1 . remove ( pkg_id, bins) ;
320
321
self . v2 . remove ( pkg_id, bins) ;
321
322
}
323
+
324
+ /// Remove a bin after it successfully had been removed in disk and the save the tracker at last.
325
+ pub fn remove_bin_then_save (
326
+ & mut self ,
327
+ pkg_id : PackageId ,
328
+ bin : & str ,
329
+ bin_path : & PathBuf ,
330
+ ) -> CargoResult < ( ) > {
331
+ paths:: remove_file ( bin_path) ?;
332
+ self . v1 . remove_bin ( pkg_id, bin) ;
333
+ self . v2 . remove_bin ( pkg_id, bin) ;
334
+ self . save ( ) ?;
335
+ Ok ( ( ) )
336
+ }
322
337
}
323
338
324
339
impl CrateListingV1 {
@@ -359,6 +374,17 @@ impl CrateListingV1 {
359
374
}
360
375
}
361
376
377
+ fn remove_bin ( & mut self , pkg_id : PackageId , bin : & str ) {
378
+ let mut installed = match self . v1 . entry ( pkg_id) {
379
+ btree_map:: Entry :: Occupied ( e) => e,
380
+ btree_map:: Entry :: Vacant ( ..) => panic ! ( "v1 unexpected missing `{}`" , pkg_id) ,
381
+ } ;
382
+ installed. get_mut ( ) . remove ( bin) ;
383
+ if installed. get ( ) . is_empty ( ) {
384
+ installed. remove ( ) ;
385
+ }
386
+ }
387
+
362
388
fn save ( & self , lock : & FileLock ) -> CargoResult < ( ) > {
363
389
let mut file = lock. file ( ) ;
364
390
file. seek ( SeekFrom :: Start ( 0 ) ) ?;
@@ -468,6 +494,17 @@ impl CrateListingV2 {
468
494
}
469
495
}
470
496
497
+ fn remove_bin ( & mut self , pkg_id : PackageId , bin : & str ) {
498
+ let mut info_entry = match self . installs . entry ( pkg_id) {
499
+ btree_map:: Entry :: Occupied ( e) => e,
500
+ btree_map:: Entry :: Vacant ( ..) => panic ! ( "v1 unexpected missing `{}`" , pkg_id) ,
501
+ } ;
502
+ info_entry. get_mut ( ) . bins . remove ( bin) ;
503
+ if info_entry. get ( ) . bins . is_empty ( ) {
504
+ info_entry. remove ( ) ;
505
+ }
506
+ }
507
+
471
508
fn save ( & self , lock : & FileLock ) -> CargoResult < ( ) > {
472
509
let mut file = lock. file ( ) ;
473
510
file. seek ( SeekFrom :: Start ( 0 ) ) ?;
0 commit comments